Skip to content

Commit f685cbb

Browse files
committed
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.
1 parent 31c47e5 commit f685cbb

File tree

3 files changed

+49
-1
lines changed

3 files changed

+49
-1
lines changed

src/backend/optimizer/prep/prepjointree.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*
1717
*
1818
* IDENTIFICATION
19-
* $PostgreSQL: pgsql/src/backend/optimizer/prep/prepjointree.c,v 1.71 2010/02/26 02:00:46 momjian Exp $
19+
* $PostgreSQL: pgsql/src/backend/optimizer/prep/prepjointree.c,v 1.72 2010/06/21 00:14:48 tgl Exp $
2020
*
2121
*-------------------------------------------------------------------------
2222
*/
@@ -1297,6 +1297,7 @@ pullup_replace_vars_callback(Var *var,
12971297
List *colnames;
12981298
List *fields;
12991299
bool save_need_phvs = rcon->need_phvs;
1300+
int save_sublevelsup = context->sublevels_up;
13001301

13011302
/*
13021303
* If generating an expansion for a var of a named rowtype (ie, this
@@ -1314,9 +1315,12 @@ pullup_replace_vars_callback(Var *var,
13141315
&colnames, &fields);
13151316
/* Adjust the generated per-field Vars, but don't insert PHVs */
13161317
rcon->need_phvs = false;
1318+
context->sublevels_up = 0; /* to match the expandRTE output */
13171319
fields = (List *) replace_rte_variables_mutator((Node *) fields,
13181320
context);
13191321
rcon->need_phvs = save_need_phvs;
1322+
context->sublevels_up = save_sublevelsup;
1323+
13201324
rowexpr = makeNode(RowExpr);
13211325
rowexpr->args = fields;
13221326
rowexpr->row_typeid = var->vartype;

src/test/regress/expected/subselect.out

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,3 +477,33 @@ group by f1,f2,fs;
477477
----+----+----
478478
(0 rows)
479479

480+
--
481+
-- Test case for bug #5514 (mishandling of whole-row Vars in subselects)
482+
--
483+
create temp table table_a(id integer);
484+
insert into table_a values (42);
485+
create temp view view_a as select * from table_a;
486+
select view_a from view_a;
487+
view_a
488+
--------
489+
(42)
490+
(1 row)
491+
492+
select (select view_a) from view_a;
493+
?column?
494+
----------
495+
(42)
496+
(1 row)
497+
498+
select (select (select view_a)) from view_a;
499+
?column?
500+
----------
501+
(42)
502+
(1 row)
503+
504+
select (select (a.*)::text) from view_a a;
505+
?column?
506+
----------
507+
(42)
508+
(1 row)
509+

src/test/regress/sql/subselect.sql

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,3 +309,17 @@ select * from
309309
(select distinct f1, f2, (select f2 from t1 x where x.f1 = up.f1) as fs
310310
from t1 up) ss
311311
group by f1,f2,fs;
312+
313+
--
314+
-- Test case for bug #5514 (mishandling of whole-row Vars in subselects)
315+
--
316+
317+
create temp table table_a(id integer);
318+
insert into table_a values (42);
319+
320+
create temp view view_a as select * from table_a;
321+
322+
select view_a from view_a;
323+
select (select view_a) from view_a;
324+
select (select (select view_a)) from view_a;
325+
select (select (a.*)::text) from view_a a;

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy