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

Commitbfd28bb

Browse files
committed
Allow subquery pullup to wrap a PlaceHolderVar in another one.
The code for wrapping subquery output expressions in PlaceHolderVarsbelieved that if the expression already was a PlaceHolderVar, it wasnever necessary to wrap that in another one. That's wrong if theexpression is underneath an outer join and involves a lateralreference to outside that scope: failing to add an additional PHVrisks evaluating the expression at the wrong place and hence notforcing it to null when the outer join should do so. This is anoversight in commit9e7e29c, which added logic to forcibly wraplateral-reference Vars in PlaceHolderVars, but didn't see that theadjacent case for PlaceHolderVars needed the same treatment.The test case we have for this doesn't fail before4be058f, but nowthat I see the problem I wonder if it is possible to demonstraterelated errors before that. That's moot though, since all suchbranches are out of support.Per bug #18284 from Holger Reise. Back-patch to all supportedbranches.Discussion:https://postgr.es/m/18284-47505a20c23647f8@postgresql.org
1 parent9a94e9a commitbfd28bb

File tree

3 files changed

+45
-2
lines changed

3 files changed

+45
-2
lines changed

‎src/backend/optimizer/prep/prepjointree.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2365,8 +2365,13 @@ pullup_replace_vars_callback(Var *var,
23652365
elseif (newnode&&IsA(newnode,PlaceHolderVar)&&
23662366
((PlaceHolderVar*)newnode)->phlevelsup==0)
23672367
{
2368-
/* No need to wrap a PlaceHolderVar with another one, either */
2369-
wrap= false;
2368+
/* The same rules apply for a PlaceHolderVar */
2369+
if (rcon->target_rte->lateral&&
2370+
!bms_is_subset(((PlaceHolderVar*)newnode)->phrels,
2371+
rcon->relids))
2372+
wrap= true;
2373+
else
2374+
wrap= false;
23702375
}
23712376
elseif (rcon->wrap_non_vars)
23722377
{

‎src/test/regress/expected/join.out

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5696,6 +5696,32 @@ select * from
56965696
Output: (COALESCE((COALESCE(b.q2, '42'::bigint)), d.q2))
56975697
(24 rows)
56985698

5699+
-- another case requiring nested PlaceHolderVars
5700+
explain (verbose, costs off)
5701+
select * from
5702+
(select 0 as val0) as ss0
5703+
left join (select 1 as val) as ss1 on true
5704+
left join lateral (select ss1.val as val_filtered where false) as ss2 on true;
5705+
QUERY PLAN
5706+
--------------------------------
5707+
Nested Loop Left Join
5708+
Output: 0, (1), ((1))
5709+
-> Result
5710+
Output: 1
5711+
-> Result
5712+
Output: (1)
5713+
One-Time Filter: false
5714+
(7 rows)
5715+
5716+
select * from
5717+
(select 0 as val0) as ss0
5718+
left join (select 1 as val) as ss1 on true
5719+
left join lateral (select ss1.val as val_filtered where false) as ss2 on true;
5720+
val0 | val | val_filtered
5721+
------+-----+--------------
5722+
0 | 1 |
5723+
(1 row)
5724+
56995725
-- case that breaks the old ph_may_need optimization
57005726
explain (verbose, costs off)
57015727
select c.*,a.*,ss1.q1,ss2.q1,ss3.* from

‎src/test/regress/sql/join.sql

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1948,6 +1948,18 @@ select * from
19481948
)onc.q2=ss2.q1,
19491949
lateral (selectss2.y offset0) ss3;
19501950

1951+
-- another case requiring nested PlaceHolderVars
1952+
explain (verbose, costs off)
1953+
select*from
1954+
(select0as val0)as ss0
1955+
left join (select1as val)as ss1on true
1956+
left join lateral (selectss1.valas val_filteredwhere false)as ss2on true;
1957+
1958+
select*from
1959+
(select0as val0)as ss0
1960+
left join (select1as val)as ss1on true
1961+
left join lateral (selectss1.valas val_filteredwhere false)as ss2on true;
1962+
19511963
-- case that breaks the old ph_may_need optimization
19521964
explain (verbose, costs off)
19531965
select c.*,a.*,ss1.q1,ss2.q1,ss3.*from

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp