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