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