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 1kG4yV-0002TL-1c for pgsql-general@arkaria.postgresql.org; Wed, 09 Sep 2020 18:37:43 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kG4yT-0000Ek-GA for pgsql-general@arkaria.postgresql.org; Wed, 09 Sep 2020 18:37:41 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kG4yT-0000Ea-1O for pgsql-general@lists.postgresql.org; Wed, 09 Sep 2020 18:37:41 +0000 Received: from mail-il1-x144.google.com ([2607:f8b0:4864:20::144]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1kG4yM-0006KK-K2 for pgsql-general@lists.postgresql.org; Wed, 09 Sep 2020 18:37:40 +0000 Received: by mail-il1-x144.google.com with SMTP id h11so3269741ilj.11 for ; Wed, 09 Sep 2020 11:37:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=yo1jEk4XzJ0sPYpc21S19HHCFUIRlLkfhk/45PwpuaQ=; b=AKGh1L++FN8R4lfu8HBUBAYswSpCjyqg1bq9WUb26Md2kQ3nNKZFU6KP53ECQM7hqP r7ZUu/v543kjaxfZpScJJzRcRKUTIbq5/4P8msdRMcQ11h0LUfF/BMfVhAQ6MV67z0CE H8dGobtV2nY5pxA8HqetMwfOPeuoYU/UyeEHvV5bc3Lu1Tvl5a7tY2hLVczZS1n3j+UQ fgRswzez9MlJUkQSdS3HYf5/vmOFiDd0mMPcPwbaF28b3LcM3KlRrVxDmHlUIScoNGnn MlVSniKbdugvUC1Sg79Hw0glpXLmJGUF9oPEPF+j7x5hxRMH6NSsIetnoLMFrWnuXx83 /NgQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=yo1jEk4XzJ0sPYpc21S19HHCFUIRlLkfhk/45PwpuaQ=; b=aKeQCn3cAreEuYfwM7OP0ckLf9UhKCm/XkXx2wai7qcDDyA7WFiC8NdpkL4Yibw77z TEwmBmNqhXkihDcdPjvJBrIKwF631wLqpJmBS6Pd0aXGvqsZ19zLsuyzp++bwGfJADQu 984isM1VV037pOz7dB/GT0qoUy4daJI8F0ZuzIKOcr4LxIYKz49TrRaOs0JhN4/fXPZ9 P5CKWHmqwVtV0g8eo3neEwV/ggdhyWL5jREVF3v+FnZoi+HJrR0fSzB4ZWl0pS+mEpCn Xg1kZPyYl5Y1lSWqeB13oEG0N5IO7I/6BdjpNinCJsbuGTRjr1O+4bObQvvJjfw4lKSB /hog== X-Gm-Message-State: AOAM531bNNp3UPFpB1UrdIXfuchfaOqENu1wDX05Ie4fypfbYACjvavN fLFgqw9ma99Xn9WH4f+nrTTgehrue/CaPnKu6F8= X-Google-Smtp-Source: ABdhPJzRUCOYYE92U1s6sljGAa8HAIFeR2fZUds5kewBGUTtXuVHR7JPIjraHTcfUKkWvbydIv8VkKYLdYgP4v/oAX0= X-Received: by 2002:a92:c7c5:: with SMTP id g5mr4623518ilk.89.1599676653879; Wed, 09 Sep 2020 11:37:33 -0700 (PDT) MIME-Version: 1.0 References: <42661.1599671583@sss.pgh.pa.us> In-Reply-To: <42661.1599671583@sss.pgh.pa.us> From: Aner Perez Date: Wed, 9 Sep 2020 14:37:22 -0400 Message-ID: Subject: Re: Unexpected results when using GROUP BY GROUPING SETS and bind variables To: Tom Lane Cc: pgsql-general@lists.postgresql.org Content-Type: multipart/alternative; boundary="000000000000b5c96d05aee5befa" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --000000000000b5c96d05aee5befa Content-Type: text/plain; charset="UTF-8" Thanks Tom, I figured as much about the second query but I thought it would be safe to use the first version with the inlined indexing. I'm not sure if you're saying that the same query without the unindexed path column in the select would be safe. Like this: -- Do not GROUP BY or SELECT on path by itself -- SELECT path[1], path[2], path[3], path[4], sum(value) FROM bind_group_by GROUP BY GROUPING SETS ( (path[1], path[2], path[3], path[4]), (path[1], path[2], path[3]), (path[1], path[2]), (path[1]), () ) ORDER BY 1, 2, 3, 4; Or if using indexed path elements in the GROUP BY is the issue and I should put the array indexing in a subselect and do the aggregation in the outer select. Like this: -- Safer Subselect Version -- SELECT level1, level2, level3, level4, sum(value) FROM ( SELECT path[1] as level1, path[2] as level2, path[3] as level3, path[4] as level4, value FROM bind_group_by ) AS expanded GROUP BY GROUPING SETS ( (level1, level2, level3, level4), (level1, level2, level3), (level1, level2), (level1), () ) ORDER BY 1, 2, 3, 4; Thanks for the insight, - Aner On Wed, Sep 9, 2020 at 1:13 PM Tom Lane wrote: > Aner Perez writes: > > [ these queries don't give the same results: ] > > > SELECT path[1], path[2], path[3], path, sum(value) > > FROM bind_group_by > > GROUP BY GROUPING SETS ( > > (path[1], path[2], path[3], path), > > (path[1], path[2], path[3]), > > (path[1], path[2]), > > (path[1]), > > () > > ) > > ORDER BY 1, 2, 3, 4 > > ; > > > PREPARE prepared_group_by (int, int, int, int, int, int, int, int, int, > > int, int, int) AS > > SELECT path[$1], path[$2], path[$3], path, sum(value) > > FROM bind_group_by > > GROUP BY GROUPING SETS ( > > (path[$4], path[$5], path[$6], path), > > (path[$7], path[$8], path[$9]), > > (path[$10], path[$11]), > > (path[$12]), > > () > > ) > > ORDER BY 1, 2, 3, 4 > > ; > > EXECUTE prepared_group_by (1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 1); > > I think you're kind of shooting yourself in the foot here by writing > a query with more than one possible interpretation. Since you have > GROUP BY items that are both the whole "path" array and specific > elements of it, it's not clear whether a targetlist reference such > as "path[1]" is meant to refer to the GROUP BY item "path[1]", or > to be a subscripting operator applied to the GROUP BY item "path". > > It appears that the parser makes the first choice in your first > query, although that seems like an implementation detail that > I wouldn't care to bet on going forward. > > In the second query, the parser is certainly not going to think that > "path[$1]" matches "path[$4]" or any of the other single-element > GROUP BY items, so it decides that it means subscripting the "path" > item. Now the result will be NULL for any grouping set that doesn't > include the "path" item. > > I haven't gone through the results one-by-one, but just by eyeball > I think this is sufficient to explain the discrepancies. > > (I note that the SQL standard only allows GROUP BY items to be > simple column references. Maybe that's not an arbitrary restriction > but is meant to forestall this sort of ambiguity? Hard to be sure.) > > regards, tom lane > --000000000000b5c96d05aee5befa Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Thanks Tom,

I figured as muc= h about the second query but I thought it would be safe to use the first ve= rsion with the inlined indexing.
I'm not sure if you're s= aying that the same query without the unindexed path column in the select w= ould be safe.=C2=A0 Like this:

-- Do not GROUP BY or SELECT on path by itself --=
SELECT path[1], path[2], p= ath[3], path[4], sum(value)
FROM bind_group_by
GROUP BY GROUPING SETS= (
=C2=A0 =C2=A0 (path[1], path[2], path[3], path[4]),
=C2=A0 =C2=A0 = (path[1], path[2], path[3]),
=C2=A0 =C2=A0 (path[1], path[2]),
=C2=A0= =C2=A0 (path[1]),
=C2=A0 =C2=A0 ()
)
ORDER BY 1, 2, 3, 4;
<= /div>

Or if using indexed path elements in the GROUP BY = is the issue and I should put the array indexing in a subselect and do the = aggregation in the outer select.=C2=A0 Like this:

= -- Safer Subselect Version --<= /div>
SELECT level1, level2, leve= l3, level4, sum(value)
FROM (
=C2=A0 =C2=A0 SELECT path[1] as level1,= path[2] as level2, path[3] as level3, path[4] as level4, value
=C2=A0 = =C2=A0 FROM bind_group_by
) AS expanded
GROUP BY GROUPING SETS (
= =C2=A0 =C2=A0 (level1, level2, level3, level4),
=C2=A0 =C2=A0 (level1, l= evel2, level3),
=C2=A0 =C2=A0 (level1, level2),
=C2=A0 =C2=A0 (level1= ),
=C2=A0 =C2=A0 ()
)
ORDER BY 1, 2, 3, 4;

Thanks for the insight,

=C2=A0 - Aner

On Wed, Sep 9, 2020 at 1:13 PM Tom Lane <tgl@sss.pgh.pa.us> wrote:
Aner Perez <aner.perez+pgsql-general@gmail.com<= /a>> writes:
> [ these queries don't give the same results: ]

> SELECT path[1], path[2], path[3], path, sum(value)
> FROM bind_group_by
> GROUP BY GROUPING SETS (
>=C2=A0 =C2=A0 =C2=A0(path[1], path[2], path[3], path),
>=C2=A0 =C2=A0 =C2=A0(path[1], path[2], path[3]),
>=C2=A0 =C2=A0 =C2=A0(path[1], path[2]),
>=C2=A0 =C2=A0 =C2=A0(path[1]),
>=C2=A0 =C2=A0 =C2=A0()
> )
> ORDER BY 1, 2, 3, 4
> ;

> PREPARE prepared_group_by (int, int, int, int, int, int, int, int, int= ,
> int, int, int) AS
>=C2=A0 =C2=A0 =C2=A0SELECT path[$1], path[$2], path[$3], path, sum(valu= e)
>=C2=A0 =C2=A0 =C2=A0FROM bind_group_by
>=C2=A0 =C2=A0 =C2=A0GROUP BY GROUPING SETS (
>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0(path[$4], path[$5], path[$6], path),=
>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0(path[$7], path[$8], path[$9]),
>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0(path[$10], path[$11]),
>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0(path[$12]),
>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0()
>=C2=A0 =C2=A0 =C2=A0)
>=C2=A0 =C2=A0 =C2=A0ORDER BY 1, 2, 3, 4
> ;
> EXECUTE prepared_group_by (1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 1);

I think you're kind of shooting yourself in the foot here by writing a query with more than one possible interpretation.=C2=A0 Since you have GROUP BY items that are both the whole "path" array and specific<= br> elements of it, it's not clear whether a targetlist reference such
as "path[1]" is meant to refer to the GROUP BY item "path[1]= ", or
to be a subscripting operator applied to the GROUP BY item "path"= .

It appears that the parser makes the first choice in your first
query, although that seems like an implementation detail that
I wouldn't care to bet on going forward.

In the second query, the parser is certainly not going to think that
"path[$1]" matches "path[$4]" or any of the other singl= e-element
GROUP BY items, so it decides that it means subscripting the "path&quo= t;
item.=C2=A0 Now the result will be NULL for any grouping set that doesn'= ;t
include the "path" item.

I haven't gone through the results one-by-one, but just by eyeball
I think this is sufficient to explain the discrepancies.

(I note that the SQL standard only allows GROUP BY items to be
simple column references.=C2=A0 Maybe that's not an arbitrary restricti= on
but is meant to forestall this sort of ambiguity?=C2=A0 Hard to be sure.)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 regards, tom lane
--000000000000b5c96d05aee5befa--