From 52b5201fe1e308a876e73355e721448618c7132f Mon Sep 17 00:00:00 2001 From: Ishjaap Singh Date: Thu, 3 Dec 2026 07:55:17 +0000 Subject: [PATCH v1] Fix deparsing of grouping sets over duplicate output columns When two output columns have equal expressions, as in SELECT a AS x, a AS y, count(*) FROM t GROUP BY ROLLUP(1, 2) pg_get_viewdef() printed the grouping set as ROLLUP(a, a). When that is parsed again, both items bind to the first matching output column, so y is no longer nulled on its own, and a view gives different results after a dump and restore. Print a grouping set item as a column number when an earlier output column has an equal expression, which gives ROLLUP(a, 2). --- src/backend/utils/adt/ruleutils.c | 36 +++++++++++++++++++++- src/test/regress/expected/groupingsets.out | 29 +++++++++++++++++ src/test/regress/sql/groupingsets.sql | 21 +++++++++++++ 3 files changed, 85 insertions(+), 1 deletion(-) diff --git a/src/backend/utils/adt/ruleutils.c b/src/backend/utils/adt/ruleutils.c index b2d5327cc20..f4a8b637a7b 100644 --- a/src/backend/utils/adt/ruleutils.c +++ b/src/backend/utils/adt/ruleutils.c @@ -435,6 +435,7 @@ static void get_setop_query(Node *setOp, Query *query, static Node *get_rule_sortgroupclause(Index ref, List *tlist, bool force_colno, deparse_context *context); +static bool grouping_ref_needs_colno(Index ref, List *tlist); static void get_rule_groupingset(GroupingSet *gset, List *targetlist, bool omit_parens, deparse_context *context); static void get_rule_orderby(List *orderList, List *targetList, @@ -6649,6 +6650,38 @@ get_rule_sortgroupclause(Index ref, List *tlist, bool force_colno, return expr; } +/* + * Must a grouping set item be printed as a column number? + * + * When parsed again, an item printed as an expression binds to the first + * targetlist entry with an equal expression. If that is an earlier entry + * than the one the item refers to, the item would move to that column. + * Grouping sets null each column separately, so that changes the query's + * results. + */ +static bool +grouping_ref_needs_colno(Index ref, List *tlist) +{ + TargetEntry *tle = get_sortgroupref_tle(ref, tlist); + ListCell *l; + + /* resjunk entries have no column number */ + if (tle->resjunk) + return false; + + foreach(l, tlist) + { + TargetEntry *other = (TargetEntry *) lfirst(l); + + if (other == tle) + break; + if (equal(strip_implicit_coercions((Node *) other->expr), + tle->expr)) + return true; + } + return false; +} + /* * Display a GroupingSet */ @@ -6678,7 +6711,8 @@ get_rule_groupingset(GroupingSet *gset, List *targetlist, appendStringInfoString(buf, sep); get_rule_sortgroupclause(ref, targetlist, - false, context); + grouping_ref_needs_colno(ref, targetlist), + context); sep = ", "; } diff --git a/src/test/regress/expected/groupingsets.out b/src/test/regress/expected/groupingsets.out index 393d63cfb0c..b656d9ebf12 100644 --- a/src/test/regress/expected/groupingsets.out +++ b/src/test/regress/expected/groupingsets.out @@ -748,6 +748,35 @@ select pg_get_viewdef('gstest_view'::regclass, true); GROUP BY ROLLUP((a, b, c), (c, d)); (1 row) +-- Grouping set items over output columns with equal expressions must be +-- printed as column numbers. Printed as expressions, they would all bind +-- to the first such column when parsed again, which changes the results. +create temp view gstest_view_dup as select a as x, a as y, count(*) + from gstest2 group by 2, rollup (1, 2); +select pg_get_viewdef('gstest_view_dup'::regclass, true); + pg_get_viewdef +----------------------------- + SELECT a AS x, + + a AS y, + + count(*) AS count + + FROM gstest2 + + GROUP BY 2, ROLLUP(a, 2); +(1 row) + +do $$ +begin + execute 'create temp view gstest_view_dup2 as ' + || pg_get_viewdef('gstest_view_dup'::regclass); +end $$; +-- should return no rows +(select * from gstest_view_dup except all select * from gstest_view_dup2) +union all +(select * from gstest_view_dup2 except all select * from gstest_view_dup); + x | y | count +---+---+------- +(0 rows) + +drop view gstest_view_dup2, gstest_view_dup; -- Nested queries with 3 or more levels of nesting select(select (select grouping(a,b) from (values (1)) v2(c)) from (values (1,2)) v1(a,b) group by (a,b)) from (values(6,7)) v3(e,f) GROUP BY ROLLUP(e,f); grouping diff --git a/src/test/regress/sql/groupingsets.sql b/src/test/regress/sql/groupingsets.sql index 91c05adf432..2d5899413d0 100644 --- a/src/test/regress/sql/groupingsets.sql +++ b/src/test/regress/sql/groupingsets.sql @@ -301,6 +301,27 @@ CREATE VIEW gstest_view AS select a, b, grouping(a,b), sum(c), count(*), max(c) select pg_get_viewdef('gstest_view'::regclass, true); +-- Grouping set items over output columns with equal expressions must be +-- printed as column numbers. Printed as expressions, they would all bind +-- to the first such column when parsed again, which changes the results. +create temp view gstest_view_dup as select a as x, a as y, count(*) + from gstest2 group by 2, rollup (1, 2); + +select pg_get_viewdef('gstest_view_dup'::regclass, true); + +do $$ +begin + execute 'create temp view gstest_view_dup2 as ' + || pg_get_viewdef('gstest_view_dup'::regclass); +end $$; + +-- should return no rows +(select * from gstest_view_dup except all select * from gstest_view_dup2) +union all +(select * from gstest_view_dup2 except all select * from gstest_view_dup); + +drop view gstest_view_dup2, gstest_view_dup; + -- Nested queries with 3 or more levels of nesting select(select (select grouping(a,b) from (values (1)) v2(c)) from (values (1,2)) v1(a,b) group by (a,b)) from (values(6,7)) v3(e,f) GROUP BY ROLLUP(e,f); select(select (select grouping(e,f) from (values (1)) v2(c)) from (values (1,2)) v1(a,b) group by (a,b)) from (values(6,7)) v3(e,f) GROUP BY ROLLUP(e,f); -- 2.43.0