HappenLee commented on code in PR #63389:
URL: https://github.com/apache/doris/pull/63389#discussion_r3401727000


##########
be/src/exprs/expr_zonemap_filter.cpp:
##########
@@ -0,0 +1,249 @@
+// 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 "exprs/expr_zonemap_filter.h"
+
+#include <algorithm>
+#include <set>
+
+#include "common/check.h"
+#include "common/logging.h"
+#include "core/column/column.h"
+#include "core/data_type/data_type_nullable.h"
+#include "exprs/hybrid_set.h"
+#include "exprs/vdirect_in_predicate.h"
+#include "exprs/vexpr.h"
+#include "exprs/vexpr_context.h"
+#include "exprs/vliteral.h"
+#include "exprs/vslot_ref.h"
+#include "runtime/runtime_state.h"
+
+namespace doris::expr_zonemap {
+namespace {
+
+constexpr size_t kInZoneMapPointCheckThreshold = 64;
+
+VExprSPtr unwrap_identity_cast(const VExprSPtr& expr) {
+    if (expr->node_type() != TExprNodeType::CAST_EXPR || 
expr->get_num_children() != 1) {
+        return expr;
+    }
+    auto child = expr->get_child(0);
+    if 
(!remove_nullable(expr->data_type())->equals(*remove_nullable(child->data_type())))
 {
+        return expr;
+    }
+    return unwrap_identity_cast(child);
+}
+
+std::optional<Field> field_from_literal_expr(const VExprSPtr& expr, 
DataTypePtr* literal_type) {
+    auto unwrapped = unwrap_identity_cast(expr);
+    auto literal = std::dynamic_pointer_cast<VLiteral>(unwrapped);
+    if (literal == nullptr) {
+        return std::nullopt;
+    }
+    Field field;
+    literal->get_column_ptr()->get(0, field);
+    *literal_type = literal->get_data_type();
+    return field;
+}
+
+struct SlotExpr {
+    int index;
+    DataTypePtr data_type;
+};
+
+std::optional<SlotExpr> slot_from_expr(const VExprSPtr& expr) {
+    auto unwrapped = unwrap_identity_cast(expr);
+    auto slot = std::dynamic_pointer_cast<VSlotRef>(unwrapped);
+    if (slot == nullptr) {
+        return std::nullopt;
+    }
+    return SlotExpr {.index = slot->column_id(), .data_type = 
slot->data_type()};
+}
+
+bool value_in_range(const Field& value, const Field& min_value, const Field& 
max_value) {
+    return field_greater_equal(value, min_value) && field_less_equal(value, 
max_value);
+}
+
+} // namespace
+
+std::optional<SlotLiteral> extract_slot_and_literal(const VExprSPtrs& args) {
+    if (args.size() != 2) {
+        return std::nullopt;
+    }
+
+    if (auto slot = slot_from_expr(args[0]); slot.has_value()) {
+        DataTypePtr literal_type;
+        auto literal = field_from_literal_expr(args[1], &literal_type);
+        if (!literal.has_value()) {
+            return std::nullopt;
+        }
+        return SlotLiteral {.slot_index = slot->index,
+                            .slot_type = std::move(slot->data_type),
+                            .literal = std::move(*literal),
+                            .literal_type = std::move(literal_type),
+                            .literal_on_left = false};
+    }
+
+    if (auto slot = slot_from_expr(args[1]); slot.has_value()) {
+        DataTypePtr literal_type;
+        auto literal = field_from_literal_expr(args[0], &literal_type);
+        if (!literal.has_value()) {
+            return std::nullopt;
+        }
+        return SlotLiteral {.slot_index = slot->index,
+                            .slot_type = std::move(slot->data_type),
+                            .literal = std::move(*literal),
+                            .literal_type = std::move(literal_type),
+                            .literal_on_left = true};
+    }
+
+    return std::nullopt;
+}
+
+bool range_stats_usable_for_zonemap(const segment_v2::ZoneMap& zone_map,
+                                    const DataTypePtr& data_type) {
+    if (zone_map.pass_all || zone_map.has_nan || zone_map.has_positive_inf ||
+        zone_map.has_negative_inf) {
+        return false;
+    }
+    DORIS_CHECK(data_type != nullptr);
+    auto primitive_type = remove_nullable(data_type)->get_primitive_type();
+    DORIS_CHECK(field_types_compatible(zone_map.min_value.get_type(), 
primitive_type));
+    DORIS_CHECK(field_types_compatible(zone_map.max_value.get_type(), 
primitive_type));
+    return true;
+}
+
+ZoneMapFilterResult eval_null_zonemap(const ZoneMapEvalContext& ctx, const 
VExprSPtrs& arguments,
+                                      bool is_null) {
+    DORIS_CHECK(arguments.size() == 1);
+    auto slot = slot_from_expr(arguments[0]);
+    DORIS_CHECK(slot.has_value());
+    const auto* zone_map_ref = ctx.zone_map(slot->index);
+    if (zone_map_ref == nullptr) {
+        return unsupported_zonemap_filter(ctx);
+    }
+    const auto& zone_map = *zone_map_ref;
+    if (is_null) {
+        return zone_map.has_null ? ZoneMapFilterResult::kMayMatch : 
ZoneMapFilterResult::kNoMatch;
+    }
+    return zone_map.has_not_null ? ZoneMapFilterResult::kMayMatch : 
ZoneMapFilterResult::kNoMatch;
+}
+
+bool can_eval_null_zonemap(const VExprSPtrs& arguments) {
+    if (arguments.size() != 1) {

Review Comment:
   这里为什么会出现arg != 1的情况?



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