Re: unnest

Lists: pgsql-hackers
From: "John Hansen" <john(at)geeknet(dot)com(dot)au>
To: <pgsql-hackers(at)postgresql(dot)org>
Subject: unnest
Date: 2004-11-05 12:09:58
Message-ID: 5066E5A966339E42AA04BA10BA706AE561FB@rodrick.geeknet.com.au
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Attached, array -> rows iterator.

select * from unnest(array[1,2,3,4,5]);

Unnest
---------------
1
2
3
4
5
5 rows

The switch statement could probably be done in a different way, but
there doesn't seem to be any good examples of how to return anyitem. If
anyone have a better way, please let me know.

Does anyone know how to check individual array elements for NULL values?
PG_ARG_ISNULL() seems to return true if ANY array element is null; ex::
array[1,2,3,null,4,5]

Comments / improvements welcome.

Kind regards,

John

Attachment Content-Type Size
Makefile application/octet-stream 225 bytes
unnest.c application/octet-stream 4.1 KB
unnest.sql application/octet-stream 128 bytes

From: Kris Jurka <books(at)ejurka(dot)com>
To: John Hansen <john(at)geeknet(dot)com(dot)au>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: unnest
Date: 2004-11-05 19:59:34
Message-ID: Pine.BSO.4.56.0411051456490.28172@leary.csoft.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Fri, 5 Nov 2004, John Hansen wrote:

> Does anyone know how to check individual array elements for NULL values?
> PG_ARG_ISNULL() seems to return true if ANY array element is null; ex::
> array[1,2,3,null,4,5]

Arrays cannot store NULL elements, check your above statement and see that
the whole thing is NULL when you introduce a NULL element:

# select array[1,2,3,null,4,5];
array
-------

(1 row)

or

# select array[1,2,3,null,4,5] IS NULL;
?column?
----------
t
(1 row)

Kris Jurka


From: Eric B(dot)Ridge <ebr(at)tcdi(dot)com>
To: "John Hansen" <john(at)geeknet(dot)com(dot)au>
Cc: <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: unnest
Date: 2004-11-09 02:09:38
Message-ID: 6920C9B9-31F4-11D9-9C25-000A95D98B3E@tcdi.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Nov 5, 2004, at 7:09 AM, John Hansen wrote:

> Attached, array -> rows iterator.
>
> select * from unnest(array[1,2,3,4,5]);

This is really handy! But there is a problem...

> The switch statement could probably be done in a different way, but
> there doesn't seem to be any good examples of how to return anyitem. If
> anyone have a better way, please let me know.

Why do you need the switch statement at all? array->elements is already
an array of Datums. Won't simply returning
array->elements[array->i]
work?

The problem is:
test=# select * from unnest('{1,2,3,4,5}'::int8[]);
unnest
----------
25314880
25314888
25314896
25314904
25314912
(5 rows)

Whereas simply returning the current Datum in array->elements returns
the correct result:

if (array->i < array->num_elements)
SRF_RETURN_NEXT(funcctx,array->elements[array->i++]);
else
SRF_RETURN_DONE(funcctx);

test=# select * from unnest('{1,2,3,4,5}'::int8[]);
unnest
--------
1
2
3
4
5
(5 rows)

Also works for the few other datatypes I checked.

Am I missing something obvious?

eric


From: Gavin Sherry <swm(at)linuxworld(dot)com(dot)au>
To: John Hansen <john(at)geeknet(dot)com(dot)au>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: unnest
Date: 2004-11-09 02:18:21
Message-ID: Pine.LNX.4.58.0411091315420.11622@linuxworld.com.au
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Fri, 5 Nov 2004, John Hansen wrote:

> Attached, array -> rows iterator.
>
> select * from unnest(array[1,2,3,4,5]);
>
> Unnest
> ---------------
> 1
> 2
> 3
> 4
> 5
> 5 rows

This mechanism is actually designed for the multiset data type in SQL.
AFAICT, our elementary one dimensional array handling mimics SQL
multisets. Is there any intention to bring this into line with the spec or
would that be mere pedantism?

Thanks,

Gavin


From: John Hansen <john(at)geeknet(dot)com(dot)au>
To: "Eric B(dot)Ridge" <ebr(at)tcdi(dot)com>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: unnest
Date: 2004-11-09 03:26:49
Message-ID: 1099970808.4172.3.camel@localhost.localdomain
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

> > The switch statement could probably be done in a different way, but
> > there doesn't seem to be any good examples of how to return anyitem. If
> > anyone have a better way, please let me know.
>
> Why do you need the switch statement at all? array->elements is already
> an array of Datums. Won't simply returning
> array->elements[array->i]
> work?

yea,. sorry,. worked it out shortly after posting this, but forgot to
repost.... so here it is... attached.

> The problem is:
> test=# select * from unnest('{1,2,3,4,5}'::int8[]);
> unnest
> ----------
> 25314880
> 25314888
> 25314896
> 25314904
> 25314912
> (5 rows)

Attachment Content-Type Size
Makefile text/x-makefile 299 bytes
unnest.c text/x-csrc 2.2 KB
unnest.sql text/x-sql 107 bytes

From: Bruce Momjian <pgman(at)candle(dot)pha(dot)pa(dot)us>
To: John Hansen <john(at)geeknet(dot)com(dot)au>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: unnest
Date: 2004-11-29 02:18:32
Message-ID: 200411290218.iAT2IWW05787@candle.pha.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers


I assume this is not something for our PostgreSQL CVS, even the later
SRF implementation.

---------------------------------------------------------------------------

John Hansen wrote:
> Attached, array -> rows iterator.
>
> select * from unnest(array[1,2,3,4,5]);
>
> Unnest
> ---------------
> 1
> 2
> 3
> 4
> 5
> 5 rows
>
> The switch statement could probably be done in a different way, but
> there doesn't seem to be any good examples of how to return anyitem. If
> anyone have a better way, please let me know.
>
> Does anyone know how to check individual array elements for NULL values?
> PG_ARG_ISNULL() seems to return true if ANY array element is null; ex::
> array[1,2,3,null,4,5]
>
> Comments / improvements welcome.
>
> Kind regards,
>
> John
>

Content-Description: Makefile

[ Attachment, skipping... ]

Content-Description: unnest.c

[ Attachment, skipping... ]

Content-Description: unnest.sql

[ Attachment, skipping... ]

>
> ---------------------------(end of broadcast)---------------------------
> TIP 2: you can get off all lists at once with the unregister command
> (send "unregister YourEmailAddressHere" to majordomo(at)postgresql(dot)org)

--
Bruce Momjian | http://candle.pha.pa.us
pgman(at)candle(dot)pha(dot)pa(dot)us | (610) 359-1001
+ If your life is a hard drive, | 13 Roberts Road
+ Christ can be your backup. | Newtown Square, Pennsylvania 19073


From: Joe Conway <mail(at)joeconway(dot)com>
To: Bruce Momjian <pgman(at)candle(dot)pha(dot)pa(dot)us>
Cc: John Hansen <john(at)geeknet(dot)com(dot)au>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: unnest
Date: 2004-11-29 17:23:56
Message-ID: 41AB5B2C.1080204@joeconway.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Bruce Momjian wrote:
> I assume this is not something for our PostgreSQL CVS, even the later
> SRF implementation.

I agree with that assessment, at least in its present state. For example:

regression=# select * from unnest(array[[1,2,3],[4,5,6]]);
unnest
--------
1
2
3
4
5
6
(6 rows)

Per SQL99 I think that ought to return something like:

-- output faked
regression=# select * from unnest(array[[1,2,3],[4,5,6]]);
unnest
--------
{1,2,3}
{4,5,6}
(2 rows)

Problem is that a polymorphic SRF cannot (currently at least) both
accept and return type anyarray.

Joe


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Joe Conway <mail(at)joeconway(dot)com>
Cc: Bruce Momjian <pgman(at)candle(dot)pha(dot)pa(dot)us>, John Hansen <john(at)geeknet(dot)com(dot)au>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: unnest
Date: 2004-11-29 18:01:11
Message-ID: 20000.1101751271@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Joe Conway <mail(at)joeconway(dot)com> writes:
> Problem is that a polymorphic SRF cannot (currently at least) both
> accept and return type anyarray.

Beyond that, would the proposed function really be SQL-compliant other
than this one point? I had the idea that UNNEST required some
fundamental changes (but I might be confusing it with something else).

regards, tom lane