From: Tom Lane <tgl@sss.pgh.pa.us>
To: Andrew Gierth <andrew@tao11.riddles.org.uk>
Cc: Heikki Linnakangas <hlinnaka@iki.fi>
Cc: Pg Bugs <pgsql-bugs@postgresql.org>
Subject: Re: Improper const-evaluation of HAVING with grouping sets and subquery pullup
Date: Wed, 25 Oct 2017 15:29:08 -0400
Message-ID: <20857.1508959748@sss.pgh.pa.us> (raw)
In-Reply-To: <17920.1508341358@sss.pgh.pa.us>
References: <7dbdcf5c-b5a6-ef89-4958-da212fe10176@iki.fi>
<1197.1507820787@sss.pgh.pa.us>
<87efq3lq93.fsf@news-spur.riddles.org.uk>
<87a80rli2w.fsf@news-spur.riddles.org.uk>
<32319.1508163874@sss.pgh.pa.us>
<8760bfku2u.fsf@news-spur.riddles.org.uk>
<2431.1508166918@sss.pgh.pa.us>
<871sm3ktl7.fsf@news-spur.riddles.org.uk>
<aefc657e-edb2-64d5-6df1-a0828f6e9104@iki.fi>
<87tvyyjg2s.fsf@news-spur.riddles.org.uk>
<26650.1508339040@sss.pgh.pa.us>
<17920.1508341358@sss.pgh.pa.us>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-bugs>
I wrote:
>> We should have used ressortgroupref matching to prevent this, but without
>> having checked the code right now, I think that we might only apply that
>> logic to non-Var tlist entries. If the Agg output tlist had mentioned
>> column 2 not column 1 of the child node, I bet we'd get the right answer.
> Indeed, the attached patch passes all regression tests and produces the
> same answers for both of Heikki's examples:
I did some back-porting work on this. The patch applies back to 9.5
where grouping sets were introduced, but it only fixes the problem
in 9.6 and later --- in 9.5, you still get the wrong output :-(.
Bisecting says the behavior changed at commit 3fc6e2d7f "Make the upper
part of the planner work by generating and comparing Paths". Ugh.
We are certainly not back-patching that into 9.5, and I'm disinclined
even to try to identify exactly why that commit changed the behavior.
Given that this is such a weird corner case, and we've not heard
complaints from actual users about it, I'm inclined just to apply
the patch in 9.6 and up and call it good.
regards, tom lane
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugsAttachments:check-sortgroupref-for-Vars-too-2.patch (text/x-diff, 3.0K) downloaddiff --git a/src/backend/optimizer/plan/setrefs.c b/src/backend/optimizer/plan/setrefs.cindex 1382b67..fa9a3f0 100644
*** a/src/backend/optimizer/plan/setrefs.c
--- b/src/backend/optimizer/plan/setrefs.c
*************** set_upper_references(PlannerInfo *root,
*** 1744,1751 ****
TargetEntry *tle = (TargetEntry *) lfirst(l);
Node *newexpr;
! /* If it's a non-Var sort/group item, first try to match by sortref */
! if (tle->ressortgroupref != 0 && !IsA(tle->expr, Var))
{
newexpr = (Node *)
search_indexed_tlist_for_sortgroupref(tle->expr,
--- 1744,1751 ----
TargetEntry *tle = (TargetEntry *) lfirst(l);
Node *newexpr;
! /* If it's a sort/group item, first try to match by sortref */
! if (tle->ressortgroupref != 0)
{
newexpr = (Node *)
search_indexed_tlist_for_sortgroupref(tle->expr,
*************** search_indexed_tlist_for_non_var(Expr *n
*** 2113,2119 ****
/*
* search_indexed_tlist_for_sortgroupref --- find a sort/group expression
- * (which is assumed not to be just a Var)
*
* If a match is found, return a Var constructed to reference the tlist item.
* If no match, return NULL.
--- 2113,2118 ----diff --git a/src/test/regress/expected/groupingsets.out b/src/test/regress/expected/groupingsets.outindex fd618af..833d515 100644
*** a/src/test/regress/expected/groupingsets.out
--- b/src/test/regress/expected/groupingsets.out
*************** select a, d, grouping(a,b,c)
*** 360,365 ****
--- 360,394 ----
2 | 2 | 2
(4 rows)
+ -- check that distinct grouping columns are kept separate+ -- even if they are equal()+ explain (costs off)+ select g as alias1, g as alias2+ from generate_series(1,3) g+ group by alias1, rollup(alias2);+ QUERY PLAN + ------------------------------------------------+ GroupAggregate+ Group Key: g, g+ Group Key: g+ -> Sort+ Sort Key: g+ -> Function Scan on generate_series g+ (6 rows)+ + select g as alias1, g as alias2+ from generate_series(1,3) g+ group by alias1, rollup(alias2);+ alias1 | alias2 + --------+--------+ 1 | 1+ 1 | + 2 | 2+ 2 | + 3 | 3+ 3 | + (6 rows)+
-- simple rescan tests
select a, b, sum(v.x)
from (values (1),(2)) v(x), gstest_data(v.x)
diff --git a/src/test/regress/sql/groupingsets.sql b/src/test/regress/sql/groupingsets.sqlindex 564ebc9..2b4ab69 100644
*** a/src/test/regress/sql/groupingsets.sql
--- b/src/test/regress/sql/groupingsets.sql
*************** select a, d, grouping(a,b,c)
*** 141,146 ****
--- 141,157 ----
from gstest3
group by grouping sets ((a,b), (a,c));
+ -- check that distinct grouping columns are kept separate+ -- even if they are equal()+ explain (costs off)+ select g as alias1, g as alias2+ from generate_series(1,3) g+ group by alias1, rollup(alias2);+ + select g as alias1, g as alias2+ from generate_series(1,3) g+ group by alias1, rollup(alias2);+
-- simple rescan tests
select a, b, sum(v.x)
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-bugs@postgresql.org
Cc: tgl@sss.pgh.pa.us, andrew@tao11.riddles.org.uk, hlinnaka@iki.fi
Subject: Re: Improper const-evaluation of HAVING with grouping sets and subquery pullup
In-Reply-To: <20857.1508959748@sss.pgh.pa.us>
References: <7dbdcf5c-b5a6-ef89-4958-da212fe10176@iki.fi> <1197.1507820787@sss.pgh.pa.us> <87efq3lq93.fsf@news-spur.riddles.org.uk> <87a80rli2w.fsf@news-spur.riddles.org.uk> <32319.1508163874@sss.pgh.pa.us> <8760bfku2u.fsf@news-spur.riddles.org.uk> <2431.1508166918@sss.pgh.pa.us> <871sm3ktl7.fsf@news-spur.riddles.org.uk> <aefc657e-edb2-64d5-6df1-a0828f6e9104@iki.fi> <87tvyyjg2s.fsf@news-spur.riddles.org.uk> <26650.1508339040@sss.pgh.pa.us> <17920.1508341358@sss.pgh.pa.us> <20857.1508959748@sss.pgh.pa.us>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
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