diff options
author | Tom Lane <tgl@sss.pgh.pa.us> | 2013-08-23 17:31:06 -0400 |
---|---|---|
committer | Tom Lane <tgl@sss.pgh.pa.us> | 2013-08-23 17:31:06 -0400 |
commit | fb5941d323816ba0141fbd5a6df1d14155bfd006 (patch) | |
tree | 6c30c23ff52a8bc1a6871a8fe89a3d31920ceca4 /src/backend/optimizer/util/tlist.c | |
parent | e9e387a5b1dde0b738d712cffe16c962e87b0094 (diff) | |
download | postgresql-fb5941d323816ba0141fbd5a6df1d14155bfd006.tar.gz postgresql-fb5941d323816ba0141fbd5a6df1d14155bfd006.zip |
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.
Diffstat (limited to 'src/backend/optimizer/util/tlist.c')
-rw-r--r-- | src/backend/optimizer/util/tlist.c | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/src/backend/optimizer/util/tlist.c b/src/backend/optimizer/util/tlist.c index 37ac8aaea22..1e44f683e17 100644 --- a/src/backend/optimizer/util/tlist.c +++ b/src/backend/optimizer/util/tlist.c @@ -74,6 +74,35 @@ tlist_member_ignore_relabel(Node *node, List *targetlist) } /* + * tlist_member_match_var + * Same as above, except that we match the provided Var on the basis + * of varno/varattno/varlevelsup only, rather than using full equal(). + * + * This is needed in some cases where we can't be sure of an exact typmod + * match. It's probably a good idea to check the vartype anyway, but + * we leave it to the caller to apply any suitable sanity checks. + */ +TargetEntry * +tlist_member_match_var(Var *var, List *targetlist) +{ + ListCell *temp; + + foreach(temp, targetlist) + { + TargetEntry *tlentry = (TargetEntry *) lfirst(temp); + Var *tlvar = (Var *) tlentry->expr; + + if (!tlvar || !IsA(tlvar, Var)) + continue; + if (var->varno == tlvar->varno && + var->varattno == tlvar->varattno && + var->varlevelsup == tlvar->varlevelsup) + return tlentry; + } + return NULL; +} + +/* * flatten_tlist * Create a target list that only contains unique variables. * |