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