Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jLgjK-000648-WB for pgsql-general@arkaria.postgresql.org; Tue, 07 Apr 2020 05:24:59 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jLgjJ-0006md-Px for pgsql-general@arkaria.postgresql.org; Tue, 07 Apr 2020 05:24:57 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jLgjJ-0006mW-Gd for pgsql-general@lists.postgresql.org; Tue, 07 Apr 2020 05:24:57 +0000 Received: from mail-pg1-x543.google.com ([2607:f8b0:4864:20::543]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1jLgjC-0001MJ-1F for pgsql-general@lists.postgresql.org; Tue, 07 Apr 2020 05:24:57 +0000 Received: by mail-pg1-x543.google.com with SMTP id g32so1175056pgb.6 for ; Mon, 06 Apr 2020 22:24:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:subject:from:in-reply-to:date:cc:message-id:references :to; bh=DSgWqAT2+PVFFLBNLB0+TupBU8GHrJy+n9ABE0W+A3c=; b=VOKuHz0WXn8VLoRQZHTQMMC5BMPmlI3YRSXjsIGK+MPFhSoJgc5UrtZjMUl9E+PkPq QfBGsaUmqsGyEHuiVi1Eeu7VdkRebT0rDtwju+2eerA3hRSL/IzkWB3fT1V591S9p53p aHvDKp3rXVzSEDP26MyoMGnpfKujESHjCUSN9ZnYTzdQkpxX4banQ7+entegeTTDER2R cbpgjO/J98CTSQ69PGg9qNhrzgNoWazKzt9IsWrmSRFfXQ0cTbz90h6qubjbQGNKDoLs qTmVKeeH3fa9Xkc4j/MRvc5b2afPVohTNvh2aRgYRYmXh5cI3HkQFgR3hW7dkjOkh5i/ K3qg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:subject:from:in-reply-to:date:cc :message-id:references:to; bh=DSgWqAT2+PVFFLBNLB0+TupBU8GHrJy+n9ABE0W+A3c=; b=gtJIU/AoeO9iHr+BW6r2ma5zwKw8/QjFrGd8jIxrt+NtmNf268MqbOA+EpYj6f8Sqi HeK4AbvgWN4MNDK+Ten3PgoNyP4iZHHXDHipodJ/17mG8ZbCAj5E5VH7Y5Tf4shdTQUX BiD91kd1J+dBvaml9NhRJb4splBYGj+z//qJX1xugC7ldOb4NvVENmNYg470t5QJ7vrw irPqzOH1s+YJLwyYKKSu4EIcucOUrWcxaiwaL90izopmu2zveyous7IYRVIBUh0MBwcM VJ0k9VvA+PQX88lhgbbQyz6GzxPsBjGNmMuDfAlaKRE/9ppIfr5JIsgtehU9Mh2YWPEH wYyg== X-Gm-Message-State: AGi0PuaAYtioqIR0OlygRsMEME+nuF/xUTtYgUqZrEHNY9YrlN76ngfc 7IYm97HRcW6wm4W5i82nbNU= X-Google-Smtp-Source: APiQypLgpjuvinR8uGK5TQx7iVn+cMxA3SSGLh13Hg0YHjU2cSPMOE0NmRYxqtlvome4Rtv87U6nfw== X-Received: by 2002:aa7:95a8:: with SMTP id a8mr915898pfk.61.1586237087450; Mon, 06 Apr 2020 22:24:47 -0700 (PDT) Received: from ?IPv6:2600:8801:8800:b8::9? ([2600:8801:8800:b8::9]) by smtp.gmail.com with ESMTPSA id a18sm525817pjh.25.2020.04.06.22.24.46 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 06 Apr 2020 22:24:47 -0700 (PDT) Content-Type: multipart/alternative; boundary="Apple-Mail=_F7138B41-B0F2-43C4-BDB2-06602D10556F" Mime-Version: 1.0 (Mac OS X Mail 13.0 \(3608.60.0.2.5\)) Subject: Re: How to unnest nested arrays From: Guyren Howe In-Reply-To: Date: Mon, 6 Apr 2020 22:24:31 -0700 Cc: "pgsql-general@lists.postgresql.org" X-Mailbutler-Message-Id: EB486A06-A159-449E-87FB-A5EAD560282B Message-Id: <154C5B90-F6D0-4865-8774-85648D328BCD@gmail.com> References: <9BFD1A82-0B06-4B57-A5F4-8525E122B12B@gmail.com> To: "David G. Johnston" X-Mailer: Apple Mail (2.3608.60.0.2.5) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --Apple-Mail=_F7138B41-B0F2-43C4-BDB2-06602D10556F Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 > On Apr 6, 2020, at 19:44 , David G. Johnston = wrote: >=20 > On Mon, Apr 6, 2020 at 6:12 PM Guyren Howe > wrote: > Consider this: >=20 > select (array[array[1, 2], array[3, 4]])[i:i] > from generate_subscripts(array[array[1, 2], array[3, 4]], 1) i >=20 > which produces: >=20 > {{1,2}} > {{3,4}} >=20 > I expect and want, from that source: >=20 > {1, 2} > {3, 4} >=20 >=20 > Also: could we _please_ get a version of unnest that doesn=E2=80=99t = explode any number of dimensions into 1? Here=E2=80=99s 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=20 unnest(cards) with ordinality c ) select c(c1.suit, c1.rank), c(c2.suit, c2.rank) from=20 individual_cards c1 join individual_cards c2 on c1.ordinality =3D c2.ordinality - 1 where=20 c1.ordinality % 2 =3D 1 =20 $function$ ; Given that Postgres often (with good cause) touts its type system, = it=E2=80=99s a shame that this basic structured type is great in many = ways, but seriously flawed in really simple ones. --Apple-Mail=_F7138B41-B0F2-43C4-BDB2-06602D10556F Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=utf-8

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=E2=80=99t explode any number of dimensions into = 1?

Here=E2=80=99s 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 =3D c2.ordinality - 1
where 
    c1.ordinality = % 2 =3D 1

    

$function$
;

Given that Postgres often (with good cause) touts its type = system, it=E2=80=99s a shame that this basic structured type is great in = many ways, but seriously flawed in really simple ones.

3D""= --Apple-Mail=_F7138B41-B0F2-43C4-BDB2-06602D10556F--