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