Skip to content

Commit 79b58c6

Browse files
committed
Make pull_var_clause() handle GroupingFuncs exactly like Aggrefs.
This follows in the footsteps of commit 2591ee8 by removing one more ill-advised shortcut from planning of GroupingFuncs. It's true that we don't intend to execute the argument expression(s) at runtime, but we still have to process any Vars appearing within them, or we risk failure at setrefs.c time (or more fundamentally, in EXPLAIN trying to print such an expression). Vars in upper plan nodes have to have referents in the next plan level, whether we ever execute 'em or not. Per bug #17479 from Michael J. Sullivan. Back-patch to all supported branches. Richard Guo Discussion: https://postgr.es/m/17479-6260deceaf0ad304@postgresql.org
1 parent 25285e5 commit 79b58c6

File tree

3 files changed

+89
-8
lines changed

3 files changed

+89
-8
lines changed

src/backend/optimizer/util/var.c

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -586,7 +586,7 @@ locate_var_of_level_walker(Node *node,
586586
* Vars within a PHV's expression are included in the result only
587587
* when PVC_RECURSE_PLACEHOLDERS is specified.
588588
*
589-
* GroupingFuncs are treated mostly like Aggrefs, and so do not need
589+
* GroupingFuncs are treated exactly like Aggrefs, and so do not need
590590
* their own flag bits.
591591
*
592592
* CurrentOfExpr nodes are ignored in all cases.
@@ -661,13 +661,7 @@ pull_var_clause_walker(Node *node, pull_var_clause_context *context)
661661
}
662662
else if (context->flags & PVC_RECURSE_AGGREGATES)
663663
{
664-
/*
665-
* We do NOT descend into the contained expression, even if the
666-
* caller asked for it, because we never actually evaluate it -
667-
* the result is driven entirely off the associated GROUP BY
668-
* clause, so we never need to extract the actual Vars here.
669-
*/
670-
return false;
664+
/* fall through to recurse into the GroupingFunc's arguments */
671665
}
672666
else
673667
elog(ERROR, "GROUPING found where not expected");

src/test/regress/expected/groupingsets.out

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,70 @@ where x = 1 and q1 = 123;
466466
---+----+-----
467467
(0 rows)
468468

469+
-- check handling of pulled-up SubPlan in GROUPING() argument (bug #17479)
470+
explain (verbose, costs off)
471+
select grouping(ss.x)
472+
from int8_tbl i1
473+
cross join lateral (select (select i1.q1) as x) ss
474+
group by ss.x;
475+
QUERY PLAN
476+
------------------------------------------------
477+
GroupAggregate
478+
Output: GROUPING((SubPlan 1)), ((SubPlan 2))
479+
Group Key: ((SubPlan 2))
480+
-> Sort
481+
Output: ((SubPlan 2)), i1.q1
482+
Sort Key: ((SubPlan 2))
483+
-> Seq Scan on public.int8_tbl i1
484+
Output: (SubPlan 2), i1.q1
485+
SubPlan 2
486+
-> Result
487+
Output: i1.q1
488+
(11 rows)
489+
490+
select grouping(ss.x)
491+
from int8_tbl i1
492+
cross join lateral (select (select i1.q1) as x) ss
493+
group by ss.x;
494+
grouping
495+
----------
496+
0
497+
0
498+
(2 rows)
499+
500+
explain (verbose, costs off)
501+
select (select grouping(ss.x))
502+
from int8_tbl i1
503+
cross join lateral (select (select i1.q1) as x) ss
504+
group by ss.x;
505+
QUERY PLAN
506+
--------------------------------------------
507+
GroupAggregate
508+
Output: (SubPlan 2), ((SubPlan 3))
509+
Group Key: ((SubPlan 3))
510+
-> Sort
511+
Output: ((SubPlan 3)), i1.q1
512+
Sort Key: ((SubPlan 3))
513+
-> Seq Scan on public.int8_tbl i1
514+
Output: (SubPlan 3), i1.q1
515+
SubPlan 3
516+
-> Result
517+
Output: i1.q1
518+
SubPlan 2
519+
-> Result
520+
Output: GROUPING((SubPlan 1))
521+
(14 rows)
522+
523+
select (select grouping(ss.x))
524+
from int8_tbl i1
525+
cross join lateral (select (select i1.q1) as x) ss
526+
group by ss.x;
527+
grouping
528+
----------
529+
0
530+
0
531+
(2 rows)
532+
469533
-- simple rescan tests
470534
select a, b, sum(v.x)
471535
from (values (1),(2)) v(x), gstest_data(v.x)

src/test/regress/sql/groupingsets.sql

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,29 @@ select * from (
188188
) ss
189189
where x = 1 and q1 = 123;
190190

191+
-- check handling of pulled-up SubPlan in GROUPING() argument (bug #17479)
192+
explain (verbose, costs off)
193+
select grouping(ss.x)
194+
from int8_tbl i1
195+
cross join lateral (select (select i1.q1) as x) ss
196+
group by ss.x;
197+
198+
select grouping(ss.x)
199+
from int8_tbl i1
200+
cross join lateral (select (select i1.q1) as x) ss
201+
group by ss.x;
202+
203+
explain (verbose, costs off)
204+
select (select grouping(ss.x))
205+
from int8_tbl i1
206+
cross join lateral (select (select i1.q1) as x) ss
207+
group by ss.x;
208+
209+
select (select grouping(ss.x))
210+
from int8_tbl i1
211+
cross join lateral (select (select i1.q1) as x) ss
212+
group by ss.x;
213+
191214
-- simple rescan tests
192215

193216
select a, b, sum(v.x)

0 commit comments

Comments
 (0)