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