morrySnow commented on code in PR #65837:
URL: https://github.com/apache/doris/pull/65837#discussion_r3635201131


##########
be/src/exec/runtime_filter/runtime_filter_bucket_pruner.cpp:
##########
@@ -0,0 +1,164 @@
+// 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.
+
+#include "exec/runtime_filter/runtime_filter_bucket_pruner.h"
+
+#include <gen_cpp/PlanNodes_types.h>
+
+#include <algorithm>
+#include <memory>
+#include <mutex>
+
+#include "core/column/column.h"
+#include "core/data_type/data_type.h"
+#include "core/data_type/primitive_type.h"
+#include "core/string_ref.h"
+#include "exprs/hybrid_set.h"
+#include "exprs/runtime_filter_expr.h"
+#include "exprs/vexpr.h"
+#include "exprs/vexpr_context.h"
+#include "exprs/vslot_ref.h"
+
+namespace doris {
+
+static void materialize_hashes(const VExprSPtr& target_expr, HybridSetBase* 
hybrid_set,
+                               std::vector<uint32_t>* hashes) {
+    DORIS_CHECK(target_expr != nullptr);
+    DORIS_CHECK(hybrid_set != nullptr);
+
+    const DataTypePtr& data_type = target_expr->data_type();
+    MutableColumnPtr column = data_type->create_column();
+    PrimitiveType primitive_type = data_type->get_primitive_type();
+    auto* iter = hybrid_set->begin();
+    while (iter->has_next()) {
+        const void* value = iter->get_value();
+        DORIS_CHECK(value != nullptr);
+        if (is_string_type(primitive_type)) {
+            const auto* string_value = reinterpret_cast<const 
StringRef*>(value);
+            column->insert_data(string_value->data, string_value->size);
+        } else {

Review Comment:
   **Nit: `insert_data(value, 0)` relies on an implicit convention.**
   
   For non-string fixed-length types (INT, BIGINT, etc.), the second argument 
`0` is passed to `insert_data()`. `ColumnVector<T>::insert_data` ignores the 
`length` parameter (marked `/*length*/`), reading exactly `sizeof(T)` bytes via 
`unaligned_load`. This is correct but the convention is not obvious to a reader.
   
   **Suggestion:** Add a brief comment:
   ```cpp
   // length=0 is unused for fixed-length types; ColumnVector::insert_data 
ignores it.
   column->insert_data(reinterpret_cast<const char*>(value), 0);
   ```



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/glue/translator/RuntimeFilterBucketPruneClassifier.java:
##########
@@ -0,0 +1,123 @@
+// 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.
+
+package org.apache.doris.nereids.glue.translator;
+
+import org.apache.doris.analysis.Expr;
+import org.apache.doris.analysis.SlotRef;
+import org.apache.doris.catalog.Column;
+import org.apache.doris.catalog.DistributionInfo;
+import org.apache.doris.catalog.HashDistributionInfo;
+import org.apache.doris.catalog.OlapTable;
+import org.apache.doris.catalog.Partition;
+import org.apache.doris.planner.OlapScanNode;
+import org.apache.doris.planner.PlanNode;
+import org.apache.doris.thrift.TRuntimeFilterType;
+
+/** Classifies direct single-column HASH targets for BE-side runtime-filter 
bucket pruning. */
+final class RuntimeFilterBucketPruneClassifier {
+    private RuntimeFilterBucketPruneClassifier() {
+    }
+
+    static Classification classify(TRuntimeFilterType filterType, Expr 
targetExpr, PlanNode scanNode) {
+        if (filterType != TRuntimeFilterType.IN && filterType != 
TRuntimeFilterType.IN_OR_BLOOM) {
+            return Classification.unsupported("runtime filter is not IN or 
IN_OR_BLOOM");
+        }
+        if (!(scanNode instanceof OlapScanNode)) {
+            return Classification.unsupported("target scan is not an 
OlapScanNode");
+        }
+        if (!(targetExpr instanceof SlotRef)) {
+            return Classification.unsupported("target expression is not a 
direct SlotRef");
+        }
+
+        Column targetColumn = ((SlotRef) targetExpr).getColumn();
+        if (targetColumn == null) {
+            return Classification.unsupported("target SlotRef has no column");
+        }
+
+        OlapScanNode olapScanNode = (OlapScanNode) scanNode;
+        OlapTable table = olapScanNode.getOlapTable();
+        if (table == null || olapScanNode.getSelectedPartitionIds().isEmpty()) 
{
+            return Classification.unsupported("target scan has no selected 
partitions");
+        }
+
+        Column distributionColumn = null;
+        for (Long partitionId : olapScanNode.getSelectedPartitionIds()) {
+            Partition partition = table.getPartition(partitionId);
+            if (partition == null) {
+                return Classification.unsupported("selected partition does not 
exist");
+            }
+            DistributionInfo distributionInfo = 
partition.getDistributionInfo();
+            if (!(distributionInfo instanceof HashDistributionInfo)) {
+                return Classification.unsupported("distribution type is not 
HASH");
+            }
+            HashDistributionInfo hashDistributionInfo = (HashDistributionInfo) 
distributionInfo;
+            if (hashDistributionInfo.getDistributionColumns().size() != 1) {
+                return Classification.unsupported("HASH distribution is not 
single-column");
+            }
+            Column currentDistributionColumn = 
hashDistributionInfo.getDistributionColumns().get(0);
+            if (!sameColumn(targetColumn, currentDistributionColumn)) {
+                return Classification.unsupported("target SlotRef is not the 
HASH distribution column");
+            }
+            if (distributionColumn != null && !sameColumn(distributionColumn, 
currentDistributionColumn)) {
+                return Classification.unsupported("selected partitions use 
different distribution columns");
+            }
+            distributionColumn = currentDistributionColumn;
+        }
+        return Classification.supported();
+    }
+
+    private static boolean sameColumn(Column targetColumn, Column 
distributionColumn) {
+        if (targetColumn == distributionColumn || 
targetColumn.equals(distributionColumn)) {
+            return true;
+        }
+        int targetUniqueId = targetColumn.getUniqueId();
+        int distributionUniqueId = distributionColumn.getUniqueId();
+        if (targetUniqueId != Column.COLUMN_UNIQUE_ID_INIT_VALUE
+                && distributionUniqueId != Column.COLUMN_UNIQUE_ID_INIT_VALUE
+                && targetUniqueId == distributionUniqueId) {
+            return true;
+        }
+        return 
targetColumn.tryGetBaseColumnName().equalsIgnoreCase(distributionColumn.getName());

Review Comment:
   **Issue: Asymmetric column-name comparison may miss pruning opportunities.**
   
   The comparison uses `targetColumn.tryGetBaseColumnName()` on the target side 
but `distributionColumn.getName()` on the distribution side. These methods can 
return different representations of the same column name:
   - `tryGetBaseColumnName()` may strip expression-manipulation 
prefixes/suffixes.
   - `getName()` returns the raw column name from the catalog definition.
   
   If the target column's name was rewritten (e.g., with a qualifier prefix) 
while the distribution column's raw name doesn't match the stripped version, 
this comparison could incorrectly return `false`, disabling bucket pruning for 
an eligible scan.
   
   **Suggestion:** Use the same name-retrieval method on both sides, or compare 
by `getUniqueId()` directly as the primary check, falling back to name 
comparison only when unique IDs are unset.



##########
be/src/exec/runtime_filter/runtime_filter_bucket_pruner.cpp:
##########
@@ -0,0 +1,164 @@
+// 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.
+
+#include "exec/runtime_filter/runtime_filter_bucket_pruner.h"
+
+#include <gen_cpp/PlanNodes_types.h>
+
+#include <algorithm>
+#include <memory>
+#include <mutex>
+
+#include "core/column/column.h"
+#include "core/data_type/data_type.h"
+#include "core/data_type/primitive_type.h"
+#include "core/string_ref.h"
+#include "exprs/hybrid_set.h"
+#include "exprs/runtime_filter_expr.h"
+#include "exprs/vexpr.h"
+#include "exprs/vexpr_context.h"
+#include "exprs/vslot_ref.h"
+
+namespace doris {
+
+static void materialize_hashes(const VExprSPtr& target_expr, HybridSetBase* 
hybrid_set,
+                               std::vector<uint32_t>* hashes) {
+    DORIS_CHECK(target_expr != nullptr);
+    DORIS_CHECK(hybrid_set != nullptr);
+
+    const DataTypePtr& data_type = target_expr->data_type();
+    MutableColumnPtr column = data_type->create_column();
+    PrimitiveType primitive_type = data_type->get_primitive_type();
+    auto* iter = hybrid_set->begin();
+    while (iter->has_next()) {
+        const void* value = iter->get_value();
+        DORIS_CHECK(value != nullptr);
+        if (is_string_type(primitive_type)) {
+            const auto* string_value = reinterpret_cast<const 
StringRef*>(value);
+            column->insert_data(string_value->data, string_value->size);
+        } else {
+            column->insert_data(reinterpret_cast<const char*>(value), 0);
+        }
+        iter->next();
+    }
+    if (hybrid_set->contain_null() && data_type->is_nullable()) {

Review Comment:
   **Issue: Inserting a default value for NULL adds a spurious hash that 
reduces pruning effectiveness.**
   
   When `hybrid_set->contain_null()` is true, `column->insert_default()` adds a 
default value whose hash is computed and included in the selected-bucket set. 
Since `NULL IN (...)` evaluates to `NULL` (unknown), not `true`, no bucket 
should be kept open solely for the NULL element in the IN list. Adding the 
default value's hash may keep an extra bucket open that could otherwise be 
pruned.
   
   While this is *safe* (it is conservative — it only fails to prune a prunable 
bucket, never incorrectly prunes a needed bucket), it represents a missed 
pruning opportunity.
   
   **Suggestion:** Either:
   1. Skip the `insert_default()` call entirely — NULL does not match any row, 
so its hash should not prevent any bucket from being pruned.
   2. Or add a comment explaining that including the NULL hash is intentionally 
conservative and why.



-- 
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