Fix right-semi-joins in HashJoin rescans
authorRichard Guo <[email protected]>
Mon, 9 Dec 2024 11:36:23 +0000 (20:36 +0900)
committerRichard Guo <[email protected]>
Mon, 9 Dec 2024 11:36:23 +0000 (20:36 +0900)
When resetting a HashJoin node for rescans, if it is a single-batch
join and there are no parameter changes for the inner subnode, we can
just reuse the existing hash table without rebuilding it.  However,
for join types that depend on the inner-tuple match flags in the hash
table, we need to reset these match flags to avoid incorrect results.
This applies to right, right-anti, right-semi, and full joins.

When I introduced "Right Semi Join" plan shapes in aa86129e1, I failed
to reset the match flags in the hash table for right-semi joins in
rescans.  This oversight has been shown to produce incorrect results.
This  fixes it.

Author: Richard Guo
Discussion: https://postgr.es/m/CAMbWs4-nQF9io2WL2SkD0eXvfPdyBc9Q=hRwfQHCGV2usa0jyA@mail.gmail.com

src/backend/executor/nodeHashjoin.c
src/test/regress/expected/join.out
src/test/regress/sql/join.sql

index 6c3009fba0fc6ac4518c343a1425642c756655c3..ea0045bc0f372054ab05d2148ba93dd18957fdc0 100644 (file)
@@ -1511,10 +1511,11 @@ ExecReScanHashJoin(HashJoinState *node)
            /*
             * Okay to reuse the hash table; needn't rescan inner, either.
             *
-            * However, if it's a right/right-anti/full join, we'd better
-            * reset the inner-tuple match flags contained in the table.
+            * However, if it's a right/right-anti/right-semi/full join, we'd
+            * better reset the inner-tuple match flags contained in the
+            * table.
             */
-           if (HJ_FILL_INNER(node))
+           if (HJ_FILL_INNER(node) || node->js.jointype == JOIN_RIGHT_SEMI)
                ExecHashTableResetMatchFlags(node->hj_HashTable);
 
            /*
index ebf2e3f851a1f2d83cf29d5e794f1f632bffc5f8..51aeb1dae66666dbabcfa5e0539d39eaade00b88 100644 (file)
@@ -3036,6 +3036,69 @@ where not exists (select 1 from tbl_ra t2 where t2.b = t1.a) and t1.b < 2;
 reset enable_hashjoin;
 reset enable_nestloop;
 --
+-- regression test for bug with hash-right-semi join
+--
+create temp table tbl_rs(a int, b int);
+insert into tbl_rs select i, i from generate_series(1,10)i;
+analyze tbl_rs;
+set enable_nestloop to off;
+set enable_hashagg to off;
+-- ensure we get a hash right semi join with SubPlan in hash clauses
+explain (costs off)
+select * from tbl_rs t1
+where (select a from tbl_rs t2
+       where exists (select 1 from
+                     (select (b in (select b from tbl_rs t3)) as c from tbl_rs t4 where t4.a = 1) s
+                     where c in (select t1.a = 1 from tbl_rs t5 union all select true))
+       order by a limit 1) >= 0;
+                                      QUERY PLAN                                      
+--------------------------------------------------------------------------------------
+ Seq Scan on tbl_rs t1
+   Filter: ((SubPlan 3) >= 0)
+   SubPlan 3
+     ->  Limit
+           InitPlan 2
+             ->  Hash Right Semi Join
+                   Hash Cond: (((t1.a = 1)) = (ANY (t4.b = (hashed SubPlan 1).col1)))
+                   ->  Append
+                         ->  Seq Scan on tbl_rs t5
+                         ->  Result
+                   ->  Hash
+                         ->  Seq Scan on tbl_rs t4
+                               Filter: (a = 1)
+                         SubPlan 1
+                           ->  Seq Scan on tbl_rs t3
+           ->  Sort
+                 Sort Key: t2.a
+                 ->  Result
+                       One-Time Filter: (InitPlan 2).col1
+                       ->  Seq Scan on tbl_rs t2
+(20 rows)
+
+-- and check we get the expected results
+select * from tbl_rs t1
+where (select a from tbl_rs t2
+       where exists (select 1 from
+                     (select (b in (select b from tbl_rs t3)) as c from tbl_rs t4 where t4.a = 1) s
+                     where c in (select t1.a = 1 from tbl_rs t5 union all select true))
+       order by a limit 1) >= 0;
+ a  | b  
+----+----
+  1 |  1
+  2 |  2
+  3 |  3
+  4 |  4
+  5 |  5
+  6 |  6
+  7 |  7
+  8 |  8
+  9 |  9
+ 10 | 10
+(10 rows)
+
+reset enable_nestloop;
+reset enable_hashagg;
+--
 -- regression test for bug #13908 (hash join with skew tuples & nbatch increase)
 --
 set work_mem to '64kB';
index 1004fc03551a737a8a352c43bd37a759381ac962..1e9dafca573dc600d16a24a6315e9c0c1b56cb29 100644 (file)
@@ -738,6 +738,36 @@ where not exists (select 1 from tbl_ra t2 where t2.b = t1.a) and t1.b < 2;
 reset enable_hashjoin;
 reset enable_nestloop;
 
+--
+-- regression test for bug with hash-right-semi join
+--
+create temp table tbl_rs(a int, b int);
+insert into tbl_rs select i, i from generate_series(1,10)i;
+analyze tbl_rs;
+
+set enable_nestloop to off;
+set enable_hashagg to off;
+
+-- ensure we get a hash right semi join with SubPlan in hash clauses
+explain (costs off)
+select * from tbl_rs t1
+where (select a from tbl_rs t2
+       where exists (select 1 from
+                     (select (b in (select b from tbl_rs t3)) as c from tbl_rs t4 where t4.a = 1) s
+                     where c in (select t1.a = 1 from tbl_rs t5 union all select true))
+       order by a limit 1) >= 0;
+
+-- and check we get the expected results
+select * from tbl_rs t1
+where (select a from tbl_rs t2
+       where exists (select 1 from
+                     (select (b in (select b from tbl_rs t3)) as c from tbl_rs t4 where t4.a = 1) s
+                     where c in (select t1.a = 1 from tbl_rs t5 union all select true))
+       order by a limit 1) >= 0;
+
+reset enable_nestloop;
+reset enable_hashagg;
+
 --
 -- regression test for bug #13908 (hash join with skew tuples & nbatch increase)
 --