Re: unnest - Mailing list pgsql-hackers

From Eric B.Ridge
Subject Re: unnest
Date
Msg-id 6920C9B9-31F4-11D9-9C25-000A95D98B3E@tcdi.com
Whole thread Raw
In response to unnest  ("John Hansen" <john@geeknet.com.au>)
Responses Re: unnest  (John Hansen <john@geeknet.com.au>)
List 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 returningarray->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



pgsql-hackers by date:

Previous
From: Oliver Jowett
Date:
Subject: Re: Increasing the length of pg_stat_activity.current_query...
Next
From: Gavin Sherry
Date:
Subject: Re: unnest