/root/doris/be/src/vec/exprs/vexpr.cpp
Line | Count | Source (jump to first uncovered line) |
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 "vec/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 | | |
32 | | #include "common/config.h" |
33 | | #include "common/exception.h" |
34 | | #include "common/status.h" |
35 | | #include "pipeline/pipeline_task.h" |
36 | | #include "runtime/define_primitive_type.h" |
37 | | #include "vec/columns/column_vector.h" |
38 | | #include "vec/columns/columns_number.h" |
39 | | #include "vec/data_types/data_type_array.h" |
40 | | #include "vec/data_types/data_type_factory.hpp" |
41 | | #include "vec/data_types/data_type_nullable.h" |
42 | | #include "vec/data_types/data_type_number.h" |
43 | | #include "vec/exprs/varray_literal.h" |
44 | | #include "vec/exprs/vcase_expr.h" |
45 | | #include "vec/exprs/vcast_expr.h" |
46 | | #include "vec/exprs/vcolumn_ref.h" |
47 | | #include "vec/exprs/vcompound_pred.h" |
48 | | #include "vec/exprs/vectorized_fn_call.h" |
49 | | #include "vec/exprs/vexpr_context.h" |
50 | | #include "vec/exprs/vin_predicate.h" |
51 | | #include "vec/exprs/vinfo_func.h" |
52 | | #include "vec/exprs/vlambda_function_call_expr.h" |
53 | | #include "vec/exprs/vlambda_function_expr.h" |
54 | | #include "vec/exprs/vliteral.h" |
55 | | #include "vec/exprs/vmap_literal.h" |
56 | | #include "vec/exprs/vmatch_predicate.h" |
57 | | #include "vec/exprs/vslot_ref.h" |
58 | | #include "vec/exprs/vstruct_literal.h" |
59 | | #include "vec/exprs/vtuple_is_null_predicate.h" |
60 | | #include "vec/utils/util.hpp" |
61 | | |
62 | | namespace doris { |
63 | | #include "common/compile_check_begin.h" |
64 | | |
65 | | class RowDescriptor; |
66 | | class RuntimeState; |
67 | | |
68 | | // NOLINTBEGIN(readability-function-cognitive-complexity) |
69 | | // NOLINTBEGIN(readability-function-size) |
70 | | TExprNode create_texpr_node_from(const void* data, const PrimitiveType& type, int precision, |
71 | 0 | int scale) { |
72 | 0 | TExprNode node; |
73 | |
|
74 | 0 | switch (type) { |
75 | 0 | case TYPE_BOOLEAN: { |
76 | 0 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_BOOLEAN>(data, &node)); |
77 | 0 | break; |
78 | 0 | } |
79 | 0 | case TYPE_TINYINT: { |
80 | 0 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_TINYINT>(data, &node)); |
81 | 0 | break; |
82 | 0 | } |
83 | 0 | case TYPE_SMALLINT: { |
84 | 0 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_SMALLINT>(data, &node)); |
85 | 0 | break; |
86 | 0 | } |
87 | 0 | case TYPE_INT: { |
88 | 0 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_INT>(data, &node)); |
89 | 0 | break; |
90 | 0 | } |
91 | 0 | case TYPE_BIGINT: { |
92 | 0 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_BIGINT>(data, &node)); |
93 | 0 | break; |
94 | 0 | } |
95 | 0 | case TYPE_LARGEINT: { |
96 | 0 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_LARGEINT>(data, &node)); |
97 | 0 | break; |
98 | 0 | } |
99 | 0 | case TYPE_FLOAT: { |
100 | 0 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_FLOAT>(data, &node)); |
101 | 0 | break; |
102 | 0 | } |
103 | 0 | case TYPE_DOUBLE: { |
104 | 0 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_DOUBLE>(data, &node)); |
105 | 0 | break; |
106 | 0 | } |
107 | 0 | case TYPE_DATEV2: { |
108 | 0 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_DATEV2>(data, &node)); |
109 | 0 | break; |
110 | 0 | } |
111 | 0 | case TYPE_DATETIMEV2: { |
112 | 0 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_DATETIMEV2>(data, &node, precision, scale)); |
113 | 0 | break; |
114 | 0 | } |
115 | 0 | case TYPE_DATE: { |
116 | 0 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_DATE>(data, &node)); |
117 | 0 | break; |
118 | 0 | } |
119 | 0 | case TYPE_DATETIME: { |
120 | 0 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_DATETIME>(data, &node)); |
121 | 0 | break; |
122 | 0 | } |
123 | 0 | case TYPE_DECIMALV2: { |
124 | 0 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_DECIMALV2>(data, &node, precision, scale)); |
125 | 0 | break; |
126 | 0 | } |
127 | 0 | case TYPE_DECIMAL32: { |
128 | 0 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_DECIMAL32>(data, &node, precision, scale)); |
129 | 0 | break; |
130 | 0 | } |
131 | 0 | case TYPE_DECIMAL64: { |
132 | 0 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_DECIMAL64>(data, &node, precision, scale)); |
133 | 0 | break; |
134 | 0 | } |
135 | 0 | case TYPE_DECIMAL128I: { |
136 | 0 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_DECIMAL128I>(data, &node, precision, scale)); |
137 | 0 | break; |
138 | 0 | } |
139 | 0 | case TYPE_DECIMAL256: { |
140 | 0 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_DECIMAL256>(data, &node, precision, scale)); |
141 | 0 | break; |
142 | 0 | } |
143 | 0 | case TYPE_CHAR: { |
144 | 0 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_CHAR>(data, &node)); |
145 | 0 | break; |
146 | 0 | } |
147 | 0 | case TYPE_VARCHAR: { |
148 | 0 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_VARCHAR>(data, &node)); |
149 | 0 | break; |
150 | 0 | } |
151 | 0 | case TYPE_STRING: { |
152 | 0 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_STRING>(data, &node)); |
153 | 0 | break; |
154 | 0 | } |
155 | 0 | case TYPE_IPV4: { |
156 | 0 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_IPV4>(data, &node)); |
157 | 0 | break; |
158 | 0 | } |
159 | 0 | case TYPE_IPV6: { |
160 | 0 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_IPV6>(data, &node)); |
161 | 0 | break; |
162 | 0 | } |
163 | 0 | default: |
164 | 0 | throw Exception(ErrorCode::INTERNAL_ERROR, "runtime filter meet invalid type {}", |
165 | 0 | int(type)); |
166 | 0 | } |
167 | 0 | return node; |
168 | 0 | } |
169 | | // NOLINTEND(readability-function-size) |
170 | | // NOLINTEND(readability-function-cognitive-complexity) |
171 | | } // namespace doris |
172 | | |
173 | | namespace doris::vectorized { |
174 | | |
175 | 0 | bool VExpr::is_acting_on_a_slot(const VExpr& expr) { |
176 | 0 | const auto& children = expr.children(); |
177 | |
|
178 | 0 | auto is_a_slot = std::any_of(children.begin(), children.end(), |
179 | 0 | [](const auto& child) { return is_acting_on_a_slot(*child); }); |
180 | |
|
181 | 0 | return is_a_slot ? true : (expr.node_type() == TExprNodeType::SLOT_REF); |
182 | 0 | } |
183 | | |
184 | | VExpr::VExpr(const TExprNode& node) |
185 | | : _node_type(node.node_type), |
186 | | _opcode(node.__isset.opcode ? node.opcode : TExprOpcode::INVALID_OPCODE), |
187 | 26 | _type(TypeDescriptor::from_thrift(node.type)) { |
188 | 26 | if (node.__isset.fn) { |
189 | 2 | _fn = node.fn; |
190 | 2 | } |
191 | | |
192 | 26 | bool is_nullable = true; |
193 | 26 | if (node.__isset.is_nullable) { |
194 | 0 | is_nullable = node.is_nullable; |
195 | 0 | } |
196 | | // If we define null literal ,should make nullable data type to get correct field instead of undefined ptr |
197 | 26 | if (node.node_type == TExprNodeType::NULL_LITERAL) { |
198 | 0 | CHECK(is_nullable); |
199 | 0 | } |
200 | 26 | _data_type = DataTypeFactory::instance().create_data_type(_type, is_nullable); |
201 | 26 | } |
202 | | |
203 | 0 | VExpr::VExpr(const VExpr& vexpr) = default; |
204 | | |
205 | | VExpr::VExpr(TypeDescriptor type, bool is_slotref, bool is_nullable) |
206 | 0 | : _opcode(TExprOpcode::INVALID_OPCODE), _type(std::move(type)) { |
207 | 0 | if (is_slotref) { |
208 | 0 | _node_type = TExprNodeType::SLOT_REF; |
209 | 0 | } |
210 | |
|
211 | 0 | _data_type = DataTypeFactory::instance().create_data_type(_type, is_nullable); |
212 | 0 | } |
213 | | |
214 | 12 | Status VExpr::prepare(RuntimeState* state, const RowDescriptor& row_desc, VExprContext* context) { |
215 | 12 | ++context->_depth_num; |
216 | 12 | if (context->_depth_num > config::max_depth_of_expr_tree) { |
217 | 0 | return Status::Error<ErrorCode::EXCEEDED_LIMIT>( |
218 | 0 | "The depth of the expression tree is too big, make it less than {}", |
219 | 0 | config::max_depth_of_expr_tree); |
220 | 0 | } |
221 | | |
222 | 12 | for (auto& i : _children) { |
223 | 2 | RETURN_IF_ERROR(i->prepare(state, row_desc, context)); |
224 | 2 | } |
225 | 12 | --context->_depth_num; |
226 | 12 | _enable_inverted_index_query = state->query_options().enable_inverted_index_query; |
227 | 12 | return Status::OK(); |
228 | 12 | } |
229 | | |
230 | | Status VExpr::open(RuntimeState* state, VExprContext* context, |
231 | 2 | FunctionContext::FunctionStateScope scope) { |
232 | 2 | for (auto& i : _children) { |
233 | 0 | RETURN_IF_ERROR(i->open(state, context, scope)); |
234 | 0 | } |
235 | 2 | if (scope == FunctionContext::FRAGMENT_LOCAL) { |
236 | 2 | RETURN_IF_ERROR(VExpr::get_const_col(context, nullptr)); |
237 | 2 | } |
238 | 2 | return Status::OK(); |
239 | 2 | } |
240 | | |
241 | 4 | void VExpr::close(VExprContext* context, FunctionContext::FunctionStateScope scope) { |
242 | 4 | for (auto& i : _children) { |
243 | 2 | i->close(context, scope); |
244 | 2 | } |
245 | 4 | } |
246 | | |
247 | | // NOLINTBEGIN(readability-function-size) |
248 | 12 | Status VExpr::create_expr(const TExprNode& expr_node, VExprSPtr& expr) { |
249 | 12 | try { |
250 | 12 | switch (expr_node.node_type) { |
251 | 0 | case TExprNodeType::BOOL_LITERAL: |
252 | 0 | case TExprNodeType::INT_LITERAL: |
253 | 0 | case TExprNodeType::LARGE_INT_LITERAL: |
254 | 0 | case TExprNodeType::IPV4_LITERAL: |
255 | 0 | case TExprNodeType::IPV6_LITERAL: |
256 | 0 | case TExprNodeType::FLOAT_LITERAL: |
257 | 0 | case TExprNodeType::DECIMAL_LITERAL: |
258 | 0 | case TExprNodeType::DATE_LITERAL: |
259 | 0 | case TExprNodeType::STRING_LITERAL: |
260 | 0 | case TExprNodeType::JSON_LITERAL: |
261 | 0 | case TExprNodeType::NULL_LITERAL: { |
262 | 0 | expr = VLiteral::create_shared(expr_node); |
263 | 0 | break; |
264 | 0 | } |
265 | 0 | case TExprNodeType::ARRAY_LITERAL: { |
266 | 0 | expr = VArrayLiteral::create_shared(expr_node); |
267 | 0 | break; |
268 | 0 | } |
269 | 0 | case TExprNodeType::MAP_LITERAL: { |
270 | 0 | expr = VMapLiteral::create_shared(expr_node); |
271 | 0 | break; |
272 | 0 | } |
273 | 0 | case TExprNodeType::STRUCT_LITERAL: { |
274 | 0 | expr = VStructLiteral::create_shared(expr_node); |
275 | 0 | break; |
276 | 0 | } |
277 | 10 | case TExprNodeType::SLOT_REF: { |
278 | 10 | expr = VSlotRef::create_shared(expr_node); |
279 | 10 | break; |
280 | 0 | } |
281 | 0 | case TExprNodeType::COLUMN_REF: { |
282 | 0 | expr = VColumnRef::create_shared(expr_node); |
283 | 0 | break; |
284 | 0 | } |
285 | 0 | case TExprNodeType::COMPOUND_PRED: { |
286 | 0 | expr = VCompoundPred::create_shared(expr_node); |
287 | 0 | break; |
288 | 0 | } |
289 | 0 | case TExprNodeType::LAMBDA_FUNCTION_EXPR: { |
290 | 0 | expr = VLambdaFunctionExpr::create_shared(expr_node); |
291 | 0 | break; |
292 | 0 | } |
293 | 0 | case TExprNodeType::LAMBDA_FUNCTION_CALL_EXPR: { |
294 | 0 | expr = VLambdaFunctionCallExpr::create_shared(expr_node); |
295 | 0 | break; |
296 | 0 | } |
297 | 0 | case TExprNodeType::ARITHMETIC_EXPR: |
298 | 0 | case TExprNodeType::BINARY_PRED: |
299 | 0 | case TExprNodeType::NULL_AWARE_BINARY_PRED: |
300 | 2 | case TExprNodeType::FUNCTION_CALL: |
301 | 2 | case TExprNodeType::COMPUTE_FUNCTION_CALL: { |
302 | 2 | expr = VectorizedFnCall::create_shared(expr_node); |
303 | 2 | break; |
304 | 2 | } |
305 | 0 | case TExprNodeType::MATCH_PRED: { |
306 | 0 | expr = VMatchPredicate::create_shared(expr_node); |
307 | 0 | break; |
308 | 2 | } |
309 | 0 | case TExprNodeType::CAST_EXPR: { |
310 | 0 | expr = VCastExpr::create_shared(expr_node); |
311 | 0 | break; |
312 | 2 | } |
313 | 0 | case TExprNodeType::IN_PRED: { |
314 | 0 | expr = VInPredicate::create_shared(expr_node); |
315 | 0 | break; |
316 | 2 | } |
317 | 0 | case TExprNodeType::CASE_EXPR: { |
318 | 0 | if (!expr_node.__isset.case_expr) { |
319 | 0 | return Status::InternalError("Case expression not set in thrift node"); |
320 | 0 | } |
321 | 0 | expr = VCaseExpr::create_shared(expr_node); |
322 | 0 | break; |
323 | 0 | } |
324 | 0 | case TExprNodeType::INFO_FUNC: { |
325 | 0 | expr = VInfoFunc::create_shared(expr_node); |
326 | 0 | break; |
327 | 0 | } |
328 | 0 | case TExprNodeType::TUPLE_IS_NULL_PRED: { |
329 | 0 | expr = VTupleIsNullPredicate::create_shared(expr_node); |
330 | 0 | break; |
331 | 0 | } |
332 | 0 | default: |
333 | 0 | return Status::InternalError("Unknown expr node type: {}", expr_node.node_type); |
334 | 12 | } |
335 | 12 | } catch (const Exception& e) { |
336 | 0 | if (e.code() == ErrorCode::INTERNAL_ERROR) { |
337 | 0 | return Status::InternalError("Create Expr failed because {}\nTExprNode={}", e.what(), |
338 | 0 | apache::thrift::ThriftDebugString(expr_node)); |
339 | 0 | } |
340 | 0 | return Status::Error<false>(e.code(), "Create Expr failed because {}", e.what()); |
341 | 0 | LOG(WARNING) << "create expr failed, TExprNode={}, reason={}" |
342 | 0 | << apache::thrift::ThriftDebugString(expr_node) << e.what(); |
343 | 0 | } |
344 | 12 | if (!expr->data_type()) { |
345 | 0 | return Status::InvalidArgument("Unknown expr type: {}", expr_node.node_type); |
346 | 0 | } |
347 | 12 | return Status::OK(); |
348 | 12 | } |
349 | | // NOLINTEND(readability-function-size) |
350 | | |
351 | | Status VExpr::create_tree_from_thrift(const std::vector<TExprNode>& nodes, int* node_idx, |
352 | 10 | VExprSPtr& root_expr, VExprContextSPtr& ctx) { |
353 | | // propagate error case |
354 | 10 | if (*node_idx >= nodes.size()) { |
355 | 0 | return Status::InternalError("Failed to reconstruct expression tree from thrift."); |
356 | 0 | } |
357 | | |
358 | | // create root expr |
359 | 10 | int root_children = nodes[*node_idx].num_children; |
360 | 10 | VExprSPtr root; |
361 | 10 | RETURN_IF_ERROR(create_expr(nodes[*node_idx], root)); |
362 | 10 | DCHECK(root != nullptr); |
363 | 10 | root_expr = root; |
364 | 10 | ctx = std::make_shared<VExprContext>(root); |
365 | | // short path for leaf node |
366 | 10 | if (root_children <= 0) { |
367 | 8 | return Status::OK(); |
368 | 8 | } |
369 | | |
370 | | // non-recursive traversal |
371 | 2 | std::stack<std::pair<VExprSPtr, int>> s; |
372 | 2 | s.emplace(root, root_children); |
373 | 4 | while (!s.empty()) { |
374 | 2 | auto& parent = s.top(); |
375 | 2 | if (parent.second > 1) { |
376 | 0 | parent.second -= 1; |
377 | 2 | } else { |
378 | 2 | s.pop(); |
379 | 2 | } |
380 | | |
381 | 2 | if (++*node_idx >= nodes.size()) { |
382 | 0 | return Status::InternalError("Failed to reconstruct expression tree from thrift."); |
383 | 0 | } |
384 | 2 | VExprSPtr expr; |
385 | 2 | RETURN_IF_ERROR(create_expr(nodes[*node_idx], expr)); |
386 | 2 | DCHECK(expr != nullptr); |
387 | 2 | parent.first->add_child(expr); |
388 | 2 | int num_children = nodes[*node_idx].num_children; |
389 | 2 | if (num_children > 0) { |
390 | 0 | s.emplace(expr, num_children); |
391 | 0 | } |
392 | 2 | } |
393 | 2 | return Status::OK(); |
394 | 2 | } |
395 | | |
396 | 10 | Status VExpr::create_expr_tree(const TExpr& texpr, VExprContextSPtr& ctx) { |
397 | 10 | if (texpr.nodes.empty()) { |
398 | 0 | ctx = nullptr; |
399 | 0 | return Status::OK(); |
400 | 0 | } |
401 | 10 | int node_idx = 0; |
402 | 10 | VExprSPtr e; |
403 | 10 | Status status = create_tree_from_thrift(texpr.nodes, &node_idx, e, ctx); |
404 | 10 | if (status.ok() && node_idx + 1 != texpr.nodes.size()) { |
405 | 0 | status = Status::InternalError( |
406 | 0 | "Expression tree only partially reconstructed. Not all thrift nodes were " |
407 | 0 | "used."); |
408 | 0 | } |
409 | 10 | if (!status.ok()) { |
410 | 0 | LOG(ERROR) << "Could not construct expr tree.\n" |
411 | 0 | << status << "\n" |
412 | 0 | << apache::thrift::ThriftDebugString(texpr); |
413 | 0 | } |
414 | 10 | return status; |
415 | 10 | } |
416 | | |
417 | 0 | Status VExpr::create_expr_trees(const std::vector<TExpr>& texprs, VExprContextSPtrs& ctxs) { |
418 | 0 | ctxs.clear(); |
419 | 0 | for (const auto& texpr : texprs) { |
420 | 0 | VExprContextSPtr ctx; |
421 | 0 | RETURN_IF_ERROR(create_expr_tree(texpr, ctx)); |
422 | 0 | ctxs.push_back(ctx); |
423 | 0 | } |
424 | 0 | return Status::OK(); |
425 | 0 | } |
426 | | |
427 | | Status VExpr::check_expr_output_type(const VExprContextSPtrs& ctxs, |
428 | 0 | const RowDescriptor& output_row_desc) { |
429 | 0 | if (ctxs.empty()) { |
430 | 0 | return Status::OK(); |
431 | 0 | } |
432 | 0 | auto name_and_types = VectorizedUtils::create_name_and_data_types(output_row_desc); |
433 | 0 | if (ctxs.size() != name_and_types.size()) { |
434 | 0 | return Status::InternalError( |
435 | 0 | "output type size not match expr size {} , expected output size {} ", ctxs.size(), |
436 | 0 | name_and_types.size()); |
437 | 0 | } |
438 | 0 | auto check_type_can_be_converted = [](DataTypePtr& from, DataTypePtr& to) -> bool { |
439 | 0 | if (to->equals(*from)) { |
440 | 0 | return true; |
441 | 0 | } |
442 | 0 | if (to->is_nullable() && !from->is_nullable()) { |
443 | 0 | return remove_nullable(to)->equals(*from); |
444 | 0 | } |
445 | 0 | return false; |
446 | 0 | }; |
447 | 0 | for (int i = 0; i < ctxs.size(); i++) { |
448 | 0 | auto real_expr_type = ctxs[i]->root()->data_type(); |
449 | 0 | auto&& [name, expected_type] = name_and_types[i]; |
450 | 0 | if (!check_type_can_be_converted(real_expr_type, expected_type)) { |
451 | 0 | return Status::InternalError( |
452 | 0 | "output type not match expr type , col name {} , expected type {} , real type " |
453 | 0 | "{}", |
454 | 0 | name, expected_type->get_name(), real_expr_type->get_name()); |
455 | 0 | } |
456 | 0 | } |
457 | 0 | return Status::OK(); |
458 | 0 | } |
459 | | |
460 | | Status VExpr::prepare(const VExprContextSPtrs& ctxs, RuntimeState* state, |
461 | 3 | const RowDescriptor& row_desc) { |
462 | 3 | for (auto ctx : ctxs) { |
463 | 0 | RETURN_IF_ERROR(ctx->prepare(state, row_desc)); |
464 | 0 | } |
465 | 3 | return Status::OK(); |
466 | 3 | } |
467 | | |
468 | 3 | Status VExpr::open(const VExprContextSPtrs& ctxs, RuntimeState* state) { |
469 | 3 | for (const auto& ctx : ctxs) { |
470 | 0 | RETURN_IF_ERROR(ctx->open(state)); |
471 | 0 | } |
472 | 3 | return Status::OK(); |
473 | 3 | } |
474 | | |
475 | | Status VExpr::clone_if_not_exists(const VExprContextSPtrs& ctxs, RuntimeState* state, |
476 | 0 | VExprContextSPtrs& new_ctxs) { |
477 | 0 | if (!new_ctxs.empty()) { |
478 | | // 'ctxs' was already cloned into '*new_ctxs', nothing to do. |
479 | 0 | DCHECK_EQ(new_ctxs.size(), ctxs.size()); |
480 | 0 | for (auto& new_ctx : new_ctxs) { |
481 | 0 | DCHECK(new_ctx->_is_clone); |
482 | 0 | } |
483 | 0 | return Status::OK(); |
484 | 0 | } |
485 | 0 | new_ctxs.resize(ctxs.size()); |
486 | 0 | for (int i = 0; i < ctxs.size(); ++i) { |
487 | 0 | RETURN_IF_ERROR(ctxs[i]->clone(state, new_ctxs[i])); |
488 | 0 | } |
489 | 0 | return Status::OK(); |
490 | 0 | } |
491 | | |
492 | 0 | std::string VExpr::debug_string() const { |
493 | | // TODO: implement partial debug string for member vars |
494 | 0 | std::stringstream out; |
495 | 0 | out << " type=" << _type.debug_string(); |
496 | |
|
497 | 0 | if (!_children.empty()) { |
498 | 0 | out << " children=" << debug_string(_children); |
499 | 0 | } |
500 | |
|
501 | 0 | return out.str(); |
502 | 0 | } |
503 | | |
504 | 0 | std::string VExpr::debug_string(const VExprSPtrs& exprs) { |
505 | 0 | std::stringstream out; |
506 | 0 | out << "["; |
507 | |
|
508 | 0 | for (int i = 0; i < exprs.size(); ++i) { |
509 | 0 | out << (i == 0 ? "" : " ") << exprs[i]->debug_string(); |
510 | 0 | } |
511 | |
|
512 | 0 | out << "]"; |
513 | 0 | return out.str(); |
514 | 0 | } |
515 | | |
516 | 0 | std::string VExpr::debug_string(const VExprContextSPtrs& ctxs) { |
517 | 0 | VExprSPtrs exprs; |
518 | 0 | for (const auto& ctx : ctxs) { |
519 | 0 | exprs.push_back(ctx->root()); |
520 | 0 | } |
521 | 0 | return debug_string(exprs); |
522 | 0 | } |
523 | | |
524 | 2 | bool VExpr::is_constant() const { |
525 | 2 | return std::all_of(_children.begin(), _children.end(), |
526 | 2 | [](const VExprSPtr& expr) { return expr->is_constant(); }); |
527 | 2 | } |
528 | | |
529 | | Status VExpr::get_const_col(VExprContext* context, |
530 | 6 | std::shared_ptr<ColumnPtrWrapper>* column_wrapper) { |
531 | 6 | if (!is_constant()) { |
532 | 6 | return Status::OK(); |
533 | 6 | } |
534 | | |
535 | 0 | if (_constant_col != nullptr) { |
536 | 0 | DCHECK(column_wrapper != nullptr); |
537 | 0 | *column_wrapper = _constant_col; |
538 | 0 | return Status::OK(); |
539 | 0 | } |
540 | | |
541 | 0 | int result = -1; |
542 | 0 | Block block; |
543 | | // If block is empty, some functions will produce no result. So we insert a column with |
544 | | // single value here. |
545 | 0 | block.insert({ColumnUInt8::create(1), std::make_shared<DataTypeUInt8>(), ""}); |
546 | |
|
547 | 0 | _getting_const_col = true; |
548 | 0 | RETURN_IF_ERROR(execute(context, &block, &result)); |
549 | 0 | _getting_const_col = false; |
550 | |
|
551 | 0 | DCHECK(result != -1); |
552 | 0 | const auto& column = block.get_by_position(result).column; |
553 | 0 | _constant_col = std::make_shared<ColumnPtrWrapper>(column); |
554 | 0 | if (column_wrapper != nullptr) { |
555 | 0 | *column_wrapper = _constant_col; |
556 | 0 | } |
557 | |
|
558 | 0 | return Status::OK(); |
559 | 0 | } |
560 | | |
561 | 2 | void VExpr::register_function_context(RuntimeState* state, VExprContext* context) { |
562 | 2 | std::vector<TypeDescriptor> arg_types; |
563 | 2 | for (auto& i : _children) { |
564 | 2 | arg_types.push_back(i->type()); |
565 | 2 | } |
566 | | |
567 | 2 | _fn_context_index = context->register_function_context(state, _type, arg_types); |
568 | 2 | } |
569 | | |
570 | | Status VExpr::init_function_context(RuntimeState* state, VExprContext* context, |
571 | | FunctionContext::FunctionStateScope scope, |
572 | 2 | const FunctionBasePtr& function) const { |
573 | 2 | FunctionContext* fn_ctx = context->fn_context(_fn_context_index); |
574 | 2 | if (scope == FunctionContext::FRAGMENT_LOCAL) { |
575 | 2 | std::vector<std::shared_ptr<ColumnPtrWrapper>> constant_cols; |
576 | 2 | for (auto c : _children) { |
577 | 2 | std::shared_ptr<ColumnPtrWrapper> const_col; |
578 | 2 | RETURN_IF_ERROR(c->get_const_col(context, &const_col)); |
579 | 2 | constant_cols.push_back(const_col); |
580 | 2 | } |
581 | 2 | fn_ctx->set_constant_cols(constant_cols); |
582 | 2 | } else { |
583 | 0 | if (function->is_udf_function()) { |
584 | 0 | auto* timer = ADD_TIMER(state->get_task()->get_task_profile(), |
585 | 0 | "UDF[" + function->get_name() + "]"); |
586 | 0 | fn_ctx->set_udf_execute_timer(timer); |
587 | 0 | } |
588 | 0 | } |
589 | | |
590 | 2 | if (scope == FunctionContext::FRAGMENT_LOCAL) { |
591 | 2 | RETURN_IF_ERROR(function->open(fn_ctx, FunctionContext::FRAGMENT_LOCAL)); |
592 | 2 | } |
593 | 2 | RETURN_IF_ERROR(function->open(fn_ctx, FunctionContext::THREAD_LOCAL)); |
594 | 2 | return Status::OK(); |
595 | 2 | } |
596 | | |
597 | | void VExpr::close_function_context(VExprContext* context, FunctionContext::FunctionStateScope scope, |
598 | 2 | const FunctionBasePtr& function) const { |
599 | 2 | if (_fn_context_index != -1) { |
600 | 2 | FunctionContext* fn_ctx = context->fn_context(_fn_context_index); |
601 | | // `close_function_context` is called in VExprContext's destructor so do not throw exceptions here. |
602 | 2 | static_cast<void>(function->close(fn_ctx, FunctionContext::THREAD_LOCAL)); |
603 | 2 | if (scope == FunctionContext::FRAGMENT_LOCAL) { |
604 | 2 | static_cast<void>(function->close(fn_ctx, FunctionContext::FRAGMENT_LOCAL)); |
605 | 2 | } |
606 | 2 | } |
607 | 2 | } |
608 | | |
609 | 0 | Status VExpr::check_constant(const Block& block, ColumnNumbers arguments) const { |
610 | 0 | if (is_constant() && !VectorizedUtils::all_arguments_are_constant(block, arguments)) { |
611 | 0 | return Status::InternalError("const check failed, expr={}", debug_string()); |
612 | 0 | } |
613 | 0 | return Status::OK(); |
614 | 0 | } |
615 | | |
616 | | Status VExpr::get_result_from_const(vectorized::Block* block, const std::string& expr_name, |
617 | 0 | int* result_column_id) { |
618 | 0 | *result_column_id = block->columns(); |
619 | 0 | auto column = ColumnConst::create(_constant_col->column_ptr, block->rows()); |
620 | 0 | block->insert({std::move(column), _data_type, expr_name}); |
621 | 0 | return Status::OK(); |
622 | 0 | } |
623 | | |
624 | | Status VExpr::_evaluate_inverted_index(VExprContext* context, const FunctionBasePtr& function, |
625 | 0 | uint32_t segment_num_rows) { |
626 | | // Pre-allocate vectors based on an estimated or known size |
627 | 0 | std::vector<segment_v2::InvertedIndexIterator*> iterators; |
628 | 0 | std::vector<vectorized::IndexFieldNameAndTypePair> data_type_with_names; |
629 | 0 | std::vector<int> column_ids; |
630 | 0 | vectorized::ColumnsWithTypeAndName arguments; |
631 | 0 | VExprSPtrs children_exprs; |
632 | | |
633 | | // Reserve space to avoid multiple reallocations |
634 | 0 | const size_t estimated_size = get_num_children(); |
635 | 0 | iterators.reserve(estimated_size); |
636 | 0 | data_type_with_names.reserve(estimated_size); |
637 | 0 | column_ids.reserve(estimated_size); |
638 | 0 | children_exprs.reserve(estimated_size); |
639 | |
|
640 | 0 | auto index_context = context->get_inverted_index_context(); |
641 | | |
642 | | // if child is cast expr, we need to ensure target data type is the same with storage data type. |
643 | | // or they are all string type |
644 | | // and if data type is array, we need to get the nested data type to ensure that. |
645 | 0 | for (const auto& child : children()) { |
646 | 0 | if (child->node_type() == TExprNodeType::CAST_EXPR) { |
647 | 0 | auto* cast_expr = assert_cast<VCastExpr*>(child.get()); |
648 | 0 | DCHECK_EQ(cast_expr->get_num_children(), 1); |
649 | 0 | if (cast_expr->get_child(0)->is_slot_ref()) { |
650 | 0 | auto* column_slot_ref = assert_cast<VSlotRef*>(cast_expr->get_child(0).get()); |
651 | 0 | auto column_id = column_slot_ref->column_id(); |
652 | 0 | const auto* storage_name_type = |
653 | 0 | context->get_inverted_index_context() |
654 | 0 | ->get_storage_name_and_type_by_column_id(column_id); |
655 | 0 | auto storage_type = remove_nullable(storage_name_type->second); |
656 | 0 | auto target_type = cast_expr->get_target_type(); |
657 | 0 | auto origin_primitive_type = storage_type->get_type_as_type_descriptor().type; |
658 | 0 | auto target_primitive_type = target_type->get_type_as_type_descriptor().type; |
659 | 0 | if (is_complex_type(storage_type)) { |
660 | 0 | if (is_array(storage_type) && is_array(target_type)) { |
661 | 0 | auto nested_storage_type = |
662 | 0 | (assert_cast<const DataTypeArray*>(storage_type.get())) |
663 | 0 | ->get_nested_type(); |
664 | 0 | origin_primitive_type = |
665 | 0 | nested_storage_type->get_type_as_type_descriptor().type; |
666 | 0 | auto nested_target_type = |
667 | 0 | (assert_cast<const DataTypeArray*>(target_type.get())) |
668 | 0 | ->get_nested_type(); |
669 | 0 | target_primitive_type = |
670 | 0 | nested_target_type->get_type_as_type_descriptor().type; |
671 | 0 | } else { |
672 | 0 | continue; |
673 | 0 | } |
674 | 0 | } |
675 | 0 | if (origin_primitive_type != TYPE_VARIANT && |
676 | 0 | (origin_primitive_type == target_primitive_type || |
677 | 0 | (is_string_type(target_primitive_type) && |
678 | 0 | is_string_type(origin_primitive_type)))) { |
679 | 0 | children_exprs.emplace_back(expr_without_cast(child)); |
680 | 0 | } |
681 | 0 | } |
682 | 0 | } else { |
683 | 0 | children_exprs.emplace_back(child); |
684 | 0 | } |
685 | 0 | } |
686 | |
|
687 | 0 | if (children_exprs.empty()) { |
688 | 0 | return Status::OK(); // Early exit if no children to process |
689 | 0 | } |
690 | | |
691 | 0 | for (const auto& child : children_exprs) { |
692 | 0 | if (child->is_slot_ref()) { |
693 | 0 | auto* column_slot_ref = assert_cast<VSlotRef*>(child.get()); |
694 | 0 | auto column_id = column_slot_ref->column_id(); |
695 | 0 | auto* iter = |
696 | 0 | context->get_inverted_index_context()->get_inverted_index_iterator_by_column_id( |
697 | 0 | column_id); |
698 | | //column does not have inverted index |
699 | 0 | if (iter == nullptr) { |
700 | 0 | continue; |
701 | 0 | } |
702 | 0 | const auto* storage_name_type = |
703 | 0 | context->get_inverted_index_context()->get_storage_name_and_type_by_column_id( |
704 | 0 | column_id); |
705 | 0 | if (storage_name_type == nullptr) { |
706 | 0 | auto err_msg = fmt::format( |
707 | 0 | "storage_name_type cannot be found for column {} while in {} " |
708 | 0 | "evaluate_inverted_index", |
709 | 0 | column_id, expr_name()); |
710 | 0 | LOG(ERROR) << err_msg; |
711 | 0 | return Status::InternalError(err_msg); |
712 | 0 | } |
713 | 0 | iterators.emplace_back(iter); |
714 | 0 | data_type_with_names.emplace_back(*storage_name_type); |
715 | 0 | column_ids.emplace_back(column_id); |
716 | 0 | } else if (child->is_literal()) { |
717 | 0 | auto* column_literal = assert_cast<VLiteral*>(child.get()); |
718 | 0 | arguments.emplace_back(column_literal->get_column_ptr(), |
719 | 0 | column_literal->get_data_type(), column_literal->expr_name()); |
720 | 0 | } |
721 | 0 | } |
722 | | |
723 | 0 | if (iterators.empty() || arguments.empty()) { |
724 | 0 | return Status::OK(); // Nothing to evaluate or no literals to compare against |
725 | 0 | } |
726 | | |
727 | 0 | auto result_bitmap = segment_v2::InvertedIndexResultBitmap(); |
728 | 0 | auto res = function->evaluate_inverted_index(arguments, data_type_with_names, iterators, |
729 | 0 | segment_num_rows, result_bitmap); |
730 | 0 | if (!res.ok()) { |
731 | 0 | return res; |
732 | 0 | } |
733 | 0 | if (!result_bitmap.is_empty()) { |
734 | 0 | index_context->set_inverted_index_result_for_expr(this, result_bitmap); |
735 | 0 | for (int column_id : column_ids) { |
736 | 0 | index_context->set_true_for_inverted_index_status(this, column_id); |
737 | 0 | } |
738 | | // set fast_execute when expr evaluated by inverted index correctly |
739 | 0 | _can_fast_execute = true; |
740 | 0 | } |
741 | 0 | return Status::OK(); |
742 | 0 | } |
743 | | |
744 | | bool VExpr::fast_execute(doris::vectorized::VExprContext* context, doris::vectorized::Block* block, |
745 | 0 | int* result_column_id) { |
746 | 0 | if (context->get_inverted_index_context() && |
747 | 0 | context->get_inverted_index_context()->get_inverted_index_result_column().contains(this)) { |
748 | 0 | uint32_t num_columns_without_result = block->columns(); |
749 | | // prepare a column to save result |
750 | 0 | auto result_column = |
751 | 0 | context->get_inverted_index_context()->get_inverted_index_result_column()[this]; |
752 | 0 | if (_data_type->is_nullable()) { |
753 | 0 | block->insert( |
754 | 0 | {ColumnNullable::create(result_column, ColumnUInt8::create(block->rows(), 0)), |
755 | 0 | _data_type, expr_name()}); |
756 | 0 | } else { |
757 | 0 | block->insert({result_column, _data_type, expr_name()}); |
758 | 0 | } |
759 | 0 | *result_column_id = num_columns_without_result; |
760 | 0 | return true; |
761 | 0 | } |
762 | 0 | return false; |
763 | 0 | } |
764 | | |
765 | 0 | bool VExpr::equals(const VExpr& other) { |
766 | 0 | return false; |
767 | 0 | } |
768 | | |
769 | | #include "common/compile_check_end.h" |
770 | | } // namespace doris::vectorized |