pg.ddx.io pgsql-general@postgresql.org mailing list archive
help / color / mirror / Atom feedHow to unnest nested arrays
5+ messages / 3 participants
[nested] [flat]
* How to unnest nested arrays
@ 2020-04-07 01:11 Guyren Howe <guyren@gmail.com>
0 siblings, 1 reply; 5+ messages in thread
From: Guyren Howe @ 2020-04-07 01:11 UTC (permalink / raw)
To: pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>
Consider this:
select (array[array[1, 2], array[3, 4]])[i:i]
from generate_subscripts(array[array[1, 2], array[3, 4]], 1) i
which produces:
{{1,2}}
{{3,4}}
I expect and want, from that source:
{1, 2}
{3, 4}
These don’t work:
select (array[array[1, 2], array[3, 4]])[i:i][:]
{{1,2}}
{{3,4}}
select (array[array[1, 2], array[3, 4]])[i:i][1:1]
{{1}}
{{3}}
Also: could we _please_ get a version of unnest that doesn’t explode any number of dimensions into 1?
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: How to unnest nested arrays
@ 2020-04-07 02:44 David G. Johnston <david.g.johnston@gmail.com>
parent: Guyren Howe <guyren@gmail.com>
0 siblings, 2 replies; 5+ messages in thread
From: David G. Johnston @ 2020-04-07 02:44 UTC (permalink / raw)
To: Guyren Howe <guyren@gmail.com>; +Cc: pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>
On Mon, Apr 6, 2020 at 6:12 PM Guyren Howe <guyren@gmail.com> wrote:
> Consider this:
>
> *select* (*array*[*array*[1, 2], *array*[3, 4]])[i:i]
> *from* *generate_subscripts*(*array*[*array*[1, 2], *array*[3, 4]], 1) i
>
> which produces:
>
> {{1,2}}
> {{3,4}}
>
> I expect and want, from that source:
>
> {1, 2}
> {3, 4}
>
>
> Also: could we _please_ get a version of unnest that doesn’t explode any
> number of dimensions into 1?
>
The inability to reduce the number of dimensions of an existing array and
the behavior of unnest are both consequences of the implementation of
arrays in PostgreSQL. If PostgreSQL could do what your main question is
asking - using arrays only - then a function to unwrap an array one
dimension at a time would likely already exist.
The phrase "explode any number of dimensions into 1 [dimension]" is not
accurate, unnest does away with the array entirely and gives you back its
contents, one cell per row. It is in fact removing all nesting embedded
within the array. There is no dimension because there is no longer an
array and the scalar types do not have dimensions - they are
non-dimensioned.
The best you can probably do in SQL is take a text representation of the
data and munge it. You'll probably find array_to_string(array, delim)
useful in that regard. There may be an extension in the wild that does
this...?
You might also investigate whether a function written in pl/perl or
pl/python gets enough information, or has better tooling available, to
handle this more gracefully.
JSON maybe...though at that point you may want to just consider changing
the model.
David J.
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: How to unnest nested arrays
@ 2020-04-07 05:03 Pavel Stehule <pavel.stehule@gmail.com>
parent: David G. Johnston <david.g.johnston@gmail.com>
1 sibling, 0 replies; 5+ messages in thread
From: Pavel Stehule @ 2020-04-07 05:03 UTC (permalink / raw)
To: David G. Johnston <david.g.johnston@gmail.com>; +Cc: Guyren Howe <guyren@gmail.com>; pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>
út 7. 4. 2020 v 4:44 odesílatel David G. Johnston <
david.g.johnston@gmail.com> napsal:
> On Mon, Apr 6, 2020 at 6:12 PM Guyren Howe <guyren@gmail.com> wrote:
>
>> Consider this:
>>
>> *select* (*array*[*array*[1, 2], *array*[3, 4]])[i:i]
>> *from* *generate_subscripts*(*array*[*array*[1, 2], *array*[3, 4]], 1) i
>>
>> which produces:
>>
>> {{1,2}}
>> {{3,4}}
>>
>> I expect and want, from that source:
>>
>> {1, 2}
>> {3, 4}
>>
>>
>> Also: could we _please_ get a version of unnest that doesn’t explode any
>> number of dimensions into 1?
>>
>
> The inability to reduce the number of dimensions of an existing array and
> the behavior of unnest are both consequences of the implementation of
> arrays in PostgreSQL. If PostgreSQL could do what your main question is
> asking - using arrays only - then a function to unwrap an array one
> dimension at a time would likely already exist.
>
> The phrase "explode any number of dimensions into 1 [dimension]" is not
> accurate, unnest does away with the array entirely and gives you back its
> contents, one cell per row. It is in fact removing all nesting embedded
> within the array. There is no dimension because there is no longer an
> array and the scalar types do not have dimensions - they are
> non-dimensioned.
>
> The best you can probably do in SQL is take a text representation of the
> data and munge it. You'll probably find array_to_string(array, delim)
> useful in that regard. There may be an extension in the wild that does
> this...?
>
> You might also investigate whether a function written in pl/perl or
> pl/python gets enough information, or has better tooling available, to
> handle this more gracefully.
>
> JSON maybe...though at that point you may want to just consider changing
> the model.
>
is possible to write own function in PLpgSQL
CREATE OR REPLACE FUNCTION unnest_nested(a anyarray, OUT r anyarray)
RETURNS SETOF anyarray AS $$
BEGIN
FOREACH r SLICE 1 IN ARRAY a
LOOP
RETURN NEXT;
END LOOP;
END;
$$ LANGUAGE plpgsql;
postgres=# select unnest_nested(array[array[1, 2], array[3, 4]]);
┌───────────────┐
│ unnest_nested │
╞═══════════════╡
│ {1,2} │
│ {3,4} │
└───────────────┘
(2 rows)
Regards
Pavel
>
> David J.
>
>
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: How to unnest nested arrays
@ 2020-04-07 05:24 Guyren Howe <guyren@gmail.com>
parent: David G. Johnston <david.g.johnston@gmail.com>
1 sibling, 1 reply; 5+ messages in thread
From: Guyren Howe @ 2020-04-07 05:24 UTC (permalink / raw)
To: David G. Johnston <david.g.johnston@gmail.com>; +Cc: pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>
> On Apr 6, 2020, at 19:44 , David G. Johnston <david.g.johnston@gmail.com> wrote:
>
> On Mon, Apr 6, 2020 at 6:12 PM Guyren Howe <guyren@gmail.com <mailto:guyren@gmail.com>> wrote:
> Consider this:
>
> select (array[array[1, 2], array[3, 4]])[i:i]
> from generate_subscripts(array[array[1, 2], array[3, 4]], 1) i
>
> which produces:
>
> {{1,2}}
> {{3,4}}
>
> I expect and want, from that source:
>
> {1, 2}
> {3, 4}
>
>
> Also: could we _please_ get a version of unnest that doesn’t explode any number of dimensions into 1?
Here’s a solution in pure SQL, for reference:
CREATE OR REPLACE FUNCTION public.pairwise(
cards card[]
)
RETURNS table(c1 card, c2 card)
LANGUAGE sql
AS $function$
with
individual_cards as (
select
*
from
unnest(cards) with ordinality c
)
select
c(c1.suit, c1.rank),
c(c2.suit, c2.rank)
from
individual_cards c1 join
individual_cards c2 on c1.ordinality = c2.ordinality - 1
where
c1.ordinality % 2 = 1
$function$
;
Given that Postgres often (with good cause) touts its type system, it’s a shame that this basic structured type is great in many ways, but seriously flawed in really simple ones.
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: How to unnest nested arrays
@ 2020-04-07 05:49 Pavel Stehule <pavel.stehule@gmail.com>
parent: Guyren Howe <guyren@gmail.com>
0 siblings, 0 replies; 5+ messages in thread
From: Pavel Stehule @ 2020-04-07 05:49 UTC (permalink / raw)
To: Guyren Howe <guyren@gmail.com>; +Cc: David G. Johnston <david.g.johnston@gmail.com>; pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>
út 7. 4. 2020 v 7:25 odesílatel Guyren Howe <guyren@gmail.com> napsal:
>
>
> On Apr 6, 2020, at 19:44 , David G. Johnston <david.g.johnston@gmail.com>
> wrote:
>
> On Mon, Apr 6, 2020 at 6:12 PM Guyren Howe <guyren@gmail.com> wrote:
>
>> Consider this:
>>
>> *select* (*array*[*array*[1, 2], *array*[3, 4]])[i:i]
>> *from* *generate_subscripts*(*array*[*array*[1, 2], *array*[3, 4]], 1) i
>>
>> which produces:
>>
>> {{1,2}}
>> {{3,4}}
>>
>> I expect and want, from that source:
>>
>> {1, 2}
>> {3, 4}
>>
>>
>> Also: could we _please_ get a version of unnest that doesn’t explode any
>> number of dimensions into 1?
>>
>
> Here’s a solution in pure SQL, for reference:
>
> *CREATE* *OR* *REPLACE* *FUNCTION* public.pairwise(
> cards card[]
> )
> *RETURNS* *table*(c1 card, c2 card)
> *LANGUAGE* *sql*
> *AS* *$function$*
> *with*
> individual_cards *as* (
> *select*
> *
> *from*
> *unnest*(cards) *with* *ordinality* c
> )
>
> *select*
> c(c1.suit, c1.*rank*),
> c(c2.suit, c2.*rank*)
> *from*
> individual_cards c1 *join*
> individual_cards c2 *on* c1.*ordinality* = c2.*ordinality* - 1
> *where*
> c1.*ordinality* % 2 = 1
>
>
> *$function$*
> ;
>
> Given that Postgres often (with good cause) touts its type system, it’s a
> shame that this basic structured type is great in many ways, but seriously
> flawed in really simple ones.
>
This task is not hard, but it is not supported by any special syntax. There
are two forces - power of syntax, and complexity of syntax.
It can be reduced little bit
create or replace function unnest_nested2(anyarray)
returns setof anyarray as $$
select array_agg(v)
from unnest($1) with ordinality v
group by (ordinality - 1) / array_length($1,1)
$$ language sql;
>
^ permalink raw reply [nested|flat] 5+ messages in thread
end of thread, other threads:[~2020-04-07 05:49 UTC | newest]
Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2020-04-07 01:11 How to unnest nested arrays Guyren Howe <guyren@gmail.com>
2020-04-07 02:44 ` David G. Johnston <david.g.johnston@gmail.com>
2020-04-07 05:03 ` Pavel Stehule <pavel.stehule@gmail.com>
2020-04-07 05:24 ` Guyren Howe <guyren@gmail.com>
2020-04-07 05:49 ` Pavel Stehule <pavel.stehule@gmail.com>
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