Re: Improper const-evaluation of HAVING with grouping sets and subquery pullup

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Andrew Gierth <andrew(at)tao11(dot)riddles(dot)org(dot)uk>
Cc: Heikki Linnakangas <hlinnaka(at)iki(dot)fi>, Pg Bugs <pgsql-bugs(at)postgresql(dot)org>
Subject: Re: Improper const-evaluation of HAVING with grouping sets and subquery pullup
Date: 2017-10-18 15:42:38
Message-ID: 17920.1508341358@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

I wrote:
> I think that Heikki's expectation is the correct one, and the reason the
> output looks the way it does is that setrefs.c is dropping the ball
> somehow and confusing the two "g" references. ...
> 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:

regression=# SELECT g as newalias1, g as newalias3
FROM generate_series(1,3) g
GROUP BY newalias1, ROLLUP(newalias3);
newalias1 | newalias3
-----------+-----------
1 | 1
3 | 3
2 | 2
2 |
3 |
1 |
(6 rows)

regards, tom lane

Attachment Content-Type Size
check-sortgroupref-for-Vars-too.patch text/x-diff 1.2 KB

In response to

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Andrew Dunstan 2017-10-18 15:46:57 Re: BUG #14849: jsonb_build_object doesn't like VARIADIC calls very much
Previous Message andrey_shvidky 2017-10-18 15:32:20 BUG #14861: Handle syntax_error