Commit f685cbba authored by Tom Lane's avatar Tom Lane

Fix mishandling of whole-row Vars referencing a view or sub-select.

If such a Var appeared within a nested sub-select, we failed to translate it
correctly during pullup of the view, because the recursive call to
replace_rte_variables_mutator was looking for the wrong sublevels_up value.
Bug was introduced during the addition of the PlaceHolderVar mechanism.
Per bug #5514 from Marcos Castedo.
parent 31c47e53
...@@ -16,7 +16,7 @@ ...@@ -16,7 +16,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/optimizer/prep/prepjointree.c,v 1.71 2010/02/26 02:00:46 momjian Exp $ * $PostgreSQL: pgsql/src/backend/optimizer/prep/prepjointree.c,v 1.72 2010/06/21 00:14:48 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -1297,6 +1297,7 @@ pullup_replace_vars_callback(Var *var, ...@@ -1297,6 +1297,7 @@ pullup_replace_vars_callback(Var *var,
List *colnames; List *colnames;
List *fields; List *fields;
bool save_need_phvs = rcon->need_phvs; bool save_need_phvs = rcon->need_phvs;
int save_sublevelsup = context->sublevels_up;
/* /*
* If generating an expansion for a var of a named rowtype (ie, this * If generating an expansion for a var of a named rowtype (ie, this
...@@ -1314,9 +1315,12 @@ pullup_replace_vars_callback(Var *var, ...@@ -1314,9 +1315,12 @@ pullup_replace_vars_callback(Var *var,
&colnames, &fields); &colnames, &fields);
/* Adjust the generated per-field Vars, but don't insert PHVs */ /* Adjust the generated per-field Vars, but don't insert PHVs */
rcon->need_phvs = false; rcon->need_phvs = false;
context->sublevels_up = 0; /* to match the expandRTE output */
fields = (List *) replace_rte_variables_mutator((Node *) fields, fields = (List *) replace_rte_variables_mutator((Node *) fields,
context); context);
rcon->need_phvs = save_need_phvs; rcon->need_phvs = save_need_phvs;
context->sublevels_up = save_sublevelsup;
rowexpr = makeNode(RowExpr); rowexpr = makeNode(RowExpr);
rowexpr->args = fields; rowexpr->args = fields;
rowexpr->row_typeid = var->vartype; rowexpr->row_typeid = var->vartype;
......
...@@ -477,3 +477,33 @@ group by f1,f2,fs; ...@@ -477,3 +477,33 @@ group by f1,f2,fs;
----+----+---- ----+----+----
(0 rows) (0 rows)
--
-- Test case for bug #5514 (mishandling of whole-row Vars in subselects)
--
create temp table table_a(id integer);
insert into table_a values (42);
create temp view view_a as select * from table_a;
select view_a from view_a;
view_a
--------
(42)
(1 row)
select (select view_a) from view_a;
?column?
----------
(42)
(1 row)
select (select (select view_a)) from view_a;
?column?
----------
(42)
(1 row)
select (select (a.*)::text) from view_a a;
?column?
----------
(42)
(1 row)
...@@ -309,3 +309,17 @@ select * from ...@@ -309,3 +309,17 @@ select * from
(select distinct f1, f2, (select f2 from t1 x where x.f1 = up.f1) as fs (select distinct f1, f2, (select f2 from t1 x where x.f1 = up.f1) as fs
from t1 up) ss from t1 up) ss
group by f1,f2,fs; group by f1,f2,fs;
--
-- Test case for bug #5514 (mishandling of whole-row Vars in subselects)
--
create temp table table_a(id integer);
insert into table_a values (42);
create temp view view_a as select * from table_a;
select view_a from view_a;
select (select view_a) from view_a;
select (select (select view_a)) from view_a;
select (select (a.*)::text) from view_a a;
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment