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

Commitea1d542

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 parent5b0287a commitea1d542

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
@@ -2364,8 +2364,13 @@ pullup_replace_vars_callback(Var *var,
23642364
elseif (newnode&&IsA(newnode,PlaceHolderVar)&&
23652365
((PlaceHolderVar*)newnode)->phlevelsup==0)
23662366
{
2367-
/* No need to wrap a PlaceHolderVar with another one, either */
2368-
wrap= false;
2367+
/* The same rules apply for a PlaceHolderVar */
2368+
if (rcon->target_rte->lateral&&
2369+
!bms_is_subset(((PlaceHolderVar*)newnode)->phrels,
2370+
rcon->relids))
2371+
wrap= true;
2372+
else
2373+
wrap= false;
23692374
}
23702375
elseif (rcon->wrap_non_vars)
23712376
{

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

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

5806+
-- another case requiring nested PlaceHolderVars
5807+
explain (verbose, costs off)
5808+
select * from
5809+
(select 0 as val0) as ss0
5810+
left join (select 1 as val) as ss1 on true
5811+
left join lateral (select ss1.val as val_filtered where false) as ss2 on true;
5812+
QUERY PLAN
5813+
--------------------------------
5814+
Nested Loop Left Join
5815+
Output: 0, (1), ((1))
5816+
-> Result
5817+
Output: 1
5818+
-> Result
5819+
Output: (1)
5820+
One-Time Filter: false
5821+
(7 rows)
5822+
5823+
select * from
5824+
(select 0 as val0) as ss0
5825+
left join (select 1 as val) as ss1 on true
5826+
left join lateral (select ss1.val as val_filtered where false) as ss2 on true;
5827+
val0 | val | val_filtered
5828+
------+-----+--------------
5829+
0 | 1 |
5830+
(1 row)
5831+
58065832
-- case that breaks the old ph_may_need optimization
58075833
explain (verbose, costs off)
58085834
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
@@ -1978,6 +1978,18 @@ select * from
19781978
)onc.q2=ss2.q1,
19791979
lateral (selectss2.y offset0) ss3;
19801980

1981+
-- another case requiring nested PlaceHolderVars
1982+
explain (verbose, costs off)
1983+
select*from
1984+
(select0as val0)as ss0
1985+
left join (select1as val)as ss1on true
1986+
left join lateral (selectss1.valas val_filteredwhere false)as ss2on true;
1987+
1988+
select*from
1989+
(select0as val0)as ss0
1990+
left join (select1as val)as ss1on true
1991+
left join lateral (selectss1.valas val_filteredwhere false)as ss2on true;
1992+
19811993
-- case that breaks the old ph_may_need optimization
19821994
explain (verbose, costs off)
19831995
select c.*,a.*,ss1.q1,ss2.q1,ss3.*from

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp