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