Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitf7bbd46

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 theoriginal grouping columns only have typmod -1. This isn't a semanticproblem since the behavior of grouping only depends on type not typmod,but it breaks locate_grouping_columns' use of tlist_member to locate thematching entry in query_planner's result tlist.We can fix this without an excessive amount of new code or complexity byrelying on the fact that locate_grouping_columns only gets called whenmake_subplanTargetList has set need_tlist_eval == false, and that can onlyhappen if all the grouping columns are simple Vars. Therefore we only needto search the sub_tlist for a matching Var, and we can reasonably define a"match" as being a match of the Var identity fieldsvarno/varattno/varlevelsup. The code still Asserts that vartype matches,but ignores vartypmod.Per bug #8393 from Evan Martin. The added regression test case isbasically the same as his example. This has been broken for a very longtime, so back-patch to all supported branches.
1 parent649839d commitf7bbd46

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
staticvoid
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
returnNULL;
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+
returntlentry;
100+
}
101+
returnNULL;
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
externTargetEntry*tlist_member(Node*node,List*targetlist);
2121
externTargetEntry*tlist_member_ignore_relabel(Node*node,List*targetlist);
22+
externTargetEntry*tlist_member_match_var(Var*var,List*targetlist);
2223

2324
externList*flatten_tlist(List*tlist,PVCAggregateBehavioraggbehavior,
2425
PVCPlaceHolderBehaviorphbehavior);

‎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
DROPFUNCTION foo(int);
288288

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

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp