pgsql: Mark expressions nullable by grouping sets

From: Richard Guo <rguo(at)postgresql(dot)org>
To: pgsql-committers(at)lists(dot)postgresql(dot)org
Subject: pgsql: Mark expressions nullable by grouping sets
Date: 2024-09-10 03:38:07
Message-ID: E1snrhe-000OKD-PI@gemulon.postgresql.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-committers

Mark expressions nullable by grouping sets

When generating window_pathkeys, distinct_pathkeys, or sort_pathkeys,
we failed to realize that the grouping/ordering expressions might be
nullable by grouping sets. As a result, we may incorrectly deem that
the PathKeys are redundant by EquivalenceClass processing and thus
remove them from the pathkeys list. That would lead to wrong results
in some cases.

To fix this issue, we mark the grouping expressions nullable by
grouping sets if that is the case. If the grouping expression is a
Var or PlaceHolderVar or constructed from those, we can just add the
RT index of the RTE_GROUP RTE to the existing nullingrels field(s);
otherwise we have to add a PlaceHolderVar to carry on the nullingrel
bit.

However, we have to manually remove this nullingrel bit from
expressions in various cases where these expressions are logically
below the grouping step, such as when we generate groupClause pathkeys
for grouping sets, or when we generate PathTarget for initial input to
grouping nodes.

Furthermore, in set_upper_references, the targetlist and quals of an
Agg node should have nullingrels that include the effects of the
grouping step, ie they will have nullingrels equal to the input
Vars/PHVs' nullingrels plus the nullingrel bit that references the
grouping RTE. In order to perform exact nullingrels matches, we also
need to manually remove this nullingrel bit.

Bump catversion because this changes the querytree produced by the
parser.

Thanks to Tom Lane for the idea to invent a new kind of RTE.

Per reports from Geoff Winkless, Tobias Wendorff, Richard Guo from
various threads.

Author: Richard Guo
Reviewed-by: Ashutosh Bapat, Sutou Kouhei
Discussion: https://postgr.es/m/CAMbWs4_dp7e7oTwaiZeBX8+P1rXw4ThkZxh1QG81rhu9Z47VsQ@mail.gmail.com

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/f5050f795aea67dfc40bbc429c8934e9439e22e7

Modified Files
--------------
src/backend/optimizer/path/equivclass.c | 12 ++
src/backend/optimizer/path/pathkeys.c | 14 +++
src/backend/optimizer/plan/initsplan.c | 4 +
src/backend/optimizer/plan/planner.c | 49 +++++++-
src/backend/optimizer/plan/setrefs.c | 23 ++++
src/backend/optimizer/util/var.c | 88 ++++++++++++-
src/backend/parser/parse_agg.c | 13 +-
src/include/catalog/catversion.h | 2 +-
src/include/optimizer/paths.h | 1 +
src/test/regress/expected/groupingsets.out | 191 +++++++++++++++++++++++++----
src/test/regress/sql/groupingsets.sql | 47 +++++++
11 files changed, 411 insertions(+), 33 deletions(-)

Browse pgsql-committers by date

  From Date Subject
Next Message Peter Eisentraut 2024-09-10 08:09:04 pgsql: Add amgettreeheight index AM API routine
Previous Message Richard Guo 2024-09-10 03:38:06 pgsql: Introduce an RTE for the grouping step