aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/backend/optimizer/plan/setrefs.c7
-rw-r--r--src/test/regress/expected/groupingsets.out29
-rw-r--r--src/test/regress/sql/groupingsets.sql11
3 files changed, 43 insertions, 4 deletions
diff --git a/src/backend/optimizer/plan/setrefs.c b/src/backend/optimizer/plan/setrefs.c
index 1382b679748..fa9a3f0b47b 100644
--- a/src/backend/optimizer/plan/setrefs.c
+++ b/src/backend/optimizer/plan/setrefs.c
@@ -1744,8 +1744,8 @@ set_upper_references(PlannerInfo *root, Plan *plan, int rtoffset)
TargetEntry *tle = (TargetEntry *) lfirst(l);
Node *newexpr;
- /* If it's a non-Var sort/group item, first try to match by sortref */
- if (tle->ressortgroupref != 0 && !IsA(tle->expr, Var))
+ /* If it's a sort/group item, first try to match by sortref */
+ if (tle->ressortgroupref != 0)
{
newexpr = (Node *)
search_indexed_tlist_for_sortgroupref(tle->expr,
@@ -2113,7 +2113,6 @@ search_indexed_tlist_for_non_var(Expr *node,
/*
* search_indexed_tlist_for_sortgroupref --- find a sort/group expression
- * (which is assumed not to be just a Var)
*
* If a match is found, return a Var constructed to reference the tlist item.
* If no match, return NULL.
@@ -2644,7 +2643,7 @@ is_converted_whole_row_reference(Node *node)
if (IsA(convexpr->arg, Var))
{
- Var *var = castNode(Var, convexpr->arg);
+ Var *var = castNode(Var, convexpr->arg);
if (var->varattno == 0)
return true;
diff --git a/src/test/regress/expected/groupingsets.out b/src/test/regress/expected/groupingsets.out
index fd618afe603..833d5151743 100644
--- a/src/test/regress/expected/groupingsets.out
+++ b/src/test/regress/expected/groupingsets.out
@@ -360,6 +360,35 @@ select a, d, grouping(a,b,c)
2 | 2 | 2
(4 rows)
+-- check that distinct grouping columns are kept separate
+-- even if they are equal()
+explain (costs off)
+select g as alias1, g as alias2
+ from generate_series(1,3) g
+ group by alias1, rollup(alias2);
+ QUERY PLAN
+------------------------------------------------
+ GroupAggregate
+ Group Key: g, g
+ Group Key: g
+ -> Sort
+ Sort Key: g
+ -> Function Scan on generate_series g
+(6 rows)
+
+select g as alias1, g as alias2
+ from generate_series(1,3) g
+ group by alias1, rollup(alias2);
+ alias1 | alias2
+--------+--------
+ 1 | 1
+ 1 |
+ 2 | 2
+ 2 |
+ 3 | 3
+ 3 |
+(6 rows)
+
-- simple rescan tests
select a, b, sum(v.x)
from (values (1),(2)) v(x), gstest_data(v.x)
diff --git a/src/test/regress/sql/groupingsets.sql b/src/test/regress/sql/groupingsets.sql
index 564ebc9b051..2b4ab692c4f 100644
--- a/src/test/regress/sql/groupingsets.sql
+++ b/src/test/regress/sql/groupingsets.sql
@@ -141,6 +141,17 @@ select a, d, grouping(a,b,c)
from gstest3
group by grouping sets ((a,b), (a,c));
+-- check that distinct grouping columns are kept separate
+-- even if they are equal()
+explain (costs off)
+select g as alias1, g as alias2
+ from generate_series(1,3) g
+ group by alias1, rollup(alias2);
+
+select g as alias1, g as alias2
+ from generate_series(1,3) g
+ group by alias1, rollup(alias2);
+
-- simple rescan tests
select a, b, sum(v.x)