github-actions[bot] commented on code in PR #62589:
URL: https://github.com/apache/doris/pull/62589#discussion_r3108286524


##########
be/src/exec/runtime_filter/runtime_filter_partition_pruner.cpp:
##########
@@ -0,0 +1,374 @@
+// 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 <optional>
+#include <utility>
+
+#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);
+    }
+}
+
+// 
NOLINTBEGIN(readability-function-cognitive-complexity,readability-function-size)
+// Complexity is inflated by macro expansion for each PrimitiveType case.
+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);    \
+        /* Returns nullopt if `node` is a NULL literal; the caller then sets 
contain_null  */  \
+        /* on the CVR instead of trying to extract a typed value (which would 
dereference  */  \
+        /* a null data pointer for the non-string branch).                     
            */  \
+        auto parse_texpr_node =                                                
                \
+                [&](const TExprNode& node) -> std::optional<std::pair<CppType, 
ColumnPtr>> {   \
+            if (node.node_type == TExprNodeType::NULL_LITERAL) {               
                \
+                return std::nullopt;                                           
                \
+            }                                                                  
                \
+            VLiteral literal(node);                                            
                \
+            auto col_ptr = literal.get_column_ptr();                           
                \
+            auto val = extract_value_from_column<TYPE_##NAME>(col_ptr);        
                \
+            return std::make_optional(std::pair<CppType, ColumnPtr> {val, 
col_ptr});           \
+        };                                                                     
                \
+        if (is_list) {                                                         
                \
+            auto empty_cvr = 
ColumnValueRange<TYPE_##NAME>::create_empty_column_value_range(   \
+                    is_nullable, precision, scale);                            
                \
+            bool list_has_null = false;                                        
                \
+            bool list_has_value = false;                                       
                \
+            for (const auto& node : tb.list_values) {                          
                \
+                auto parsed = parse_texpr_node(node);                          
                \
+                if (!parsed) {                                                 
                \
+                    list_has_null = true;                                      
                \
+                    continue;                                                  
                \
+                }                                                              
                \
+                auto& [val, col_ptr] = *parsed;                                
                \
+                boundary.literal_columns.push_back(std::move(col_ptr));        
                \
+                static_cast<void>(empty_cvr.add_fixed_value(val));             
                \
+                list_has_value = true;                                         
                \
+            }                                                                  
                \
+            if (list_has_value) {                                              
                \
+                cvr.intersection(empty_cvr);                                   
                \
+            }                                                                  
                \
+            if (list_has_null && is_nullable) {                                
                \
+                /* Track NULL membership on ParsedBoundary; calling          
*/                \
+                /* cvr.set_contain_null(true) here would invoke              
*/                \
+                /* set_empty_value_range() and discard the concrete fixed    
*/                \
+                /* values we just inserted, turning {NULL, v} into a         
*/                \
+                /* NULL-only boundary.                                       
*/                \
+                boundary.contains_null = true;                                 
                \
+                if (!list_has_value) {                                         
                \
+                    boundary.only_null = true;                                 
                \
+                }                                                              
                \
+            }                                                                  
                \
+        } else {                                                               
                \
+            if (tb.__isset.range_start) {                                      
                \
+                auto parsed = parse_texpr_node(tb.range_start);                
                \
+                if (parsed) {                                                  
                \
+                    auto& [val, col_ptr] = *parsed;                            
                \
+                    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 parsed = parse_texpr_node(tb.range_end);                  
                \
+                if (parsed) {                                                  
                \
+                    auto& [val, col_ptr] = *parsed;                            
                \
+                    boundary.literal_columns.push_back(std::move(col_ptr));    
                \
+                    /* Multi-column RANGE projection emits a CLOSED upper 
bound (see       */  \
+                    /* TPartitionBoundary.range_end_inclusive comment); 
single-column RANGE */ \
+                    /* keeps the natural OPEN upper bound matching Doris 
semantics.         */ \
+                    SQLFilterOp upper_op =                                     
                \
+                            (tb.__isset.range_end_inclusive && 
tb.range_end_inclusive)         \
+                                    ? FILTER_LESS_OR_EQUAL                     
                \
+                                    : FILTER_LESS;                             
                \
+                    static_cast<void>(cvr.add_range(upper_op, 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());
+    }
+}
+// 
NOLINTEND(readability-function-cognitive-complexity,readability-function-size)
+
+static SQLFilterOp convert_opcode_to_filter_op(TExprOpcode::type op) {
+    switch (op) {
+    case TExprOpcode::LE:
+        return FILTER_LESS_OR_EQUAL;
+    case TExprOpcode::LT:
+        return FILTER_LESS;
+    case TExprOpcode::GE:
+        return FILTER_LARGER_OR_EQUAL;
+    case TExprOpcode::GT:
+        return FILTER_LARGER;
+    default:
+        return FILTER_IN; // sentinel: caller should skip
+    }
+}
+
+void RuntimeFilterPartitionPruner::_try_prune_by_single_rf(
+        const VExprSPtr& impl, SlotId slot_id, phmap::flat_hash_set<int64_t>& 
newly_pruned) {
+    auto boundaries_it = _slot_to_boundaries.find(slot_id);
+    if (boundaries_it == _slot_to_boundaries.end()) {
+        return;
+    }
+
+    // Pre-compute whether the RF "matches NULL" -- i.e. whether the RF would
+    // accept a row whose probe value is NULL. This is encoded by the set's
+    // contain_null() (see FilterBase::contain_null = _null_aware && 
_contain_null).
+    // For BINARY_PRED predicates (=, <, >, <=, >=) NULL never compares true,
+    // so they never match a NULL probe row -- treat as not containing NULL.
+    auto hybrid_set_for_null = impl->get_set_func();

Review Comment:
   `rf_contains_null` is only derived from `get_set_func()`, which means it 
works for direct-IN filters but stays false for the other null-aware RF 
implementations. In particular, `BLOOM_PRED` carries its NULL bit on 
`get_bloom_filter_func()->contain_null()`, and null-aware min/max filters 
arrive as `NULL_AWARE_BINARY_PRED` rather than `BINARY_PRED`. With the current 
logic, a LIST partition like `VALUES IN ((NULL))` is pruned by the 
`pb.only_null` branch for a null-safe `<=>` join as soon as the RF is realized 
as bloom or null-aware min/max, even though NULL probe rows should match and 
the partition must be kept.



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