From: Andrew Gierth <andrew@tao11.riddles.org.uk>
To: pgsql-bugs@postgresql.org
Subject: Re: BUG #7808: unnest doesn't handle nulls in array of composite typescorrectly
Date: Sat, 23 Jul 2016 09:25:03 +0100
Message-ID: <87wpkcpyud.fsf@news-spur.riddles.org.uk> (raw)
References: <E1TurJE-0006Es-TK@wrigleys.postgresql.org>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-bugs>
This bug was reported three and a half years ago and apparently
ignored... but it came to my attention in the IS NULL discussion.
This patch doesn't address unnest() explicitly, rather it modifies
ExecMakeTableFunctionResult to treat an isnull return equivalently to an
all-nulls tuple. This isn't ideal, especially in view of the points
discussed in the other threads; it leaves these inconsistencies:
create type c1 as (a text, b numeric);
select u, u is distinct from null from (select unnest(array[null::c1,row('a',1)::c1,null::c1]) as u) s;
u | ?column?
-------+----------
| f
(a,1) | t
| f
(3 rows)
select u, u is distinct from null from unnest(array[null::c1,row('a',1)::c1,null::c1]) u;
u | ?column?
-------+----------
(,) | t
(a,1) | t
(,) | t
(3 rows)
But as far as I can tell, the spec actually requires that unnest cope
with nulls and produce actual columns; the syntax transformations result
in something roughly like:
SELECT (e).* FROM (SELECT a[i] AS e FROM ...)
and I don't see anything that licenses (e).* to fail just because a[i] is
the null value of a row type.
--
Andrew (irc:RhodiumToad)
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugsAttachments:rangefunc_nulls.patch (text/x-patch, 5.4K) downloaddiff --git a/src/backend/executor/execQual.c b/src/backend/executor/execQual.cindex d04d1a8..2bb97c7 100644--- a/src/backend/executor/execQual.c+++ b/src/backend/executor/execQual.c@@ -2073,6 +2073,9 @@ ExecMakeTableFunctionResult(ExprState *funcexpr,
MemoryContext oldcontext;
bool direct_function_call;
bool first_time = true;
+ Datum *nulldatums = NULL;+ bool *nullflags = NULL;+ int64 initial_nulls = 0;
callerContext = CurrentMemoryContext;
@@ -2228,21 +2231,35 @@ ExecMakeTableFunctionResult(ExprState *funcexpr,
if (rsinfo.isDone == ExprEndResult)
break;
- /*- * Can't do anything very useful with NULL rowtype values. For a- * function returning set, we consider this a protocol violation- * (but another alternative would be to just ignore the result and- * "continue" to get another row). For a function not returning- * set, we fall out of the loop; we'll cons up an all-nulls result- * row below.- */- if (returnsTuple && fcinfo.isnull)+ if (fcinfo.isnull)
{
- if (!returnsSet)+ /*+ * We used to error out on NULL rowtype values, but this is+ * quite awkward for unnest(). Instead, we substitute an+ * all-nulls row of the expected rowtype (see below). For a+ * function not returning set, we fall out of the loop; we'll+ * cons up an all-nulls result row below.+ */+ if (returnsTuple && !returnsSet)
break;
- ereport(ERROR,- (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),- errmsg("function returning set of rows cannot return null value")));++ /*+ * but since we don't build the tuplestore for tuple-returning+ * funcs until we see a non-null, just count the number of+ * initial nulls+ */+ if (!tupdesc && returnsTuple)+ {+ ++initial_nulls;++ /*+ * Are we done?+ */+ if (rsinfo.isDone != ExprMultipleResult)+ break;++ continue;+ }
}
/*
@@ -2280,12 +2297,24 @@ ExecMakeTableFunctionResult(ExprState *funcexpr,
MemoryContextSwitchTo(oldcontext);
rsinfo.setResult = tupstore;
rsinfo.setDesc = tupdesc;
++ if (initial_nulls > 0)+ {+ int natts = tupdesc->natts;++ nulldatums = (Datum *) palloc0(natts * sizeof(Datum));+ nullflags = (bool *) palloc(natts * sizeof(bool));+ memset(nullflags, true, natts * sizeof(bool));++ while (initial_nulls--)+ tuplestore_putvalues(tupstore, tupdesc, nulldatums, nullflags);+ }
}
/*
* Store current resultset item.
*/
- if (returnsTuple)+ if (returnsTuple && !fcinfo.isnull)
{
HeapTupleHeader td;
@@ -2310,6 +2339,18 @@ ExecMakeTableFunctionResult(ExprState *funcexpr,
tuplestore_puttuple(tupstore, &tmptup);
}
+ else if (returnsTuple)+ {+ if (!nulldatums)+ {+ int natts = tupdesc->natts;++ nulldatums = (Datum *) palloc0(natts * sizeof(Datum));+ nullflags = (bool *) palloc(natts * sizeof(bool));+ memset(nullflags, true, natts * sizeof(bool));+ }+ tuplestore_putvalues(tupstore, tupdesc, nulldatums, nullflags);+ }
else
tuplestore_putvalues(tupstore, tupdesc, &result, &fcinfo.isnull);
@@ -2350,18 +2391,20 @@ no_function_result:
MemoryContextSwitchTo(econtext->ecxt_per_query_memory);
tupstore = tuplestore_begin_heap(randomAccess, false, work_mem);
rsinfo.setResult = tupstore;
- if (!returnsSet)+ if (!returnsSet || initial_nulls > 0)
{
int natts = expectedDesc->natts;
- Datum *nulldatums;- bool *nullflags;
MemoryContextSwitchTo(econtext->ecxt_per_tuple_memory);
nulldatums = (Datum *) palloc0(natts * sizeof(Datum));
nullflags = (bool *) palloc(natts * sizeof(bool));
memset(nullflags, true, natts * sizeof(bool));
MemoryContextSwitchTo(econtext->ecxt_per_query_memory);
- tuplestore_putvalues(tupstore, expectedDesc, nulldatums, nullflags);++ do+ {+ tuplestore_putvalues(tupstore, expectedDesc, nulldatums, nullflags);+ } while (--initial_nulls > 0);
}
}
diff --git a/src/test/regress/expected/rangefuncs.out b/src/test/regress/expected/rangefuncs.outindex 00ef421..4d50db0 100644--- a/src/test/regress/expected/rangefuncs.out+++ b/src/test/regress/expected/rangefuncs.out@@ -2076,3 +2076,28 @@ select x from int8_tbl, extractq2_2_opt(int8_tbl) f(x);
-4567890123456789
(5 rows)
+-- handling of nulls in SRF results (bug #7808)+create type foo2 as (a integer, b text);+select * from unnest(array[(1,'foo')::foo2, null::foo2]) u;+ a | b +---+-----+ 1 | foo+ | +(2 rows)++select * from unnest(array[null::foo2, null::foo2]) u;+ a | b +---+---+ | + | +(2 rows)++select * from unnest(array[null::foo2, (1,'foo')::foo2, null::foo2]) u;+ a | b +---+-----+ | + 1 | foo+ | +(3 rows)++drop type foo2;diff --git a/src/test/regress/sql/rangefuncs.sql b/src/test/regress/sql/rangefuncs.sqlindex 9484023..fd76932 100644--- a/src/test/regress/sql/rangefuncs.sql+++ b/src/test/regress/sql/rangefuncs.sql@@ -639,3 +639,13 @@ explain (verbose, costs off)
select x from int8_tbl, extractq2_2_opt(int8_tbl) f(x);
select x from int8_tbl, extractq2_2_opt(int8_tbl) f(x);
++-- handling of nulls in SRF results (bug #7808)++create type foo2 as (a integer, b text);++select * from unnest(array[(1,'foo')::foo2, null::foo2]) u;+select * from unnest(array[null::foo2, null::foo2]) u;+select * from unnest(array[null::foo2, (1,'foo')::foo2, null::foo2]) u;++drop type foo2;
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-bugs@postgresql.org
Cc: andrew@tao11.riddles.org.uk
Subject: Re: BUG #7808: unnest doesn't handle nulls in array of composite typescorrectly
In-Reply-To: <87wpkcpyud.fsf@news-spur.riddles.org.uk>
References: <E1TurJE-0006Es-TK@wrigleys.postgresql.org> <87wpkcpyud.fsf@news-spur.riddles.org.uk>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox