Copilot commented on code in PR #62589:
URL: https://github.com/apache/doris/pull/62589#discussion_r3099229731


##########
be/src/exec/runtime_filter/runtime_filter_partition_pruner.cpp:
##########
@@ -0,0 +1,293 @@
+// 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_partition_pruner.h"
+
+#include <gen_cpp/PlanNodes_types.h>
+
+#include "exprs/hybrid_set.h"
+#include "exprs/vexpr.h"
+#include "exprs/vexpr_context.h"
+#include "exprs/vliteral.h"
+#include "exprs/vslot_ref.h"
+#include "runtime/descriptors.h"
+
+namespace doris {
+
+// Helper to extract a typed value from a ColumnPtr's first row.
+// Must be a template function so that `if constexpr` properly discards
+// the inapplicable branch during instantiation.
+template <PrimitiveType PT>
+static typename PrimitiveTypeTraits<PT>::CppType 
extract_value_from_column(const ColumnPtr& col) {
+    using CppType = typename PrimitiveTypeTraits<PT>::CppType;
+    auto data = col->get_data_at(0);
+    if constexpr (is_string_type(PT)) {
+        return CppType(data.data, data.size);
+    } else {
+        return *reinterpret_cast<const CppType*>(data.data);
+    }
+}
+
+void RuntimeFilterPartitionPruner::parse_boundaries(
+        const std::vector<TPartitionBoundary>& boundaries,
+        const phmap::flat_hash_map<int, SlotDescriptor*>& slot_descs) {
+    for (const auto& tb : boundaries) {
+        if (!tb.__isset.partition_id || !tb.__isset.slot_id) {
+            continue;
+        }
+        SlotId slot_id = tb.slot_id;
+
+        auto slot_it = slot_descs.find(slot_id);
+        if (slot_it == slot_descs.end()) {
+            continue;
+        }
+        SlotDescriptor* slot = slot_it->second;
+        PrimitiveType ptype = slot->type()->get_primitive_type();
+        int precision = cast_set<int>(slot->type()->get_precision());
+        int scale = cast_set<int>(slot->type()->get_scale());
+        bool is_nullable = slot->is_nullable();
+
+        ParsedBoundary boundary;
+        boundary.partition_id = tb.partition_id;
+        boundary.slot_id = slot_id;
+        boundary.is_nullable = is_nullable;
+
+        bool parsed_ok = false;
+
+#define BUILD_BOUNDARY_CVR(NAME)                                               
                \
+    case TYPE_##NAME: {                                                        
                \
+        using CppType = typename PrimitiveTypeTraits<TYPE_##NAME>::CppType;    
                \
+        bool is_list = tb.__isset.list_values && !tb.list_values.empty();      
                \
+        bool is_range = tb.__isset.range_start || tb.__isset.range_end;        
                \
+        if (!is_list && !is_range) break;                                      
                \
+        ColumnValueRange<TYPE_##NAME> cvr(slot->col_name(), is_nullable, 
precision, scale);    \
+        auto parse_texpr_node = [&](const TExprNode& node) -> 
std::pair<CppType, ColumnPtr> { \
+            VLiteral literal(node);                                            
                \
+            auto col_ptr = literal.get_column_ptr();                           
                \
+            auto val = extract_value_from_column<TYPE_##NAME>(col_ptr);        
                \
+            return {val, col_ptr};                                             
                \
+        };                                                                     
                \
+        if (is_list) {                                                         
                \
+            auto empty_cvr = 
ColumnValueRange<TYPE_##NAME>::create_empty_column_value_range(    \
+                    is_nullable, precision, scale);                            
                 \
+            for (const auto& node : tb.list_values) {                          
                \
+                auto [val, col_ptr] = parse_texpr_node(node);                  
                \
+                boundary.literal_columns.push_back(std::move(col_ptr));        
                \
+                static_cast<void>(empty_cvr.add_fixed_value(val));             
                \
+            }                                                                  
                \
+            cvr.intersection(empty_cvr);                                       
                \
+        } else {                                                               
                \
+            if (tb.__isset.range_start) {                                      
                \
+                auto [val, col_ptr] = parse_texpr_node(tb.range_start);        
                \
+                boundary.literal_columns.push_back(std::move(col_ptr));        
                \
+                static_cast<void>(cvr.add_range(FILTER_LARGER_OR_EQUAL, val)); 
                \
+            }                                                                  
                \
+            if (tb.__isset.range_end) {                                        
                \
+                auto [val, col_ptr] = parse_texpr_node(tb.range_end);          
                \
+                boundary.literal_columns.push_back(std::move(col_ptr));        
                \
+                static_cast<void>(cvr.add_range(FILTER_LESS, val));            
                \
+            }                                                                  
                \
+        }                                                                      
                \
+        boundary.boundary_cvr = std::move(cvr);                                
                \
+        parsed_ok = true;                                                      
                \
+        break;                                                                 
                \
+    }
+
+        switch (ptype) {
+            BUILD_BOUNDARY_CVR(TINYINT)
+            BUILD_BOUNDARY_CVR(SMALLINT)
+            BUILD_BOUNDARY_CVR(INT)
+            BUILD_BOUNDARY_CVR(BIGINT)
+            BUILD_BOUNDARY_CVR(LARGEINT)
+            BUILD_BOUNDARY_CVR(FLOAT)
+            BUILD_BOUNDARY_CVR(DOUBLE)
+            BUILD_BOUNDARY_CVR(CHAR)
+            BUILD_BOUNDARY_CVR(DATE)
+            BUILD_BOUNDARY_CVR(DATETIME)
+            BUILD_BOUNDARY_CVR(DATEV2)
+            BUILD_BOUNDARY_CVR(DATETIMEV2)
+            BUILD_BOUNDARY_CVR(TIMESTAMPTZ)
+            BUILD_BOUNDARY_CVR(VARCHAR)
+            BUILD_BOUNDARY_CVR(STRING)
+            BUILD_BOUNDARY_CVR(DECIMAL32)
+            BUILD_BOUNDARY_CVR(DECIMAL64)
+            BUILD_BOUNDARY_CVR(DECIMAL128I)
+            BUILD_BOUNDARY_CVR(DECIMAL256)
+            BUILD_BOUNDARY_CVR(DECIMALV2)
+            BUILD_BOUNDARY_CVR(BOOLEAN)
+            BUILD_BOUNDARY_CVR(IPV4)
+            BUILD_BOUNDARY_CVR(IPV6)
+        default:
+            break;
+        }
+#undef BUILD_BOUNDARY_CVR
+
+        if (parsed_ok) {
+            _partition_column_slot_ids.insert(slot_id);
+            _slot_to_boundaries[slot_id].push_back(std::move(boundary));
+        }
+    }
+
+    // Count distinct partition IDs across all boundaries.
+    if (!_partition_column_slot_ids.empty()) {
+        phmap::flat_hash_set<int64_t> all_partition_ids;
+        for (const auto& [_, slot_boundaries] : _slot_to_boundaries) {
+            for (const auto& pb : slot_boundaries) {
+                all_partition_ids.insert(pb.partition_id);
+            }
+        }
+        _total_partition_count = 
static_cast<int64_t>(all_partition_ids.size());
+    }
+}
+
+int64_t RuntimeFilterPartitionPruner::prune_by_runtime_filters(
+        const VExprContextSPtrs& conjuncts) {
+    if (_partition_column_slot_ids.empty()) {
+        return 0;
+    }
+
+    phmap::flat_hash_set<int64_t> newly_pruned;
+
+    for (const auto& conjunct_ctx : conjuncts) {
+        VExprSPtr root = conjunct_ctx->root();
+        if (!root->is_rf_wrapper()) {
+            continue;
+        }
+
+        VExprSPtr impl = root->get_impl();
+        if (!impl) {
+            continue;
+        }
+
+        // Determine the target slot_id from the RF's probe expression.
+        // For IN filter: impl is VDirectInPredicate, children()[0] is VSlotRef
+        // For MinMax filter: impl is VectorizedFnCall (binary pred), 
children()[0] is VSlotRef
+        if (impl->children().empty() || !impl->children()[0]->is_slot_ref()) {
+            continue;
+        }
+        auto* slot_ref = assert_cast<VSlotRef*>(impl->children()[0].get());
+        SlotId slot_id = slot_ref->slot_id();
+        if (!_partition_column_slot_ids.contains(slot_id)) {
+            continue;
+        }
+
+        auto boundaries_it = _slot_to_boundaries.find(slot_id);
+        if (boundaries_it == _slot_to_boundaries.end()) {
+            continue;
+        }
+
+        for (const auto& pb : boundaries_it->second) {
+            if (_pruned_partition_ids.contains(pb.partition_id) ||
+                newly_pruned.contains(pb.partition_id)) {
+                continue;
+            }

Review Comment:
   `prune_by_runtime_filters()` reads `_pruned_partition_ids` (e.g. 
`.contains()`) without holding `_prune_mutex`, but the same set can be written 
under the mutex at the end of the method and read concurrently by 
`is_partition_pruned()`/`pruned_partition_count()`. This is a data race because 
`phmap::flat_hash_set` is not thread-safe for concurrent read/write. Please 
guard *all* accesses to `_pruned_partition_ids` with the mutex (e.g. take a 
`shared_lock` for read checks or a `unique_lock` for the whole pruning pass).



##########
gensrc/thrift/PlanNodes.thrift:
##########
@@ -1386,6 +1418,18 @@ enum TMinMaxRuntimeFilterType {
   MIN_MAX = 4
 }
 
+// Monotonicity of a runtime filter's target expression, used by BE-side
+// partition pruning.  For Range partitions, only MONOTONIC_INCREASING or
+// MONOTONIC_DECREASING target expressions allow safe boundary transformation
+// and pruning.  List partitions can always be pruned regardless of 
monotonicity.
+// When the target expression is a plain SlotRef (identity), FE may omit this
+// field; BE treats an absent value as NON_MONOTONIC (conservative).

Review Comment:
   The comments about `target_expr_monotonicity` conflict: the enum comment 
says FE may omit the field for a SlotRef and BE treats it as NON_MONOTONIC, but 
the `TRuntimeFilterDesc` comment later says FE should set MONOTONIC_INCREASING 
for a SlotRef. Please reconcile these docstrings so they reflect the intended 
behavior (and don’t encourage omitting a field that disables pruning).
   



##########
fe/fe-core/src/main/java/org/apache/doris/planner/RuntimeFilter.java:
##########
@@ -280,6 +285,36 @@ public TRuntimeFilterDesc toThrift() {
         } else if (builderNode instanceof SetOperationNode) {
             tFilter.setNullAware(true);
         }
+
+        // Set target_expr_monotonicity for partition column identity path.
+        // When the RF target is a direct SlotRef on a partition column, the
+        // mapping from column value to partition is monotonic (identity), so
+        // the BE can use the RF values to prune partitions.
+        for (RuntimeFilterTarget target : targets) {
+            if (target.expr instanceof SlotRef && target.node instanceof 
OlapScanNode) {
+                SlotRef slotRef = (SlotRef) target.expr;
+                Column col = slotRef.getColumn();
+                if (col != null) {
+                    OlapScanNode scanNode = (OlapScanNode) target.node;
+                    OlapTable table = scanNode.getOlapTable();
+                    PartitionType partType = 
table.getPartitionInfo().getType();
+                    if (partType == PartitionType.RANGE || partType == 
PartitionType.LIST) {
+                        for (Column partCol : 
table.getPartitionInfo().getPartitionColumns()) {
+                            if 
(partCol.getName().equalsIgnoreCase(col.getName())) {
+                                tFilter.setTargetExprMonotonicity(
+                                        
TTargetExprMonotonicity.MONOTONIC_INCREASING);
+                                break;
+                            }
+                        }
+                    }
+                }
+            }
+            // Only check the first target; if there are multiple targets on
+            // different tables, monotonicity may differ. We conservatively
+            // set it based on the first target only.
+            break;
+        }

Review Comment:
   The loop that sets `target_expr_monotonicity` always `break`s after the 
first target, even when the first target is not an `OlapScanNode` / 
partition-column `SlotRef`. That means the field may stay unset even if a later 
target is eligible, unnecessarily disabling partition pruning. Consider 
scanning targets until an eligible partition-column SlotRef is found (or 
explicitly selecting the intended target deterministically) before deciding to 
stop.



##########
regression-test/conf/regression-conf.groovy:
##########
@@ -24,8 +24,8 @@ defaultDb = "regression_test"
 // init cmd like: select @@session.tx_read_only
 // at each time we connect.
 // add allowLoadLocalInfile so that the jdbc can execute mysql load data from 
client.
-jdbcUrl = 
"jdbc:mysql://127.0.0.1:9030/?useLocalSessionState=true&allowLoadLocalInfile=true&zeroDateTimeBehavior=round"
-targetJdbcUrl = 
"jdbc:mysql://127.0.0.1:9030/?useLocalSessionState=true&allowLoadLocalInfile=true&zeroDateTimeBehavior=round"
+jdbcUrl = 
"jdbc:mysql://127.0.0.1:9333/?useLocalSessionState=true&allowLoadLocalInfile=true&zeroDateTimeBehavior=round"
+targetJdbcUrl = 
"jdbc:mysql://127.0.0.1:9333/?useLocalSessionState=true&allowLoadLocalInfile=true&zeroDateTimeBehavior=round"

Review Comment:
   `jdbcUrl` / `targetJdbcUrl` were changed from 9030 to 9333, but other 
related settings in this same config (e.g. `ccrDownstreamUrl`, `doris_port`, 
and `ccrDownstreamFeThriftAddress`) still use 9030. This makes the regression 
test config internally inconsistent and will likely break CI environments that 
expect default ports. Please revert these port changes or make them 
configurable (e.g. via env/property) while keeping repo defaults consistent.
   



##########
gensrc/thrift/PlanNodes.thrift:
##########
@@ -913,6 +938,13 @@ struct TOlapScanNode {
   24: optional bool enable_mor_value_predicate_pushdown
   // Read MOR table as DUP table: skip merge, skip delete sign
   25: optional bool read_mor_as_dup
+  // Partition-id → tablet-id list mapping; used together with 
partition_boundaries
+  // for BE-side runtime filter partition pruning.
+  26: optional map<Types.TPartitionId, list<Types.TTabletId>> 
partiton_to_tablets
+  // Partition boundary descriptors for BE-side runtime filter partition 
pruning.
+  // Only partitions that are candidates for pruning are included; partitions 
FE
+  // does not want pruned (e.g. default catch-all) are omitted from this list.
+  27: optional list<TPartitionBoundary> partition_boundaries

Review Comment:
   The field name `partiton_to_tablets` appears to be a typo (missing 'i' in 
'partition'). Even though Thrift field names don't affect wire compatibility, 
this typo becomes part of generated code APIs and will be hard to undo later. 
Please rename it to `partition_to_tablets` before merging (keeping field id 26 
unchanged).



##########
regression-test/conf/regression-conf.groovy:
##########
@@ -35,7 +35,7 @@ syncerAddress = "127.0.0.1:9190"
 feSyncerUser = "root"
 feSyncerPassword = ""
 
-feHttpAddress = "127.0.0.1:8030"
+feHttpAddress = "127.0.0.1:8333"

Review Comment:
   `feHttpAddress` was changed from 8030 to 8333 while other regression-test 
defaults remain on standard ports. This will cause profile-related and 
HTTP-based regression tests to fail unless the environment is customized. 
Please keep the committed default `feHttpAddress` aligned with the standard FE 
HTTP port (or make it configurable without changing the default).
   



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