/root/doris/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 "core/data_type/define_primitive_type.h" |
34 | | #include "exprs/expr_zonemap_filter.h" |
35 | | #include "exprs/function/in.h" |
36 | | #include "exprs/function/simple_function_factory.h" |
37 | | #include "exprs/vexpr_context.h" |
38 | | #include "exprs/vslot_ref.h" |
39 | | #include "runtime/runtime_state.h" |
40 | | |
41 | | namespace doris { |
42 | | class RowDescriptor; |
43 | | class RuntimeState; |
44 | | } // namespace doris |
45 | | |
46 | | namespace doris { |
47 | | #include "common/compile_check_begin.h" |
48 | | |
49 | | namespace { |
50 | | |
51 | 4 | size_t raw_in_value_size(PrimitiveType primitive_type) { |
52 | 4 | switch (primitive_type) { |
53 | 0 | #define RETURN_RAW_IN_SIZE(TYPE) \ |
54 | 0 | case TYPE: \ |
55 | 0 | return sizeof(typename PrimitiveTypeTraits<TYPE>::CppType) |
56 | 0 | RETURN_RAW_IN_SIZE(TYPE_BOOLEAN); |
57 | 0 | RETURN_RAW_IN_SIZE(TYPE_TINYINT); |
58 | 0 | RETURN_RAW_IN_SIZE(TYPE_SMALLINT); |
59 | 0 | RETURN_RAW_IN_SIZE(TYPE_INT); |
60 | 0 | RETURN_RAW_IN_SIZE(TYPE_BIGINT); |
61 | 0 | RETURN_RAW_IN_SIZE(TYPE_LARGEINT); |
62 | 0 | RETURN_RAW_IN_SIZE(TYPE_FLOAT); |
63 | 0 | RETURN_RAW_IN_SIZE(TYPE_DOUBLE); |
64 | 0 | RETURN_RAW_IN_SIZE(TYPE_DATE); |
65 | 0 | RETURN_RAW_IN_SIZE(TYPE_DATETIME); |
66 | 0 | RETURN_RAW_IN_SIZE(TYPE_DATEV2); |
67 | 0 | RETURN_RAW_IN_SIZE(TYPE_DATETIMEV2); |
68 | 0 | RETURN_RAW_IN_SIZE(TYPE_TIMESTAMPTZ); |
69 | 0 | RETURN_RAW_IN_SIZE(TYPE_TIMEV2); |
70 | 0 | RETURN_RAW_IN_SIZE(TYPE_DECIMAL32); |
71 | 0 | RETURN_RAW_IN_SIZE(TYPE_DECIMAL64); |
72 | 0 | RETURN_RAW_IN_SIZE(TYPE_DECIMALV2); |
73 | 0 | RETURN_RAW_IN_SIZE(TYPE_DECIMAL128I); |
74 | 0 | RETURN_RAW_IN_SIZE(TYPE_DECIMAL256); |
75 | 0 | RETURN_RAW_IN_SIZE(TYPE_IPV4); |
76 | 0 | RETURN_RAW_IN_SIZE(TYPE_IPV6); |
77 | 0 | #undef RETURN_RAW_IN_SIZE |
78 | 4 | default: |
79 | 4 | return 0; |
80 | 4 | } |
81 | 4 | } |
82 | | |
83 | | } // namespace |
84 | | |
85 | | VInPredicate::VInPredicate(const TExprNode& node) |
86 | 31 | : VExpr(node), _is_not_in(node.in_predicate.is_not_in) {} |
87 | | |
88 | | Status VInPredicate::prepare(RuntimeState* state, const RowDescriptor& desc, |
89 | 10 | VExprContext* context) { |
90 | 10 | RETURN_IF_ERROR_OR_PREPARED(VExpr::prepare(state, desc, context)); |
91 | | |
92 | 10 | if (_children.empty()) { |
93 | 0 | return Status::InternalError("no Function operator in."); |
94 | 0 | } |
95 | | |
96 | 10 | _expr_name = |
97 | 10 | fmt::format("({} {} set)", _children[0]->expr_name(), _is_not_in ? "not_in" : "in"); |
98 | | |
99 | 10 | ColumnsWithTypeAndName argument_template; |
100 | 10 | argument_template.reserve(get_num_children()); |
101 | 32 | for (auto child : _children) { |
102 | 32 | argument_template.emplace_back(nullptr, child->data_type(), child->expr_name()); |
103 | 32 | } |
104 | | |
105 | | // construct the proper function_name |
106 | 10 | std::string head(_is_not_in ? "not_" : ""); |
107 | 10 | std::string real_function_name = head + std::string(function_name); |
108 | 10 | auto arg_type = remove_nullable(argument_template[0].type); |
109 | 10 | if (is_complex_type(arg_type->get_primitive_type())) { |
110 | 0 | real_function_name = "collection_" + real_function_name; |
111 | 0 | } |
112 | 10 | _function = SimpleFunctionFactory::instance().get_function(real_function_name, |
113 | 10 | argument_template, _data_type, {}); |
114 | 10 | if (_function == nullptr) { |
115 | 0 | return Status::NotSupported("Function {} is not implemented", real_function_name); |
116 | 0 | } |
117 | | |
118 | 10 | VExpr::register_function_context(state, context); |
119 | 10 | _prepare_finished = true; |
120 | | |
121 | 10 | if (state->query_options().__isset.in_list_value_count_threshold) { |
122 | 10 | _in_list_value_count_threshold = state->query_options().in_list_value_count_threshold; |
123 | 10 | } |
124 | 10 | return Status::OK(); |
125 | 10 | } |
126 | | |
127 | | Status VInPredicate::open(RuntimeState* state, VExprContext* context, |
128 | 5 | FunctionContext::FunctionStateScope scope) { |
129 | 5 | DCHECK(_prepare_finished); |
130 | 15 | for (auto& child : _children) { |
131 | 15 | RETURN_IF_ERROR(child->open(state, context, scope)); |
132 | 15 | } |
133 | 5 | RETURN_IF_ERROR(VExpr::init_function_context(state, context, scope, _function)); |
134 | 5 | if (scope == FunctionContext::FRAGMENT_LOCAL) { |
135 | 5 | RETURN_IF_ERROR(VExpr::get_const_col(context, nullptr)); |
136 | 5 | } |
137 | | |
138 | 5 | _is_args_all_constant = std::all_of(_children.begin() + 1, _children.end(), |
139 | 10 | [](const VExprSPtr& expr) { return expr->is_constant(); }); |
140 | 5 | if (scope == FunctionContext::FRAGMENT_LOCAL && _is_args_all_constant && |
141 | 5 | !_zonemap_materialized) { |
142 | 5 | RETURN_IF_ERROR(_materialize_for_zonemap_filter(context)); |
143 | 5 | } |
144 | 5 | _open_finished = true; |
145 | 5 | return Status::OK(); |
146 | 5 | } |
147 | | |
148 | 17 | void VInPredicate::close(VExprContext* context, FunctionContext::FunctionStateScope scope) { |
149 | 17 | VExpr::close_function_context(context, scope, _function); |
150 | 17 | VExpr::close(context, scope); |
151 | 17 | } |
152 | | |
153 | 1 | Status VInPredicate::evaluate_inverted_index(VExprContext* context, uint32_t segment_num_rows) { |
154 | 1 | DCHECK_GE(get_num_children(), 2); |
155 | 1 | return _evaluate_inverted_index(context, _function, segment_num_rows); |
156 | 1 | } |
157 | | |
158 | 5 | Status VInPredicate::_materialize_for_zonemap_filter(VExprContext* context) { |
159 | 5 | _seg_filter_values.clear(); |
160 | 5 | _seg_filter_contains_null = false; |
161 | 5 | _zonemap_materialized = false; |
162 | 5 | _direct_filter_set.reset(); |
163 | 5 | if (_children.size() < 2 || !_children[0]->is_slot_ref()) { |
164 | 0 | return Status::OK(); |
165 | 0 | } |
166 | | |
167 | 5 | const auto data_type = remove_nullable(_children[0]->data_type()); |
168 | 5 | DORIS_CHECK(data_type != nullptr); |
169 | 5 | if (is_complex_type(data_type->get_primitive_type())) { |
170 | 0 | return Status::OK(); |
171 | 0 | } |
172 | | |
173 | 5 | DORIS_CHECK(context != nullptr); |
174 | 5 | auto* fn_ctx = context->fn_context(_fn_context_index); |
175 | 5 | DORIS_CHECK(fn_ctx != nullptr); |
176 | 5 | auto* in_state = |
177 | 5 | reinterpret_cast<InState*>(fn_ctx->get_function_state(FunctionContext::FRAGMENT_LOCAL)); |
178 | 5 | DORIS_CHECK(in_state != nullptr); |
179 | 5 | DORIS_CHECK(in_state->use_set); |
180 | 5 | DORIS_CHECK(in_state->hybrid_set != nullptr); |
181 | 5 | _direct_filter_set = in_state->hybrid_set; |
182 | | |
183 | 5 | expr_zonemap::InZonemapMaterializedSet materialized; |
184 | 5 | RETURN_IF_ERROR(expr_zonemap::materialize_hybrid_set_for_zonemap_filter( |
185 | 5 | *in_state->hybrid_set, data_type, &materialized)); |
186 | 5 | _seg_filter_contains_null = materialized.contains_null; |
187 | 5 | _seg_filter_values = std::move(materialized.values); |
188 | 5 | _seg_filter_min = std::move(materialized.min_value); |
189 | 5 | _seg_filter_max = std::move(materialized.max_value); |
190 | 5 | _zonemap_materialized = true; |
191 | 5 | return Status::OK(); |
192 | 5 | } |
193 | | |
194 | 4 | ZoneMapFilterResult VInPredicate::evaluate_zonemap_filter(const ZoneMapEvalContext& ctx) const { |
195 | 4 | if (_is_not_in && _seg_filter_contains_null) { |
196 | 1 | return ZoneMapFilterResult::kNoMatch; |
197 | 1 | } |
198 | 3 | return expr_zonemap::eval_in_zonemap(ctx, get_child(0), _is_not_in, _seg_filter_values, |
199 | 3 | _seg_filter_min, _seg_filter_max); |
200 | 4 | } |
201 | | |
202 | 10 | bool VInPredicate::can_evaluate_zonemap_filter() const { |
203 | 10 | return _zonemap_materialized && std::dynamic_pointer_cast<VSlotRef>(get_child(0)) != nullptr; |
204 | 10 | } |
205 | | |
206 | | ZoneMapFilterResult VInPredicate::evaluate_dictionary_filter( |
207 | 2 | const DictionaryEvalContext& ctx) const { |
208 | 2 | return expr_zonemap::eval_in_dictionary(ctx, get_child(0), _is_not_in, _seg_filter_values); |
209 | 2 | } |
210 | | |
211 | 5 | bool VInPredicate::can_evaluate_dictionary_filter() const { |
212 | 5 | return _zonemap_materialized && !_is_not_in && |
213 | 5 | std::dynamic_pointer_cast<VSlotRef>(get_child(0)) != nullptr; |
214 | 5 | } |
215 | | |
216 | 2 | ZoneMapFilterResult VInPredicate::evaluate_bloom_filter(const BloomFilterEvalContext& ctx) const { |
217 | 2 | return expr_zonemap::eval_in_bloom_filter(ctx, get_child(0), _is_not_in, _seg_filter_values); |
218 | 2 | } |
219 | | |
220 | 3 | bool VInPredicate::can_evaluate_bloom_filter() const { |
221 | 3 | return _zonemap_materialized && !_is_not_in && |
222 | 3 | std::dynamic_pointer_cast<VSlotRef>(get_child(0)) != nullptr; |
223 | 3 | } |
224 | | |
225 | | bool VInPredicate::can_execute_on_raw_fixed_values(const DataTypePtr& data_type, |
226 | 4 | int column_id) const { |
227 | 4 | if (!_zonemap_materialized || _direct_filter_set == nullptr || data_type == nullptr || |
228 | 4 | !_is_args_all_constant) { |
229 | 0 | return false; |
230 | 0 | } |
231 | 4 | const auto slot = std::dynamic_pointer_cast<VSlotRef>(get_child(0)); |
232 | 4 | if (slot == nullptr || slot->column_id() != column_id || slot->data_type() == nullptr) { |
233 | 0 | return false; |
234 | 0 | } |
235 | 4 | const auto raw_type = remove_nullable(data_type); |
236 | 4 | return remove_nullable(slot->data_type())->equals(*raw_type) && |
237 | 4 | raw_in_value_size(raw_type->get_primitive_type()) != 0; |
238 | 4 | } |
239 | | |
240 | | Status VInPredicate::execute_on_raw_fixed_values(const uint8_t* values, size_t num_values, |
241 | | size_t value_width, const DataTypePtr& data_type, |
242 | 0 | int column_id, uint8_t* matches) const { |
243 | 0 | if (!can_execute_on_raw_fixed_values(data_type, column_id)) { |
244 | 0 | return Status::NotSupported("IN predicate cannot evaluate raw fixed-width values"); |
245 | 0 | } |
246 | 0 | DORIS_CHECK(values != nullptr || num_values == 0); |
247 | 0 | DORIS_CHECK(matches != nullptr || num_values == 0); |
248 | 0 | const size_t expected_width = |
249 | 0 | raw_in_value_size(remove_nullable(data_type)->get_primitive_type()); |
250 | 0 | if (value_width != expected_width) { |
251 | 0 | return Status::Corruption("Raw IN width {} does not match expected {}", value_width, |
252 | 0 | expected_width); |
253 | 0 | } |
254 | | // NOT IN with a NULL literal is UNKNOWN for every non-null physical value. Definition levels |
255 | | // handle input NULLs, while this guard preserves the remaining three-valued SQL invariant. |
256 | 0 | if (_is_not_in && _seg_filter_contains_null) { |
257 | 0 | std::fill(matches, matches + num_values, uint8_t {0}); |
258 | 0 | } else if (_is_not_in) { |
259 | 0 | _direct_filter_set->find_batch_raw_fixed_negative(values, num_values, value_width, matches); |
260 | 0 | } else { |
261 | 0 | _direct_filter_set->find_batch_raw_fixed(values, num_values, value_width, matches); |
262 | 0 | } |
263 | 0 | return Status::OK(); |
264 | 0 | } |
265 | | |
266 | | bool VInPredicate::can_execute_on_raw_binary_values(const DataTypePtr& data_type, |
267 | 10 | int column_id) const { |
268 | 10 | if (!_zonemap_materialized || _direct_filter_set == nullptr || data_type == nullptr || |
269 | 10 | !_is_args_all_constant || |
270 | 10 | !is_string_type(remove_nullable(data_type)->get_primitive_type())) { |
271 | 0 | return false; |
272 | 0 | } |
273 | 10 | const auto slot = std::dynamic_pointer_cast<VSlotRef>(get_child(0)); |
274 | 10 | return slot != nullptr && slot->column_id() == column_id && slot->data_type() != nullptr && |
275 | 10 | is_string_type(remove_nullable(slot->data_type())->get_primitive_type()); |
276 | 10 | } |
277 | | |
278 | | Status VInPredicate::execute_on_raw_binary_values(const StringRef* values, size_t num_values, |
279 | | const DataTypePtr& data_type, int column_id, |
280 | 2 | uint8_t* matches) const { |
281 | 2 | if (!can_execute_on_raw_binary_values(data_type, column_id)) { |
282 | 0 | return Status::NotSupported("IN predicate cannot evaluate raw binary values"); |
283 | 0 | } |
284 | 2 | DORIS_CHECK(values != nullptr || num_values == 0); |
285 | 2 | DORIS_CHECK(matches != nullptr || num_values == 0); |
286 | 2 | if (_is_not_in && _seg_filter_contains_null) { |
287 | 0 | std::fill(matches, matches + num_values, uint8_t {0}); |
288 | 2 | } else if (_is_not_in) { |
289 | 1 | _direct_filter_set->find_batch_raw_binary_negative(values, num_values, matches); |
290 | 1 | } else { |
291 | | // Decoder-owned slices remain valid for the whole batch, so regular SQL IN/NOT IN can |
292 | | // probe the prepared hash set without constructing and then compacting a ColumnString. |
293 | 1 | _direct_filter_set->find_batch_raw_binary(values, num_values, matches); |
294 | 1 | } |
295 | 2 | return Status::OK(); |
296 | 2 | } |
297 | | |
298 | | Status VInPredicate::execute_column(VExprContext* context, const Block* block, Selector* selector, |
299 | 0 | size_t count, ColumnPtr& result_column) const { |
300 | 0 | if (is_const_and_have_executed()) { // const have execute in open function |
301 | 0 | result_column = get_result_from_const(count); |
302 | 0 | return Status::OK(); |
303 | 0 | } |
304 | 0 | if (fast_execute(context, selector, count, result_column)) { |
305 | 0 | return Status::OK(); |
306 | 0 | } |
307 | 0 | DCHECK(_open_finished || block == nullptr); |
308 | | |
309 | | // This is an optimization. For expressions like colA IN (1, 2, 3, 4), |
310 | | // where all values inside the IN clause are constants, |
311 | | // a hash set is created during open, and it will not be accessed again during execute |
312 | | // Here, _children[0] is colA |
313 | 0 | const size_t args_size = _is_args_all_constant ? 1 : _children.size(); |
314 | |
|
315 | 0 | ColumnNumbers arguments; |
316 | 0 | arguments.reserve(args_size); |
317 | 0 | Block temp_block; |
318 | 0 | for (int i = 0; i < args_size; ++i) { |
319 | 0 | ColumnPtr column; |
320 | 0 | RETURN_IF_ERROR(_children[i]->execute_column(context, block, selector, count, column)); |
321 | 0 | arguments.push_back(i); |
322 | 0 | temp_block.insert({column, _children[i]->execute_type(block), _children[i]->expr_name()}); |
323 | 0 | } |
324 | | |
325 | 0 | int num_columns_without_result = temp_block.columns(); |
326 | 0 | temp_block.insert({nullptr, _data_type, _expr_name}); |
327 | |
|
328 | 0 | RETURN_IF_ERROR(_function->execute(context->fn_context(_fn_context_index), temp_block, |
329 | 0 | arguments, num_columns_without_result, temp_block.rows())); |
330 | 0 | result_column = temp_block.get_by_position(num_columns_without_result).column; |
331 | 0 | DCHECK_EQ(result_column->size(), count); |
332 | 0 | return Status::OK(); |
333 | 0 | } |
334 | | |
335 | 0 | size_t VInPredicate::estimate_memory(const size_t rows) { |
336 | 0 | if (is_const_and_have_executed()) { |
337 | 0 | return 0; |
338 | 0 | } |
339 | | |
340 | 0 | size_t estimate_size = 0; |
341 | |
|
342 | 0 | for (int i = 0; i < _children.size(); ++i) { |
343 | 0 | estimate_size += _children[i]->estimate_memory(rows); |
344 | 0 | } |
345 | |
|
346 | 0 | if (_data_type->is_nullable()) { |
347 | 0 | estimate_size += rows * sizeof(uint8_t); |
348 | 0 | } |
349 | |
|
350 | 0 | estimate_size += rows * sizeof(uint8_t); |
351 | |
|
352 | 0 | return estimate_size; |
353 | 0 | } |
354 | | |
355 | 6 | const std::string& VInPredicate::expr_name() const { |
356 | 6 | return _expr_name; |
357 | 6 | } |
358 | | |
359 | 5 | std::string VInPredicate::debug_string() const { |
360 | 5 | std::stringstream out; |
361 | 5 | out << "InPredicate(" << children()[0]->debug_string() << " " << _is_not_in << ",["; |
362 | 5 | int num_children = get_num_children(); |
363 | | |
364 | 17 | for (int i = 1; i < num_children; ++i) { |
365 | 12 | out << (i == 1 ? "" : " ") << children()[i]->debug_string(); |
366 | 12 | } |
367 | | |
368 | 5 | out << "])"; |
369 | 5 | return out.str(); |
370 | 5 | } |
371 | | |
372 | | #include "common/compile_check_end.h" |
373 | | } // namespace doris |