pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
BUG #7808: unnest doesn't handle nulls in array of composite types correctly
8+ messages / 4 participants
[nested] [flat]

* BUG #7808: unnest doesn't handle nulls in array of composite types correctly
@ 2013-01-14 21:11 joe@tanga.com
  2013-01-14 21:30 ` Re: BUG #7808: unnest doesn't handle nulls in array of composite types correctly Stephen Frost <sfrost@snowman.net>
  2016-07-23 08:25 ` Re: BUG #7808: unnest doesn't handle nulls in array of composite typescorrectly Andrew Gierth <andrew@tao11.riddles.org.uk>
  0 siblings, 2 replies; 8+ messages in thread

From: joe@tanga.com @ 2013-01-14 21:11 UTC (permalink / raw)
  To: pgsql-bugs

The following bug has been logged on the website:

Bug reference:      7808
Logged by:          Joe Van Dyk
Email address:      joe@tanga.com
PostgreSQL version: 9.2.1
Operating system:   OSX
Description:        

RhodiumToad says this is a bug in unnest, but honestly I don't quite
understand it all. 

He said: "if you have an array of composite, then a null element provokes
that error, as opposed to an element all of whose columns are null.
basically, unnest(array[null::g])  breaks, while
unnest(array[row(null,null)::g])  works"

My goal is to remove nulls from an array. The array could be an array of a
composite type.

begin;                                                                      
                       
                                                                            
                       
create table f (id integer);                                                
                       
insert into f values (1), (2);                                              
                       
                                                                            
                       
create table g (id integer, f_id integer);                                  
                       
insert into g values (1, 1);                                                
                       
insert into g values (2, 1);                                                
                       
                                                                            
                       
create function no_nulls(anyarray) returns anyarray as $$                   
                       
  select array(select x from unnest($1) x where not (x is null))            
                       
$$ language sql;                                                            
                       
                                                                            
                       
select f.id, no_nulls(array_agg(g))                                         
                       
from f                                                                      
                       
left join g on g.f_id = f.id                                                
                       
group by f;

                                                                            
                 
Expected Result:                                                            
                       
 id |     array_agg                                                         
                       
----+-------------------                                                    
                       
  1 | {"(1,1)","(2,1)"}                                                     
                       
  2 | {}                                                                    
                       
                                                                            
                       
                                                                            
                       
Getting this error:                                                         
                       
                                                                            
                       
psql:/tmp/n.sql:18: ERROR:  function returning set of rows cannot return
null value                 
CONTEXT:  SQL function "no_nulls" statement 1                               
                       
 



-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: BUG #7808: unnest doesn't handle nulls in array of composite types correctly
  2013-01-14 21:11 BUG #7808: unnest doesn't handle nulls in array of composite types correctly joe@tanga.com
@ 2013-01-14 21:30 ` Stephen Frost <sfrost@snowman.net>
  1 sibling, 0 replies; 8+ messages in thread

From: Stephen Frost @ 2013-01-14 21:30 UTC (permalink / raw)
  To: joe@tanga.com; +Cc: pgsql-bugs

* joe@tanga.com (joe@tanga.com) wrote:
> My goal is to remove nulls from an array. The array could be an array of a
> composite type.

A much simpler case is:

=> create type xt as (a integer);
CREATE TYPE
=> select * from unnest(array[null::xt]);
ERROR:  function returning set of rows cannot return null value
=> select * from unnest(array[row(null)::xt]);
 a 
---
  
(1 row)

	Thanks,

		Stephen

Attachments:

  signature.asc (application/pgp-signature, 197B) download

^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: BUG #7808: unnest doesn't handle nulls in array of composite typescorrectly
  2013-01-14 21:11 BUG #7808: unnest doesn't handle nulls in array of composite types correctly joe@tanga.com
@ 2016-07-23 08:25 ` Andrew Gierth <andrew@tao11.riddles.org.uk>
  2016-07-23 14:49   ` Re: BUG #7808: unnest doesn't handle nulls in array of composite typescorrectly Tom Lane <tgl@sss.pgh.pa.us>
  1 sibling, 1 reply; 8+ messages in thread

From: Andrew Gierth @ 2016-07-23 08:25 UTC (permalink / raw)
  To: pgsql-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-bugs

Attachments:

rangefunc_nulls.patch (text/x-patch, 5.4K) download 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;
^ permalink raw reply [nested|flat] 8+ messages in thread

* Re: BUG #7808: unnest doesn't handle nulls in array of composite typescorrectly
  2013-01-14 21:11 BUG #7808: unnest doesn't handle nulls in array of composite types correctly joe@tanga.com
  2016-07-23 08:25 ` Re: BUG #7808: unnest doesn't handle nulls in array of composite typescorrectly Andrew Gierth <andrew@tao11.riddles.org.uk>
@ 2016-07-23 14:49   ` Tom Lane <tgl@sss.pgh.pa.us>
  2016-07-23 23:47     ` Re: BUG #7808: unnest doesn't handle nulls in array of composite typescorrectly Andrew Gierth <andrew@tao11.riddles.org.uk>
  0 siblings, 1 reply; 8+ messages in thread

From: Tom Lane @ 2016-07-23 14:49 UTC (permalink / raw)
  To: Andrew Gierth <andrew@tao11.riddles.org.uk>; +Cc: pgsql-bugs

Andrew Gierth <andrew@tao11.riddles.org.uk> writes:
> 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.

I do not see how you can propose this, which creates an explicit
equivalence between a plain null and an all-nulls row, and simultaneously
advocate that we change IS NULL to remove its treatment of those things
as equivalent.

I think the theory behind the existing code here is that if the SRF wants
its output to be interpreted as an all-nulls row, it can perfectly well
return an all-nulls row.  I wonder whether we should address this by
adjusting unnest's behavior instead.

			regards, tom lane


-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: BUG #7808: unnest doesn't handle nulls in array of composite typescorrectly
  2013-01-14 21:11 BUG #7808: unnest doesn't handle nulls in array of composite types correctly joe@tanga.com
  2016-07-23 08:25 ` Re: BUG #7808: unnest doesn't handle nulls in array of composite typescorrectly Andrew Gierth <andrew@tao11.riddles.org.uk>
  2016-07-23 14:49   ` Re: BUG #7808: unnest doesn't handle nulls in array of composite typescorrectly Tom Lane <tgl@sss.pgh.pa.us>
@ 2016-07-23 23:47     ` Andrew Gierth <andrew@tao11.riddles.org.uk>
  2016-07-27 01:37       ` Re: BUG #7808: unnest doesn't handle nulls in array of composite typescorrectly Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 8+ messages in thread

From: Andrew Gierth @ 2016-07-23 23:47 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: pgsql-bugs

>>>>> "Tom" == Tom Lane <tgl@sss.pgh.pa.us> writes:

 Tom> I do not see how you can propose this, which creates an explicit
 Tom> equivalence between a plain null and an all-nulls row, and
 Tom> simultaneously advocate that we change IS NULL to remove its
 Tom> treatment of those things as equivalent.

Because the proposal to change IS NULL is a discussion point that I'm
not dogmatically committed to.

 Tom> I think the theory behind the existing code here is that if the
 Tom> SRF wants its output to be interpreted as an all-nulls row, it can
 Tom> perfectly well return an all-nulls row.  I wonder whether we
 Tom> should address this by adjusting unnest's behavior instead.

One problem with changing unnest() is that it makes the operation
array(select unnest(a)) return an array that's not equal to the original
one (and which takes up a lot more space if there are many nulls). This
is far more likely to break existing code than simply allowing what was
previously an error case to work.

-- 
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-bugs



^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: BUG #7808: unnest doesn't handle nulls in array of composite typescorrectly
  2013-01-14 21:11 BUG #7808: unnest doesn't handle nulls in array of composite types correctly joe@tanga.com
  2016-07-23 08:25 ` Re: BUG #7808: unnest doesn't handle nulls in array of composite typescorrectly Andrew Gierth <andrew@tao11.riddles.org.uk>
  2016-07-23 14:49   ` Re: BUG #7808: unnest doesn't handle nulls in array of composite typescorrectly Tom Lane <tgl@sss.pgh.pa.us>
  2016-07-23 23:47     ` Re: BUG #7808: unnest doesn't handle nulls in array of composite typescorrectly Andrew Gierth <andrew@tao11.riddles.org.uk>
@ 2016-07-27 01:37       ` Tom Lane <tgl@sss.pgh.pa.us>
  2016-07-27 03:59         ` Re: BUG #7808: unnest doesn't handle nulls in array of composite typescorrectly Andrew Gierth <andrew@tao11.riddles.org.uk>
  0 siblings, 1 reply; 8+ messages in thread

From: Tom Lane @ 2016-07-27 01:37 UTC (permalink / raw)
  To: Andrew Gierth <andrew@tao11.riddles.org.uk>; +Cc: pgsql-bugs

Andrew Gierth <andrew@tao11.riddles.org.uk> writes:
> "Tom" == Tom Lane <tgl@sss.pgh.pa.us> writes:
>  Tom> I think the theory behind the existing code here is that if the
>  Tom> SRF wants its output to be interpreted as an all-nulls row, it can
>  Tom> perfectly well return an all-nulls row.  I wonder whether we
>  Tom> should address this by adjusting unnest's behavior instead.

> One problem with changing unnest() is that it makes the operation
> array(select unnest(a)) return an array that's not equal to the original
> one (and which takes up a lot more space if there are many nulls).

Fair point.

I didn't much like the "initial_nulls" counter in your patch, but actually
there's no reason we can't just push an all-nulls row into the tuplestore
immediately on seeing a null, the same way as happens in the last-ditch
case at the bottom of ExecMakeTableFunctionResult.  I whacked that around
a bit and pushed it.

			regards, tom lane


-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: BUG #7808: unnest doesn't handle nulls in array of composite typescorrectly
  2013-01-14 21:11 BUG #7808: unnest doesn't handle nulls in array of composite types correctly joe@tanga.com
  2016-07-23 08:25 ` Re: BUG #7808: unnest doesn't handle nulls in array of composite typescorrectly Andrew Gierth <andrew@tao11.riddles.org.uk>
  2016-07-23 14:49   ` Re: BUG #7808: unnest doesn't handle nulls in array of composite typescorrectly Tom Lane <tgl@sss.pgh.pa.us>
  2016-07-23 23:47     ` Re: BUG #7808: unnest doesn't handle nulls in array of composite typescorrectly Andrew Gierth <andrew@tao11.riddles.org.uk>
  2016-07-27 01:37       ` Re: BUG #7808: unnest doesn't handle nulls in array of composite typescorrectly Tom Lane <tgl@sss.pgh.pa.us>
@ 2016-07-27 03:59         ` Andrew Gierth <andrew@tao11.riddles.org.uk>
  2016-07-27 12:48           ` Re: BUG #7808: unnest doesn't handle nulls in array of composite typescorrectly Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 8+ messages in thread

From: Andrew Gierth @ 2016-07-27 03:59 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: pgsql-bugs

>>>>> "Tom" == Tom Lane <tgl@sss.pgh.pa.us> writes:

 Tom> I didn't much like the "initial_nulls" counter in your patch, but
 Tom> actually there's no reason we can't just push an all-nulls row
 Tom> into the tuplestore immediately on seeing a null, the same way as
 Tom> happens in the last-ditch case at the bottom of
 Tom> ExecMakeTableFunctionResult.  I whacked that around a bit and
 Tom> pushed it.

I did think about that, but the existing code seems to go out of its way
to build the tuplestore using the tupdesc obtained from the actual
function result row, rather than the "expected" tupdesc, and it wasn't
really obvious why that was.

-- 
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-bugs



^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: BUG #7808: unnest doesn't handle nulls in array of composite typescorrectly
  2013-01-14 21:11 BUG #7808: unnest doesn't handle nulls in array of composite types correctly joe@tanga.com
  2016-07-23 08:25 ` Re: BUG #7808: unnest doesn't handle nulls in array of composite typescorrectly Andrew Gierth <andrew@tao11.riddles.org.uk>
  2016-07-23 14:49   ` Re: BUG #7808: unnest doesn't handle nulls in array of composite typescorrectly Tom Lane <tgl@sss.pgh.pa.us>
  2016-07-23 23:47     ` Re: BUG #7808: unnest doesn't handle nulls in array of composite typescorrectly Andrew Gierth <andrew@tao11.riddles.org.uk>
  2016-07-27 01:37       ` Re: BUG #7808: unnest doesn't handle nulls in array of composite typescorrectly Tom Lane <tgl@sss.pgh.pa.us>
  2016-07-27 03:59         ` Re: BUG #7808: unnest doesn't handle nulls in array of composite typescorrectly Andrew Gierth <andrew@tao11.riddles.org.uk>
@ 2016-07-27 12:48           ` Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 0 replies; 8+ messages in thread

From: Tom Lane @ 2016-07-27 12:48 UTC (permalink / raw)
  To: Andrew Gierth <andrew@tao11.riddles.org.uk>; +Cc: pgsql-bugs

Andrew Gierth <andrew@tao11.riddles.org.uk> writes:
> I did think about that, but the existing code seems to go out of its way
> to build the tuplestore using the tupdesc obtained from the actual
> function result row, rather than the "expected" tupdesc, and it wasn't
> really obvious why that was.

As I noted in a comment, the key hazard would be that heap_form_tuple
copies the tupdesc's type identifier info into the created composite
Datum, thus possibly leading to a mixture of type IDs in the stored
tuples.  But tuplestore stores MinimalTuples which omit that data,
so it doesn't matter.  I have a feeling that the existing code here
might've been older than the changes to store MinimalTuples rather
than regular tuple datums in tuplestores; or at least, it was written
with the assumption that that was important.

			regards, tom lane


-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



^ permalink  raw  reply  [nested|flat] 8+ messages in thread


end of thread, other threads:[~2016-07-27 12:48 UTC | newest]

Thread overview: 8+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2013-01-14 21:11 BUG #7808: unnest doesn't handle nulls in array of composite types correctly joe@tanga.com
2013-01-14 21:30 ` Stephen Frost <sfrost@snowman.net>
2016-07-23 08:25 ` Re: BUG #7808: unnest doesn't handle nulls in array of composite typescorrectly Andrew Gierth <andrew@tao11.riddles.org.uk>
2016-07-23 14:49   ` Re: BUG #7808: unnest doesn't handle nulls in array of composite typescorrectly Tom Lane <tgl@sss.pgh.pa.us>
2016-07-23 23:47     ` Re: BUG #7808: unnest doesn't handle nulls in array of composite typescorrectly Andrew Gierth <andrew@tao11.riddles.org.uk>
2016-07-27 01:37       ` Re: BUG #7808: unnest doesn't handle nulls in array of composite typescorrectly Tom Lane <tgl@sss.pgh.pa.us>
2016-07-27 03:59         ` Re: BUG #7808: unnest doesn't handle nulls in array of composite typescorrectly Andrew Gierth <andrew@tao11.riddles.org.uk>
2016-07-27 12:48           ` Re: BUG #7808: unnest doesn't handle nulls in array of composite typescorrectly Tom Lane <tgl@sss.pgh.pa.us>

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