Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 6520d4a

Browse files
committed
Fix incorrect handling of subquery pullup in the presence of grouping sets.
If we flatten a subquery whose target list contains constants or expressions, when those output columns are used in GROUPING SET columns, the planner was capable of doing the wrong thing by merging a pulled-up expression into the surrounding expression during const-simplification. Then the late processing that attempts to match subexpressions to grouping sets would fail to match those subexpressions to grouping sets, with the effect that they'd not go to null when expected. To fix, wrap such subquery outputs in PlaceHolderVars, ensuring that they preserve their separate identity throughout the planner's expression processing. This is a bit of a band-aid, because the wrapper defeats const-simplification even in places where it would be safe to allow. But a nicer fix would likely be too invasive to back-patch, and the consequences of the missed optimizations probably aren't large in most cases. Back-patch to 9.5 where grouping sets were introduced. Heikki Linnakangas, with small mods and better test cases by me; additional review by Andrew Gierth Discussion: https://postgr.es/m/7dbdcf5c-b5a6-ef89-4958-da212fe10176@iki.fi
1 parent bda5281 commit 6520d4a

File tree

3 files changed

+105
-8
lines changed

3 files changed

+105
-8
lines changed

src/backend/optimizer/prep/prepjointree.c

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1002,11 +1002,8 @@ pull_up_simple_subquery(PlannerInfo *root, Node *jtnode, RangeTblEntry *rte,
10021002

10031003
/*
10041004
* The subquery's targetlist items are now in the appropriate form to
1005-
* insert into the top query, but if we are under an outer join then
1006-
* non-nullable items and lateral references may have to be turned into
1007-
* PlaceHolderVars. If we are dealing with an appendrel member then
1008-
* anything that's not a simple Var has to be turned into a
1009-
* PlaceHolderVar. Set up required context data for pullup_replace_vars.
1005+
* insert into the top query, except that we may need to wrap them in
1006+
* PlaceHolderVars. Set up required context data for pullup_replace_vars.
10101007
*/
10111008
rvcontext.root = root;
10121009
rvcontext.targetlist = subquery->targetList;
@@ -1018,13 +1015,48 @@ pull_up_simple_subquery(PlannerInfo *root, Node *jtnode, RangeTblEntry *rte,
10181015
rvcontext.relids = NULL;
10191016
rvcontext.outer_hasSubLinks = &parse->hasSubLinks;
10201017
rvcontext.varno = varno;
1021-
rvcontext.need_phvs = (lowest_nulling_outer_join != NULL ||
1022-
containing_appendrel != NULL);
1023-
rvcontext.wrap_non_vars = (containing_appendrel != NULL);
1018+
/* these flags will be set below, if needed */
1019+
rvcontext.need_phvs = false;
1020+
rvcontext.wrap_non_vars = false;
10241021
/* initialize cache array with indexes 0 .. length(tlist) */
10251022
rvcontext.rv_cache = palloc0((list_length(subquery->targetList) + 1) *
10261023
sizeof(Node *));
10271024

1025+
/*
1026+
* If we are under an outer join then non-nullable items and lateral
1027+
* references may have to be turned into PlaceHolderVars.
1028+
*/
1029+
if (lowest_nulling_outer_join != NULL)
1030+
rvcontext.need_phvs = true;
1031+
1032+
/*
1033+
* If we are dealing with an appendrel member then anything that's not a
1034+
* simple Var has to be turned into a PlaceHolderVar. We force this to
1035+
* ensure that what we pull up doesn't get merged into a surrounding
1036+
* expression during later processing and then fail to match the
1037+
* expression actually available from the appendrel.
1038+
*/
1039+
if (containing_appendrel != NULL)
1040+
{
1041+
rvcontext.need_phvs = true;
1042+
rvcontext.wrap_non_vars = true;
1043+
}
1044+
1045+
/*
1046+
* If the parent query uses grouping sets, we need a PlaceHolderVar for
1047+
* anything that's not a simple Var. Again, this ensures that expressions
1048+
* retain their separate identity so that they will match grouping set
1049+
* columns when appropriate. (It'd be sufficient to wrap values used in
1050+
* grouping set columns, and do so only in non-aggregated portions of the
1051+
* tlist and havingQual, but that would require a lot of infrastructure
1052+
* that pullup_replace_vars hasn't currently got.)
1053+
*/
1054+
if (parse->groupingSets)
1055+
{
1056+
rvcontext.need_phvs = true;
1057+
rvcontext.wrap_non_vars = true;
1058+
}
1059+
10281060
/*
10291061
* Replace all of the top query's references to the subquery's outputs
10301062
* with copies of the adjusted subtlist items, being careful not to

src/test/regress/expected/groupingsets.out

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,51 @@ select g as alias1, g as alias2
381381
3 |
382382
(6 rows)
383383

384+
-- check that pulled-up subquery outputs still go to null when appropriate
385+
select four, x
386+
from (select four, ten, 'foo'::text as x from tenk1) as t
387+
group by grouping sets (four, x)
388+
having x = 'foo';
389+
four | x
390+
------+-----
391+
| foo
392+
(1 row)
393+
394+
select four, x || 'x'
395+
from (select four, ten, 'foo'::text as x from tenk1) as t
396+
group by grouping sets (four, x)
397+
order by four;
398+
four | ?column?
399+
------+----------
400+
0 |
401+
1 |
402+
2 |
403+
3 |
404+
| foox
405+
(5 rows)
406+
407+
select (x+y)*1, sum(z)
408+
from (select 1 as x, 2 as y, 3 as z) s
409+
group by grouping sets (x+y, x);
410+
?column? | sum
411+
----------+-----
412+
3 | 3
413+
| 3
414+
(2 rows)
415+
416+
select x, not x as not_x, q2 from
417+
(select *, q1 = 1 as x from int8_tbl i1) as t
418+
group by grouping sets(x, q2)
419+
order by x, q2;
420+
x | not_x | q2
421+
---+-------+-------------------
422+
f | t |
423+
| | -4567890123456789
424+
| | 123
425+
| | 456
426+
| | 4567890123456789
427+
(5 rows)
428+
384429
-- simple rescan tests
385430
select a, b, sum(v.x)
386431
from (values (1),(2)) v(x), gstest_data(v.x)

src/test/regress/sql/groupingsets.sql

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,26 @@ select g as alias1, g as alias2
141141
from generate_series(1,3) g
142142
group by alias1, rollup(alias2);
143143

144+
-- check that pulled-up subquery outputs still go to null when appropriate
145+
select four, x
146+
from (select four, ten, 'foo'::text as x from tenk1) as t
147+
group by grouping sets (four, x)
148+
having x = 'foo';
149+
150+
select four, x || 'x'
151+
from (select four, ten, 'foo'::text as x from tenk1) as t
152+
group by grouping sets (four, x)
153+
order by four;
154+
155+
select (x+y)*1, sum(z)
156+
from (select 1 as x, 2 as y, 3 as z) s
157+
group by grouping sets (x+y, x);
158+
159+
select x, not x as not_x, q2 from
160+
(select *, q1 = 1 as x from int8_tbl i1) as t
161+
group by grouping sets(x, q2)
162+
order by x, q2;
163+
144164
-- simple rescan tests
145165

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

0 commit comments

Comments
 (0)