This is an automated email from the ASF dual-hosted git repository.

yiguolei pushed a commit to branch branch-4.1
in repository https://gitbox.apache.org/repos/asf/doris.git


The following commit(s) were added to refs/heads/branch-4.1 by this push:
     new 027f8f8b033 branch-4.1: [fix](runtime-filter) Fix race condition in 
Set operator runtime filter processing #62434 (#62811)
027f8f8b033 is described below

commit 027f8f8b033c9f90a456fcba1896bae81ed3470b
Author: Pxl <[email protected]>
AuthorDate: Sat Apr 25 10:05:34 2026 +0800

    branch-4.1: [fix](runtime-filter) Fix race condition in Set operator 
runtime filter processing #62434 (#62811)
    
    Cherry-pick of #62434 to branch-4.1.
    
    ## Conflict resolution
    
    Only file-location conflicts. branch-4.1 had renamed
    `regression-test/{data,suites}/query_p0/runtime_filter/` to
    `regression-test/{data,suites}/nereids_p0/runtime_filter/`. Moved the
    two new regression files (`set_operator_in_filter.groovy` and
    `set_operator_in_filter.out`) to the renamed
    `nereids_p0/runtime_filter/` location accordingly.
    
    `be/src/exec/operator/set_sink_operator.{cpp,h}` merged automatically.
    No code logic from #62434 was changed.
    
    ## Original PR
    
    #62434
    
    Co-authored-by: Copilot <[email protected]>
---
 be/src/exec/operator/set_sink_operator.cpp         |   7 +-
 be/src/exec/operator/set_sink_operator.h           |   5 +
 .../runtime_filter/set_operator_in_filter.out      |  23 ++++
 .../runtime_filter/set_operator_in_filter.groovy   | 138 +++++++++++++++++++++
 4 files changed, 172 insertions(+), 1 deletion(-)

diff --git a/be/src/exec/operator/set_sink_operator.cpp 
b/be/src/exec/operator/set_sink_operator.cpp
index 11e1cb61600..27a11f31a66 100644
--- a/be/src/exec/operator/set_sink_operator.cpp
+++ b/be/src/exec/operator/set_sink_operator.cpp
@@ -45,7 +45,7 @@ Status SetSinkLocalState<is_intersect>::close(RuntimeState* 
state, Status exec_s
     if (!_terminated && _runtime_filter_producer_helper && 
!state->is_cancelled()) {
         try {
             RETURN_IF_ERROR(_runtime_filter_producer_helper->process(
-                    state, &_shared_state->build_block, 
_shared_state->get_hash_table_size()));
+                    state, &_shared_state->build_block, 
_build_hash_table_size));
         } catch (Exception& e) {
             return Status::InternalError(
                     "rf process meet error: {}, _terminated: {}, 
_finish_dependency: {}",
@@ -101,6 +101,11 @@ Status SetSinkOperatorX<is_intersect>::sink(RuntimeState* 
state, Block* in_block
         COUNTER_SET(local_state._hash_table_size, (int64_t)hash_table_size);
         COUNTER_SET(local_state._valid_element_in_hash_table, 
valid_element_in_hash_tbl);
 
+        // Snapshot hash table size before enabling the probe pipeline. The 
probe side
+        // can shrink the hash table via _refresh_hash_table() after 
set_ready(), so
+        // close() must use this saved value for runtime filter processing.
+        local_state._build_hash_table_size = hash_table_size;
+
         
local_state._shared_state->probe_finished_children_dependency[_cur_child_id + 1]
                 ->set_ready();
         DCHECK_GT(_child_quantity, 1);
diff --git a/be/src/exec/operator/set_sink_operator.h 
b/be/src/exec/operator/set_sink_operator.h
index 5d518113d4b..2ed03b4b0ae 100644
--- a/be/src/exec/operator/set_sink_operator.h
+++ b/be/src/exec/operator/set_sink_operator.h
@@ -46,6 +46,7 @@ public:
     Status open(RuntimeState* state) override;
     Status terminate(RuntimeState* state) override;
     Status close(RuntimeState* state, Status exec_status) override;
+    Dependency* finishdependency() override { return _finish_dependency.get(); 
}
 
 private:
     friend class SetSinkOperatorX<is_intersect>;
@@ -61,6 +62,10 @@ private:
 
     std::shared_ptr<RuntimeFilterProducerHelperSet> 
_runtime_filter_producer_helper;
     std::shared_ptr<CountedFinishDependency> _finish_dependency;
+    // Snapshot of hash table size taken in sink(eos) before set_ready(). The 
probe side can
+    // modify the hash table via _refresh_hash_table() after set_ready(), so 
close() must use
+    // this saved value instead of calling get_hash_table_size() again.
+    uint64_t _build_hash_table_size = 0;
 };
 
 template <bool is_intersect>
diff --git 
a/regression-test/data/nereids_p0/runtime_filter/set_operator_in_filter.out 
b/regression-test/data/nereids_p0/runtime_filter/set_operator_in_filter.out
new file mode 100644
index 00000000000..b0d2a4133b5
--- /dev/null
+++ b/regression-test/data/nereids_p0/runtime_filter/set_operator_in_filter.out
@@ -0,0 +1,23 @@
+-- This file is automatically generated. You should know what you did if you 
want to edit this
+-- !intersect_in_rf --
+1
+11
+13
+15
+3
+5
+7
+9
+
+-- !except_in_rf --
+
+-- !intersect_in_rf_zero --
+1
+11
+13
+15
+3
+5
+7
+9
+
diff --git 
a/regression-test/suites/nereids_p0/runtime_filter/set_operator_in_filter.groovy
 
b/regression-test/suites/nereids_p0/runtime_filter/set_operator_in_filter.groovy
new file mode 100644
index 00000000000..c44f3caba1c
--- /dev/null
+++ 
b/regression-test/suites/nereids_p0/runtime_filter/set_operator_in_filter.groovy
@@ -0,0 +1,138 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+// Regression test for race condition in Set operator (INTERSECT/EXCEPT) with
+// IN_FILTER runtime filter type.
+//
+// Before the fix, when runtime_filter_type=1 (IN) and 
runtime_filter_max_in_num
+// was small, a race between the build sink's close() and the probe's hash 
table
+// refresh could cause RuntimeFilterWrapper::insert() to fail with 
InternalError.
+// The fix snapshots hash_table_size in sink(eos) before set_ready() and uses 
the
+// saved value in close() for RF processing. Also overrides finishdependency() 
so
+// the pipeline task properly waits for sync_filter_size completion.
+
+suite("set_operator_in_filter") {
+    sql "DROP TABLE IF EXISTS set_rf_t1"
+    sql "DROP TABLE IF EXISTS set_rf_t2"
+    sql "DROP TABLE IF EXISTS set_rf_t3"
+
+    sql """
+        CREATE TABLE set_rf_t1 (
+            k1 INT NOT NULL,
+            v1 VARCHAR(64) NOT NULL
+        )
+        DUPLICATE KEY(k1)
+        DISTRIBUTED BY HASH(k1) BUCKETS 3
+        PROPERTIES("replication_num" = "1");
+    """
+
+    sql """
+        CREATE TABLE set_rf_t2 (
+            k1 INT NOT NULL,
+            v1 VARCHAR(64) NOT NULL
+        )
+        DUPLICATE KEY(k1)
+        DISTRIBUTED BY HASH(k1) BUCKETS 3
+        PROPERTIES("replication_num" = "1");
+    """
+
+    sql """
+        CREATE TABLE set_rf_t3 (
+            k1 INT NOT NULL,
+            v1 VARCHAR(64) NOT NULL
+        )
+        DUPLICATE KEY(k1)
+        DISTRIBUTED BY HASH(k1) BUCKETS 3
+        PROPERTIES("replication_num" = "1");
+    """
+
+    // Insert >10 unique values into t1 so that with max_in_num=10 the IN 
filter
+    // would overflow. This is the key condition that triggers the original 
bug.
+    sql """
+        INSERT INTO set_rf_t1 VALUES
+        (1,'a'),(2,'b'),(3,'c'),(4,'d'),(5,'e'),
+        (6,'f'),(7,'g'),(8,'h'),(9,'i'),(10,'j'),
+        (11,'k'),(12,'l'),(13,'m'),(14,'n'),(15,'o'),
+        (16,'p'),(17,'q'),(18,'r'),(19,'s'),(20,'t');
+    """
+
+    // t2 shares some values with t1 (for INTERSECT) and has extras (for 
EXCEPT)
+    sql """
+        INSERT INTO set_rf_t2 VALUES
+        (1,'a'),(3,'c'),(5,'e'),(7,'g'),(9,'i'),
+        (11,'k'),(13,'m'),(15,'o'),(17,'q'),(19,'s'),
+        (21,'u'),(22,'v');
+    """
+
+    // t3 is the probe-side table for the join that triggers RF generation
+    sql """
+        INSERT INTO set_rf_t3 VALUES
+        (1,'x'),(3,'x'),(5,'x'),(7,'x'),(9,'x'),
+        (11,'x'),(13,'x'),(15,'x'),(100,'x'),(200,'x');
+    """
+
+    sql "sync"
+
+    // Use pure IN_FILTER type with a small max_in_num to reproduce the 
conditions
+    // that triggered the race condition bug.
+    sql "set runtime_filter_type = 1"
+    sql "set runtime_filter_max_in_num = 10"
+    sql "set disable_join_reorder = true"
+
+    // INTERSECT with join: the INTERSECT probe shrinks the hash table, which
+    // previously could race with RF processing in the build sink's close().
+    order_qt_intersect_in_rf """
+        SELECT T.k1 FROM (
+            SELECT k1 FROM set_rf_t1
+            INTERSECT
+            SELECT k1 FROM set_rf_t2
+        ) T JOIN set_rf_t3 ON T.k1 = set_rf_t3.k1;
+    """
+
+    // EXCEPT with join: similar scenario but with EXCEPT semantics.
+    order_qt_except_in_rf """
+        SELECT T.k1 FROM (
+            SELECT k1 FROM set_rf_t1
+            EXCEPT
+            SELECT k1 FROM set_rf_t2
+        ) T JOIN set_rf_t3 ON T.k1 = set_rf_t3.k1;
+    """
+
+    // Run INTERSECT query multiple times to increase coverage of the timing
+    // window. Before the fix, this would intermittently fail.
+    for (int i = 0; i < 5; i++) {
+        sql """
+            SELECT T.k1 FROM (
+                SELECT k1 FROM set_rf_t1
+                INTERSECT
+                SELECT k1 FROM set_rf_t2
+            ) T JOIN set_rf_t3 ON T.k1 = set_rf_t3.k1
+            ORDER BY T.k1;
+        """
+    }
+
+    // Also test with max_in_num=0 (always overflow immediately)
+    sql "set runtime_filter_max_in_num = 0"
+
+    order_qt_intersect_in_rf_zero """
+        SELECT T.k1 FROM (
+            SELECT k1 FROM set_rf_t1
+            INTERSECT
+            SELECT k1 FROM set_rf_t2
+        ) T JOIN set_rf_t3 ON T.k1 = set_rf_t3.k1;
+    """
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to