Skip to content

Commit fb5941d

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 e9e387a commit fb5941d

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
@@ -2508,7 +2508,8 @@ choose_hashed_distinct(PlannerInfo *root,
25082508
* 'groupColIdx' receives an array of column numbers for the GROUP BY
25092509
* expressions (if there are any) in the subplan's target list.
25102510
* 'need_tlist_eval' is set true if we really need to evaluate the
2511-
* result tlist.
2511+
* returned tlist as-is. (Note: locate_grouping_columns assumes
2512+
* that if this is FALSE, all grouping columns are simple Vars.)
25122513
*
25132514
* The result is the targetlist to be passed to the subplan.
25142515
*/
@@ -2605,6 +2606,7 @@ make_subplanTargetList(PlannerInfo *root,
26052606
* This is only needed if we don't use the sub_tlist chosen by
26062607
* make_subplanTargetList. We have to forget the column indexes found
26072608
* by that routine and re-locate the grouping exprs in the real sub_tlist.
2609+
* We assume the grouping exprs are just Vars (see make_subplanTargetList).
26082610
*/
26092611
static void
26102612
locate_grouping_columns(PlannerInfo *root,
@@ -2628,11 +2630,24 @@ locate_grouping_columns(PlannerInfo *root,
26282630
foreach(gl, root->parse->groupClause)
26292631
{
26302632
SortGroupClause *grpcl = (SortGroupClause *) lfirst(gl);
2631-
Node *groupexpr = get_sortgroupclause_expr(grpcl, tlist);
2632-
TargetEntry *te = tlist_member(groupexpr, sub_tlist);
2633+
Var *groupexpr = (Var *) get_sortgroupclause_expr(grpcl, tlist);
2634+
TargetEntry *te;
26332635

2636+
/*
2637+
* The grouping column returned by create_plan might not have the same
2638+
* typmod as the original Var. (This can happen in cases where a
2639+
* set-returning function has been inlined, so that we now have more
2640+
* knowledge about what it returns than we did when the original Var
2641+
* was created.) So we can't use tlist_member() to search the tlist;
2642+
* instead use tlist_member_match_var. For safety, still check that
2643+
* the vartype matches.
2644+
*/
2645+
if (!(groupexpr && IsA(groupexpr, Var)))
2646+
elog(ERROR, "grouping column is not a Var as expected");
2647+
te = tlist_member_match_var(groupexpr, sub_tlist);
26342648
if (!te)
26352649
elog(ERROR, "failed to locate grouping columns");
2650+
Assert(((Var *) te->expr)->vartype == groupexpr->vartype);
26362651
groupColIdx[keyno++] = te->resno;
26372652
}
26382653
}

src/backend/optimizer/util/tlist.c

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

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

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)