Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1bQsFD-0004SZ-5s for pgsql-bugs@arkaria.postgresql.org; Sat, 23 Jul 2016 08:25:11 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1bQsFC-0003f4-O7 for pgsql-bugs@arkaria.postgresql.org; Sat, 23 Jul 2016 08:25:10 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1bQsFB-0003eK-M1 for pgsql-bugs@postgresql.org; Sat, 23 Jul 2016 08:25:09 +0000 Received: from lungold.riddles.org.uk ([82.68.208.19]) by makus.postgresql.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84_2) (envelope-from ) id 1bQsF8-0003mL-Ci for pgsql-bugs@postgresql.org; Sat, 23 Jul 2016 08:25:08 +0000 Received: from localhost ([127.0.0.1]:63373 helo=caithnard.riddles.org.uk) by lungold.riddles.org.uk with esmtp (Exim 4.84 (FreeBSD)) (envelope-from ) id 1bQsF6-000OzS-5t for pgsql-bugs@postgresql.org; Sat, 23 Jul 2016 08:25:04 +0000 From: Andrew Gierth To: pgsql-bugs@postgresql.org Subject: Re: BUG #7808: unnest doesn't handle nulls in array of composite typescorrectly Message-ID: <87wpkcpyud.fsf@news-spur.riddles.org.uk> References: User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (berkeley-unix) Date: Sat, 23 Jul 2016 09:25:03 +0100 MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" X-Pg-Spam-Score: -3.2 (---) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org --=-=-= Content-Type: text/plain 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) --=-=-= Content-Type: text/x-patch Content-Disposition: attachment; filename=rangefunc_nulls.patch Content-Description: range function nulls patch diff --git a/src/backend/executor/execQual.c b/src/backend/executor/execQual.c index 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.out index 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.sql index 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; --=-=-= Content-Type: text/plain Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs --=-=-=--