Skip to content

Commit f7bbd46

Browse files
committed
In locate_grouping_columns(), don't expect an exact match of Var typmods.
It's possible that inlining of SQL functions (or perhaps other changes?) has exposed typmod information not known at parse time. In such cases, Vars generated by query_planner might have valid typmod values while the original grouping columns only have typmod -1. This isn't a semantic problem since the behavior of grouping only depends on type not typmod, but it breaks locate_grouping_columns' use of tlist_member to locate the matching entry in query_planner's result tlist. We can fix this without an excessive amount of new code or complexity by relying on the fact that locate_grouping_columns only gets called when make_subplanTargetList has set need_tlist_eval == false, and that can only happen if all the grouping columns are simple Vars. Therefore we only need to search the sub_tlist for a matching Var, and we can reasonably define a "match" as being a match of the Var identity fields varno/varattno/varlevelsup. The code still Asserts that vartype matches, but ignores vartypmod. Per bug #8393 from Evan Martin. The added regression test case is basically the same as his example. This has been broken for a very long time, so back-patch to all supported branches.
1 parent 649839d commit f7bbd46

File tree

5 files changed

+66
-3
lines changed

5 files changed

+66
-3
lines changed

src/backend/optimizer/plan/planner.c

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2533,7 +2533,8 @@ choose_hashed_distinct(PlannerInfo *root,
25332533
* 'groupColIdx' receives an array of column numbers for the GROUP BY
25342534
* expressions (if there are any) in the subplan's target list.
25352535
* 'need_tlist_eval' is set true if we really need to evaluate the
2536-
* result tlist.
2536+
* returned tlist as-is. (Note: locate_grouping_columns assumes
2537+
* that if this is FALSE, all grouping columns are simple Vars.)
25372538
*
25382539
* The result is the targetlist to be passed to the subplan.
25392540
*/
@@ -2634,6 +2635,7 @@ make_subplanTargetList(PlannerInfo *root,
26342635
* This is only needed if we don't use the sub_tlist chosen by
26352636
* make_subplanTargetList. We have to forget the column indexes found
26362637
* by that routine and re-locate the grouping exprs in the real sub_tlist.
2638+
* We assume the grouping exprs are just Vars (see make_subplanTargetList).
26372639
*/
26382640
static void
26392641
locate_grouping_columns(PlannerInfo *root,
@@ -2657,11 +2659,24 @@ locate_grouping_columns(PlannerInfo *root,
26572659
foreach(gl, root->parse->groupClause)
26582660
{
26592661
SortGroupClause *grpcl = (SortGroupClause *) lfirst(gl);
2660-
Node *groupexpr = get_sortgroupclause_expr(grpcl, tlist);
2661-
TargetEntry *te = tlist_member(groupexpr, sub_tlist);
2662+
Var *groupexpr = (Var *) get_sortgroupclause_expr(grpcl, tlist);
2663+
TargetEntry *te;
26622664

2665+
/*
2666+
* The grouping column returned by create_plan might not have the same
2667+
* typmod as the original Var. (This can happen in cases where a
2668+
* set-returning function has been inlined, so that we now have more
2669+
* knowledge about what it returns than we did when the original Var
2670+
* was created.) So we can't use tlist_member() to search the tlist;
2671+
* instead use tlist_member_match_var. For safety, still check that
2672+
* the vartype matches.
2673+
*/
2674+
if (!(groupexpr && IsA(groupexpr, Var)))
2675+
elog(ERROR, "grouping column is not a Var as expected");
2676+
te = tlist_member_match_var(groupexpr, sub_tlist);
26632677
if (!te)
26642678
elog(ERROR, "failed to locate grouping columns");
2679+
Assert(((Var *) te->expr)->vartype == groupexpr->vartype);
26652680
groupColIdx[keyno++] = te->resno;
26662681
}
26672682
}

src/backend/optimizer/util/tlist.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,35 @@ tlist_member_ignore_relabel(Node *node, List *targetlist)
7272
return NULL;
7373
}
7474

75+
/*
76+
* tlist_member_match_var
77+
* Same as above, except that we match the provided Var on the basis
78+
* of varno/varattno/varlevelsup only, rather than using full equal().
79+
*
80+
* This is needed in some cases where we can't be sure of an exact typmod
81+
* match. It's probably a good idea to check the vartype anyway, but
82+
* we leave it to the caller to apply any suitable sanity checks.
83+
*/
84+
TargetEntry *
85+
tlist_member_match_var(Var *var, List *targetlist)
86+
{
87+
ListCell *temp;
88+
89+
foreach(temp, targetlist)
90+
{
91+
TargetEntry *tlentry = (TargetEntry *) lfirst(temp);
92+
Var *tlvar = (Var *) tlentry->expr;
93+
94+
if (!tlvar || !IsA(tlvar, Var))
95+
continue;
96+
if (var->varno == tlvar->varno &&
97+
var->varattno == tlvar->varattno &&
98+
var->varlevelsup == tlvar->varlevelsup)
99+
return tlentry;
100+
}
101+
return NULL;
102+
}
103+
75104
/*
76105
* flatten_tlist
77106
* Create a target list that only contains unique variables.

src/include/optimizer/tlist.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
extern TargetEntry *tlist_member(Node *node, List *targetlist);
2121
extern TargetEntry *tlist_member_ignore_relabel(Node *node, List *targetlist);
22+
extern TargetEntry *tlist_member_match_var(Var *var, List *targetlist);
2223

2324
extern List *flatten_tlist(List *tlist, PVCAggregateBehavior aggbehavior,
2425
PVCPlaceHolderBehavior phbehavior);

src/test/regress/expected/rangefuncs.out

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,17 @@ SELECT * FROM foo(3);
574574
(9 rows)
575575

576576
DROP FUNCTION foo(int);
577+
-- case that causes change of typmod knowledge during inlining
578+
CREATE OR REPLACE FUNCTION foo()
579+
RETURNS TABLE(a varchar(5))
580+
AS $$ SELECT 'hello'::varchar(5) $$ LANGUAGE sql STABLE;
581+
SELECT * FROM foo() GROUP BY 1;
582+
a
583+
-------
584+
hello
585+
(1 row)
586+
587+
DROP FUNCTION foo();
577588
--
578589
-- some tests on SQL functions with RETURNING
579590
--

src/test/regress/sql/rangefuncs.sql

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,13 @@ AS $$ SELECT a, b
286286
SELECT * FROM foo(3);
287287
DROP FUNCTION foo(int);
288288

289+
-- case that causes change of typmod knowledge during inlining
290+
CREATE OR REPLACE FUNCTION foo()
291+
RETURNS TABLE(a varchar(5))
292+
AS $$ SELECT 'hello'::varchar(5) $$ LANGUAGE sql STABLE;
293+
SELECT * FROM foo() GROUP BY 1;
294+
DROP FUNCTION foo();
295+
289296
--
290297
-- some tests on SQL functions with RETURNING
291298
--

0 commit comments

Comments
 (0)