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