Re: BUG #16585: Wrong filtering on a COALESCE field after using GROUPING SETS
Andrew Gierth <andrew@tao11.riddles.org.uk>
From: Andrew Gierth <andrew@tao11.riddles.org.uk>
To: Tom Lane <tgl@sss.pgh.pa.us>
Cc: Andy Fan <zhihui.fan1213@gmail.com>, David Rowley <dgrowleyml@gmail.com>,
pavelsivash@gmail.com,
PostgreSQL mailing lists <pgsql-bugs@lists.postgresql.org>
Date: 2020-08-21T13:04:50Z
Lists: pgsql-bugs
>>>>> "Tom" == Tom Lane <tgl@sss.pgh.pa.us> writes: Tom> One thing that I find curious is that if I alter this example to use Tom> grouping sets, say Tom> regression=# explain select * from (select hundred, ten, abs(hundred) a, count(*) c from tenk1 group by grouping sets (1,2)) ss where a = 42; Tom> QUERY PLAN Tom> ----------------------------------------------------------------- Tom> HashAggregate (cost=495.00..546.65 rows=2 width=20) Tom> Hash Key: tenk1.hundred Tom> Hash Key: tenk1.ten Tom> Filter: (abs(tenk1.hundred) = 42) Tom> -> Seq Scan on tenk1 (cost=0.00..445.00 rows=10000 width=8) Tom> (5 rows) Tom> i.e. it's not seeing the abs() condition as pushable below the Tom> aggregation. I'm not quite sure if that's a necessary restriction Tom> or a missed optimization. subquery_planner isn't transferring HAVING clauses to WHERE if that would cross a nontrivial GROUPING SETS. It could in theory do so by inspecting whether the referenced columns are in all grouping sets or none, but currently the planner doesn't have any reason to compute that intersection and it would add quite a bit of complexity to that specific point in the code. (Without grouping sets, a HAVING clause is movable to WHERE if it's non-volatile and has no aggregations, since that implies it must evaluate to the same value for each row in any group.) In this example, pushing the condition below the aggregate would be wrong anyway, no? -- Andrew (irc:RhodiumToad)
Commits
-
Avoid pushing quals down into sub-queries that have grouping sets.
- de627adaad3a 13.0 landed
- d3701bc8a25b 9.6.20 landed
- b439adcabbf9 11.10 landed
- 7edd36eae1fd 9.5.24 landed
- 6fa403e61241 10.15 landed
- 6b701eaaa90e 12.5 landed
- 4d346def1555 14.0 landed