8000 Fix planner failure with full join in RHS of left join. · prmdeveloper/postgres@6882dbd · GitHub
[go: up one dir, main page]

Skip to content

Commit 6882dbd

Browse files
committed
Fix planner failure with full join in RHS of left join.
Given a left join containing a full join in its righthand side, with the left join's joinclause referencing only one side of the full join (in a non-strict fashion, so that the full join doesn't get simplified), the planner could fail with "failed to build any N-way joins" or related errors. This happened because the full join was seen as overlapping the left join's RHS, and then recent changes within join_is_legal() caused that function to conclude that the full join couldn't validly be formed. Rather than try to rejigger join_is_legal() yet more to allow this, I think it's better to fix initsplan.c so that the required join order is explicit in the SpecialJoinInfo data structure. The previous coding there essentially ignored full joins, relying on the fact that we don't flatten them in the joinlist data structure to preserve their ordering. That's sufficient to prevent a wrong plan from being formed, but as this example shows, it's not sufficient to ensure that the right plan will be formed. We need to work a bit harder to ensure that the right plan looks sane according to the SpecialJoinInfos. Per bug #14105 from Vojtech Rylko. This was apparently induced by commit 8703059 (though now that I've seen it, I wonder whether there are related cases that could have failed before that); so back-patch to all active branches. Unfortunately, that patch also went into 9.0, so this bug is a regression that won't be fixed in that branch.
1 parent 9028f40 commit 6882dbd

File tree

3 files changed

+72
-1
lines changed

3 files changed

+72
-1
lines changed

src/backend/optimizer/plan/initsplan.c

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -648,9 +648,32 @@ make_outerjoininfo(PlannerInfo *root,
648648
{
649649
SpecialJoinInfo *otherinfo = (SpecialJoinInfo *) lfirst(l);
650650

651-
/* ignore full joins --- other mechanisms preserve their ordering */
651+
/*
652+
* A full join is an optimization barrier: we can't associate into or
653+
* out of it. Hence, if it overlaps either LHS or RHS of the current
654+
* rel, expand that side's min relset to cover the whole full join.
655+
*/
652656
if (otherinfo->jointype == JOIN_FULL)
657+
{
658+
if (bms_overlap(left_rels, otherinfo->syn_lefthand) ||
659+
bms_overlap(left_rels, otherinfo->syn_righthand))
660+
{
661+
min_lefthand = bms_add_members(min_lefthand,
662+
otherinfo->syn_lefthand);
663+
min_lefthand = bms_add_members(min_lefthand,
664+
otherinfo->syn_righthand);
665+
}
666+
if (bms_overlap(right_rels, otherinfo->syn_lefthand) ||
667+
bms_overlap(right_rels, otherinfo->syn_righthand))
668+
{
669+
min_righthand = bms_add_members(min_righthand,
670+
otherinfo->syn_lefthand);
671+
min_righthand = bms_add_members(min_righthand,
672+
otherinfo->syn_righthand);
673+
}
674+
/* Needn't do anything else with the full join */
653675
continue;
676+
}
654677

655678
/*
656679
* For a lower OJ in our LHS, if our join condition uses the lower

src/test/regress/expected/join.out

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3048,6 +3048,37 @@ select * from
30483048
doh! | 123 | 456 | hi de ho neighbor |
30493049
(2 rows)
30503050

3051+
--
3052+
-- test successful handling of full join underneath left join (bug #14105)
3053+
--
3054+
explain (costs off)
3055+
select * from
3056+
(select 1 as id) as xx
3057+
left join
3058+
(tenk1 as a1 full join (select 1 as id) as yy on (a1.unique1 = yy.id))
3059+
on (xx.id = coalesce(yy.id));
3060+
QUERY PLAN
3061+
---------------------------------------
3062+
Nested Loop Left Join
3063+
Join Filter: ((1) = COALESCE((1)))
3064+
-> Result
3065+
-> Hash Full Join
3066+
Hash Cond: (a1.unique1 = (1))
3067+
-> Seq Scan on tenk1 a1
3068+
-> Hash
3069+
-> Result
3070+
(8 rows)
3071+
3072+
select * from
3073+
(select 1 as id) as xx
3074+
left join
3075+
(tenk1 as a1 full join (select 1 as id) as yy on (a1.unique1 = yy.id))
3076+
on (xx.id = coalesce(yy.id));
3077+
id | unique1 | unique2 | two | four | ten | twenty | hundred | thousand | twothousand | fivethous | tenthous | odd | even | stringu1 | stringu2 | string4 | id
3078+
----+---------+---------+-----+------+-----+--------+---------+----------+-------------+-----------+----------+-----+------+----------+----------+---------+----
3079+
1 | 1 | 2838 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 2 | 3 | BAAAAA | EFEAAA | OOOOxx | 1
3080+
(1 row)
3081+
30513082
--
30523083
-- test ability to push constants through outer join clauses
30533084
--

src/test/regress/sql/join.sql

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -851,6 +851,23 @@ select * from
851851
left join int4_tbl i4
852852
on i8.q1 = i4.f1;
853853

854+
--
855+
-- test successful handling of full join underneath left join (bug #14105)
856+
--
857+
858+
explain (costs off)
859+
select * from
860+
(select 1 as id) as xx
861+
left join
862+
(tenk1 as a1 full join (select 1 as id) as yy on (a1.unique1 = yy.id))
863+
on (xx.id = coalesce(yy.id));
864+
865+
select * from
866+
(select 1 as id) as xx
867+
left join
868+
(tenk1 as a1 full join (select 1 as id) as yy on (a1.unique1 = yy.id))
869+
on (xx.id = coalesce(yy.id));
870+
854871
--
855872
-- test ability to push constants through outer join clauses
856873
--

0 commit comments

Comments
 (0)
0