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