hello-stephen commented on code in PR #71454:
URL: https://github.com/apache/doris/pull/71454#discussion_r4809176403


##########
be/src/exec/pipeline/pipeline_fragment_context.cpp:
##########
@@ -357,6 +359,87 @@ Status 
PipelineFragmentContext::_build_and_prepare_full_pipeline(ThreadPool* thr
     return Status::OK();
 }
 
+size_t PipelineFragmentContext::_task_instance_count(const Pipeline& pipeline) 
const {
+    const auto instances = _params.local_params.size();
+    return pipeline.num_tasks() > 1 ? instances : std::min(instances, size_t 
{1});
+}
+
+void PipelineFragmentContext::_plan_local_broadcast_rf_routes() {
+    _local_broadcast_rf_merge_bypass_ids.clear();
+    // A recursive fragment may be rebuilt with a different task layout in a 
later round.
+    // Use the original request: notify_close() can clear the runtime flag 
before a rerun.
+    if (_params.__isset.need_notify_close && _params.need_notify_close) {
+        return;
+    }
+
+    struct Candidate {
+        const TRuntimeFilterDesc* desc;
+        size_t build_instances;
+        std::set<int> targets;
+        bool needs_merge = false;
+        bool invalid = false;
+    };
+    std::map<int, Candidate> candidates;
+    for (const auto& pipeline : _pipelines) {
+        auto* build = 
dynamic_cast<HashJoinBuildSinkOperatorX*>(pipeline->sink());
+        if (!build || !build->is_broadcast_join()) {
+            continue;
+        }
+        for (const auto& desc : build->runtime_filter_descs()) {
+            // Only a complete broadcast input can replace a union of producer 
payloads.
+            if (!desc.is_broadcast_join || !desc.has_local_targets || 
desc.has_remote_targets ||
+                desc.planId_to_target_expr.empty() || 
_task_instance_count(*pipeline) == 0) {
+                continue;
+            }
+            auto [it, inserted] = candidates.try_emplace(
+                    desc.filter_id, Candidate {&desc, 
_task_instance_count(*pipeline), {}});
+            if (!inserted) {
+                // Multiple build definitions do not establish one instance 
coverage relation.
+                it->second.invalid = true;
+            }
+        }
+    }
+    if (candidates.empty()) {
+        return;
+    }
+
+    for (const auto& pipeline : _pipelines) {
+        for (const auto& op : pipeline->operators()) {
+            const auto* descriptors = op->scan_runtime_filter_descs();
+            if (!descriptors) {
+                continue;
+            }
+            for (const auto& desc : *descriptors) {
+                auto it = candidates.find(desc.filter_id);
+                if (it == candidates.end()) {
+                    continue;
+                }
+                auto& candidate = it->second;
+                const auto node_id = op->node_id();
+                // Actual task instance sets are prefixes of local_params, not 
[0, num_tasks).
+                // Build={0,1,2,3}, Scan={0} is covered; Build={0}, 
Scan={0,1,2,3} is not.
+                if (!desc.is_broadcast_join || !desc.has_local_targets || 
desc.has_remote_targets ||
+                    !desc.planId_to_target_expr.contains(node_id) ||
+                    !candidate.desc->planId_to_target_expr.contains(node_id) ||
+                    !candidate.targets.insert(node_id).second ||
+                    _task_instance_count(*pipeline) > 
candidate.build_instances) {
+                    candidate.invalid = true;
+                }
+                candidate.needs_merge |= op->is_serial_operator() ||
+                                         (desc.__isset.force_local_merge && 
desc.force_local_merge);
+            }
+        }
+    }
+    for (const auto& [id, candidate] : candidates) {
+        // Missing targets include scans in another fragment and non-scan 
consumers (e.g. CTE).
+        // Keep already-direct filters unchanged, including their left-semi 
optimization boundary.
+        if (!candidate.invalid && candidate.needs_merge &&
+            candidate.targets.size() == 
candidate.desc->planId_to_target_expr.size()) {
+            _local_broadcast_rf_merge_bypass_ids.insert(id);

Review Comment:
   [P2] Avoid retaining one full runtime filter per private broadcast build. 
With the FE local exchange planner, 
`enable_broadcast_join_force_passthrough=true` inserts a local exchange for a 
parallel OLAP probe, giving its filter `force_local_merge=true`. If 
`enable_share_hash_table_for_broadcast_join=false`, this bypass lets each scan 
consumer retain its own complete producer wrapper until fragment teardown. The 
old route dropped the private wrappers after publication and had all consumers 
share one merged wrapper. On a BE with 16 instances and a 16 MiB Bloom filter, 
that is 256 MiB retained after publication instead of 16 MiB. Keep the merge 
route when private builds would retain multiple copies, or publish one shared 
complete payload.



##########
regression-test/suites/query_p0/runtime_filter/local_broadcast_rf_direct_publish.groovy:
##########
@@ -0,0 +1,91 @@
+// 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.
+
+suite("local_broadcast_rf_direct_publish") {
+    sql "DROP TABLE IF EXISTS rf_bb_probe"
+    sql "DROP TABLE IF EXISTS rf_bb_probe_parallel"
+    sql "DROP TABLE IF EXISTS rf_bb_build"
+    sql "DROP TABLE IF EXISTS rf_bb_empty"
+    sql """
+        CREATE TABLE rf_bb_probe (k INT, tag INT)
+        DUPLICATE KEY(k) DISTRIBUTED BY HASH(k) BUCKETS 2
+        PROPERTIES ("replication_num" = "1")
+    """
+    sql """
+        CREATE TABLE rf_bb_probe_parallel (k INT, tag INT)
+        DUPLICATE KEY(k) DISTRIBUTED BY HASH(k) BUCKETS 16
+        PROPERTIES ("replication_num" = "1")
+    """
+    sql """
+        CREATE TABLE rf_bb_build (k INT, tag INT)
+        DUPLICATE KEY(k) DISTRIBUTED BY HASH(k) BUCKETS 4
+        PROPERTIES ("replication_num" = "1")
+    """
+    sql "CREATE TABLE rf_bb_empty LIKE rf_bb_build"
+    sql "INSERT INTO rf_bb_probe VALUES 
(1,10),(2,20),(2,21),(3,30),(NULL,40),(99,90)"
+    sql "INSERT INTO rf_bb_probe_parallel SELECT * FROM rf_bb_probe"
+    sql "INSERT INTO rf_bb_build VALUES (2,200),(2,201),(3,300),(NULL,400)"
+
+    sql "SET parallel_pipeline_task_num=4"
+    sql "SET parallel_prepare_threshold=1"
+    sql "SET disable_join_reorder=true"
+    sql "SET ignore_storage_data_distribution=true"
+    sql "SET enable_local_shuffle=true"
+    sql "SET enable_sql_cache=false"
+    sql "SET enable_runtime_filter_prune=false"
+    sql "SET runtime_filter_wait_time_ms=10000"
+    sql "SET enable_left_semi_direct_return_opt=true"
+
+    for (def planner : [true, false]) {
+        sql "SET enable_local_shuffle_planner=${planner}"
+        for (def shared : [true, false]) {
+            sql "SET enable_share_hash_table_for_broadcast_join=${shared}"
+            for (def serial : [true, false]) {
+                sql "SET force_to_local_shuffle=${serial}"
+                def probe = serial ? "rf_bb_probe" : "rf_bb_probe_parallel"
+                for (def rf : [0, 1, 2]) {
+                    sql "SET runtime_filter_mode=${rf == 0 ? 'OFF' : 'LOCAL'}"
+                    if (rf != 0) {
+                        sql "SET runtime_filter_type=${rf}"
+                    }
+                    def tag = "${planner}_${shared}_${serial}_${rf}"
+                    "order_qt_equal_${tag}" """

Review Comment:
   [P3] Verify direct publication through normal fragment preparation. Removing 
`_plan_local_broadcast_rf_routes()` from `_build_and_prepare_full_pipeline()` 
would leave these row checks unchanged. The routing unit test calls the helper 
directly, and the producer tests inject the bypass ID, so neither would catch 
that loss of the optimization. Add one case that uses normal preparation and 
asserts instance-local registration or zero local merge arrivals.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


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

Reply via email to