| From: | Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com> | 
|---|---|
| To: | Jim Nasby <Jim(dot)Nasby(at)bluetreble(dot)com> | 
| Cc: | Andrew Dunstan <andrew(at)dunslane(dot)net>, PostgreSQL Hackers <pgsql-hackers(at)postgresql(dot)org> | 
| Subject: | Re: proposal: row_to_array function | 
| Date: | 2015-01-27 20:33:43 | 
| Message-ID: | CAFj8pRCuMu4r=bO36dsNueSO9mYXZFgg+7qW8jXChKJcyEHyRQ@mail.gmail.com | 
| Views: | Whole Thread | Raw Message | Download mbox | Resend email | 
| Thread: | |
| Lists: | pgsql-hackers | 
Example:
postgres=# do $$
declare r record;
declare k text; v text;
begin
  for r in select * from foo loop
    foreach k,v in array row_to_array(r) loop
      raise notice 'k: %, v: %', k, v;
    end loop;
  end loop;
end;
$$;
NOTICE:  k: a, v: 2
NOTICE:  k: b, v: NAZDAR
NOTICE:  k: c, v: 2015-01-27
NOTICE:  k: a, v: 2
NOTICE:  k: b, v: AHOJ
NOTICE:  k: c, v: 2015-01-27
DO
Regards
Pavel
2015-01-27 21:26 GMT+01:00 Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>:
> Hello
>
> here is a initial version of row_to_array function - transform any row to
> array in format proposed by Andrew.
>
> Regards
>
> Pavel
>
> 2015-01-27 19:58 GMT+01:00 Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>:
>
>> Hi
>>
>> 2015-01-27 11:41 GMT+01:00 Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>:
>>
>>>
>>>
>>> 2015-01-26 21:44 GMT+01:00 Jim Nasby <Jim(dot)Nasby(at)bluetreble(dot)com>:
>>>
>>>> On 1/25/15 4:23 AM, Pavel Stehule wrote:
>>>>
>>>>>
>>>>> I tested a concept iteration over array in format [key1, value1, key2,
>>>>> value2, .. ] - what is nice, it works for [[key1,value1],[key2, value2],
>>>>> ...] too
>>>>>
>>>>> It is only a few lines more to current code, and this change doesn't
>>>>> break a compatibility.
>>>>>
>>>>> Do you think, so this patch is acceptable?
>>>>>
>>>>> Ideas, comments?
>>>>>
>>>>
>>>> Aside from fixing the comments... I think this needs more tests on
>>>> corner cases. For example, what happens when you do
>>>>
>>>> foreach a, b, c in array(array(1,2),array(3,4)) ?
>>>>
>>>
>>> it is relative simple behave -- empty values are NULL
>>>
>>> array(1,2),array(3,4) -- do you think ARRAY[[1,2],[3,4]] is effectively
>>> ARRAY[1,2,3,4]
>>>
>>>
>>>>
>>>> Or the opposite case of
>>>>
>>>> foreach a,b in array(array(1,2,3))
>>>>
>>>> Also, what about:
>>>>
>>>> foreach a,b in '{{{1,2},{3,4}},{{5,6},{7,8}}}'::int[] ?
>>>
>>>
>>>
>>>  postgres=# select array(select
>>> unnest('{{{1,2},{3,4}},{{5,6},{7,8}}}'::int[]));
>>>        array
>>> -------------------
>>>  {1,2,3,4,5,6,7,8}
>>> (1 row)
>>>
>>> so it generate pairs {1,2}{3,4},{5,6},{7,8}
>>>
>>
>> I fixed situation when array has not enough elements.
>>
>> More tests, simple doc
>>
>> Regards
>>
>> Pavel
>>
>>
>>>
>>> Regards
>>>
>>> Pavel Stehule
>>>
>>>
>>>> --
>>>> Jim Nasby, Data Architect, Blue Treble Consulting
>>>> Data in Trouble? Get it in Treble! http://BlueTreble.com
>>>>
>>>
>>>
>>
>
| From | Date | Subject | |
|---|---|---|---|
| Next Message | Tom Lane | 2015-01-27 20:56:22 | Re: jsonb, unicode escapes and escaped backslashes | 
| Previous Message | Pavel Stehule | 2015-01-27 20:26:54 | Re: proposal: row_to_array function |