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