From ad87ceb13fbbb2cd941cb2e67275604f282f5a25 Mon Sep 17 00:00:00 2001 From: tnayak Date: Thu, 14 Mar 2024 14:31:18 -0700 Subject: [PATCH] [#21292] YSQL: Fix logic to detect unbatchable filters Summary: This change fixes a bug where we would not detect some unbatchable filters when forming an indexpath if the indexpath acceptable batched values from more than one relation. Needs backports to 2.20 and 2.18 Jira: DB-10208 Test Plan: ./yb_buid.sh --java-test "org.yb.pgsql.TestPgRegressJoin' Reviewers: mtakahara Reviewed By: mtakahara Subscribers: yql Differential Revision: https://phorge.dev.yugabyte.com/D33179 --- .../src/backend/optimizer/path/indxpath.c | 2 +- .../regress/expected/yb_join_batching.out | 25 +++++++++++++++++++ .../src/test/regress/sql/yb_join_batching.sql | 11 ++++++++ 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/src/postgres/src/backend/optimizer/path/indxpath.c b/src/postgres/src/backend/optimizer/path/indxpath.c index 0fbc055eb6f8..c871222f3223 100644 --- a/src/postgres/src/backend/optimizer/path/indxpath.c +++ b/src/postgres/src/backend/optimizer/path/indxpath.c @@ -718,7 +718,7 @@ yb_get_batched_index_paths(PlannerInfo *root, RelOptInfo *rel, yb_get_batched_restrictinfo(rinfo, batchedrelids, index->rel->relids); - if (!bms_is_subset(batched_and_inner_relids, rinfo->clause_relids)) + if (!bms_overlap(rinfo->clause_relids, batchedrelids)) continue; Assert(bms_overlap(rinfo->clause_relids, batchedrelids)); diff --git a/src/postgres/src/test/regress/expected/yb_join_batching.out b/src/postgres/src/test/regress/expected/yb_join_batching.out index fc64d45abda2..675408ceb7ed 100644 --- a/src/postgres/src/test/regress/expected/yb_join_batching.out +++ b/src/postgres/src/test/regress/expected/yb_join_batching.out @@ -1829,6 +1829,31 @@ a = int4table.a; drop table oidtable; drop table int4table; +create table ss1(a int); +create table ss2(a int); +create table ss3(a int, b int, primary key(a asc, b asc)); +-- Should not result in an illegal BNL on the inner side +/*+Leading((ss1 (ss2 ss3)))*/ explain (costs off) select * from ss1, ss2, ss3 where ss3.a = ss2.a and ss3.b = ss1.a and ss3.a <> ss2.a; + QUERY PLAN +------------------------------------------------------------------------------ + YB Batched Nested Loop Join + Join Filter: (ss3.b = ss1.a) + -> Seq Scan on ss1 + -> Nested Loop + -> Seq Scan on ss2 + -> Index Scan using ss3_pkey on ss3 + Index Cond: ((b = ANY (ARRAY[ss1.a, $1, $2])) AND (a = ss2.a)) + Filter: (a <> ss2.a) +(8 rows) + +/*+Leading((ss1 (ss2 ss3)))*/ select * from ss1, ss2, ss3 where ss3.a = ss2.a and ss3.b = ss1.a and ss3.a <> ss2.a; + a | a | a | b +---+---+---+--- +(0 rows) + +drop table ss1; +drop table ss2; +drop table ss3; SELECT '' AS "xxx", * FROM J1_TBL AS tx order by 1, 2, 3, 4; xxx | i | j | t diff --git a/src/postgres/src/test/regress/sql/yb_join_batching.sql b/src/postgres/src/test/regress/sql/yb_join_batching.sql index 5c44f85f9ba6..94fd6db2ca23 100644 --- a/src/postgres/src/test/regress/sql/yb_join_batching.sql +++ b/src/postgres/src/test/regress/sql/yb_join_batching.sql @@ -515,6 +515,17 @@ a = int4table.a; drop table oidtable; drop table int4table; +create table ss1(a int); +create table ss2(a int); +create table ss3(a int, b int, primary key(a asc, b asc)); + +-- Should not result in an illegal BNL on the inner side +/*+Leading((ss1 (ss2 ss3)))*/ explain (costs off) select * from ss1, ss2, ss3 where ss3.a = ss2.a and ss3.b = ss1.a and ss3.a <> ss2.a; +/*+Leading((ss1 (ss2 ss3)))*/ select * from ss1, ss2, ss3 where ss3.a = ss2.a and ss3.b = ss1.a and ss3.a <> ss2.a; +drop table ss1; +drop table ss2; +drop table ss3; + SELECT '' AS "xxx", * FROM J1_TBL AS tx order by 1, 2, 3, 4;