Allow pushdown of HAVING clauses with grouping sets
authorRichard Guo <[email protected]>
Wed, 9 Oct 2024 08:19:04 +0000 (17:19 +0900)
committerRichard Guo <[email protected]>
Wed, 9 Oct 2024 08:19:04 +0000 (17:19 +0900)
commit67a54b9e83d331eadd3a595e6c3bfec06288d2c4
tree98bde85da279ae7a3f0441323e32d00eb8f46697
parent828e94c9d2fd87c06a75354361543119d9937068
Allow pushdown of HAVING clauses with grouping sets

In some cases, we may want to transfer a HAVING clause into WHERE in
hopes of eliminating tuples before aggregation instead of after.

Previously, we couldn't do this if there were any nonempty grouping
sets, because we didn't have a way to tell if the HAVING clause
referenced any columns that were nullable by the grouping sets, and
moving such a clause into WHERE could potentially change the results.

Now, with expressions marked nullable by grouping sets with the RT
index of the RTE_GROUP RTE, it is much easier to identify those
clauses that reference any nullable-by-grouping-sets columns: we just
need to check if the RT index of the RTE_GROUP RTE is present in the
clause.  For other HAVING clauses, they can be safely pushed down.

Author: Richard Guo
Discussion: https://postgr.es/m/CAMbWs4-NpzPgtKU=hgnvyn+J-GanxQCjrUi7piNzZ=upiCV=2Q@mail.gmail.com
src/backend/optimizer/plan/planner.c
src/test/regress/expected/groupingsets.out
src/test/regress/sql/groupingsets.sql