/root/doris/be/src/exprs/vexpr.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/vexpr.h" |
19 | | |
20 | | #include <fmt/format.h> |
21 | | #include <gen_cpp/Exprs_types.h> |
22 | | #include <gen_cpp/FrontendService_types.h> |
23 | | #include <thrift/protocol/TDebugProtocol.h> |
24 | | |
25 | | #include <algorithm> |
26 | | #include <boost/algorithm/string/split.hpp> |
27 | | #include <boost/iterator/iterator_facade.hpp> |
28 | | #include <cstdint> |
29 | | #include <memory> |
30 | | #include <stack> |
31 | | #include <string_view> |
32 | | #include <utility> |
33 | | |
34 | | #include "common/config.h" |
35 | | #include "common/exception.h" |
36 | | #include "common/status.h" |
37 | | #include "core/column/column_vector.h" |
38 | | #include "core/data_type/data_type_array.h" |
39 | | #include "core/data_type/data_type_decimal.h" |
40 | | #include "core/data_type/data_type_factory.hpp" |
41 | | #include "core/data_type/data_type_nullable.h" |
42 | | #include "core/data_type/data_type_number.h" |
43 | | #include "core/data_type/define_primitive_type.h" |
44 | | #include "core/field.h" |
45 | | #include "core/string_ref.h" |
46 | | #include "core/value/timestamptz_value.h" |
47 | | #include "exec/common/util.hpp" |
48 | | #include "exec/pipeline/pipeline_task.h" |
49 | | #include "exprs/short_circuit_evaluation_expr.h" |
50 | | #include "exprs/varray_literal.h" |
51 | | #include "exprs/vcase_expr.h" |
52 | | #include "exprs/vcast_expr.h" |
53 | | #include "exprs/vcolumn_ref.h" |
54 | | #include "exprs/vcompound_pred.h" |
55 | | #include "exprs/vcondition_expr.h" |
56 | | #include "exprs/vectorized_fn_call.h" |
57 | | #include "exprs/vexpr_context.h" |
58 | | #include "exprs/vexpr_fwd.h" |
59 | | #include "exprs/vin_predicate.h" |
60 | | #include "exprs/vinfo_func.h" |
61 | | #include "exprs/virtual_slot_ref.h" |
62 | | #include "exprs/vlambda_function_call_expr.h" |
63 | | #include "exprs/vlambda_function_expr.h" |
64 | | #include "exprs/vliteral.h" |
65 | | #include "exprs/vmap_literal.h" |
66 | | #include "exprs/vmatch_predicate.h" |
67 | | #include "exprs/vsearch.h" |
68 | | #include "exprs/vslot_ref.h" |
69 | | #include "exprs/vstruct_literal.h" |
70 | | #include "storage/index/ann/ann_search_params.h" |
71 | | #include "storage/index/ann/ann_topn_runtime.h" |
72 | | #include "storage/index/inverted/inverted_index_parser.h" |
73 | | #include "storage/index/zone_map/zonemap_eval_context.h" |
74 | | #include "storage/segment/column_reader.h" |
75 | | |
76 | | namespace doris { |
77 | | #include "common/compile_check_begin.h" |
78 | | |
79 | | class RowDescriptor; |
80 | | class RuntimeState; |
81 | | |
82 | 0 | ZoneMapFilterResult VExpr::evaluate_zonemap_filter(const ZoneMapEvalContext& ctx) const { |
83 | 0 | return unsupported_zonemap_filter(ctx); |
84 | 0 | } |
85 | | |
86 | 0 | ZoneMapFilterResult VExpr::evaluate_dictionary_filter(const DictionaryEvalContext& ctx) const { |
87 | 0 | return ZoneMapFilterResult::kUnsupported; |
88 | 0 | } |
89 | | |
90 | 0 | ZoneMapFilterResult VExpr::evaluate_bloom_filter(const BloomFilterEvalContext& ctx) const { |
91 | 0 | return ZoneMapFilterResult::kUnsupported; |
92 | 0 | } |
93 | | |
94 | | // NOLINTBEGIN(readability-function-cognitive-complexity) |
95 | | // NOLINTBEGIN(readability-function-size) |
96 | | TExprNode create_texpr_node_from(const void* data, const PrimitiveType& type, int precision, |
97 | 456k | int scale) { |
98 | 456k | TExprNode node; |
99 | | |
100 | 456k | switch (type) { |
101 | 15 | case TYPE_BOOLEAN: { |
102 | 15 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_BOOLEAN>(data, &node)); |
103 | 15 | break; |
104 | 15 | } |
105 | 2.10k | case TYPE_TINYINT: { |
106 | 2.10k | THROW_IF_ERROR(create_texpr_literal_node<TYPE_TINYINT>(data, &node)); |
107 | 2.10k | break; |
108 | 2.10k | } |
109 | 2.10k | case TYPE_SMALLINT: { |
110 | 711 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_SMALLINT>(data, &node)); |
111 | 711 | break; |
112 | 711 | } |
113 | 393k | case TYPE_INT: { |
114 | 393k | THROW_IF_ERROR(create_texpr_literal_node<TYPE_INT>(data, &node)); |
115 | 393k | break; |
116 | 393k | } |
117 | 393k | case TYPE_BIGINT: { |
118 | 58.2k | THROW_IF_ERROR(create_texpr_literal_node<TYPE_BIGINT>(data, &node)); |
119 | 58.2k | break; |
120 | 58.2k | } |
121 | 58.2k | case TYPE_LARGEINT: { |
122 | 88 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_LARGEINT>(data, &node)); |
123 | 88 | break; |
124 | 88 | } |
125 | 88 | case TYPE_FLOAT: { |
126 | 8 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_FLOAT>(data, &node)); |
127 | 8 | break; |
128 | 8 | } |
129 | 60 | case TYPE_DOUBLE: { |
130 | 60 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_DOUBLE>(data, &node)); |
131 | 60 | break; |
132 | 60 | } |
133 | 632 | case TYPE_DATEV2: { |
134 | 632 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_DATEV2>(data, &node)); |
135 | 632 | break; |
136 | 632 | } |
137 | 632 | case TYPE_DATETIMEV2: { |
138 | 269 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_DATETIMEV2>(data, &node, precision, scale)); |
139 | 269 | break; |
140 | 269 | } |
141 | 269 | case TYPE_DATE: { |
142 | 44 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_DATE>(data, &node)); |
143 | 44 | break; |
144 | 44 | } |
145 | 75 | case TYPE_DATETIME: { |
146 | 75 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_DATETIME>(data, &node)); |
147 | 75 | break; |
148 | 75 | } |
149 | 75 | case TYPE_DECIMALV2: { |
150 | 8 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_DECIMALV2>(data, &node, precision, scale)); |
151 | 8 | break; |
152 | 8 | } |
153 | 171 | case TYPE_DECIMAL32: { |
154 | 171 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_DECIMAL32>(data, &node, precision, scale)); |
155 | 171 | break; |
156 | 171 | } |
157 | 392 | case TYPE_DECIMAL64: { |
158 | 392 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_DECIMAL64>(data, &node, precision, scale)); |
159 | 392 | break; |
160 | 392 | } |
161 | 392 | case TYPE_DECIMAL128I: { |
162 | 94 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_DECIMAL128I>(data, &node, precision, scale)); |
163 | 94 | break; |
164 | 94 | } |
165 | 315 | case TYPE_DECIMAL256: { |
166 | 315 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_DECIMAL256>(data, &node, precision, scale)); |
167 | 315 | break; |
168 | 315 | } |
169 | 315 | case TYPE_CHAR: { |
170 | 49 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_CHAR>(data, &node)); |
171 | 49 | break; |
172 | 49 | } |
173 | 459 | case TYPE_VARCHAR: { |
174 | 459 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_VARCHAR>(data, &node)); |
175 | 459 | break; |
176 | 459 | } |
177 | 459 | case TYPE_STRING: { |
178 | 29 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_STRING>(data, &node)); |
179 | 29 | break; |
180 | 29 | } |
181 | 403 | case TYPE_IPV4: { |
182 | 403 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_IPV4>(data, &node)); |
183 | 403 | break; |
184 | 403 | } |
185 | 403 | case TYPE_IPV6: { |
186 | 44 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_IPV6>(data, &node)); |
187 | 44 | break; |
188 | 44 | } |
189 | 44 | case TYPE_TIMEV2: { |
190 | 0 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_TIMEV2>(data, &node, precision, scale)); |
191 | 0 | break; |
192 | 0 | } |
193 | 74 | case TYPE_TIMESTAMPTZ: { |
194 | 74 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_TIMESTAMPTZ>(data, &node, precision, scale)); |
195 | 74 | break; |
196 | 74 | } |
197 | 74 | default: |
198 | 0 | throw Exception(ErrorCode::INTERNAL_ERROR, "runtime filter meet invalid type {}", |
199 | 0 | int(type)); |
200 | 456k | } |
201 | 455k | return node; |
202 | 456k | } |
203 | | |
204 | | TExprNode create_texpr_node_from(const Field& field, const PrimitiveType& type, int precision, |
205 | 940 | int scale) { |
206 | 940 | TExprNode node; |
207 | 940 | switch (type) { |
208 | 1 | case TYPE_BOOLEAN: { |
209 | 1 | const auto& storage = static_cast<bool>(field.get<TYPE_BOOLEAN>()); |
210 | 1 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_BOOLEAN>(&storage, &node)); |
211 | 1 | break; |
212 | 1 | } |
213 | 1 | case TYPE_TINYINT: { |
214 | 0 | const auto& storage = static_cast<int8_t>(field.get<TYPE_TINYINT>()); |
215 | 0 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_TINYINT>(&storage, &node)); |
216 | 0 | break; |
217 | 0 | } |
218 | 1 | case TYPE_SMALLINT: { |
219 | 1 | const auto& storage = static_cast<int16_t>(field.get<TYPE_SMALLINT>()); |
220 | 1 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_SMALLINT>(&storage, &node)); |
221 | 1 | break; |
222 | 1 | } |
223 | 44 | case TYPE_INT: { |
224 | 44 | const auto& storage = static_cast<int32_t>(field.get<TYPE_INT>()); |
225 | 44 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_INT>(&storage, &node)); |
226 | 44 | break; |
227 | 44 | } |
228 | 44 | case TYPE_BIGINT: { |
229 | 1 | const auto& storage = static_cast<int64_t>(field.get<TYPE_BIGINT>()); |
230 | 1 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_BIGINT>(&storage, &node)); |
231 | 1 | break; |
232 | 1 | } |
233 | 1 | case TYPE_LARGEINT: { |
234 | 1 | const auto& storage = static_cast<int128_t>(field.get<TYPE_LARGEINT>()); |
235 | 1 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_LARGEINT>(&storage, &node)); |
236 | 1 | break; |
237 | 1 | } |
238 | 1 | case TYPE_FLOAT: { |
239 | 1 | const auto& storage = static_cast<float>(field.get<TYPE_FLOAT>()); |
240 | 1 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_FLOAT>(&storage, &node)); |
241 | 1 | break; |
242 | 1 | } |
243 | 1 | case TYPE_DOUBLE: { |
244 | 1 | const auto& storage = static_cast<double>(field.get<TYPE_DOUBLE>()); |
245 | 1 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_DOUBLE>(&storage, &node)); |
246 | 1 | break; |
247 | 1 | } |
248 | 1 | case TYPE_DATEV2: { |
249 | 1 | const auto& storage = field.get<TYPE_DATEV2>(); |
250 | | |
251 | 1 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_DATEV2>(&storage, &node)); |
252 | 1 | break; |
253 | 1 | } |
254 | 2 | case TYPE_DATETIMEV2: { |
255 | 2 | const auto& storage = field.get<TYPE_DATETIMEV2>(); |
256 | 2 | THROW_IF_ERROR( |
257 | 2 | create_texpr_literal_node<TYPE_DATETIMEV2>(&storage, &node, precision, scale)); |
258 | 2 | break; |
259 | 2 | } |
260 | 2 | case TYPE_TIMESTAMPTZ: { |
261 | 1 | const auto& storage = field.get<TYPE_TIMESTAMPTZ>(); |
262 | | |
263 | 1 | THROW_IF_ERROR( |
264 | 1 | create_texpr_literal_node<TYPE_TIMESTAMPTZ>(&storage, &node, precision, scale)); |
265 | 1 | break; |
266 | 1 | } |
267 | 1 | case TYPE_DATE: { |
268 | 1 | const auto& storage = field.get<TYPE_DATE>(); |
269 | 1 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_DATE>(&storage, &node)); |
270 | 1 | break; |
271 | 1 | } |
272 | 1 | case TYPE_DATETIME: { |
273 | 1 | const auto& storage = field.get<TYPE_DATETIME>(); |
274 | 1 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_DATETIME>(&storage, &node)); |
275 | 1 | break; |
276 | 1 | } |
277 | 1 | case TYPE_DECIMALV2: { |
278 | 1 | const auto& storage = field.get<TYPE_DECIMALV2>(); |
279 | | |
280 | 1 | THROW_IF_ERROR( |
281 | 1 | create_texpr_literal_node<TYPE_DECIMALV2>(&storage, &node, precision, scale)); |
282 | 1 | break; |
283 | 1 | } |
284 | 2 | case TYPE_DECIMAL32: { |
285 | 2 | const auto& storage = field.get<TYPE_DECIMAL32>(); |
286 | 2 | THROW_IF_ERROR( |
287 | 2 | create_texpr_literal_node<TYPE_DECIMAL32>(&storage, &node, precision, scale)); |
288 | 2 | break; |
289 | 2 | } |
290 | 2 | case TYPE_DECIMAL64: { |
291 | 2 | const auto& storage = field.get<TYPE_DECIMAL64>(); |
292 | 2 | THROW_IF_ERROR( |
293 | 2 | create_texpr_literal_node<TYPE_DECIMAL64>(&storage, &node, precision, scale)); |
294 | 2 | break; |
295 | 2 | } |
296 | 2 | case TYPE_DECIMAL128I: { |
297 | 2 | const auto& storage = field.get<TYPE_DECIMAL128I>(); |
298 | 2 | THROW_IF_ERROR( |
299 | 2 | create_texpr_literal_node<TYPE_DECIMAL128I>(&storage, &node, precision, scale)); |
300 | 2 | break; |
301 | 2 | } |
302 | 2 | case TYPE_DECIMAL256: { |
303 | 2 | const auto& storage = field.get<TYPE_DECIMAL256>(); |
304 | 2 | THROW_IF_ERROR( |
305 | 2 | create_texpr_literal_node<TYPE_DECIMAL256>(&storage, &node, precision, scale)); |
306 | 2 | break; |
307 | 2 | } |
308 | 269 | case TYPE_CHAR: { |
309 | 269 | const auto& storage = field.get<TYPE_CHAR>(); |
310 | 269 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_CHAR>(&storage, &node)); |
311 | 269 | break; |
312 | 269 | } |
313 | 479 | case TYPE_VARCHAR: { |
314 | 479 | const auto& storage = field.get<TYPE_VARCHAR>(); |
315 | 479 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_VARCHAR>(&storage, &node)); |
316 | 479 | break; |
317 | 479 | } |
318 | 479 | case TYPE_STRING: { |
319 | 126 | const auto& storage = field.get<TYPE_STRING>(); |
320 | 126 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_STRING>(&storage, &node)); |
321 | 126 | break; |
322 | 126 | } |
323 | 126 | case TYPE_IPV4: { |
324 | 0 | const auto& storage = field.get<TYPE_IPV4>(); |
325 | 0 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_IPV4>(&storage, &node)); |
326 | 0 | break; |
327 | 0 | } |
328 | 0 | case TYPE_IPV6: { |
329 | 0 | const auto& storage = field.get<TYPE_IPV6>(); |
330 | 0 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_IPV6>(&storage, &node)); |
331 | 0 | break; |
332 | 0 | } |
333 | 1 | case TYPE_TIMEV2: { |
334 | 1 | const auto& storage = field.get<TYPE_TIMEV2>(); |
335 | 1 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_TIMEV2>(&storage, &node)); |
336 | 1 | break; |
337 | 1 | } |
338 | 1 | case TYPE_VARBINARY: { |
339 | 0 | const auto& svf = field.get<TYPE_VARBINARY>(); |
340 | 0 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_VARBINARY>(&svf, &node)); |
341 | 0 | break; |
342 | 0 | } |
343 | 0 | default: |
344 | 0 | throw Exception(ErrorCode::INTERNAL_ERROR, "runtime filter meet invalid type {}", |
345 | 0 | int(type)); |
346 | 940 | } |
347 | 940 | return node; |
348 | 940 | } |
349 | | |
350 | | // NOLINTEND(readability-function-size) |
351 | | // NOLINTEND(readability-function-cognitive-complexity) |
352 | | } // namespace doris |
353 | | |
354 | | namespace doris { |
355 | | |
356 | 18.2k | bool VExpr::is_acting_on_a_slot(const VExpr& expr) { |
357 | 18.2k | const auto& children = expr.children(); |
358 | | |
359 | 18.2k | auto is_a_slot = std::any_of(children.begin(), children.end(), |
360 | 18.2k | [](const auto& child) { return is_acting_on_a_slot(*child); }); |
361 | | |
362 | 18.2k | return is_a_slot ? true |
363 | 18.2k | : (expr.node_type() == TExprNodeType::SLOT_REF || |
364 | 7.14k | expr.node_type() == TExprNodeType::VIRTUAL_SLOT_REF); |
365 | 18.2k | } |
366 | | |
367 | | VExpr::VExpr(const TExprNode& node) |
368 | 7.83M | : _node_type(node.node_type), |
369 | 7.83M | _opcode(node.__isset.opcode ? node.opcode : TExprOpcode::INVALID_OPCODE) { |
370 | 7.83M | if (node.__isset.fn) { |
371 | 635k | _fn = node.fn; |
372 | 635k | } |
373 | | |
374 | 7.83M | bool is_nullable = true; |
375 | 7.83M | if (node.__isset.is_nullable) { |
376 | 7.24M | is_nullable = node.is_nullable; |
377 | 7.24M | } |
378 | | // If we define null literal ,should make nullable data type to get correct field instead of undefined ptr |
379 | 7.83M | if (node.node_type == TExprNodeType::NULL_LITERAL) { |
380 | 119k | CHECK(is_nullable); |
381 | 119k | } |
382 | 7.83M | _data_type = get_data_type_with_default_argument( |
383 | 7.83M | DataTypeFactory::instance().create_data_type(node.type, is_nullable)); |
384 | 7.83M | } |
385 | | |
386 | 111 | VExpr::VExpr(const VExpr& vexpr) = default; |
387 | | |
388 | | VExpr::VExpr(DataTypePtr type, bool is_slotref) |
389 | 5.03k | : _opcode(TExprOpcode::INVALID_OPCODE), |
390 | 5.03k | _data_type(get_data_type_with_default_argument(type)) { |
391 | 5.03k | if (is_slotref) { |
392 | 4.14k | _node_type = TExprNodeType::SLOT_REF; |
393 | 4.14k | } |
394 | 5.03k | } |
395 | | |
396 | 20 | TExprNode VExpr::clone_texpr_node() const { |
397 | 20 | TExprNode node; |
398 | 20 | node.__set_node_type(_node_type); |
399 | 20 | node.__set_opcode(_opcode); |
400 | 20 | node.__set_type(create_type_desc(remove_nullable(_data_type)->get_primitive_type(), |
401 | 20 | static_cast<int>(_data_type->get_precision()), |
402 | 20 | static_cast<int>(_data_type->get_scale()))); |
403 | 20 | node.__set_is_nullable(_data_type->is_nullable()); |
404 | 20 | node.__set_num_children(get_num_children()); |
405 | 20 | node.__set_fn(_fn); |
406 | 20 | return node; |
407 | 20 | } |
408 | | |
409 | 0 | Status VExpr::clone_node(VExprSPtr* cloned_expr) const { |
410 | 0 | DORIS_CHECK(cloned_expr != nullptr); |
411 | 0 | return Status::NotSupported("Cannot clone expression {} for file-local rewrite", expr_name()); |
412 | 0 | } |
413 | | |
414 | | Status VExpr::deep_clone(VExprSPtr* cloned_expr, |
415 | 551 | const VExprCloneNodeOverride& clone_node_override) const { |
416 | 551 | DORIS_CHECK(cloned_expr != nullptr); |
417 | | |
418 | 551 | VExprSPtr cloned; |
419 | 551 | if (clone_node_override) { |
420 | 530 | RETURN_IF_ERROR(clone_node_override(*this, &cloned)); |
421 | 530 | } |
422 | 551 | if (cloned == nullptr) { |
423 | 548 | RETURN_IF_ERROR(clone_node(&cloned)); |
424 | 548 | } |
425 | 551 | DORIS_CHECK(cloned != nullptr); |
426 | | |
427 | 551 | VExprSPtrs cloned_children; |
428 | 551 | cloned_children.reserve(_children.size()); |
429 | 551 | for (const auto& child : _children) { |
430 | 382 | DORIS_CHECK(child != nullptr); |
431 | 382 | VExprSPtr cloned_child; |
432 | 382 | RETURN_IF_ERROR(child->deep_clone(&cloned_child, clone_node_override)); |
433 | 382 | cloned_children.push_back(std::move(cloned_child)); |
434 | 382 | } |
435 | 551 | cloned->set_children(std::move(cloned_children)); |
436 | 551 | cloned->reset_prepare_state(); |
437 | 551 | *cloned_expr = std::move(cloned); |
438 | 551 | return Status::OK(); |
439 | 551 | } |
440 | | |
441 | 6.95M | Status VExpr::prepare(RuntimeState* state, const RowDescriptor& row_desc, VExprContext* context) { |
442 | 6.95M | ++context->_depth_num; |
443 | 6.95M | if (context->_depth_num > config::max_depth_of_expr_tree) { |
444 | 0 | return Status::Error<ErrorCode::EXCEEDED_LIMIT>( |
445 | 0 | "The depth of the expression tree is too big, make it less than {}", |
446 | 0 | config::max_depth_of_expr_tree); |
447 | 0 | } |
448 | | |
449 | 6.95M | for (auto& i : _children) { |
450 | 1.45M | RETURN_IF_ERROR(i->prepare(state, row_desc, context)); |
451 | 1.45M | } |
452 | 6.95M | --context->_depth_num; |
453 | 6.95M | #ifndef BE_TEST |
454 | 6.95M | _enable_inverted_index_query = state->query_options().enable_inverted_index_query; |
455 | 6.95M | #endif |
456 | 6.95M | return Status::OK(); |
457 | 6.95M | } |
458 | | |
459 | | Status VExpr::open(RuntimeState* state, VExprContext* context, |
460 | 23.5M | FunctionContext::FunctionStateScope scope) { |
461 | 23.5M | for (auto& i : _children) { |
462 | 429k | RETURN_IF_ERROR(i->open(state, context, scope)); |
463 | 429k | } |
464 | 23.5M | if (scope == FunctionContext::FRAGMENT_LOCAL) { |
465 | 6.26M | RETURN_IF_ERROR(VExpr::get_const_col(context, nullptr)); |
466 | 6.26M | } |
467 | 23.5M | return Status::OK(); |
468 | 23.5M | } |
469 | | |
470 | 1.07k | void VExpr::reset_prepare_state() { |
471 | 1.07k | _prepared = false; |
472 | 1.07k | _prepare_finished = false; |
473 | 1.07k | _open_finished = false; |
474 | 1.07k | for (auto& child : _children) { |
475 | 523 | child->reset_prepare_state(); |
476 | 523 | } |
477 | 1.07k | } |
478 | | |
479 | 25.6M | void VExpr::close(VExprContext* context, FunctionContext::FunctionStateScope scope) { |
480 | 25.6M | for (auto& i : _children) { |
481 | 3.95M | i->close(context, scope); |
482 | 3.95M | } |
483 | 25.6M | } |
484 | | |
485 | | // NOLINTBEGIN(readability-function-size) |
486 | 7.21M | Status VExpr::create_expr(const TExprNode& expr_node, VExprSPtr& expr) { |
487 | 7.21M | try { |
488 | 7.21M | switch (expr_node.node_type) { |
489 | 22.2k | case TExprNodeType::BOOL_LITERAL: |
490 | 750k | case TExprNodeType::INT_LITERAL: |
491 | 798k | case TExprNodeType::LARGE_INT_LITERAL: |
492 | 800k | case TExprNodeType::IPV4_LITERAL: |
493 | 802k | case TExprNodeType::IPV6_LITERAL: |
494 | 846k | case TExprNodeType::FLOAT_LITERAL: |
495 | 902k | case TExprNodeType::DECIMAL_LITERAL: |
496 | 1.07M | case TExprNodeType::DATE_LITERAL: |
497 | 1.07M | case TExprNodeType::TIMEV2_LITERAL: |
498 | 1.72M | case TExprNodeType::STRING_LITERAL: |
499 | 1.72M | case TExprNodeType::JSON_LITERAL: |
500 | 1.72M | case TExprNodeType::VARBINARY_LITERAL: |
501 | 1.84M | case TExprNodeType::NULL_LITERAL: { |
502 | 1.84M | expr = VLiteral::create_shared(expr_node); |
503 | 1.84M | break; |
504 | 1.72M | } |
505 | 12.3k | case TExprNodeType::ARRAY_LITERAL: { |
506 | 12.3k | expr = VArrayLiteral::create_shared(expr_node); |
507 | 12.3k | break; |
508 | 1.72M | } |
509 | 11.9k | case TExprNodeType::MAP_LITERAL: { |
510 | 11.9k | expr = VMapLiteral::create_shared(expr_node); |
511 | 11.9k | break; |
512 | 1.72M | } |
513 | 1.46k | case TExprNodeType::STRUCT_LITERAL: { |
514 | 1.46k | expr = VStructLiteral::create_shared(expr_node); |
515 | 1.46k | break; |
516 | 1.72M | } |
517 | 4.64M | case TExprNodeType::SLOT_REF: { |
518 | 4.64M | if (expr_node.slot_ref.__isset.is_virtual_slot && expr_node.slot_ref.is_virtual_slot) { |
519 | 255 | expr = VirtualSlotRef::create_shared(expr_node); |
520 | 255 | expr->_node_type = TExprNodeType::VIRTUAL_SLOT_REF; |
521 | 4.64M | } else { |
522 | 4.64M | expr = VSlotRef::create_shared(expr_node); |
523 | 4.64M | } |
524 | 4.64M | break; |
525 | 1.72M | } |
526 | 1.29k | case TExprNodeType::COLUMN_REF: { |
527 | 1.29k | expr = VColumnRef::create_shared(expr_node); |
528 | 1.29k | break; |
529 | 1.72M | } |
530 | 5.13k | case TExprNodeType::COMPOUND_PRED: { |
531 | 5.13k | expr = VCompoundPred::create_shared(expr_node); |
532 | 5.13k | break; |
533 | 1.72M | } |
534 | 978 | case TExprNodeType::LAMBDA_FUNCTION_EXPR: { |
535 | 978 | expr = VLambdaFunctionExpr::create_shared(expr_node); |
536 | 978 | break; |
537 | 1.72M | } |
538 | 978 | case TExprNodeType::LAMBDA_FUNCTION_CALL_EXPR: { |
539 | 978 | expr = VLambdaFunctionCallExpr::create_shared(expr_node); |
540 | 978 | break; |
541 | 1.72M | } |
542 | 84.6k | case TExprNodeType::ARITHMETIC_EXPR: |
543 | 413k | case TExprNodeType::BINARY_PRED: |
544 | 413k | case TExprNodeType::NULL_AWARE_BINARY_PRED: |
545 | 413k | case TExprNodeType::COMPUTE_FUNCTION_CALL: { |
546 | 413k | expr = VectorizedFnCall::create_shared(expr_node); |
547 | 413k | break; |
548 | 413k | } |
549 | 148k | case TExprNodeType::FUNCTION_CALL: { |
550 | 148k | if (expr_node.fn.name.function_name == "if") { |
551 | 1.56k | if (expr_node.__isset.short_circuit_evaluation && |
552 | 1.56k | expr_node.short_circuit_evaluation) { |
553 | 1.05k | expr = ShortCircuitIfExpr::create_shared(expr_node); |
554 | 1.05k | } else { |
555 | 509 | expr = VectorizedIfExpr::create_shared(expr_node); |
556 | 509 | } |
557 | 1.56k | break; |
558 | 147k | } else if (expr_node.fn.name.function_name == "ifnull" || |
559 | 147k | expr_node.fn.name.function_name == "nvl") { |
560 | 495 | if (expr_node.__isset.short_circuit_evaluation && |
561 | 495 | expr_node.short_circuit_evaluation) { |
562 | 262 | expr = ShortCircuitIfNullExpr::create_shared(expr_node); |
563 | 262 | } else { |
564 | 233 | expr = VectorizedIfNullExpr::create_shared(expr_node); |
565 | 233 | } |
566 | 495 | break; |
567 | 146k | } else if (expr_node.fn.name.function_name == "coalesce") { |
568 | 149 | if (expr_node.__isset.short_circuit_evaluation && |
569 | 149 | expr_node.short_circuit_evaluation) { |
570 | 72 | expr = ShortCircuitCoalesceExpr::create_shared(expr_node); |
571 | 77 | } else { |
572 | 77 | expr = VectorizedCoalesceExpr::create_shared(expr_node); |
573 | 77 | } |
574 | 149 | break; |
575 | 149 | } |
576 | 146k | expr = VectorizedFnCall::create_shared(expr_node); |
577 | 146k | break; |
578 | 148k | } |
579 | 1.69k | case TExprNodeType::MATCH_PRED: { |
580 | 1.69k | expr = VMatchPredicate::create_shared(expr_node); |
581 | 1.69k | break; |
582 | 148k | } |
583 | 133k | case TExprNodeType::CAST_EXPR: { |
584 | 133k | expr = VCastExpr::create_shared(expr_node); |
585 | 133k | break; |
586 | 148k | } |
587 | 11 | case TExprNodeType::TRY_CAST_EXPR: { |
588 | 11 | expr = TryCastExpr::create_shared(expr_node); |
589 | 11 | break; |
590 | 148k | } |
591 | 1.34k | case TExprNodeType::IN_PRED: { |
592 | 1.34k | expr = VInPredicate::create_shared(expr_node); |
593 | 1.34k | break; |
594 | 148k | } |
595 | 269 | case TExprNodeType::CASE_EXPR: { |
596 | 269 | if (!expr_node.__isset.case_expr) { |
597 | 0 | return Status::InternalError("Case expression not set in thrift node"); |
598 | 0 | } |
599 | 269 | if (expr_node.__isset.short_circuit_evaluation && expr_node.short_circuit_evaluation) { |
600 | 99 | expr = ShortCircuitCaseExpr::create_shared(expr_node); |
601 | 170 | } else { |
602 | 170 | expr = VCaseExpr::create_shared(expr_node); |
603 | 170 | } |
604 | 269 | break; |
605 | 269 | } |
606 | 0 | case TExprNodeType::INFO_FUNC: { |
607 | 0 | expr = VInfoFunc::create_shared(expr_node); |
608 | 0 | break; |
609 | 269 | } |
610 | 469 | case TExprNodeType::SEARCH_EXPR: { |
611 | 469 | expr = VSearchExpr::create_shared(expr_node); |
612 | 469 | break; |
613 | 269 | } |
614 | 0 | default: |
615 | 0 | return Status::InternalError("Unknown expr node type: {}", expr_node.node_type); |
616 | 7.21M | } |
617 | 7.21M | } catch (const Exception& e) { |
618 | 0 | if (e.code() == ErrorCode::INTERNAL_ERROR) { |
619 | 0 | return Status::InternalError("Create Expr failed because {}\nTExprNode={}", e.what(), |
620 | 0 | apache::thrift::ThriftDebugString(expr_node)); |
621 | 0 | } |
622 | 0 | return Status::Error<false>(e.code(), "Create Expr failed because {}", e.what()); |
623 | 0 | LOG(WARNING) << "create expr failed, TExprNode={}, reason={}" |
624 | 0 | << apache::thrift::ThriftDebugString(expr_node) << e.what(); |
625 | 0 | } |
626 | 7.22M | if (!expr->data_type()) { |
627 | 0 | return Status::InvalidArgument("Unknown expr type: {}", expr_node.node_type); |
628 | 0 | } |
629 | 7.22M | return Status::OK(); |
630 | 7.22M | } |
631 | | // NOLINTEND(readability-function-size) |
632 | | |
633 | | Status VExpr::create_tree_from_thrift(const std::vector<TExprNode>& nodes, int* node_idx, |
634 | 5.87M | VExprSPtr& root_expr, VExprContextSPtr& ctx) { |
635 | | // propagate error case |
636 | 5.87M | if (*node_idx >= nodes.size()) { |
637 | 0 | return Status::InternalError("Failed to reconstruct expression tree from thrift."); |
638 | 0 | } |
639 | | |
640 | | // create root expr |
641 | 5.87M | int root_children = nodes[*node_idx].num_children; |
642 | 5.87M | VExprSPtr root; |
643 | 5.87M | RETURN_IF_ERROR(create_expr(nodes[*node_idx], root)); |
644 | 5.87M | DCHECK(root != nullptr); |
645 | 5.87M | root_expr = root; |
646 | 5.87M | ctx = std::make_shared<VExprContext>(root); |
647 | | // short path for leaf node |
648 | 5.87M | if (root_children <= 0) { |
649 | 5.35M | return Status::OK(); |
650 | 5.35M | } |
651 | | |
652 | | // non-recursive traversal |
653 | 517k | using VExprSPtrCountPair = std::pair<VExprSPtr, int>; |
654 | 517k | std::stack<std::shared_ptr<VExprSPtrCountPair>> s; |
655 | 517k | s.emplace(std::make_shared<VExprSPtrCountPair>(root, root_children)); |
656 | 1.78M | while (!s.empty()) { |
657 | | // copy the shared ptr resource to avoid dangling reference |
658 | 1.27M | auto parent = s.top(); |
659 | | // Decrement or pop |
660 | 1.27M | if (parent->second > 1) { |
661 | 611k | parent->second -= 1; |
662 | 659k | } else { |
663 | 659k | s.pop(); |
664 | 659k | } |
665 | | |
666 | 1.27M | DCHECK(parent->first != nullptr); |
667 | 1.27M | if (++*node_idx >= nodes.size()) { |
668 | 0 | return Status::InternalError("Failed to reconstruct expression tree from thrift."); |
669 | 0 | } |
670 | | |
671 | 1.27M | VExprSPtr expr; |
672 | 1.27M | RETURN_IF_ERROR(create_expr(nodes[*node_idx], expr)); |
673 | 1.27M | DCHECK(expr != nullptr); |
674 | 1.27M | parent->first->add_child(expr); |
675 | | // push to stack if has children |
676 | 1.27M | int num_children = nodes[*node_idx].num_children; |
677 | 1.27M | if (num_children > 0) { |
678 | 138k | s.emplace(std::make_shared<VExprSPtrCountPair>(expr, num_children)); |
679 | 138k | } |
680 | 1.27M | } |
681 | 517k | return Status::OK(); |
682 | 517k | } |
683 | | |
684 | 5.70M | Status VExpr::create_expr_tree(const TExpr& texpr, VExprContextSPtr& ctx) { |
685 | 5.70M | if (texpr.nodes.empty()) { |
686 | 7 | ctx = nullptr; |
687 | 7 | return Status::OK(); |
688 | 7 | } |
689 | 5.70M | int node_idx = 0; |
690 | 5.70M | VExprSPtr e; |
691 | 5.70M | Status status = create_tree_from_thrift(texpr.nodes, &node_idx, e, ctx); |
692 | 5.72M | if (status.ok() && node_idx + 1 != texpr.nodes.size()) { |
693 | 0 | status = Status::InternalError( |
694 | 0 | "Expression tree only partially reconstructed. Not all thrift nodes were " |
695 | 0 | "used."); |
696 | 0 | } |
697 | 5.70M | if (!status.ok()) { |
698 | 0 | LOG(ERROR) << "Could not construct expr tree.\n" |
699 | 0 | << status << "\n" |
700 | 0 | << apache::thrift::ThriftDebugString(texpr); |
701 | 0 | } |
702 | 5.70M | return status; |
703 | 5.70M | } |
704 | | |
705 | 1.19M | Status VExpr::create_expr_trees(const std::vector<TExpr>& texprs, VExprContextSPtrs& ctxs) { |
706 | 1.19M | ctxs.clear(); |
707 | 4.71M | for (const auto& texpr : texprs) { |
708 | 4.71M | VExprContextSPtr ctx; |
709 | 4.71M | RETURN_IF_ERROR(create_expr_tree(texpr, ctx)); |
710 | 4.71M | ctxs.push_back(ctx); |
711 | 4.71M | } |
712 | 1.19M | return Status::OK(); |
713 | 1.19M | } |
714 | | |
715 | | Status VExpr::check_expr_output_type(const VExprContextSPtrs& ctxs, |
716 | 244k | const RowDescriptor& output_row_desc) { |
717 | 244k | if (ctxs.empty()) { |
718 | 6 | return Status::OK(); |
719 | 6 | } |
720 | 244k | auto name_and_types = VectorizedUtils::create_name_and_data_types(output_row_desc); |
721 | 244k | if (ctxs.size() != name_and_types.size()) { |
722 | 0 | return Status::InternalError( |
723 | 0 | "output type size not match expr size {} , expected output size {} ", ctxs.size(), |
724 | 0 | name_and_types.size()); |
725 | 0 | } |
726 | 1.49M | auto check_type_can_be_converted = [](DataTypePtr& from, DataTypePtr& to) -> bool { |
727 | 1.49M | if (to->equals_ignore_precision(*from)) { |
728 | 1.48M | return true; |
729 | 1.48M | } |
730 | 1.76k | if (to->is_nullable() && !from->is_nullable()) { |
731 | 93 | return remove_nullable(to)->equals_ignore_precision(*from); |
732 | 93 | } |
733 | 1.67k | return false; |
734 | 1.76k | }; |
735 | 1.73M | for (int i = 0; i < ctxs.size(); i++) { |
736 | 1.49M | auto real_expr_type = get_data_type_with_default_argument(ctxs[i]->root()->data_type()); |
737 | 1.49M | auto&& [name, expected_type] = name_and_types[i]; |
738 | 1.49M | if (!check_type_can_be_converted(real_expr_type, expected_type)) { |
739 | 0 | return Status::InternalError( |
740 | 0 | "output type not match expr type, col name {} , expected type {} , real type " |
741 | 0 | "{}", |
742 | 0 | name, expected_type->get_name(), real_expr_type->get_name()); |
743 | 0 | } |
744 | 1.49M | } |
745 | 244k | return Status::OK(); |
746 | 244k | } |
747 | | |
748 | | Status VExpr::prepare(const VExprContextSPtrs& ctxs, RuntimeState* state, |
749 | 1.84M | const RowDescriptor& row_desc) { |
750 | 5.08M | for (auto ctx : ctxs) { |
751 | 5.08M | RETURN_IF_ERROR(ctx->prepare(state, row_desc)); |
752 | 5.08M | } |
753 | 1.84M | return Status::OK(); |
754 | 1.84M | } |
755 | | |
756 | 1.85M | Status VExpr::open(const VExprContextSPtrs& ctxs, RuntimeState* state) { |
757 | 5.08M | for (const auto& ctx : ctxs) { |
758 | 5.08M | RETURN_IF_ERROR(ctx->open(state)); |
759 | 5.08M | } |
760 | 1.84M | return Status::OK(); |
761 | 1.85M | } |
762 | | |
763 | 1.19M | bool VExpr::contains_blockable_function(const VExprContextSPtrs& ctxs) { |
764 | 1.74M | return std::any_of(ctxs.begin(), ctxs.end(), [](const VExprContextSPtr& ctx) { |
765 | 1.74M | return ctx != nullptr && ctx->root() != nullptr && ctx->root()->is_blockable(); |
766 | 1.74M | }); |
767 | 1.19M | } |
768 | | |
769 | | Status VExpr::clone_if_not_exists(const VExprContextSPtrs& ctxs, RuntimeState* state, |
770 | 0 | VExprContextSPtrs& new_ctxs) { |
771 | 0 | if (!new_ctxs.empty()) { |
772 | | // 'ctxs' was already cloned into '*new_ctxs', nothing to do. |
773 | 0 | DCHECK_EQ(new_ctxs.size(), ctxs.size()); |
774 | 0 | for (auto& new_ctx : new_ctxs) { |
775 | 0 | DCHECK(new_ctx->_is_clone); |
776 | 0 | } |
777 | 0 | return Status::OK(); |
778 | 0 | } |
779 | 0 | new_ctxs.resize(ctxs.size()); |
780 | 0 | for (int i = 0; i < ctxs.size(); ++i) { |
781 | 0 | RETURN_IF_ERROR(ctxs[i]->clone(state, new_ctxs[i])); |
782 | 0 | } |
783 | 0 | return Status::OK(); |
784 | 0 | } |
785 | | |
786 | 403 | std::string VExpr::debug_string() const { |
787 | | // TODO: implement partial debug string for member vars |
788 | 403 | std::stringstream out; |
789 | 403 | out << " type=" << _data_type->get_name(); |
790 | | |
791 | 403 | if (!_children.empty()) { |
792 | 0 | out << " children=" << debug_string(_children); |
793 | 0 | } |
794 | | |
795 | 403 | return out.str(); |
796 | 403 | } |
797 | | |
798 | 0 | std::string VExpr::debug_string(const VExprSPtrs& exprs) { |
799 | 0 | std::stringstream out; |
800 | 0 | out << "["; |
801 | |
|
802 | 0 | for (int i = 0; i < exprs.size(); ++i) { |
803 | 0 | out << (i == 0 ? "" : " ") << exprs[i]->debug_string(); |
804 | 0 | } |
805 | |
|
806 | 0 | out << "]"; |
807 | 0 | return out.str(); |
808 | 0 | } |
809 | | |
810 | 0 | std::string VExpr::debug_string(const VExprContextSPtrs& ctxs) { |
811 | 0 | VExprSPtrs exprs; |
812 | 0 | for (const auto& ctx : ctxs) { |
813 | 0 | exprs.push_back(ctx->root()); |
814 | 0 | } |
815 | 0 | return debug_string(exprs); |
816 | 0 | } |
817 | | |
818 | 6.22M | bool VExpr::is_constant() const { |
819 | 6.22M | return std::all_of(_children.begin(), _children.end(), |
820 | 6.22M | [](const VExprSPtr& expr) { return expr->is_constant(); }); |
821 | 6.22M | } |
822 | | |
823 | | Status VExpr::get_const_col(VExprContext* context, |
824 | 8.58M | std::shared_ptr<ColumnPtrWrapper>* column_wrapper) { |
825 | 8.58M | if (!is_constant()) { |
826 | 5.59M | return Status::OK(); |
827 | 5.59M | } |
828 | | |
829 | 2.98M | if (_constant_col != nullptr && column_wrapper == nullptr) { |
830 | 0 | return Status::OK(); |
831 | 2.98M | } else if (_constant_col != nullptr) { |
832 | 973k | *column_wrapper = _constant_col; |
833 | 973k | return Status::OK(); |
834 | 973k | } |
835 | | |
836 | 2.01M | ColumnPtr result; |
837 | 2.01M | RETURN_IF_ERROR(execute_column(context, nullptr, nullptr, 1, result)); |
838 | 2.01M | _constant_col = std::make_shared<ColumnPtrWrapper>(result); |
839 | 2.01M | if (column_wrapper != nullptr) { |
840 | 0 | *column_wrapper = _constant_col; |
841 | 0 | } |
842 | | |
843 | 2.01M | return Status::OK(); |
844 | 2.01M | } |
845 | | |
846 | 700k | void VExpr::register_function_context(RuntimeState* state, VExprContext* context) { |
847 | 700k | std::vector<DataTypePtr> arg_types; |
848 | 1.25M | for (auto& i : _children) { |
849 | 1.25M | arg_types.push_back(i->data_type()); |
850 | 1.25M | } |
851 | | |
852 | 700k | _fn_context_index = context->register_function_context(state, _data_type, arg_types); |
853 | 700k | } |
854 | | |
855 | | Status VExpr::init_function_context(RuntimeState* state, VExprContext* context, |
856 | | FunctionContext::FunctionStateScope scope, |
857 | 2.01M | const FunctionBasePtr& function) const { |
858 | 2.01M | FunctionContext* fn_ctx = context->fn_context(_fn_context_index); |
859 | 2.01M | if (scope == FunctionContext::FRAGMENT_LOCAL) { |
860 | 702k | std::vector<std::shared_ptr<ColumnPtrWrapper>> constant_cols; |
861 | 1.25M | for (auto c : _children) { |
862 | 1.25M | std::shared_ptr<ColumnPtrWrapper> const_col; |
863 | 1.25M | RETURN_IF_ERROR(c->get_const_col(context, &const_col)); |
864 | 1.25M | constant_cols.push_back(const_col); |
865 | 1.25M | } |
866 | 702k | fn_ctx->set_constant_cols(constant_cols); |
867 | 702k | } |
868 | | |
869 | 2.01M | if (scope == FunctionContext::FRAGMENT_LOCAL) { |
870 | 702k | RETURN_IF_ERROR(function->open(fn_ctx, FunctionContext::FRAGMENT_LOCAL)); |
871 | 702k | } |
872 | 2.01M | RETURN_IF_ERROR(function->open(fn_ctx, FunctionContext::THREAD_LOCAL)); |
873 | 2.01M | return Status::OK(); |
874 | 2.01M | } |
875 | | |
876 | | void VExpr::close_function_context(VExprContext* context, FunctionContext::FunctionStateScope scope, |
877 | 2.02M | const FunctionBasePtr& function) const { |
878 | 2.02M | if (_fn_context_index != -1) { |
879 | 2.02M | FunctionContext* fn_ctx = context->fn_context(_fn_context_index); |
880 | | // `close_function_context` is called in VExprContext's destructor so do not throw exceptions here. |
881 | 2.02M | static_cast<void>(function->close(fn_ctx, FunctionContext::THREAD_LOCAL)); |
882 | 2.02M | if (scope == FunctionContext::FRAGMENT_LOCAL) { |
883 | 701k | static_cast<void>(function->close(fn_ctx, FunctionContext::FRAGMENT_LOCAL)); |
884 | 701k | } |
885 | 2.02M | } |
886 | 2.02M | } |
887 | | |
888 | 0 | Status VExpr::check_constant(const Block& block, ColumnNumbers arguments) const { |
889 | 0 | if (is_constant() && !VectorizedUtils::all_arguments_are_constant(block, arguments)) { |
890 | 0 | return Status::InternalError("const check failed, expr={}", debug_string()); |
891 | 0 | } |
892 | 0 | return Status::OK(); |
893 | 0 | } |
894 | | |
895 | 395k | uint64_t VExpr::get_digest(uint64_t seed) const { |
896 | 395k | auto digest = seed; |
897 | 786k | for (auto child : _children) { |
898 | 786k | digest = child->get_digest(digest); |
899 | 786k | if (digest == 0) { |
900 | 284 | return 0; |
901 | 284 | } |
902 | 786k | } |
903 | | |
904 | 395k | auto& fn_name = _fn.name.function_name; |
905 | 395k | if (!fn_name.empty()) { |
906 | 393k | digest = HashUtil::hash64(fn_name.c_str(), fn_name.size(), digest); |
907 | 393k | } else { |
908 | 2.38k | digest = HashUtil::hash64((const char*)&_node_type, sizeof(_node_type), digest); |
909 | 2.38k | digest = HashUtil::hash64((const char*)&_opcode, sizeof(_opcode), digest); |
910 | 2.38k | } |
911 | 395k | return digest; |
912 | 395k | } |
913 | | |
914 | 82.6k | ColumnPtr VExpr::get_result_from_const(size_t count) const { |
915 | 82.6k | return ColumnConst::create(_constant_col->column_ptr, count); |
916 | 82.6k | } |
917 | | |
918 | | Status VExpr::_evaluate_inverted_index(VExprContext* context, const FunctionBasePtr& function, |
919 | 13.7k | uint32_t segment_num_rows) { |
920 | | // Pre-allocate vectors based on an estimated or known size |
921 | 13.7k | std::vector<segment_v2::IndexIterator*> iterators; |
922 | 13.7k | std::vector<IndexFieldNameAndTypePair> data_type_with_names; |
923 | 13.7k | std::vector<int> column_ids; |
924 | 13.7k | ColumnsWithTypeAndName arguments; |
925 | 13.7k | VExprSPtrs children_exprs; |
926 | | |
927 | | // Reserve space to avoid multiple reallocations |
928 | 13.7k | const size_t estimated_size = get_num_children(); |
929 | 13.7k | iterators.reserve(estimated_size); |
930 | 13.7k | data_type_with_names.reserve(estimated_size); |
931 | 13.7k | column_ids.reserve(estimated_size); |
932 | 13.7k | children_exprs.reserve(estimated_size); |
933 | | |
934 | 13.7k | auto index_context = context->get_index_context(); |
935 | | |
936 | | // if child is cast expr, we need to ensure target data type is the same with storage data type. |
937 | | // or they are all string type |
938 | | // and if data type is array, we need to get the nested data type to ensure that. |
939 | 28.5k | for (const auto& child : children()) { |
940 | 28.5k | if (child->node_type() == TExprNodeType::CAST_EXPR) { |
941 | 2.30k | auto* cast_expr = assert_cast<VCastExpr*>(child.get()); |
942 | 2.30k | DCHECK_EQ(cast_expr->get_num_children(), 1); |
943 | 2.30k | if (cast_expr->get_child(0)->is_slot_ref()) { |
944 | 2.22k | auto* column_slot_ref = assert_cast<VSlotRef*>(cast_expr->get_child(0).get()); |
945 | 2.22k | auto column_id = column_slot_ref->column_id(); |
946 | 2.22k | const auto* storage_name_type = |
947 | 2.22k | context->get_index_context()->get_storage_name_and_type_by_column_id( |
948 | 2.22k | column_id); |
949 | 2.22k | auto storage_type = remove_nullable(storage_name_type->second); |
950 | 2.22k | auto target_type = remove_nullable(cast_expr->get_target_type()); |
951 | 2.22k | auto origin_primitive_type = storage_type->get_primitive_type(); |
952 | 2.22k | auto target_primitive_type = target_type->get_primitive_type(); |
953 | 2.22k | if (is_complex_type(storage_type->get_primitive_type())) { |
954 | 543 | if (storage_type->get_primitive_type() == TYPE_ARRAY && |
955 | 543 | target_type->get_primitive_type() == TYPE_ARRAY) { |
956 | 540 | auto nested_storage_type = |
957 | 540 | (assert_cast<const DataTypeArray*>(storage_type.get())) |
958 | 540 | ->get_nested_type(); |
959 | 540 | origin_primitive_type = nested_storage_type->get_primitive_type(); |
960 | 540 | auto nested_target_type = |
961 | 540 | (assert_cast<const DataTypeArray*>(target_type.get())) |
962 | 540 | ->get_nested_type(); |
963 | 540 | target_primitive_type = nested_target_type->get_primitive_type(); |
964 | 540 | } else { |
965 | 3 | continue; |
966 | 3 | } |
967 | 543 | } |
968 | 2.21k | if (origin_primitive_type != TYPE_VARIANT && |
969 | 2.21k | (storage_type->equals(*target_type) || |
970 | 2.02k | (is_string_type(target_primitive_type) && |
971 | 1.16k | is_string_type(origin_primitive_type)))) { |
972 | 861 | children_exprs.emplace_back(expr_without_cast(child)); |
973 | 861 | } |
974 | 2.21k | } else { |
975 | 85 | return Status::OK(); // for example: cast("abc") as ipv4 case |
976 | 85 | } |
977 | 26.2k | } else { |
978 | 26.2k | children_exprs.emplace_back(child); |
979 | 26.2k | } |
980 | 28.5k | } |
981 | | |
982 | 13.6k | if (children_exprs.empty()) { |
983 | 256 | return Status::OK(); // Early exit if no children to process |
984 | 256 | } |
985 | | |
986 | 25.4k | for (const auto& child : children_exprs) { |
987 | 25.4k | if (child->is_slot_ref()) { |
988 | 11.7k | auto* column_slot_ref = assert_cast<VSlotRef*>(child.get()); |
989 | 11.7k | auto column_id = column_slot_ref->column_id(); |
990 | 11.7k | auto* iter = context->get_index_context()->get_inverted_index_iterator_by_column_id( |
991 | 11.7k | column_id); |
992 | | //column does not have inverted index |
993 | 11.7k | if (iter == nullptr) { |
994 | 3.04k | continue; |
995 | 3.04k | } |
996 | 8.69k | const auto* storage_name_type = |
997 | 8.69k | context->get_index_context()->get_storage_name_and_type_by_column_id(column_id); |
998 | 8.69k | if (storage_name_type == nullptr) { |
999 | 0 | auto err_msg = fmt::format( |
1000 | 0 | "storage_name_type cannot be found for column {} while in {} " |
1001 | 0 | "evaluate_inverted_index", |
1002 | 0 | column_id, expr_name()); |
1003 | 0 | LOG(ERROR) << err_msg; |
1004 | 0 | return Status::InternalError(err_msg); |
1005 | 0 | } |
1006 | 8.69k | iterators.emplace_back(iter); |
1007 | 8.69k | data_type_with_names.emplace_back(*storage_name_type); |
1008 | 8.69k | column_ids.emplace_back(column_id); |
1009 | 13.7k | } else if (child->is_literal()) { |
1010 | 12.1k | auto* column_literal = assert_cast<VLiteral*>(child.get()); |
1011 | 12.1k | arguments.emplace_back(column_literal->get_column_ptr(), |
1012 | 12.1k | column_literal->get_data_type(), column_literal->expr_name()); |
1013 | 12.1k | } else if (child->can_push_down_to_index()) { |
1014 | 0 | RETURN_IF_ERROR(child->evaluate_inverted_index(context, segment_num_rows)); |
1015 | 1.59k | } else { |
1016 | 1.59k | return Status::OK(); // others cases |
1017 | 1.59k | } |
1018 | 25.4k | } |
1019 | | |
1020 | | // is null or is not null has no arguments |
1021 | 11.7k | if (iterators.empty() || (arguments.empty() && !(function->get_name() == "is_not_null_pred" || |
1022 | 3.59k | function->get_name() == "is_null_pred"))) { |
1023 | 3.59k | return Status::OK(); // Nothing to evaluate or no literals to compare against |
1024 | 3.59k | } |
1025 | | |
1026 | 8.17k | const InvertedIndexAnalyzerCtx* analyzer_ctx = nullptr; |
1027 | 8.46k | if (auto index_ctx = context->get_index_context(); index_ctx != nullptr) { |
1028 | 8.46k | analyzer_ctx = index_ctx->get_analyzer_ctx_for_expr(this); |
1029 | 8.46k | } |
1030 | | |
1031 | 8.17k | auto result_bitmap = segment_v2::InvertedIndexResultBitmap(); |
1032 | | // Pass analyzer_key to function (used by match predicates for multi-analyzer index selection) |
1033 | 8.17k | auto res = function->evaluate_inverted_index(arguments, data_type_with_names, iterators, |
1034 | 8.17k | segment_num_rows, analyzer_ctx, result_bitmap); |
1035 | 8.17k | if (!res.ok()) { |
1036 | 317 | return res; |
1037 | 317 | } |
1038 | 7.85k | if (!result_bitmap.is_empty()) { |
1039 | 6.48k | index_context->set_index_result_for_expr(this, result_bitmap); |
1040 | 6.56k | for (int column_id : column_ids) { |
1041 | 6.56k | index_context->set_true_for_index_status(this, column_id); |
1042 | 6.56k | } |
1043 | 6.48k | } |
1044 | 7.85k | return Status::OK(); |
1045 | 8.17k | } |
1046 | | |
1047 | 0 | size_t VExpr::estimate_memory(const size_t rows) { |
1048 | 0 | if (is_const_and_have_executed()) { |
1049 | 0 | return 0; |
1050 | 0 | } |
1051 | | |
1052 | 0 | size_t estimate_size = 0; |
1053 | 0 | for (auto& child : _children) { |
1054 | 0 | estimate_size += child->estimate_memory(rows); |
1055 | 0 | } |
1056 | |
|
1057 | 0 | if (_data_type->have_maximum_size_of_value()) { |
1058 | 0 | estimate_size += rows * _data_type->get_size_of_value_in_memory(); |
1059 | 0 | } else { |
1060 | 0 | estimate_size += rows * 64; /// TODO: need a more reasonable value |
1061 | 0 | } |
1062 | 0 | return estimate_size; |
1063 | 0 | } |
1064 | | |
1065 | | bool VExpr::fast_execute(VExprContext* context, Selector* selector, size_t count, |
1066 | 352k | ColumnPtr& result_column) const { |
1067 | 352k | if (context->get_index_context() && |
1068 | 352k | context->get_index_context()->get_index_result_column().contains(this)) { |
1069 | | // prepare a column to save result |
1070 | 786 | result_column = filter_column_with_selector( |
1071 | 786 | context->get_index_context()->get_index_result_column()[this], selector, count); |
1072 | 786 | if (_data_type->is_nullable()) { |
1073 | 677 | result_column = make_nullable(result_column); |
1074 | 677 | } |
1075 | 786 | return true; |
1076 | 786 | } |
1077 | 351k | return false; |
1078 | 352k | } |
1079 | | |
1080 | 0 | bool VExpr::equals(const VExpr& other) { |
1081 | 0 | return false; |
1082 | 0 | } |
1083 | | |
1084 | | Status VExpr::evaluate_ann_range_search( |
1085 | | const segment_v2::AnnRangeSearchRuntime& runtime, |
1086 | | const std::vector<std::unique_ptr<segment_v2::IndexIterator>>& index_iterators, |
1087 | | const std::vector<ColumnId>& idx_to_cid, |
1088 | | const std::vector<std::unique_ptr<segment_v2::ColumnIterator>>& column_iterators, |
1089 | | size_t rows_of_segment, roaring::Roaring& row_bitmap, AnnIndexStats& ann_index_stats, |
1090 | 5.12k | AnnRangeSearchEvaluationResult& result) { |
1091 | 5.12k | result = {}; |
1092 | 5.12k | return Status::OK(); |
1093 | 5.12k | } |
1094 | | |
1095 | | void VExpr::prepare_ann_range_search(const doris::VectorSearchUserParams& params, |
1096 | | segment_v2::AnnRangeSearchRuntime& range_search_runtime, |
1097 | 16.7k | bool& suitable_for_ann_index) { |
1098 | 16.7k | if (!suitable_for_ann_index) { |
1099 | 0 | return; |
1100 | 0 | } |
1101 | 16.7k | for (auto& child : _children) { |
1102 | 10.6k | child->prepare_ann_range_search(params, range_search_runtime, suitable_for_ann_index); |
1103 | 10.6k | if (!suitable_for_ann_index) { |
1104 | 444 | return; |
1105 | 444 | } |
1106 | 10.6k | } |
1107 | 16.7k | } |
1108 | | |
1109 | | Status VExpr::execute_filter(VExprContext* context, const Block* block, |
1110 | | uint8_t* __restrict result_filter_data, size_t rows, bool accept_null, |
1111 | 44.7k | bool* can_filter_all) const { |
1112 | 44.7k | ColumnPtr filter_column; |
1113 | 44.7k | RETURN_IF_ERROR(execute_column(context, block, nullptr, rows, filter_column)); |
1114 | 44.7k | if (const auto* const_column = check_and_get_column<ColumnConst>(*filter_column)) { |
1115 | | // const(nullable) or const(bool) |
1116 | 1.95k | const bool result = accept_null |
1117 | 1.95k | ? (const_column->is_null_at(0) || const_column->get_bool(0)) |
1118 | 1.95k | : (!const_column->is_null_at(0) && const_column->get_bool(0)); |
1119 | 1.95k | if (!result) { |
1120 | | // filter all |
1121 | 1.05k | *can_filter_all = true; |
1122 | 1.05k | memset(result_filter_data, 0, rows); |
1123 | 1.05k | return Status::OK(); |
1124 | 1.05k | } |
1125 | 42.8k | } else if (const auto* nullable_column = check_and_get_column<ColumnNullable>(*filter_column)) { |
1126 | | // nullable(bool) |
1127 | 22.5k | const ColumnPtr& nested_column = nullable_column->get_nested_column_ptr(); |
1128 | 22.5k | const IColumn::Filter& filter = assert_cast<const ColumnUInt8&>(*nested_column).get_data(); |
1129 | 22.5k | const auto* __restrict filter_data = filter.data(); |
1130 | 22.5k | const auto* __restrict null_map_data = nullable_column->get_null_map_data().data(); |
1131 | | |
1132 | 22.5k | if (accept_null) { |
1133 | 1.01k | for (size_t i = 0; i < rows; ++i) { |
1134 | 1.00k | result_filter_data[i] &= (null_map_data[i]) || filter_data[i]; |
1135 | 1.00k | } |
1136 | 22.5k | } else { |
1137 | 23.1M | for (size_t i = 0; i < rows; ++i) { |
1138 | 23.0M | result_filter_data[i] &= (!null_map_data[i]) & filter_data[i]; |
1139 | 23.0M | } |
1140 | 22.5k | } |
1141 | | |
1142 | 22.5k | if (memchr(result_filter_data, 0x1, rows) == nullptr) { |
1143 | 8.07k | *can_filter_all = true; |
1144 | 8.07k | return Status::OK(); |
1145 | 8.07k | } |
1146 | 22.5k | } else { |
1147 | | // bool |
1148 | 20.2k | const IColumn::Filter& filter = assert_cast<const ColumnUInt8&>(*filter_column).get_data(); |
1149 | 20.2k | const auto* __restrict filter_data = filter.data(); |
1150 | | |
1151 | 49.7M | for (size_t i = 0; i < rows; ++i) { |
1152 | 49.6M | result_filter_data[i] &= filter_data[i]; |
1153 | 49.6M | } |
1154 | | |
1155 | 20.2k | if (memchr(result_filter_data, 0x1, rows) == nullptr) { |
1156 | 4.06k | *can_filter_all = true; |
1157 | 4.06k | return Status::OK(); |
1158 | 4.06k | } |
1159 | 20.2k | } |
1160 | | |
1161 | 31.5k | return Status::OK(); |
1162 | 44.7k | } |
1163 | | |
1164 | | #include "common/compile_check_end.h" |
1165 | | } // namespace doris |