be/src/exprs/vin_predicate.cpp
Line | Count | Source |
1 | | // Licensed to the Apache Software Foundation (ASF) under one |
2 | | // or more contributor license agreements. See the NOTICE file |
3 | | // distributed with this work for additional information |
4 | | // regarding copyright ownership. The ASF licenses this file |
5 | | // to you under the Apache License, Version 2.0 (the |
6 | | // "License"); you may not use this file except in compliance |
7 | | // with the License. You may obtain a copy of the License at |
8 | | // |
9 | | // http://www.apache.org/licenses/LICENSE-2.0 |
10 | | // |
11 | | // Unless required by applicable law or agreed to in writing, |
12 | | // software distributed under the License is distributed on an |
13 | | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | | // KIND, either express or implied. See the License for the |
15 | | // specific language governing permissions and limitations |
16 | | // under the License. |
17 | | |
18 | | #include "exprs/vin_predicate.h" |
19 | | |
20 | | #include <fmt/format.h> |
21 | | #include <gen_cpp/Exprs_types.h> |
22 | | #include <glog/logging.h> |
23 | | |
24 | | #include <algorithm> |
25 | | #include <cstddef> |
26 | | #include <ostream> |
27 | | |
28 | | #include "common/status.h" |
29 | | #include "core/block/block.h" |
30 | | #include "core/block/column_numbers.h" |
31 | | #include "core/block/column_with_type_and_name.h" |
32 | | #include "core/block/columns_with_type_and_name.h" |
33 | | #include "exprs/expr_zonemap_filter.h" |
34 | | #include "exprs/function/in.h" |
35 | | #include "exprs/function/simple_function_factory.h" |
36 | | #include "exprs/vexpr_context.h" |
37 | | #include "exprs/vslot_ref.h" |
38 | | #include "runtime/runtime_state.h" |
39 | | |
40 | | namespace doris { |
41 | | class RowDescriptor; |
42 | | class RuntimeState; |
43 | | } // namespace doris |
44 | | |
45 | | namespace doris { |
46 | | |
47 | | VInPredicate::VInPredicate(const TExprNode& node) |
48 | 20 | : VExpr(node), _is_not_in(node.in_predicate.is_not_in) {} |
49 | | |
50 | | Status VInPredicate::prepare(RuntimeState* state, const RowDescriptor& desc, |
51 | 15 | VExprContext* context) { |
52 | 15 | RETURN_IF_ERROR_OR_PREPARED(VExpr::prepare(state, desc, context)); |
53 | | |
54 | 15 | if (_children.empty()) { |
55 | 0 | return Status::InternalError("no Function operator in."); |
56 | 0 | } |
57 | | |
58 | 15 | _expr_name = |
59 | 15 | fmt::format("({} {} set)", _children[0]->expr_name(), _is_not_in ? "not_in" : "in"); |
60 | | |
61 | 15 | ColumnsWithTypeAndName argument_template; |
62 | 15 | argument_template.reserve(get_num_children()); |
63 | 109 | for (auto child : _children) { |
64 | 109 | argument_template.emplace_back(nullptr, child->data_type(), child->expr_name()); |
65 | 109 | } |
66 | | |
67 | | // construct the proper function_name |
68 | 15 | std::string head(_is_not_in ? "not_" : ""); |
69 | 15 | std::string real_function_name = head + std::string(function_name); |
70 | 15 | auto arg_type = remove_nullable(argument_template[0].type); |
71 | 15 | if (is_complex_type(arg_type->get_primitive_type())) { |
72 | 0 | real_function_name = "collection_" + real_function_name; |
73 | 0 | } |
74 | 15 | _function = SimpleFunctionFactory::instance().get_function(real_function_name, |
75 | 15 | argument_template, _data_type, {}); |
76 | 15 | if (_function == nullptr) { |
77 | 0 | return Status::NotSupported("Function {} is not implemented", real_function_name); |
78 | 0 | } |
79 | | |
80 | 15 | VExpr::register_function_context(state, context); |
81 | 15 | _prepare_finished = true; |
82 | | |
83 | 15 | if (state->query_options().__isset.in_list_value_count_threshold) { |
84 | 15 | _in_list_value_count_threshold = state->query_options().in_list_value_count_threshold; |
85 | 15 | } |
86 | 15 | return Status::OK(); |
87 | 15 | } |
88 | | |
89 | | Status VInPredicate::open(RuntimeState* state, VExprContext* context, |
90 | 18 | FunctionContext::FunctionStateScope scope) { |
91 | 18 | DCHECK(_prepare_finished); |
92 | 178 | for (auto& child : _children) { |
93 | 178 | RETURN_IF_ERROR(child->open(state, context, scope)); |
94 | 178 | } |
95 | 18 | RETURN_IF_ERROR(VExpr::init_function_context(state, context, scope, _function)); |
96 | 18 | if (scope == FunctionContext::FRAGMENT_LOCAL) { |
97 | 10 | RETURN_IF_ERROR(VExpr::get_const_col(context, nullptr)); |
98 | 10 | } |
99 | | |
100 | 18 | _is_args_all_constant = std::all_of(_children.begin() + 1, _children.end(), |
101 | 160 | [](const VExprSPtr& expr) { return expr->is_constant(); }); |
102 | 18 | if (scope == FunctionContext::FRAGMENT_LOCAL && _is_args_all_constant) { |
103 | 10 | RETURN_IF_ERROR(_materialize_for_zonemap_filter(context)); |
104 | 10 | } |
105 | 18 | _open_finished = true; |
106 | 18 | return Status::OK(); |
107 | 18 | } |
108 | | |
109 | 28 | void VInPredicate::close(VExprContext* context, FunctionContext::FunctionStateScope scope) { |
110 | 28 | VExpr::close_function_context(context, scope, _function); |
111 | 28 | VExpr::close(context, scope); |
112 | 28 | } |
113 | | |
114 | 1 | Status VInPredicate::evaluate_inverted_index(VExprContext* context, uint32_t segment_num_rows) { |
115 | 1 | DCHECK_GE(get_num_children(), 2); |
116 | 1 | return _evaluate_inverted_index(context, _function, segment_num_rows); |
117 | 1 | } |
118 | | |
119 | 10 | Status VInPredicate::_materialize_for_zonemap_filter(VExprContext* context) { |
120 | 10 | _seg_filter_values.clear(); |
121 | 10 | _seg_filter_contains_null = false; |
122 | 10 | _zonemap_materialized = false; |
123 | 10 | if (_children.size() < 2 || !_children[0]->is_slot_ref()) { |
124 | 0 | return Status::OK(); |
125 | 0 | } |
126 | | |
127 | 10 | const auto data_type = remove_nullable(_children[0]->data_type()); |
128 | 10 | DORIS_CHECK(data_type != nullptr); |
129 | 10 | if (is_complex_type(data_type->get_primitive_type())) { |
130 | 0 | return Status::OK(); |
131 | 0 | } |
132 | | |
133 | 10 | DORIS_CHECK(context != nullptr); |
134 | 10 | auto* fn_ctx = context->fn_context(_fn_context_index); |
135 | 10 | DORIS_CHECK(fn_ctx != nullptr); |
136 | 10 | auto* in_state = |
137 | 10 | reinterpret_cast<InState*>(fn_ctx->get_function_state(FunctionContext::FRAGMENT_LOCAL)); |
138 | 10 | DORIS_CHECK(in_state != nullptr); |
139 | 10 | DORIS_CHECK(in_state->use_set); |
140 | 10 | DORIS_CHECK(in_state->hybrid_set != nullptr); |
141 | | |
142 | 10 | expr_zonemap::InZonemapMaterializedSet materialized; |
143 | 10 | RETURN_IF_ERROR(expr_zonemap::materialize_hybrid_set_for_zonemap_filter( |
144 | 10 | *in_state->hybrid_set, data_type, &materialized)); |
145 | 10 | _seg_filter_contains_null = materialized.contains_null; |
146 | 10 | _seg_filter_values = std::move(materialized.values); |
147 | 10 | _seg_filter_min = std::move(materialized.min_value); |
148 | 10 | _seg_filter_max = std::move(materialized.max_value); |
149 | 10 | _zonemap_materialized = true; |
150 | 10 | return Status::OK(); |
151 | 10 | } |
152 | | |
153 | 2 | ZoneMapFilterResult VInPredicate::evaluate_zonemap_filter(const ZoneMapEvalContext& ctx) const { |
154 | 2 | if (_is_not_in && _seg_filter_contains_null) { |
155 | 1 | return ZoneMapFilterResult::kNoMatch; |
156 | 1 | } |
157 | 1 | return expr_zonemap::eval_in_zonemap(ctx, get_child(0), _is_not_in, _seg_filter_values, |
158 | 1 | _seg_filter_min, _seg_filter_max); |
159 | 2 | } |
160 | | |
161 | 0 | bool VInPredicate::can_evaluate_zonemap_filter() const { |
162 | 0 | return _zonemap_materialized && std::dynamic_pointer_cast<VSlotRef>(get_child(0)) != nullptr; |
163 | 0 | } |
164 | | |
165 | | Status VInPredicate::execute_column_impl(VExprContext* context, const Block* block, |
166 | | const Selector* selector, size_t count, |
167 | 0 | ColumnPtr& result_column) const { |
168 | 0 | if (is_const_and_have_executed()) { // const have execute in open function |
169 | 0 | result_column = get_result_from_const(count); |
170 | 0 | return Status::OK(); |
171 | 0 | } |
172 | 0 | if (fast_execute(context, selector, count, result_column)) { |
173 | 0 | return Status::OK(); |
174 | 0 | } |
175 | 0 | DCHECK(_open_finished || block == nullptr); |
176 | | |
177 | | // This is an optimization. For expressions like colA IN (1, 2, 3, 4), |
178 | | // where all values inside the IN clause are constants, |
179 | | // a hash set is created during open, and it will not be accessed again during execute |
180 | | // Here, _children[0] is colA |
181 | 0 | const size_t args_size = _is_args_all_constant ? 1 : _children.size(); |
182 | |
|
183 | 0 | ColumnNumbers arguments; |
184 | 0 | arguments.reserve(args_size); |
185 | 0 | Block temp_block; |
186 | 0 | for (int i = 0; i < args_size; ++i) { |
187 | 0 | ColumnPtr column; |
188 | 0 | RETURN_IF_ERROR(_children[i]->execute_column(context, block, selector, count, column)); |
189 | 0 | arguments.push_back(i); |
190 | 0 | temp_block.insert({column, _children[i]->execute_type(block), _children[i]->expr_name()}); |
191 | 0 | } |
192 | | |
193 | 0 | int num_columns_without_result = temp_block.columns(); |
194 | 0 | temp_block.insert({nullptr, _data_type, _expr_name}); |
195 | |
|
196 | 0 | RETURN_IF_ERROR(_function->execute(context->fn_context(_fn_context_index), temp_block, |
197 | 0 | arguments, num_columns_without_result, temp_block.rows())); |
198 | 0 | result_column = temp_block.get_by_position(num_columns_without_result).column; |
199 | 0 | DCHECK_EQ(result_column->size(), count); |
200 | 0 | return Status::OK(); |
201 | 0 | } |
202 | | |
203 | 0 | size_t VInPredicate::estimate_memory(const size_t rows) { |
204 | 0 | if (is_const_and_have_executed()) { |
205 | 0 | return 0; |
206 | 0 | } |
207 | | |
208 | 0 | size_t estimate_size = 0; |
209 | |
|
210 | 0 | for (int i = 0; i < _children.size(); ++i) { |
211 | 0 | estimate_size += _children[i]->estimate_memory(rows); |
212 | 0 | } |
213 | |
|
214 | 0 | if (_data_type->is_nullable()) { |
215 | 0 | estimate_size += rows * sizeof(uint8_t); |
216 | 0 | } |
217 | |
|
218 | 0 | estimate_size += rows * sizeof(uint8_t); |
219 | |
|
220 | 0 | return estimate_size; |
221 | 0 | } |
222 | | |
223 | 6 | const std::string& VInPredicate::expr_name() const { |
224 | 6 | return _expr_name; |
225 | 6 | } |
226 | | |
227 | 5 | std::string VInPredicate::debug_string() const { |
228 | 5 | std::stringstream out; |
229 | 5 | out << "InPredicate(" << children()[0]->debug_string() << " " << _is_not_in << ",["; |
230 | 5 | int num_children = get_num_children(); |
231 | | |
232 | 17 | for (int i = 1; i < num_children; ++i) { |
233 | 12 | out << (i == 1 ? "" : " ") << children()[i]->debug_string(); |
234 | 12 | } |
235 | | |
236 | 5 | out << "])"; |
237 | 5 | return out.str(); |
238 | 5 | } |
239 | | |
240 | | } // namespace doris |