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