pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Denis de Bernardy <ddebernardy@yahoo.com>
To: pgsql-bugs@postgresql.org
Subject: Re: BUG #8226: Inconsistent unnesting of arrays
Date: Wed, 12 Jun 2013 12:19:51 +0200
Message-ID: <336D0D5D-A04D-4E71-B78F-484017B74F81@yahoo.com> (raw)
In-Reply-To: <CAM-w4HNZ-6X9eAwtCY72Gff3ihOpUF-L2x2+3Wt7=+A7aaJ5+A@mail.gmail.com>
References: <E1UmgtE-0002si-0x@wrigleys.postgresql.org>
	<CAM-w4HNZ-6X9eAwtCY72Gff3ihOpUF-L2x2+3Wt7=+A7aaJ5+A@mail.gmail.com>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-bugs>

The actual query was something like:

select id, person, unnest(groups) as grp from people

… where groups is a crazy column containing an array that needed to be joined with another table. In this case, you cannot do your suggested solution, which would look like this:

select id, person, grp from people, unnest(groups) as grp

Admittedly, there are other ways to rewrite the above, but — if I may — that's entirely besides the point of the bug report. The Stack Overflow question got me curious about what occurred when two separate arrays are unnested.

Testing revealed the inconsistency, which I tend to view as a bug.

This statement works as expected, unnesting the first array, then cross joining the second accordingly:

>> select 1 as a, unnest('{2,3}'::int[]) as b, unnest('{4,5,6}'::int[])


This seems to only unnest one of the arrays, and match the element with the same subscript in the other array:

>> select 1 as a, unnest('{2,3}'::int[]) as b, unnest('{4,5}'::int[])


Methinks the behavior should be consistent. It should always do one (presumably like in the first statement) or the other (which leads to undefined behavior in the first statement).

Or it should raise some kind of warning, e.g. "you're using undocumented/unsupported/deprecated/broken syntactic sugar".

Denis


On Jun 12, 2013, at 12:05 PM, Greg Stark wrote:

> On Wed, Jun 12, 2013 at 9:58 AM,  <ddebernardy@yahoo.com> wrote:
>> denis=# select 1 as a, unnest('{2,3}'::int[]) as b, unnest('{4,5}'::int[])
> 
> set returning functions in the target list of the select don't behave
> the way you're thinking. What you probably want to do is move the
> unnest() to the FROM clause:
> 
> select 1 as a, b, c from unnest('{2,3}'::int[]) as b(b),
> unnest('{4,5}'::int[]) as c(c)
> 
> 
> -- 
> greg



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



view thread (7+ messages)  latest in thread

Message-ID: <336D0D5D-A04D-4E71-B78F-484017B74F81@yahoo.com>
Permalink:  ../336D0D5D-A04D-4E71-B78F-484017B74F81@yahoo.com/
Also on:    postgresql.org/message-id/336D0D5D-A04D-4E71-B78F-484017B74F81@yahoo.com

 · 

reply

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: ddebernardy@yahoo.com
  Subject: Re: BUG #8226: Inconsistent unnesting of arrays
  In-Reply-To: <336D0D5D-A04D-4E71-B78F-484017B74F81@yahoo.com>
  References: <E1UmgtE-0002si-0x@wrigleys.postgresql.org> <CAM-w4HNZ-6X9eAwtCY72Gff3ihOpUF-L2x2+3Wt7=+A7aaJ5+A@mail.gmail.com> <336D0D5D-A04D-4E71-B78F-484017B74F81@yahoo.com>

* 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