yiguolei commented on code in PR #63389: URL: https://github.com/apache/doris/pull/63389#discussion_r3451556377
########## be/src/exprs/expr_zonemap_filter.cpp: ########## @@ -0,0 +1,269 @@ +// 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 "core/string_ref.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/runtime_state.h" + +namespace doris::expr_zonemap { +namespace { + +constexpr size_t kInZoneMapPointCheckThreshold = 64; + +std::optional<Field> field_from_literal_expr(const VExprSPtr& expr, DataTypePtr* literal_type) { + auto literal = std::dynamic_pointer_cast<VLiteral>(expr); + 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 slot = std::dynamic_pointer_cast<VSlotRef>(expr); + 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 + +TExprNode create_texpr_node_from_hybrid_set_value(const void* data, const PrimitiveType& type, + int precision, int scale) { + if (is_string_type(type)) { + const auto* value = reinterpret_cast<const StringRef*>(data); + auto field = Field::create_field<TYPE_STRING>(String(value->data, value->size)); + return create_texpr_node_from(field, type, precision, scale); + } + return create_texpr_node_from(data, type, precision, scale); +} + +Status materialize_hybrid_set_for_zonemap_filter(HybridSetBase& set, const DataTypePtr& data_type, + InZonemapMaterializedSet* result) { + DORIS_CHECK(result != nullptr); + DORIS_CHECK(data_type != nullptr); + const auto value_type = remove_nullable(data_type); + DORIS_CHECK(value_type != nullptr); + + result->contains_null = set.contain_null(); + result->values.clear(); + result->min_value = Field(); + result->max_value = Field(); + + auto* iterator = set.begin(); + while (iterator->has_next()) { + const void* value = iterator->get_value(); + if (value != nullptr) { + TExprNode literal_node = create_texpr_node_from_hybrid_set_value( + value, value_type->get_primitive_type(), value_type->get_precision(), + value_type->get_scale()); + auto literal = VLiteral::create_shared(literal_node); + Field field; + literal->get_column_ptr()->get(0, field); + result->values.emplace_back(std::move(field)); + } + iterator->next(); + } + + if (!result->values.empty()) { + auto minmax = std::ranges::minmax_element(result->values, expr_zonemap::field_less); + result->min_value = *minmax.min; + result->max_value = *minmax.max; + } + return Status::OK(); +} + +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, Review Comment: 原来如果是column id 就继续column id 用下去,别再换一个名字叫index了,各种概念来回变换挺复杂的 -- 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]
