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