Skip to content

Commit a4df781

Browse files
committed
Fix an oversight in checking whether a join with LATERAL refs is legal.
In many cases, we can implement a semijoin as a plain innerjoin by first passing the righthand-side relation through a unique-ification step. However, one of the cases where this does NOT work is where the RHS has a LATERAL reference to the LHS; that makes the RHS dependent on the LHS so that unique-ification is meaningless. joinpath.c understood this, and so would not generate any join paths of this kind ... but join_is_legal neglected to check for the case, so it would think that we could do it. The upshot would be a "could not devise a query plan for the given query" failure once we had failed to generate any join paths at all for the bogus join pair. Back-patch to 9.3 where LATERAL was added.
1 parent caae9f7 commit a4df781

File tree

3 files changed

+54
-2
lines changed

3 files changed

+54
-2
lines changed

src/backend/optimizer/path/joinrels.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,9 @@ join_is_legal(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2,
536536
if (!bms_is_subset(ljinfo->lateral_lhs, rel1->relids))
537537
return false; /* rel1 can't compute the required parameter */
538538
if (match_sjinfo &&
539-
(reversed || match_sjinfo->jointype == JOIN_FULL))
539+
(reversed ||
540+
unique_ified ||
541+
match_sjinfo->jointype == JOIN_FULL))
540542
return false; /* not implementable as nestloop */
541543
}
542544
if (bms_is_subset(ljinfo->lateral_rhs, rel1->relids) &&
@@ -549,7 +551,9 @@ join_is_legal(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2,
549551
if (!bms_is_subset(ljinfo->lateral_lhs, rel2->relids))
550552
return false; /* rel2 can't compute the required parameter */
551553
if (match_sjinfo &&
552-
(!reversed || match_sjinfo->jointype == JOIN_FULL))
554+
(!reversed ||
555+
unique_ified ||
556+
match_sjinfo->jointype == JOIN_FULL))
553557
return false; /* not implementable as nestloop */
554558
}
555559
}

src/test/regress/expected/join.out

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4282,6 +4282,41 @@ select * from
42824282
Output: 3
42834283
(11 rows)
42844284

4285+
-- check we don't try to do a unique-ified semijoin with LATERAL
4286+
explain (verbose, costs off)
4287+
select * from
4288+
(values (0,9998), (1,1000)) v(id,x),
4289+
lateral (select f1 from int4_tbl
4290+
where f1 = any (select unique1 from tenk1
4291+
where unique2 = v.x offset 0)) ss;
4292+
QUERY PLAN
4293+
----------------------------------------------------------------------
4294+
Nested Loop
4295+
Output: "*VALUES*".column1, "*VALUES*".column2, int4_tbl.f1
4296+
-> Values Scan on "*VALUES*"
4297+
Output: "*VALUES*".column1, "*VALUES*".column2
4298+
-> Hash Semi Join
4299+
Output: int4_tbl.f1
4300+
Hash Cond: (int4_tbl.f1 = tenk1.unique1)
4301+
-> Seq Scan on public.int4_tbl
4302+
Output: int4_tbl.f1
4303+
-> Hash
4304+
Output: tenk1.unique1
4305+
-> Index Scan using tenk1_unique2 on public.tenk1
4306+
Output: tenk1.unique1
4307+
Index Cond: (tenk1.unique2 = "*VALUES*".column2)
4308+
(14 rows)
4309+
4310+
select * from
4311+
(values (0,9998), (1,1000)) v(id,x),
4312+
lateral (select f1 from int4_tbl
4313+
where f1 = any (select unique1 from tenk1
4314+
where unique2 = v.x offset 0)) ss;
4315+
id | x | f1
4316+
----+------+----
4317+
0 | 9998 | 0
4318+
(1 row)
4319+
42854320
-- test some error cases where LATERAL should have been used but wasn't
42864321
select f1,g from int4_tbl a, (select f1 as g) ss;
42874322
ERROR: column "f1" does not exist

src/test/regress/sql/join.sql

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1270,6 +1270,19 @@ select * from
12701270
select * from (select 3 as z) z where z.z = x.x
12711271
) zz on zz.z = y.y;
12721272

1273+
-- check we don't try to do a unique-ified semijoin with LATERAL
1274+
explain (verbose, costs off)
1275+
select * from
1276+
(values (0,9998), (1,1000)) v(id,x),
1277+
lateral (select f1 from int4_tbl
1278+
where f1 = any (select unique1 from tenk1
1279+
where unique2 = v.x offset 0)) ss;
1280+
select * from
1281+
(values (0,9998), (1,1000)) v(id,x),
1282+
lateral (select f1 from int4_tbl
1283+
where f1 = any (select unique1 from tenk1
1284+
where unique2 = v.x offset 0)) ss;
1285+
12731286
-- test some error cases where LATERAL should have been used but wasn't
12741287
select f1,g from int4_tbl a, (select f1 as g) ss;
12751288
select f1,g from int4_tbl a, (select a.f1 as g) ss;

0 commit comments

Comments
 (0)