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 | 30.8k | int scale) { |
83 | 30.8k | TExprNode node; |
84 | | |
85 | 30.8k | switch (type) { |
86 | 50 | case TYPE_BOOLEAN: { |
87 | 50 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_BOOLEAN>(data, &node)); |
88 | 50 | break; |
89 | 50 | } |
90 | 584 | case TYPE_TINYINT: { |
91 | 584 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_TINYINT>(data, &node)); |
92 | 584 | break; |
93 | 584 | } |
94 | 584 | case TYPE_SMALLINT: { |
95 | 264 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_SMALLINT>(data, &node)); |
96 | 264 | break; |
97 | 264 | } |
98 | 6.40k | case TYPE_INT: { |
99 | 6.40k | THROW_IF_ERROR(create_texpr_literal_node<TYPE_INT>(data, &node)); |
100 | 6.40k | break; |
101 | 6.40k | } |
102 | 20.6k | case TYPE_BIGINT: { |
103 | 20.6k | THROW_IF_ERROR(create_texpr_literal_node<TYPE_BIGINT>(data, &node)); |
104 | 20.6k | break; |
105 | 20.6k | } |
106 | 20.6k | case TYPE_LARGEINT: { |
107 | 64 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_LARGEINT>(data, &node)); |
108 | 64 | break; |
109 | 64 | } |
110 | 64 | case TYPE_FLOAT: { |
111 | 64 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_FLOAT>(data, &node)); |
112 | 64 | break; |
113 | 64 | } |
114 | 64 | case TYPE_DOUBLE: { |
115 | 19 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_DOUBLE>(data, &node)); |
116 | 19 | break; |
117 | 19 | } |
118 | 318 | case TYPE_DATEV2: { |
119 | 318 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_DATEV2>(data, &node)); |
120 | 318 | break; |
121 | 318 | } |
122 | 318 | case TYPE_DATETIMEV2: { |
123 | 236 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_DATETIMEV2>(data, &node, precision, scale)); |
124 | 236 | break; |
125 | 236 | } |
126 | 236 | case TYPE_DATE: { |
127 | 6 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_DATE>(data, &node)); |
128 | 6 | break; |
129 | 6 | } |
130 | 6 | case TYPE_DATETIME: { |
131 | 4 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_DATETIME>(data, &node)); |
132 | 4 | break; |
133 | 4 | } |
134 | 4 | case TYPE_DECIMALV2: { |
135 | 4 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_DECIMALV2>(data, &node, precision, scale)); |
136 | 4 | break; |
137 | 4 | } |
138 | 24 | case TYPE_DECIMAL32: { |
139 | 24 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_DECIMAL32>(data, &node, precision, scale)); |
140 | 24 | break; |
141 | 24 | } |
142 | 571 | case TYPE_DECIMAL64: { |
143 | 571 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_DECIMAL64>(data, &node, precision, scale)); |
144 | 571 | break; |
145 | 571 | } |
146 | 571 | case TYPE_DECIMAL128I: { |
147 | 153 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_DECIMAL128I>(data, &node, precision, scale)); |
148 | 153 | break; |
149 | 153 | } |
150 | 153 | case TYPE_DECIMAL256: { |
151 | 26 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_DECIMAL256>(data, &node, precision, scale)); |
152 | 26 | break; |
153 | 26 | } |
154 | 66 | case TYPE_CHAR: { |
155 | 66 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_CHAR>(data, &node)); |
156 | 66 | break; |
157 | 66 | } |
158 | 502 | case TYPE_VARCHAR: { |
159 | 502 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_VARCHAR>(data, &node)); |
160 | 502 | break; |
161 | 502 | } |
162 | 765 | case TYPE_STRING: { |
163 | 765 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_STRING>(data, &node)); |
164 | 765 | break; |
165 | 765 | } |
166 | 765 | case TYPE_IPV4: { |
167 | 2 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_IPV4>(data, &node)); |
168 | 2 | break; |
169 | 2 | } |
170 | 2 | 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 | 9 | case TYPE_TIMESTAMPTZ: { |
179 | 9 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_TIMESTAMPTZ>(data, &node, precision, scale)); |
180 | 9 | break; |
181 | 9 | } |
182 | 9 | default: |
183 | 0 | throw Exception(ErrorCode::INTERNAL_ERROR, "runtime filter meet invalid type {}", |
184 | 0 | int(type)); |
185 | 30.8k | } |
186 | 30.7k | return node; |
187 | 30.8k | } |
188 | | |
189 | | TExprNode create_texpr_node_from(const Field& field, const PrimitiveType& type, int precision, |
190 | 1.67k | int scale) { |
191 | 1.67k | TExprNode node; |
192 | 1.67k | 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 | 129 | case TYPE_INT: { |
209 | 129 | const auto& storage = static_cast<int32_t>(field.get<TYPE_INT>()); |
210 | 129 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_INT>(&storage, &node)); |
211 | 129 | break; |
212 | 129 | } |
213 | 129 | case TYPE_BIGINT: { |
214 | 59 | const auto& storage = static_cast<int64_t>(field.get<TYPE_BIGINT>()); |
215 | 59 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_BIGINT>(&storage, &node)); |
216 | 59 | break; |
217 | 59 | } |
218 | 59 | 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 | 1.37k | case TYPE_DECIMAL64: { |
276 | 1.37k | const auto& storage = field.get<TYPE_DECIMAL64>(); |
277 | 1.37k | THROW_IF_ERROR( |
278 | 1.37k | create_texpr_literal_node<TYPE_DECIMAL64>(&storage, &node, precision, scale)); |
279 | 1.37k | break; |
280 | 1.37k | } |
281 | 1.37k | 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 | 26 | case TYPE_CHAR: { |
294 | 26 | const auto& storage = field.get<TYPE_CHAR>(); |
295 | 26 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_CHAR>(&storage, &node)); |
296 | 26 | break; |
297 | 26 | } |
298 | 42 | case TYPE_VARCHAR: { |
299 | 42 | const auto& storage = field.get<TYPE_VARCHAR>(); |
300 | 42 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_VARCHAR>(&storage, &node)); |
301 | 42 | break; |
302 | 42 | } |
303 | 42 | case TYPE_STRING: { |
304 | 29 | const auto& storage = field.get<TYPE_STRING>(); |
305 | 29 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_STRING>(&storage, &node)); |
306 | 29 | break; |
307 | 29 | } |
308 | 29 | 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 | 1.67k | } |
332 | 1.67k | return node; |
333 | 1.67k | } |
334 | | |
335 | | // NOLINTEND(readability-function-size) |
336 | | // NOLINTEND(readability-function-cognitive-complexity) |
337 | | } // namespace doris |
338 | | |
339 | | namespace doris { |
340 | | |
341 | 14.4k | bool VExpr::is_acting_on_a_slot(const VExpr& expr) { |
342 | 14.4k | if (expr.node_type() == TExprNodeType::SEARCH_EXPR) { |
343 | 565 | return true; |
344 | 565 | } |
345 | 13.8k | const auto& children = expr.children(); |
346 | | |
347 | 13.8k | auto is_a_slot = std::any_of(children.begin(), children.end(), |
348 | 13.8k | [](const auto& child) { return is_acting_on_a_slot(*child); }); |
349 | | |
350 | 13.8k | return is_a_slot ? true |
351 | 13.8k | : (expr.node_type() == TExprNodeType::SLOT_REF || |
352 | 5.14k | expr.node_type() == TExprNodeType::VIRTUAL_SLOT_REF); |
353 | 14.4k | } |
354 | | |
355 | | VExpr::VExpr(const TExprNode& node) |
356 | 9.15M | : _node_type(node.node_type), |
357 | 9.15M | _opcode(node.__isset.opcode ? node.opcode : TExprOpcode::INVALID_OPCODE) { |
358 | 9.15M | if (node.__isset.fn) { |
359 | 733k | _fn = node.fn; |
360 | 733k | } |
361 | | |
362 | 9.15M | bool is_nullable = true; |
363 | 9.15M | if (node.__isset.is_nullable) { |
364 | 9.07M | is_nullable = node.is_nullable; |
365 | 9.07M | } |
366 | | // If we define null literal ,should make nullable data type to get correct field instead of undefined ptr |
367 | 9.15M | if (node.node_type == TExprNodeType::NULL_LITERAL) { |
368 | 128k | CHECK(is_nullable); |
369 | 128k | } |
370 | 9.15M | _data_type = get_data_type_with_default_argument( |
371 | 9.15M | DataTypeFactory::instance().create_data_type(node.type, is_nullable)); |
372 | 9.15M | } |
373 | | |
374 | 0 | VExpr::VExpr(const VExpr& vexpr) = default; |
375 | | |
376 | | VExpr::VExpr(DataTypePtr type, bool is_slotref) |
377 | 534 | : _opcode(TExprOpcode::INVALID_OPCODE), |
378 | 534 | _data_type(get_data_type_with_default_argument(type)) { |
379 | 534 | if (is_slotref) { |
380 | 525 | _node_type = TExprNodeType::SLOT_REF; |
381 | 525 | } |
382 | 534 | } |
383 | | |
384 | 8.81M | Status VExpr::prepare(RuntimeState* state, const RowDescriptor& row_desc, VExprContext* context) { |
385 | 8.81M | ++context->_depth_num; |
386 | 8.81M | 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 | 8.81M | for (auto& i : _children) { |
393 | 1.71M | RETURN_IF_ERROR(i->prepare(state, row_desc, context)); |
394 | 1.71M | } |
395 | 8.81M | --context->_depth_num; |
396 | 8.81M | #ifndef BE_TEST |
397 | 8.81M | _enable_inverted_index_query = state->query_options().enable_inverted_index_query; |
398 | 8.81M | #endif |
399 | 8.81M | return Status::OK(); |
400 | 8.81M | } |
401 | | |
402 | | Status VExpr::open(RuntimeState* state, VExprContext* context, |
403 | 30.8M | FunctionContext::FunctionStateScope scope) { |
404 | 30.8M | for (auto& i : _children) { |
405 | 458k | RETURN_IF_ERROR(i->open(state, context, scope)); |
406 | 458k | } |
407 | 30.8M | if (scope == FunctionContext::FRAGMENT_LOCAL) { |
408 | 7.98M | RETURN_IF_ERROR(VExpr::get_const_col(context, nullptr)); |
409 | 7.98M | } |
410 | 30.8M | return Status::OK(); |
411 | 30.8M | } |
412 | | |
413 | 33.1M | void VExpr::close(VExprContext* context, FunctionContext::FunctionStateScope scope) { |
414 | 33.1M | for (auto& i : _children) { |
415 | 4.37M | i->close(context, scope); |
416 | 4.37M | } |
417 | 33.1M | } |
418 | | |
419 | | // NOLINTBEGIN(readability-function-size) |
420 | 9.03M | Status VExpr::create_expr(const TExprNode& expr_node, VExprSPtr& expr) { |
421 | 9.03M | try { |
422 | 9.03M | switch (expr_node.node_type) { |
423 | 24.1k | case TExprNodeType::BOOL_LITERAL: |
424 | 759k | case TExprNodeType::INT_LITERAL: |
425 | 807k | case TExprNodeType::LARGE_INT_LITERAL: |
426 | 809k | case TExprNodeType::IPV4_LITERAL: |
427 | 811k | case TExprNodeType::IPV6_LITERAL: |
428 | 859k | case TExprNodeType::FLOAT_LITERAL: |
429 | 925k | case TExprNodeType::DECIMAL_LITERAL: |
430 | 1.09M | case TExprNodeType::DATE_LITERAL: |
431 | 1.09M | case TExprNodeType::TIMEV2_LITERAL: |
432 | 1.87M | case TExprNodeType::STRING_LITERAL: |
433 | 1.87M | case TExprNodeType::JSON_LITERAL: |
434 | 1.87M | case TExprNodeType::VARBINARY_LITERAL: |
435 | 2.00M | case TExprNodeType::NULL_LITERAL: { |
436 | 2.00M | expr = VLiteral::create_shared(expr_node); |
437 | 2.00M | break; |
438 | 1.87M | } |
439 | 18.6k | case TExprNodeType::ARRAY_LITERAL: { |
440 | 18.6k | expr = VArrayLiteral::create_shared(expr_node); |
441 | 18.6k | break; |
442 | 1.87M | } |
443 | 12.0k | case TExprNodeType::MAP_LITERAL: { |
444 | 12.0k | expr = VMapLiteral::create_shared(expr_node); |
445 | 12.0k | break; |
446 | 1.87M | } |
447 | 1.60k | case TExprNodeType::STRUCT_LITERAL: { |
448 | 1.60k | expr = VStructLiteral::create_shared(expr_node); |
449 | 1.60k | break; |
450 | 1.87M | } |
451 | 6.16M | case TExprNodeType::SLOT_REF: { |
452 | 6.16M | if (expr_node.slot_ref.__isset.is_virtual_slot && expr_node.slot_ref.is_virtual_slot) { |
453 | 263 | expr = VirtualSlotRef::create_shared(expr_node); |
454 | 263 | expr->_node_type = TExprNodeType::VIRTUAL_SLOT_REF; |
455 | 6.16M | } else { |
456 | 6.16M | expr = VSlotRef::create_shared(expr_node); |
457 | 6.16M | } |
458 | 6.16M | break; |
459 | 1.87M | } |
460 | 1.31k | case TExprNodeType::COLUMN_REF: { |
461 | 1.31k | expr = VColumnRef::create_shared(expr_node); |
462 | 1.31k | break; |
463 | 1.87M | } |
464 | 3.26k | case TExprNodeType::COMPOUND_PRED: { |
465 | 3.26k | expr = VCompoundPred::create_shared(expr_node); |
466 | 3.26k | break; |
467 | 1.87M | } |
468 | 990 | case TExprNodeType::LAMBDA_FUNCTION_EXPR: { |
469 | 990 | expr = VLambdaFunctionExpr::create_shared(expr_node); |
470 | 990 | break; |
471 | 1.87M | } |
472 | 990 | case TExprNodeType::LAMBDA_FUNCTION_CALL_EXPR: { |
473 | 990 | expr = VLambdaFunctionCallExpr::create_shared(expr_node); |
474 | 990 | break; |
475 | 1.87M | } |
476 | 84.5k | case TExprNodeType::ARITHMETIC_EXPR: |
477 | 549k | case TExprNodeType::BINARY_PRED: |
478 | 549k | case TExprNodeType::NULL_AWARE_BINARY_PRED: |
479 | 549k | case TExprNodeType::COMPUTE_FUNCTION_CALL: { |
480 | 549k | expr = VectorizedFnCall::create_shared(expr_node); |
481 | 549k | break; |
482 | 549k | } |
483 | 148k | case TExprNodeType::FUNCTION_CALL: { |
484 | 148k | if (expr_node.fn.name.function_name == "if") { |
485 | 3.28k | if (expr_node.__isset.short_circuit_evaluation && |
486 | 3.28k | expr_node.short_circuit_evaluation) { |
487 | 541 | expr = ShortCircuitIfExpr::create_shared(expr_node); |
488 | 2.73k | } else { |
489 | 2.73k | expr = VectorizedIfExpr::create_shared(expr_node); |
490 | 2.73k | } |
491 | 3.28k | break; |
492 | 145k | } else if (expr_node.fn.name.function_name == "ifnull" || |
493 | 145k | expr_node.fn.name.function_name == "nvl") { |
494 | 263 | if (expr_node.__isset.short_circuit_evaluation && |
495 | 263 | expr_node.short_circuit_evaluation) { |
496 | 125 | expr = ShortCircuitIfNullExpr::create_shared(expr_node); |
497 | 138 | } else { |
498 | 138 | expr = VectorizedIfNullExpr::create_shared(expr_node); |
499 | 138 | } |
500 | 263 | break; |
501 | 144k | } else if (expr_node.fn.name.function_name == "coalesce") { |
502 | 151 | if (expr_node.__isset.short_circuit_evaluation && |
503 | 151 | expr_node.short_circuit_evaluation) { |
504 | 61 | expr = ShortCircuitCoalesceExpr::create_shared(expr_node); |
505 | 90 | } else { |
506 | 90 | expr = VectorizedCoalesceExpr::create_shared(expr_node); |
507 | 90 | } |
508 | 151 | break; |
509 | 151 | } |
510 | 144k | expr = VectorizedFnCall::create_shared(expr_node); |
511 | 144k | break; |
512 | 148k | } |
513 | 1.72k | case TExprNodeType::MATCH_PRED: { |
514 | 1.72k | expr = VMatchPredicate::create_shared(expr_node); |
515 | 1.72k | break; |
516 | 148k | } |
517 | 132k | case TExprNodeType::CAST_EXPR: { |
518 | 132k | expr = VCastExpr::create_shared(expr_node); |
519 | 132k | break; |
520 | 148k | } |
521 | 7 | case TExprNodeType::TRY_CAST_EXPR: { |
522 | 7 | expr = TryCastExpr::create_shared(expr_node); |
523 | 7 | break; |
524 | 148k | } |
525 | 1.88k | case TExprNodeType::IN_PRED: { |
526 | 1.88k | expr = VInPredicate::create_shared(expr_node); |
527 | 1.88k | break; |
528 | 148k | } |
529 | 296 | case TExprNodeType::CASE_EXPR: { |
530 | 296 | if (!expr_node.__isset.case_expr) { |
531 | 0 | return Status::InternalError("Case expression not set in thrift node"); |
532 | 0 | } |
533 | 296 | if (expr_node.__isset.short_circuit_evaluation && expr_node.short_circuit_evaluation) { |
534 | 66 | expr = ShortCircuitCaseExpr::create_shared(expr_node); |
535 | 230 | } else { |
536 | 230 | expr = VCaseExpr::create_shared(expr_node); |
537 | 230 | } |
538 | 296 | break; |
539 | 296 | } |
540 | 0 | case TExprNodeType::INFO_FUNC: { |
541 | 0 | expr = VInfoFunc::create_shared(expr_node); |
542 | 0 | break; |
543 | 296 | } |
544 | 464 | case TExprNodeType::SEARCH_EXPR: { |
545 | 464 | expr = VSearchExpr::create_shared(expr_node); |
546 | 464 | break; |
547 | 296 | } |
548 | 0 | default: |
549 | 0 | return Status::InternalError("Unknown expr node type: {}", expr_node.node_type); |
550 | 9.03M | } |
551 | 9.03M | } 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 | 9.05M | if (!expr->data_type()) { |
561 | 0 | return Status::InvalidArgument("Unknown expr type: {}", expr_node.node_type); |
562 | 0 | } |
563 | 9.05M | return Status::OK(); |
564 | 9.05M | } |
565 | | // NOLINTEND(readability-function-size) |
566 | | |
567 | | Status VExpr::create_tree_from_thrift(const std::vector<TExprNode>& nodes, int* node_idx, |
568 | 7.38M | VExprSPtr& root_expr, VExprContextSPtr& ctx) { |
569 | | // propagate error case |
570 | 7.38M | 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 | 7.38M | int root_children = nodes[*node_idx].num_children; |
576 | 7.38M | VExprSPtr root; |
577 | 7.38M | RETURN_IF_ERROR(create_expr(nodes[*node_idx], root)); |
578 | 7.38M | DCHECK(root != nullptr); |
579 | 7.38M | root_expr = root; |
580 | 7.38M | ctx = std::make_shared<VExprContext>(root); |
581 | | // short path for leaf node |
582 | 7.38M | if (root_children <= 0) { |
583 | 6.69M | return Status::OK(); |
584 | 6.69M | } |
585 | | |
586 | | // non-recursive traversal |
587 | 691k | using VExprSPtrCountPair = std::pair<VExprSPtr, int>; |
588 | 691k | std::stack<std::shared_ptr<VExprSPtrCountPair>> s; |
589 | 691k | s.emplace(std::make_shared<VExprSPtrCountPair>(root, root_children)); |
590 | 2.31M | while (!s.empty()) { |
591 | | // copy the shared ptr resource to avoid dangling reference |
592 | 1.62M | auto parent = s.top(); |
593 | | // Decrement or pop |
594 | 1.62M | if (parent->second > 1) { |
595 | 786k | parent->second -= 1; |
596 | 841k | } else { |
597 | 841k | s.pop(); |
598 | 841k | } |
599 | | |
600 | 1.62M | DCHECK(parent->first != nullptr); |
601 | 1.62M | if (++*node_idx >= nodes.size()) { |
602 | 0 | return Status::InternalError("Failed to reconstruct expression tree from thrift."); |
603 | 0 | } |
604 | | |
605 | 1.62M | VExprSPtr expr; |
606 | 1.62M | RETURN_IF_ERROR(create_expr(nodes[*node_idx], expr)); |
607 | 1.62M | DCHECK(expr != nullptr); |
608 | 1.62M | parent->first->add_child(expr); |
609 | | // push to stack if has children |
610 | 1.62M | int num_children = nodes[*node_idx].num_children; |
611 | 1.62M | if (num_children > 0) { |
612 | 143k | s.emplace(std::make_shared<VExprSPtrCountPair>(expr, num_children)); |
613 | 143k | } |
614 | 1.62M | } |
615 | 691k | return Status::OK(); |
616 | 691k | } |
617 | | |
618 | 7.20M | Status VExpr::create_expr_tree(const TExpr& texpr, VExprContextSPtr& ctx) { |
619 | 7.20M | if (texpr.nodes.empty()) { |
620 | 7 | ctx = nullptr; |
621 | 7 | return Status::OK(); |
622 | 7 | } |
623 | 7.20M | int node_idx = 0; |
624 | 7.20M | VExprSPtr e; |
625 | 7.20M | Status status = create_tree_from_thrift(texpr.nodes, &node_idx, e, ctx); |
626 | 7.22M | 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 | 7.20M | 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 | 7.20M | return status; |
637 | 7.20M | } |
638 | | |
639 | 1.35M | Status VExpr::create_expr_trees(const std::vector<TExpr>& texprs, VExprContextSPtrs& ctxs) { |
640 | 1.35M | ctxs.clear(); |
641 | 6.18M | for (const auto& texpr : texprs) { |
642 | 6.18M | VExprContextSPtr ctx; |
643 | 6.18M | RETURN_IF_ERROR(create_expr_tree(texpr, ctx)); |
644 | 6.18M | ctxs.push_back(ctx); |
645 | 6.18M | } |
646 | 1.35M | return Status::OK(); |
647 | 1.35M | } |
648 | | |
649 | | Status VExpr::check_expr_output_type(const VExprContextSPtrs& ctxs, |
650 | 299k | const RowDescriptor& output_row_desc) { |
651 | 299k | if (ctxs.empty()) { |
652 | 6 | return Status::OK(); |
653 | 6 | } |
654 | 299k | auto name_and_types = VectorizedUtils::create_name_and_data_types(output_row_desc); |
655 | 299k | 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 | 2.16M | auto check_type_can_be_converted = [](DataTypePtr& from, DataTypePtr& to) -> bool { |
661 | 2.16M | if (to->equals(*from)) { |
662 | 2.16M | return true; |
663 | 2.16M | } |
664 | 1.19k | if (to->is_nullable() && !from->is_nullable()) { |
665 | 87 | return remove_nullable(to)->equals(*from); |
666 | 87 | } |
667 | 1.10k | return false; |
668 | 1.19k | }; |
669 | 2.46M | for (int i = 0; i < ctxs.size(); i++) { |
670 | 2.16M | auto real_expr_type = get_data_type_with_default_argument(ctxs[i]->root()->data_type()); |
671 | 2.16M | auto&& [name, expected_type] = name_and_types[i]; |
672 | 2.16M | 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 | 2.16M | } |
679 | 299k | return Status::OK(); |
680 | 299k | } |
681 | | |
682 | | Status VExpr::prepare(const VExprContextSPtrs& ctxs, RuntimeState* state, |
683 | 2.06M | const RowDescriptor& row_desc) { |
684 | 6.56M | for (auto ctx : ctxs) { |
685 | 6.56M | RETURN_IF_ERROR(ctx->prepare(state, row_desc)); |
686 | 6.56M | } |
687 | 2.06M | return Status::OK(); |
688 | 2.06M | } |
689 | | |
690 | 2.07M | Status VExpr::open(const VExprContextSPtrs& ctxs, RuntimeState* state) { |
691 | 6.55M | for (const auto& ctx : ctxs) { |
692 | 6.55M | RETURN_IF_ERROR(ctx->open(state)); |
693 | 6.55M | } |
694 | 2.07M | return Status::OK(); |
695 | 2.07M | } |
696 | | |
697 | 1.42M | bool VExpr::contains_blockable_function(const VExprContextSPtrs& ctxs) { |
698 | 2.59M | return std::any_of(ctxs.begin(), ctxs.end(), [](const VExprContextSPtr& ctx) { |
699 | 2.59M | return ctx != nullptr && ctx->root() != nullptr && ctx->root()->is_blockable(); |
700 | 2.59M | }); |
701 | 1.42M | } |
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 | 999 | std::string VExpr::debug_string() const { |
721 | | // TODO: implement partial debug string for member vars |
722 | 999 | std::stringstream out; |
723 | 999 | out << " type=" << _data_type->get_name(); |
724 | | |
725 | 999 | if (!_children.empty()) { |
726 | 152 | out << " children=" << debug_string(_children); |
727 | 152 | } |
728 | | |
729 | 999 | return out.str(); |
730 | 999 | } |
731 | | |
732 | 152 | std::string VExpr::debug_string(const VExprSPtrs& exprs) { |
733 | 152 | std::stringstream out; |
734 | 152 | out << "["; |
735 | | |
736 | 304 | for (int i = 0; i < exprs.size(); ++i) { |
737 | 152 | out << (i == 0 ? "" : " ") << exprs[i]->debug_string(); |
738 | 152 | } |
739 | | |
740 | 152 | out << "]"; |
741 | 152 | return out.str(); |
742 | 152 | } |
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 | 6.86M | bool VExpr::is_constant() const { |
753 | 6.86M | return std::all_of(_children.begin(), _children.end(), |
754 | 6.86M | [](const VExprSPtr& expr) { return expr->is_constant(); }); |
755 | 6.86M | } |
756 | | |
757 | | Status VExpr::get_const_col(VExprContext* context, |
758 | 10.7M | std::shared_ptr<ColumnPtrWrapper>* column_wrapper) { |
759 | 10.7M | if (!is_constant()) { |
760 | 7.44M | return Status::OK(); |
761 | 7.44M | } |
762 | | |
763 | 3.35M | if (_constant_col != nullptr) { |
764 | 1.21M | DCHECK(column_wrapper != nullptr); |
765 | 1.21M | *column_wrapper = _constant_col; |
766 | 1.21M | return Status::OK(); |
767 | 1.21M | } |
768 | | |
769 | 2.14M | ColumnPtr result; |
770 | 2.14M | RETURN_IF_ERROR(execute_column(context, nullptr, nullptr, 1, result)); |
771 | 2.14M | _constant_col = std::make_shared<ColumnPtrWrapper>(result); |
772 | 2.14M | if (column_wrapper != nullptr) { |
773 | 0 | *column_wrapper = _constant_col; |
774 | 0 | } |
775 | | |
776 | 2.14M | return Status::OK(); |
777 | 2.14M | } |
778 | | |
779 | 832k | void VExpr::register_function_context(RuntimeState* state, VExprContext* context) { |
780 | 832k | std::vector<DataTypePtr> arg_types; |
781 | 1.50M | for (auto& i : _children) { |
782 | 1.50M | arg_types.push_back(i->data_type()); |
783 | 1.50M | } |
784 | | |
785 | 832k | _fn_context_index = context->register_function_context(state, _data_type, arg_types); |
786 | 832k | } |
787 | | |
788 | | Status VExpr::init_function_context(RuntimeState* state, VExprContext* context, |
789 | | FunctionContext::FunctionStateScope scope, |
790 | 2.20M | const FunctionBasePtr& function) const { |
791 | 2.20M | FunctionContext* fn_ctx = context->fn_context(_fn_context_index); |
792 | 2.20M | if (scope == FunctionContext::FRAGMENT_LOCAL) { |
793 | 833k | std::vector<std::shared_ptr<ColumnPtrWrapper>> constant_cols; |
794 | 1.50M | for (auto c : _children) { |
795 | 1.50M | std::shared_ptr<ColumnPtrWrapper> const_col; |
796 | 1.50M | RETURN_IF_ERROR(c->get_const_col(context, &const_col)); |
797 | 1.50M | constant_cols.push_back(const_col); |
798 | 1.50M | } |
799 | 833k | fn_ctx->set_constant_cols(constant_cols); |
800 | 833k | } |
801 | | |
802 | 2.20M | if (scope == FunctionContext::FRAGMENT_LOCAL) { |
803 | 833k | RETURN_IF_ERROR(function->open(fn_ctx, FunctionContext::FRAGMENT_LOCAL)); |
804 | 833k | } |
805 | 2.20M | RETURN_IF_ERROR(function->open(fn_ctx, FunctionContext::THREAD_LOCAL)); |
806 | 2.20M | return Status::OK(); |
807 | 2.20M | } |
808 | | |
809 | | void VExpr::close_function_context(VExprContext* context, FunctionContext::FunctionStateScope scope, |
810 | 2.20M | const FunctionBasePtr& function) const { |
811 | 2.20M | if (_fn_context_index != -1) { |
812 | 2.20M | 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 | 2.20M | static_cast<void>(function->close(fn_ctx, FunctionContext::THREAD_LOCAL)); |
815 | 2.20M | if (scope == FunctionContext::FRAGMENT_LOCAL) { |
816 | 833k | static_cast<void>(function->close(fn_ctx, FunctionContext::FRAGMENT_LOCAL)); |
817 | 833k | } |
818 | 2.20M | } |
819 | 2.20M | } |
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 | 522k | uint64_t VExpr::get_digest(uint64_t seed) const { |
829 | 522k | auto digest = seed; |
830 | 1.03M | for (auto child : _children) { |
831 | 1.03M | digest = child->get_digest(digest); |
832 | 1.03M | if (digest == 0) { |
833 | 293 | return 0; |
834 | 293 | } |
835 | 1.03M | } |
836 | | |
837 | 522k | auto& fn_name = _fn.name.function_name; |
838 | 522k | if (!fn_name.empty()) { |
839 | 518k | digest = HashUtil::hash64(fn_name.c_str(), fn_name.size(), digest); |
840 | 518k | } else { |
841 | 3.95k | digest = HashUtil::hash64((const char*)&_node_type, sizeof(_node_type), digest); |
842 | 3.95k | digest = HashUtil::hash64((const char*)&_opcode, sizeof(_opcode), digest); |
843 | 3.95k | } |
844 | 522k | return digest; |
845 | 522k | } |
846 | | |
847 | 84.4k | ColumnPtr VExpr::get_result_from_const(size_t count) const { |
848 | 84.4k | return ColumnConst::create(_constant_col->column_ptr, count); |
849 | 84.4k | } |
850 | | |
851 | | Status VExpr::_evaluate_inverted_index(VExprContext* context, const FunctionBasePtr& function, |
852 | 14.9k | uint32_t segment_num_rows) { |
853 | | // Pre-allocate vectors based on an estimated or known size |
854 | 14.9k | std::vector<segment_v2::IndexIterator*> iterators; |
855 | 14.9k | std::vector<IndexFieldNameAndTypePair> data_type_with_names; |
856 | 14.9k | std::vector<int> column_ids; |
857 | 14.9k | ColumnsWithTypeAndName arguments; |
858 | 14.9k | VExprSPtrs children_exprs; |
859 | | |
860 | | // Reserve space to avoid multiple reallocations |
861 | 14.9k | const size_t estimated_size = get_num_children(); |
862 | 14.9k | iterators.reserve(estimated_size); |
863 | 14.9k | data_type_with_names.reserve(estimated_size); |
864 | 14.9k | column_ids.reserve(estimated_size); |
865 | 14.9k | children_exprs.reserve(estimated_size); |
866 | | |
867 | 14.9k | 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 | 30.6k | for (const auto& child : children()) { |
873 | 30.6k | if (child->node_type() == TExprNodeType::CAST_EXPR) { |
874 | 2.65k | auto* cast_expr = assert_cast<VCastExpr*>(child.get()); |
875 | 2.65k | DCHECK_EQ(cast_expr->get_num_children(), 1); |
876 | 2.65k | if (cast_expr->get_child(0)->is_slot_ref()) { |
877 | 2.53k | auto* column_slot_ref = assert_cast<VSlotRef*>(cast_expr->get_child(0).get()); |
878 | 2.53k | auto column_id = column_slot_ref->column_id(); |
879 | 2.53k | const auto* storage_name_type = |
880 | 2.53k | context->get_index_context()->get_storage_name_and_type_by_column_id( |
881 | 2.53k | column_id); |
882 | 2.53k | auto storage_type = remove_nullable(storage_name_type->second); |
883 | 2.53k | auto target_type = remove_nullable(cast_expr->get_target_type()); |
884 | 2.53k | auto origin_primitive_type = storage_type->get_primitive_type(); |
885 | 2.53k | auto target_primitive_type = target_type->get_primitive_type(); |
886 | 2.53k | if (is_complex_type(storage_type->get_primitive_type())) { |
887 | 342 | if (storage_type->get_primitive_type() == TYPE_ARRAY && |
888 | 342 | target_type->get_primitive_type() == TYPE_ARRAY) { |
889 | 338 | auto nested_storage_type = |
890 | 338 | (assert_cast<const DataTypeArray*>(storage_type.get())) |
891 | 338 | ->get_nested_type(); |
892 | 338 | origin_primitive_type = nested_storage_type->get_primitive_type(); |
893 | 338 | auto nested_target_type = |
894 | 338 | (assert_cast<const DataTypeArray*>(target_type.get())) |
895 | 338 | ->get_nested_type(); |
896 | 338 | target_primitive_type = nested_target_type->get_primitive_type(); |
897 | 338 | } else { |
898 | 4 | continue; |
899 | 4 | } |
900 | 342 | } |
901 | 2.53k | if (origin_primitive_type != TYPE_VARIANT && |
902 | 2.53k | (storage_type->equals(*target_type) || |
903 | 2.10k | (is_string_type(target_primitive_type) && |
904 | 1.50k | is_string_type(origin_primitive_type)))) { |
905 | 595 | children_exprs.emplace_back(expr_without_cast(child)); |
906 | 595 | } |
907 | 2.53k | } else { |
908 | 118 | return Status::OK(); // for example: cast("abc") as ipv4 case |
909 | 118 | } |
910 | 28.0k | } else { |
911 | 28.0k | children_exprs.emplace_back(child); |
912 | 28.0k | } |
913 | 30.6k | } |
914 | | |
915 | 14.8k | if (children_exprs.empty()) { |
916 | 156 | return Status::OK(); // Early exit if no children to process |
917 | 156 | } |
918 | | |
919 | 27.4k | for (const auto& child : children_exprs) { |
920 | 27.4k | if (child->is_slot_ref()) { |
921 | 12.3k | auto* column_slot_ref = assert_cast<VSlotRef*>(child.get()); |
922 | 12.3k | auto column_id = column_slot_ref->column_id(); |
923 | 12.3k | auto* iter = context->get_index_context()->get_inverted_index_iterator_by_column_id( |
924 | 12.3k | column_id); |
925 | | //column does not have inverted index |
926 | 12.3k | if (iter == nullptr) { |
927 | 2.44k | continue; |
928 | 2.44k | } |
929 | 9.86k | const auto* storage_name_type = |
930 | 9.86k | context->get_index_context()->get_storage_name_and_type_by_column_id(column_id); |
931 | 9.86k | 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 | 9.86k | iterators.emplace_back(iter); |
940 | 9.86k | data_type_with_names.emplace_back(*storage_name_type); |
941 | 9.86k | column_ids.emplace_back(column_id); |
942 | 15.1k | } else if (child->is_literal()) { |
943 | 13.9k | auto* column_literal = assert_cast<VLiteral*>(child.get()); |
944 | 13.9k | arguments.emplace_back(column_literal->get_column_ptr(), |
945 | 13.9k | column_literal->get_data_type(), column_literal->expr_name()); |
946 | 13.9k | } else if (child->can_push_down_to_index()) { |
947 | 0 | RETURN_IF_ERROR(child->evaluate_inverted_index(context, segment_num_rows)); |
948 | 1.18k | } else { |
949 | 1.18k | return Status::OK(); // others cases |
950 | 1.18k | } |
951 | 27.4k | } |
952 | | |
953 | | // is null or is not null has no arguments |
954 | 13.4k | if (iterators.empty() || (arguments.empty() && !(function->get_name() == "is_not_null_pred" || |
955 | 4.06k | function->get_name() == "is_null_pred"))) { |
956 | 4.06k | return Status::OK(); // Nothing to evaluate or no literals to compare against |
957 | 4.06k | } |
958 | | |
959 | 9.42k | const InvertedIndexAnalyzerCtx* analyzer_ctx = nullptr; |
960 | 9.57k | if (auto index_ctx = context->get_index_context(); index_ctx != nullptr) { |
961 | 9.57k | analyzer_ctx = index_ctx->get_analyzer_ctx_for_expr(this); |
962 | 9.57k | } |
963 | | |
964 | 9.42k | auto result_bitmap = segment_v2::InvertedIndexResultBitmap(); |
965 | | // Pass analyzer_key to function (used by match predicates for multi-analyzer index selection) |
966 | 9.42k | auto res = function->evaluate_inverted_index(arguments, data_type_with_names, iterators, |
967 | 9.42k | segment_num_rows, analyzer_ctx, result_bitmap); |
968 | 9.42k | if (!res.ok()) { |
969 | 324 | return res; |
970 | 324 | } |
971 | 9.10k | if (!result_bitmap.is_empty()) { |
972 | 7.45k | index_context->set_index_result_for_expr(this, result_bitmap); |
973 | 7.54k | for (int column_id : column_ids) { |
974 | 7.54k | index_context->set_true_for_index_status(this, column_id); |
975 | 7.54k | } |
976 | 7.45k | } |
977 | 9.10k | return Status::OK(); |
978 | 9.42k | } |
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 | 501k | ColumnPtr& result_column) const { |
1000 | 501k | if (context->get_index_context() && |
1001 | 501k | context->get_index_context()->get_index_result_column().contains(this)) { |
1002 | | // prepare a column to save result |
1003 | 1.02k | result_column = filter_column_with_selector( |
1004 | 1.02k | context->get_index_context()->get_index_result_column()[this], selector, count); |
1005 | 1.02k | if (_data_type->is_nullable()) { |
1006 | 820 | result_column = make_nullable(result_column); |
1007 | 820 | } |
1008 | 1.02k | return true; |
1009 | 1.02k | } |
1010 | 500k | return false; |
1011 | 501k | } |
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 | 5.14k | roaring::Roaring& row_bitmap, AnnIndexStats& ann_index_stats) { |
1023 | 5.14k | return Status::OK(); |
1024 | 5.14k | } |
1025 | | |
1026 | | void VExpr::prepare_ann_range_search(const doris::VectorSearchUserParams& params, |
1027 | | segment_v2::AnnRangeSearchRuntime& range_search_runtime, |
1028 | 18.6k | bool& suitable_for_ann_index) { |
1029 | 18.6k | if (!suitable_for_ann_index) { |
1030 | 0 | return; |
1031 | 0 | } |
1032 | 18.6k | for (auto& child : _children) { |
1033 | 12.0k | child->prepare_ann_range_search(params, range_search_runtime, suitable_for_ann_index); |
1034 | 12.0k | if (!suitable_for_ann_index) { |
1035 | 1 | return; |
1036 | 1 | } |
1037 | 12.0k | } |
1038 | 18.6k | } |
1039 | | |
1040 | 26.9k | bool VExpr::ann_range_search_executedd() { |
1041 | 26.9k | return _has_been_executed; |
1042 | 26.9k | } |
1043 | | |
1044 | 53 | bool VExpr::ann_dist_is_fulfilled() const { |
1045 | 53 | return _virtual_column_is_fulfilled; |
1046 | 53 | } |
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 | 161k | bool* can_filter_all) const { |
1051 | 161k | ColumnPtr filter_column; |
1052 | 161k | RETURN_IF_ERROR(execute_column(context, block, nullptr, rows, filter_column)); |
1053 | 161k | if (const auto* const_column = check_and_get_column<ColumnConst>(*filter_column)) { |
1054 | | // const(nullable) or const(bool) |
1055 | 18.4k | const bool result = accept_null |
1056 | 18.4k | ? (const_column->is_null_at(0) || const_column->get_bool(0)) |
1057 | 18.4k | : (!const_column->is_null_at(0) && const_column->get_bool(0)); |
1058 | 18.4k | if (!result) { |
1059 | | // filter all |
1060 | 2.07k | *can_filter_all = true; |
1061 | 2.07k | memset(result_filter_data, 0, rows); |
1062 | 2.07k | return Status::OK(); |
1063 | 2.07k | } |
1064 | 142k | } else if (const auto* nullable_column = check_and_get_column<ColumnNullable>(*filter_column)) { |
1065 | | // nullable(bool) |
1066 | 102k | const ColumnPtr& nested_column = nullable_column->get_nested_column_ptr(); |
1067 | 102k | const IColumn::Filter& filter = assert_cast<const ColumnUInt8&>(*nested_column).get_data(); |
1068 | 102k | const auto* __restrict filter_data = filter.data(); |
1069 | 102k | const auto* __restrict null_map_data = nullable_column->get_null_map_data().data(); |
1070 | | |
1071 | 102k | if (accept_null) { |
1072 | 1.01k | for (size_t i = 0; i < rows; ++i) { |
1073 | 1.00k | result_filter_data[i] &= (null_map_data[i]) || filter_data[i]; |
1074 | 1.00k | } |
1075 | 102k | } else { |
1076 | 170M | for (size_t i = 0; i < rows; ++i) { |
1077 | 170M | result_filter_data[i] &= (!null_map_data[i]) & filter_data[i]; |
1078 | 170M | } |
1079 | 102k | } |
1080 | | |
1081 | 102k | if (!simd::contain_one(result_filter_data, rows)) { |
1082 | 28.5k | *can_filter_all = true; |
1083 | 28.5k | return Status::OK(); |
1084 | 28.5k | } |
1085 | 102k | } else { |
1086 | | // bool |
1087 | 40.6k | const IColumn::Filter& filter = assert_cast<const ColumnUInt8&>(*filter_column).get_data(); |
1088 | 40.6k | const auto* __restrict filter_data = filter.data(); |
1089 | | |
1090 | 72.2M | for (size_t i = 0; i < rows; ++i) { |
1091 | 72.1M | result_filter_data[i] &= filter_data[i]; |
1092 | 72.1M | } |
1093 | | |
1094 | 40.6k | if (!simd::contain_one(result_filter_data, rows)) { |
1095 | 5.57k | *can_filter_all = true; |
1096 | 5.57k | return Status::OK(); |
1097 | 5.57k | } |
1098 | 40.6k | } |
1099 | | |
1100 | 125k | return Status::OK(); |
1101 | 161k | } |
1102 | | |
1103 | | } // namespace doris |