Re: proposal: row_to_array function
От | Pavel Stehule |
---|---|
Тема | Re: proposal: row_to_array function |
Дата | |
Msg-id | CAFj8pRBwPtONjMpMSPxkgTwK46O0FA6XXoOQksJeRS3Kdokxsg@mail.gmail.com обсуждение исходный текст |
Ответ на | Re: proposal: row_to_array function (Pavel Stehule <pavel.stehule@gmail.com>) |
Список | pgsql-hackers |
Dne 28.1.2015 0:25 "Jim Nasby" <Jim.Nasby@bluetreble.com> napsal(a):
>
> On 1/27/15 12:58 PM, Pavel Stehule wrote:
>>
>> 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
>
>
> Hrm, this wasn't what I was expecting:
>
> + select foreach_test_ab(array[1,2,3,4]);
> + NOTICE: a: 1, b: 2
> + NOTICE: a: 3, b: 4
>
> I was expecting that foreach a,b array would be expecting something in the array to have a dimension of 2. :(It is inconsist (your expectation) with current implementation of FOREACH. It doesnt produce a array when SLICING is missing. And it doesnt calculate with dimensions.
I would not to change this rule. It is not ambigonuous and it allows to work with
1d, 2d, 3d dimensions array. You can process Andrew format well and my proposed format (2d array) well too.
CREATE OR REPLACE FUNCTION iterate_over_pairs(text[])
RETURNS void AS $$
DECLARE v1 text; v2 text; e text; i int := 0;
BEGIN
FOREACH e IN ARRAY $1 LOOP
IF i % 2 = 0 THEN v1 := e;
ELSE v2 := e; RAISE NOTICE 'v1: %, v2: %', v1, v2; END IF;
i := i + 1;
END LOOP;
END;
$$ LANGUAGE plpgsql;
postgres=# SELECT iterate_over_pairs(ARRAY[1,2,3,4]::text[]);
NOTICE: v1: 1, v2: 2
NOTICE: v1: 3, v2: 4
iterate_over_pairs
--------------------
(1 row)
postgres=# SELECT iterate_over_pairs(ARRAY[[1,2],[3,4]]::text[]);
NOTICE: v1: 1, v2: 2
NOTICE: v1: 3, v2: 4
iterate_over_pairs
--------------------
(1 row)
..
There can be differen behave when SLICING is used. There we can iterate exactly with dimensions. We can design a behave in this case?
>
> I think this is bad, because this:
>
> foreach_test_ab('{{1,2,3},{4,5,6}}'::int[]);
>
> will give you 1,2; 3,4; 5,6. I don't see any way that that makes sense. Even if it did make sense, I'm more concerned that adding this will seriously paint us into a corner when it comes to the (to me) more rational case of returning {1,2,3},{4,5,6}.
>
> I think we need to think some more about this, at least to make sure we're not painting ourselves into a corner for more appropriate array iteration.
>
> --
> Jim Nasby, Data Architect, Blue Treble Consulting
> Data in Trouble? Get it in Treble! http://BlueTreble.com
В списке pgsql-hackers по дате отправления: