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 | 2.05M | int scale) { |
90 | 2.05M | TExprNode node; |
91 | | |
92 | 2.05M | switch (type) { |
93 | 58 | case TYPE_BOOLEAN: { |
94 | 58 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_BOOLEAN>(data, &node)); |
95 | 58 | break; |
96 | 58 | } |
97 | 675 | case TYPE_TINYINT: { |
98 | 675 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_TINYINT>(data, &node)); |
99 | 675 | break; |
100 | 675 | } |
101 | 675 | case TYPE_SMALLINT: { |
102 | 513 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_SMALLINT>(data, &node)); |
103 | 513 | break; |
104 | 513 | } |
105 | 2.05M | case TYPE_INT: { |
106 | 2.05M | THROW_IF_ERROR(create_texpr_literal_node<TYPE_INT>(data, &node)); |
107 | 2.05M | break; |
108 | 2.05M | } |
109 | 2.05M | case TYPE_BIGINT: { |
110 | 4.32k | THROW_IF_ERROR(create_texpr_literal_node<TYPE_BIGINT>(data, &node)); |
111 | 4.32k | break; |
112 | 4.32k | } |
113 | 4.32k | case TYPE_LARGEINT: { |
114 | 171 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_LARGEINT>(data, &node)); |
115 | 171 | break; |
116 | 171 | } |
117 | 171 | case TYPE_FLOAT: { |
118 | 58 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_FLOAT>(data, &node)); |
119 | 58 | break; |
120 | 58 | } |
121 | 58 | case TYPE_DOUBLE: { |
122 | 36 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_DOUBLE>(data, &node)); |
123 | 36 | break; |
124 | 36 | } |
125 | 524 | case TYPE_DATEV2: { |
126 | 524 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_DATEV2>(data, &node)); |
127 | 524 | break; |
128 | 524 | } |
129 | 545 | case TYPE_DATETIMEV2: { |
130 | 545 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_DATETIMEV2>(data, &node, precision, scale)); |
131 | 545 | break; |
132 | 545 | } |
133 | 545 | case TYPE_DATE: { |
134 | 32 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_DATE>(data, &node)); |
135 | 32 | break; |
136 | 32 | } |
137 | 48 | case TYPE_DATETIME: { |
138 | 48 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_DATETIME>(data, &node)); |
139 | 48 | break; |
140 | 48 | } |
141 | 48 | case TYPE_DECIMALV2: { |
142 | 10 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_DECIMALV2>(data, &node, precision, scale)); |
143 | 10 | break; |
144 | 10 | } |
145 | 52 | case TYPE_DECIMAL32: { |
146 | 52 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_DECIMAL32>(data, &node, precision, scale)); |
147 | 52 | break; |
148 | 52 | } |
149 | 333 | case TYPE_DECIMAL64: { |
150 | 333 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_DECIMAL64>(data, &node, precision, scale)); |
151 | 333 | break; |
152 | 333 | } |
153 | 333 | case TYPE_DECIMAL128I: { |
154 | 165 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_DECIMAL128I>(data, &node, precision, scale)); |
155 | 165 | break; |
156 | 165 | } |
157 | 165 | case TYPE_DECIMAL256: { |
158 | 128 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_DECIMAL256>(data, &node, precision, scale)); |
159 | 128 | break; |
160 | 128 | } |
161 | 128 | case TYPE_CHAR: { |
162 | 47 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_CHAR>(data, &node)); |
163 | 47 | break; |
164 | 47 | } |
165 | 180 | case TYPE_VARCHAR: { |
166 | 180 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_VARCHAR>(data, &node)); |
167 | 180 | break; |
168 | 180 | } |
169 | 413 | case TYPE_STRING: { |
170 | 413 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_STRING>(data, &node)); |
171 | 413 | break; |
172 | 413 | } |
173 | 413 | case TYPE_VARBINARY: { |
174 | 0 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_VARBINARY>(data, &node)); |
175 | 0 | break; |
176 | 0 | } |
177 | 32 | case TYPE_IPV4: { |
178 | 32 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_IPV4>(data, &node)); |
179 | 32 | break; |
180 | 32 | } |
181 | 32 | 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 | 77 | case TYPE_TIMESTAMPTZ: { |
190 | 77 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_TIMESTAMPTZ>(data, &node, precision, scale)); |
191 | 77 | break; |
192 | 77 | } |
193 | 77 | default: |
194 | 0 | throw Exception(ErrorCode::INTERNAL_ERROR, "runtime filter meet invalid type {}", |
195 | 0 | int(type)); |
196 | 2.05M | } |
197 | 2.05M | return node; |
198 | 2.05M | } |
199 | | |
200 | | TExprNode create_texpr_node_from(const Field& field, const PrimitiveType& type, int precision, |
201 | 2.56k | int scale) { |
202 | 2.56k | TExprNode node; |
203 | 2.56k | switch (type) { |
204 | 21 | case TYPE_BOOLEAN: { |
205 | 21 | const auto& storage = static_cast<bool>(field.get<TYPE_BOOLEAN>()); |
206 | 21 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_BOOLEAN>(&storage, &node)); |
207 | 21 | break; |
208 | 21 | } |
209 | 21 | case TYPE_TINYINT: { |
210 | 20 | const auto& storage = static_cast<int8_t>(field.get<TYPE_TINYINT>()); |
211 | 20 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_TINYINT>(&storage, &node)); |
212 | 20 | break; |
213 | 20 | } |
214 | 21 | case TYPE_SMALLINT: { |
215 | 21 | const auto& storage = static_cast<int16_t>(field.get<TYPE_SMALLINT>()); |
216 | 21 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_SMALLINT>(&storage, &node)); |
217 | 21 | break; |
218 | 21 | } |
219 | 97 | case TYPE_INT: { |
220 | 97 | const auto& storage = static_cast<int32_t>(field.get<TYPE_INT>()); |
221 | 97 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_INT>(&storage, &node)); |
222 | 97 | break; |
223 | 97 | } |
224 | 97 | case TYPE_BIGINT: { |
225 | 51 | const auto& storage = static_cast<int64_t>(field.get<TYPE_BIGINT>()); |
226 | 51 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_BIGINT>(&storage, &node)); |
227 | 51 | break; |
228 | 51 | } |
229 | 51 | 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 | 21 | case TYPE_DATEV2: { |
245 | 21 | const auto& storage = field.get<TYPE_DATEV2>(); |
246 | | |
247 | 21 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_DATEV2>(&storage, &node)); |
248 | 21 | break; |
249 | 21 | } |
250 | 22 | case TYPE_DATETIMEV2: { |
251 | 22 | const auto& storage = field.get<TYPE_DATETIMEV2>(); |
252 | 22 | THROW_IF_ERROR( |
253 | 22 | create_texpr_literal_node<TYPE_DATETIMEV2>(&storage, &node, precision, scale)); |
254 | 22 | break; |
255 | 22 | } |
256 | 22 | 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 | 687 | case TYPE_DECIMAL64: { |
287 | 687 | const auto& storage = field.get<TYPE_DECIMAL64>(); |
288 | 687 | THROW_IF_ERROR( |
289 | 687 | create_texpr_literal_node<TYPE_DECIMAL64>(&storage, &node, precision, scale)); |
290 | 687 | break; |
291 | 687 | } |
292 | 687 | 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 | 286 | case TYPE_CHAR: { |
305 | 286 | const auto& storage = field.get<TYPE_CHAR>(); |
306 | 286 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_CHAR>(&storage, &node)); |
307 | 286 | break; |
308 | 286 | } |
309 | 662 | case TYPE_VARCHAR: { |
310 | 662 | const auto& storage = field.get<TYPE_VARCHAR>(); |
311 | 662 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_VARCHAR>(&storage, &node)); |
312 | 662 | break; |
313 | 662 | } |
314 | 662 | case TYPE_STRING: { |
315 | 657 | const auto& storage = field.get<TYPE_STRING>(); |
316 | 657 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_STRING>(&storage, &node)); |
317 | 657 | break; |
318 | 657 | } |
319 | 657 | 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 | 2.56k | } |
343 | 2.56k | return node; |
344 | 2.56k | } |
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 | 10.0M | : _node_type(node.node_type), |
354 | 10.0M | _opcode(node.__isset.opcode ? node.opcode : TExprOpcode::INVALID_OPCODE) { |
355 | 10.0M | if (node.__isset.fn) { |
356 | 653k | _fn = node.fn; |
357 | 653k | } |
358 | | |
359 | 10.0M | bool is_nullable = true; |
360 | 10.0M | if (node.__isset.is_nullable) { |
361 | 8.01M | is_nullable = node.is_nullable; |
362 | 8.01M | } |
363 | | // If we define null literal ,should make nullable data type to get correct field instead of undefined ptr |
364 | 10.0M | if (node.node_type == TExprNodeType::NULL_LITERAL) { |
365 | 265k | CHECK(is_nullable); |
366 | 265k | } |
367 | 10.0M | _data_type = get_data_type_with_default_argument( |
368 | 10.0M | DataTypeFactory::instance().create_data_type(node.type, is_nullable)); |
369 | 10.0M | } |
370 | | |
371 | 30.5k | VExpr::VExpr(const VExpr& vexpr) = default; |
372 | | |
373 | | VExpr::VExpr(DataTypePtr type, bool is_slotref) |
374 | 247k | : _opcode(TExprOpcode::INVALID_OPCODE), |
375 | 247k | _data_type(get_data_type_with_default_argument(type)) { |
376 | 247k | if (is_slotref) { |
377 | 211k | _node_type = TExprNodeType::SLOT_REF; |
378 | 211k | } |
379 | 247k | } |
380 | | |
381 | 20.5k | TExprNode VExpr::clone_texpr_node() const { |
382 | 20.5k | TExprNode node; |
383 | 20.5k | node.__set_node_type(_node_type); |
384 | 20.5k | node.__set_opcode(_opcode); |
385 | 20.5k | node.__set_type(create_type_desc(remove_nullable(_data_type)->get_primitive_type(), |
386 | 20.5k | static_cast<int>(_data_type->get_precision()), |
387 | 20.5k | static_cast<int>(_data_type->get_scale()))); |
388 | 20.5k | node.__set_is_nullable(_data_type->is_nullable()); |
389 | 20.5k | node.__set_num_children(get_num_children()); |
390 | 20.5k | node.__set_fn(_fn); |
391 | 20.5k | return node; |
392 | 20.5k | } |
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 | 113k | const VExprCloneNodeOverride& clone_node_override) const { |
401 | 113k | DORIS_CHECK(cloned_expr != nullptr); |
402 | | |
403 | 113k | VExprSPtr cloned; |
404 | 113k | if (clone_node_override) { |
405 | 87.3k | RETURN_IF_ERROR(clone_node_override(*this, &cloned)); |
406 | 87.3k | } |
407 | 113k | if (cloned == nullptr) { |
408 | 112k | RETURN_IF_ERROR(clone_node(&cloned)); |
409 | 112k | } |
410 | 113k | DORIS_CHECK(cloned != nullptr); |
411 | | |
412 | 113k | VExprSPtrs cloned_children; |
413 | 113k | cloned_children.reserve(_children.size()); |
414 | 113k | for (const auto& child : _children) { |
415 | 71.8k | DORIS_CHECK(child != nullptr); |
416 | 71.8k | VExprSPtr cloned_child; |
417 | 71.8k | RETURN_IF_ERROR(child->deep_clone(&cloned_child, clone_node_override)); |
418 | 71.8k | cloned_children.push_back(std::move(cloned_child)); |
419 | 71.8k | } |
420 | 113k | cloned->set_children(std::move(cloned_children)); |
421 | 113k | cloned->reset_prepare_state(); |
422 | 113k | *cloned_expr = std::move(cloned); |
423 | 113k | return Status::OK(); |
424 | 113k | } |
425 | | |
426 | 7.70M | Status VExpr::prepare(RuntimeState* state, const RowDescriptor& row_desc, VExprContext* context) { |
427 | 7.70M | ++context->_depth_num; |
428 | 7.70M | 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 | 7.70M | for (auto& i : _children) { |
435 | 1.56M | RETURN_IF_ERROR(i->prepare(state, row_desc, context)); |
436 | 1.56M | } |
437 | 7.70M | --context->_depth_num; |
438 | 7.70M | #ifndef BE_TEST |
439 | 7.70M | _enable_inverted_index_query = state->query_options().enable_inverted_index_query; |
440 | 7.70M | #endif |
441 | 7.70M | return Status::OK(); |
442 | 7.70M | } |
443 | | |
444 | | Status VExpr::open(RuntimeState* state, VExprContext* context, |
445 | 25.8M | FunctionContext::FunctionStateScope scope) { |
446 | 25.8M | for (auto& i : _children) { |
447 | 422k | RETURN_IF_ERROR(i->open(state, context, scope)); |
448 | 422k | } |
449 | 25.8M | if (scope == FunctionContext::FRAGMENT_LOCAL) { |
450 | 7.10M | RETURN_IF_ERROR(VExpr::get_const_col(context, nullptr)); |
451 | 7.10M | } |
452 | 25.8M | return Status::OK(); |
453 | 25.8M | } |
454 | | |
455 | 209k | void VExpr::reset_prepare_state() { |
456 | 209k | _prepared = false; |
457 | 209k | _prepare_finished = false; |
458 | 209k | _open_finished = false; |
459 | 209k | for (auto& child : _children) { |
460 | 96.3k | child->reset_prepare_state(); |
461 | 96.3k | } |
462 | 209k | } |
463 | | |
464 | 28.0M | void VExpr::close(VExprContext* context, FunctionContext::FunctionStateScope scope) { |
465 | 28.0M | for (auto& i : _children) { |
466 | 4.18M | i->close(context, scope); |
467 | 4.18M | } |
468 | 28.0M | } |
469 | | |
470 | | // NOLINTBEGIN(readability-function-size) |
471 | 7.98M | Status VExpr::create_expr(const TExprNode& expr_node, VExprSPtr& expr) { |
472 | 7.98M | try { |
473 | 7.98M | switch (expr_node.node_type) { |
474 | 23.2k | case TExprNodeType::BOOL_LITERAL: |
475 | 783k | case TExprNodeType::INT_LITERAL: |
476 | 831k | case TExprNodeType::LARGE_INT_LITERAL: |
477 | 833k | case TExprNodeType::IPV4_LITERAL: |
478 | 835k | case TExprNodeType::IPV6_LITERAL: |
479 | 880k | case TExprNodeType::FLOAT_LITERAL: |
480 | 941k | case TExprNodeType::DECIMAL_LITERAL: |
481 | 1.11M | case TExprNodeType::DATE_LITERAL: |
482 | 1.11M | case TExprNodeType::TIMEV2_LITERAL: |
483 | 1.81M | case TExprNodeType::STRING_LITERAL: |
484 | 1.81M | case TExprNodeType::JSON_LITERAL: |
485 | 1.81M | case TExprNodeType::VARBINARY_LITERAL: |
486 | 2.08M | case TExprNodeType::NULL_LITERAL: { |
487 | 2.08M | expr = VLiteral::create_shared(expr_node); |
488 | 2.08M | break; |
489 | 1.81M | } |
490 | 15.5k | case TExprNodeType::ARRAY_LITERAL: { |
491 | 15.5k | expr = VArrayLiteral::create_shared(expr_node); |
492 | 15.5k | break; |
493 | 1.81M | } |
494 | 12.0k | case TExprNodeType::MAP_LITERAL: { |
495 | 12.0k | expr = VMapLiteral::create_shared(expr_node); |
496 | 12.0k | break; |
497 | 1.81M | } |
498 | 1.53k | case TExprNodeType::STRUCT_LITERAL: { |
499 | 1.53k | expr = VStructLiteral::create_shared(expr_node); |
500 | 1.53k | break; |
501 | 1.81M | } |
502 | 5.10M | case TExprNodeType::SLOT_REF: { |
503 | 5.10M | if (expr_node.slot_ref.__isset.is_virtual_slot && expr_node.slot_ref.is_virtual_slot) { |
504 | 209 | expr = VirtualSlotRef::create_shared(expr_node); |
505 | 209 | expr->_node_type = TExprNodeType::VIRTUAL_SLOT_REF; |
506 | 5.10M | } else { |
507 | 5.10M | expr = VSlotRef::create_shared(expr_node); |
508 | 5.10M | } |
509 | 5.10M | break; |
510 | 1.81M | } |
511 | 1.33k | case TExprNodeType::COLUMN_REF: { |
512 | 1.33k | expr = VColumnRef::create_shared(expr_node); |
513 | 1.33k | break; |
514 | 1.81M | } |
515 | 5.51k | case TExprNodeType::COMPOUND_PRED: { |
516 | 5.51k | expr = VCompoundPred::create_shared(expr_node); |
517 | 5.51k | break; |
518 | 1.81M | } |
519 | 1.01k | case TExprNodeType::LAMBDA_FUNCTION_EXPR: { |
520 | 1.01k | expr = VLambdaFunctionExpr::create_shared(expr_node); |
521 | 1.01k | break; |
522 | 1.81M | } |
523 | 1.01k | case TExprNodeType::LAMBDA_FUNCTION_CALL_EXPR: { |
524 | 1.01k | expr = VLambdaFunctionCallExpr::create_shared(expr_node); |
525 | 1.01k | break; |
526 | 1.81M | } |
527 | 84.8k | case TExprNodeType::ARITHMETIC_EXPR: |
528 | 449k | case TExprNodeType::BINARY_PRED: |
529 | 449k | case TExprNodeType::NULL_AWARE_BINARY_PRED: |
530 | 449k | case TExprNodeType::COMPUTE_FUNCTION_CALL: { |
531 | 449k | expr = VectorizedFnCall::create_shared(expr_node); |
532 | 449k | break; |
533 | 449k | } |
534 | 168k | case TExprNodeType::FUNCTION_CALL: { |
535 | 168k | if (expr_node.fn.name.function_name == "if") { |
536 | 2.98k | if (expr_node.__isset.short_circuit_evaluation && |
537 | 2.98k | expr_node.short_circuit_evaluation) { |
538 | 677 | expr = ShortCircuitIfExpr::create_shared(expr_node); |
539 | 2.30k | } else { |
540 | 2.30k | expr = VectorizedIfExpr::create_shared(expr_node); |
541 | 2.30k | } |
542 | 2.98k | break; |
543 | 165k | } else if (expr_node.fn.name.function_name == "ifnull" || |
544 | 165k | expr_node.fn.name.function_name == "nvl") { |
545 | 1.70k | if (expr_node.__isset.short_circuit_evaluation && |
546 | 1.70k | expr_node.short_circuit_evaluation) { |
547 | 204 | expr = ShortCircuitIfNullExpr::create_shared(expr_node); |
548 | 1.50k | } else { |
549 | 1.50k | expr = VectorizedIfNullExpr::create_shared(expr_node); |
550 | 1.50k | } |
551 | 1.70k | break; |
552 | 163k | } else if (expr_node.fn.name.function_name == "coalesce") { |
553 | 162 | if (expr_node.__isset.short_circuit_evaluation && |
554 | 162 | expr_node.short_circuit_evaluation) { |
555 | 65 | expr = ShortCircuitCoalesceExpr::create_shared(expr_node); |
556 | 97 | } else { |
557 | 97 | expr = VectorizedCoalesceExpr::create_shared(expr_node); |
558 | 97 | } |
559 | 162 | break; |
560 | 162 | } |
561 | 163k | expr = VectorizedFnCall::create_shared(expr_node); |
562 | 163k | break; |
563 | 168k | } |
564 | 1.70k | case TExprNodeType::MATCH_PRED: { |
565 | 1.70k | expr = VMatchPredicate::create_shared(expr_node); |
566 | 1.70k | break; |
567 | 168k | } |
568 | 135k | case TExprNodeType::CAST_EXPR: { |
569 | 135k | expr = VCastExpr::create_shared(expr_node); |
570 | 135k | break; |
571 | 168k | } |
572 | 11 | case TExprNodeType::TRY_CAST_EXPR: { |
573 | 11 | expr = TryCastExpr::create_shared(expr_node); |
574 | 11 | break; |
575 | 168k | } |
576 | 1.63k | case TExprNodeType::IN_PRED: { |
577 | 1.63k | expr = VInPredicate::create_shared(expr_node); |
578 | 1.63k | break; |
579 | 168k | } |
580 | 288 | case TExprNodeType::CASE_EXPR: { |
581 | 288 | if (!expr_node.__isset.case_expr) { |
582 | 0 | return Status::InternalError("Case expression not set in thrift node"); |
583 | 0 | } |
584 | 288 | if (expr_node.__isset.short_circuit_evaluation && expr_node.short_circuit_evaluation) { |
585 | 103 | expr = ShortCircuitCaseExpr::create_shared(expr_node); |
586 | 185 | } else { |
587 | 185 | expr = VCaseExpr::create_shared(expr_node); |
588 | 185 | } |
589 | 288 | break; |
590 | 288 | } |
591 | 0 | case TExprNodeType::INFO_FUNC: { |
592 | 0 | expr = VInfoFunc::create_shared(expr_node); |
593 | 0 | break; |
594 | 288 | } |
595 | 463 | case TExprNodeType::SEARCH_EXPR: { |
596 | 463 | expr = VSearchExpr::create_shared(expr_node); |
597 | 463 | break; |
598 | 288 | } |
599 | 0 | default: |
600 | 0 | return Status::InternalError("Unknown expr node type: {}", expr_node.node_type); |
601 | 7.98M | } |
602 | 7.98M | } 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 | 7.98M | if (!expr->data_type()) { |
612 | 0 | return Status::InvalidArgument("Unknown expr type: {}", expr_node.node_type); |
613 | 0 | } |
614 | 7.98M | return Status::OK(); |
615 | 7.98M | } |
616 | | // NOLINTEND(readability-function-size) |
617 | | |
618 | | Status VExpr::create_tree_from_thrift(const std::vector<TExprNode>& nodes, int* node_idx, |
619 | 6.46M | VExprSPtr& root_expr, VExprContextSPtr& ctx) { |
620 | | // propagate error case |
621 | 6.46M | 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 | 6.46M | int root_children = nodes[*node_idx].num_children; |
627 | 6.46M | VExprSPtr root; |
628 | 6.46M | RETURN_IF_ERROR(create_expr(nodes[*node_idx], root)); |
629 | 6.46M | DCHECK(root != nullptr); |
630 | 6.46M | root_expr = root; |
631 | 6.46M | ctx = std::make_shared<VExprContext>(root); |
632 | | // short path for leaf node |
633 | 6.46M | if (root_children <= 0) { |
634 | 5.83M | return Status::OK(); |
635 | 5.83M | } |
636 | | |
637 | | // non-recursive traversal |
638 | 627k | using VExprSPtrCountPair = std::pair<VExprSPtr, int>; |
639 | 627k | std::stack<std::shared_ptr<VExprSPtrCountPair>> s; |
640 | 627k | s.emplace(std::make_shared<VExprSPtrCountPair>(root, root_children)); |
641 | 2.13M | while (!s.empty()) { |
642 | | // copy the shared ptr resource to avoid dangling reference |
643 | 1.50M | auto parent = s.top(); |
644 | | // Decrement or pop |
645 | 1.50M | if (parent->second > 1) { |
646 | 727k | parent->second -= 1; |
647 | 780k | } else { |
648 | 780k | s.pop(); |
649 | 780k | } |
650 | | |
651 | 1.50M | DCHECK(parent->first != nullptr); |
652 | 1.50M | if (++*node_idx >= nodes.size()) { |
653 | 0 | return Status::InternalError("Failed to reconstruct expression tree from thrift."); |
654 | 0 | } |
655 | | |
656 | 1.50M | VExprSPtr expr; |
657 | 1.50M | RETURN_IF_ERROR(create_expr(nodes[*node_idx], expr)); |
658 | 1.50M | DCHECK(expr != nullptr); |
659 | 1.50M | parent->first->add_child(expr); |
660 | | // push to stack if has children |
661 | 1.50M | int num_children = nodes[*node_idx].num_children; |
662 | 1.50M | if (num_children > 0) { |
663 | 149k | s.emplace(std::make_shared<VExprSPtrCountPair>(expr, num_children)); |
664 | 149k | } |
665 | 1.50M | } |
666 | 627k | return Status::OK(); |
667 | 627k | } |
668 | | |
669 | 6.36M | Status VExpr::create_expr_tree(const TExpr& texpr, VExprContextSPtr& ctx) { |
670 | 6.36M | if (texpr.nodes.empty()) { |
671 | 9 | ctx = nullptr; |
672 | 9 | return Status::OK(); |
673 | 9 | } |
674 | 6.36M | int node_idx = 0; |
675 | 6.36M | VExprSPtr e; |
676 | 6.36M | Status status = create_tree_from_thrift(texpr.nodes, &node_idx, e, ctx); |
677 | 6.37M | 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 | 6.36M | 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 | 6.36M | return status; |
688 | 6.36M | } |
689 | | |
690 | 1.23M | Status VExpr::create_expr_trees(const std::vector<TExpr>& texprs, VExprContextSPtrs& ctxs) { |
691 | 1.23M | ctxs.clear(); |
692 | 5.35M | for (const auto& texpr : texprs) { |
693 | 5.35M | VExprContextSPtr ctx; |
694 | 5.35M | RETURN_IF_ERROR(create_expr_tree(texpr, ctx)); |
695 | 5.35M | ctxs.push_back(ctx); |
696 | 5.35M | } |
697 | 1.23M | return Status::OK(); |
698 | 1.23M | } |
699 | | |
700 | | Status VExpr::check_expr_output_type(const VExprContextSPtrs& ctxs, |
701 | 281k | const RowDescriptor& output_row_desc) { |
702 | 281k | if (ctxs.empty()) { |
703 | 6 | return Status::OK(); |
704 | 6 | } |
705 | 281k | auto name_and_types = VectorizedUtils::create_name_and_data_types(output_row_desc); |
706 | 281k | 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 | 1.82M | auto check_type_can_be_converted = [](DataTypePtr& from, DataTypePtr& to) -> bool { |
712 | 1.82M | if (to->equals(*from)) { |
713 | 1.82M | return true; |
714 | 1.82M | } |
715 | 784 | if (to->is_nullable() && !from->is_nullable()) { |
716 | 87 | return remove_nullable(to)->equals(*from); |
717 | 87 | } |
718 | 697 | return false; |
719 | 784 | }; |
720 | 2.10M | for (int i = 0; i < ctxs.size(); i++) { |
721 | 1.82M | auto real_expr_type = get_data_type_with_default_argument(ctxs[i]->root()->data_type()); |
722 | 1.82M | auto&& [name, expected_type] = name_and_types[i]; |
723 | 1.82M | 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 | 1.82M | } |
730 | 281k | return Status::OK(); |
731 | 281k | } |
732 | | |
733 | | Status VExpr::prepare(const VExprContextSPtrs& ctxs, RuntimeState* state, |
734 | 1.94M | const RowDescriptor& row_desc) { |
735 | 5.66M | for (auto ctx : ctxs) { |
736 | 5.66M | RETURN_IF_ERROR(ctx->prepare(state, row_desc)); |
737 | 5.66M | } |
738 | 1.94M | return Status::OK(); |
739 | 1.94M | } |
740 | | |
741 | 1.96M | Status VExpr::open(const VExprContextSPtrs& ctxs, RuntimeState* state) { |
742 | 5.66M | for (const auto& ctx : ctxs) { |
743 | 5.66M | RETURN_IF_ERROR(ctx->open(state)); |
744 | 5.66M | } |
745 | 1.96M | return Status::OK(); |
746 | 1.96M | } |
747 | | |
748 | 1.52M | bool VExpr::contains_blockable_function(const VExprContextSPtrs& ctxs) { |
749 | 2.17M | return std::any_of(ctxs.begin(), ctxs.end(), [](const VExprContextSPtr& ctx) { |
750 | 2.17M | return ctx != nullptr && ctx->root() != nullptr && ctx->root()->is_blockable(); |
751 | 2.17M | }); |
752 | 1.52M | } |
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 | 406 | std::string VExpr::debug_string() const { |
772 | | // TODO: implement partial debug string for member vars |
773 | 406 | std::stringstream out; |
774 | 406 | out << " type=" << _data_type->get_name(); |
775 | | |
776 | 406 | if (!_children.empty()) { |
777 | 66 | out << " children=" << debug_string(_children); |
778 | 66 | } |
779 | | |
780 | 406 | return out.str(); |
781 | 406 | } |
782 | | |
783 | 66 | std::string VExpr::debug_string(const VExprSPtrs& exprs) { |
784 | 66 | std::stringstream out; |
785 | 66 | out << "["; |
786 | | |
787 | 132 | for (int i = 0; i < exprs.size(); ++i) { |
788 | 66 | out << (i == 0 ? "" : " ") << exprs[i]->debug_string(); |
789 | 66 | } |
790 | | |
791 | 66 | out << "]"; |
792 | 66 | return out.str(); |
793 | 66 | } |
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 | 6.50M | bool VExpr::is_constant() const { |
804 | 6.50M | return std::all_of(_children.begin(), _children.end(), |
805 | 6.50M | [](const VExprSPtr& expr) { return expr->is_constant(); }); |
806 | 6.50M | } |
807 | | |
808 | | Status VExpr::get_const_col(VExprContext* context, |
809 | 9.61M | std::shared_ptr<ColumnPtrWrapper>* column_wrapper) { |
810 | 9.61M | if (!is_constant()) { |
811 | 6.47M | return Status::OK(); |
812 | 6.47M | } |
813 | | |
814 | 3.13M | if (_constant_col != nullptr && column_wrapper == nullptr) { |
815 | 0 | return Status::OK(); |
816 | 3.13M | } else if (_constant_col != nullptr) { |
817 | 1.02M | *column_wrapper = _constant_col; |
818 | 1.02M | return Status::OK(); |
819 | 1.02M | } |
820 | | |
821 | 2.11M | ColumnPtr result; |
822 | 2.11M | RETURN_IF_ERROR(execute_column(context, nullptr, nullptr, 1, result)); |
823 | 2.11M | _constant_col = std::make_shared<ColumnPtrWrapper>(result); |
824 | 2.11M | if (column_wrapper != nullptr) { |
825 | 0 | *column_wrapper = _constant_col; |
826 | 0 | } |
827 | | |
828 | 2.11M | return Status::OK(); |
829 | 2.11M | } |
830 | | |
831 | 767k | void VExpr::register_function_context(RuntimeState* state, VExprContext* context) { |
832 | 767k | std::vector<DataTypePtr> arg_types; |
833 | 1.37M | for (auto& i : _children) { |
834 | 1.37M | arg_types.push_back(i->data_type()); |
835 | 1.37M | } |
836 | | |
837 | 767k | _fn_context_index = context->register_function_context(state, _data_type, arg_types); |
838 | 767k | } |
839 | | |
840 | | Status VExpr::init_function_context(RuntimeState* state, VExprContext* context, |
841 | | FunctionContext::FunctionStateScope scope, |
842 | 2.13M | const FunctionBasePtr& function) const { |
843 | 2.13M | FunctionContext* fn_ctx = context->fn_context(_fn_context_index); |
844 | 2.13M | if (scope == FunctionContext::FRAGMENT_LOCAL) { |
845 | 768k | std::vector<std::shared_ptr<ColumnPtrWrapper>> constant_cols; |
846 | 1.37M | for (auto c : _children) { |
847 | 1.37M | std::shared_ptr<ColumnPtrWrapper> const_col; |
848 | 1.37M | RETURN_IF_ERROR(c->get_const_col(context, &const_col)); |
849 | 1.37M | constant_cols.push_back(const_col); |
850 | 1.37M | } |
851 | 768k | fn_ctx->set_constant_cols(constant_cols); |
852 | 768k | } |
853 | | |
854 | 2.13M | if (scope == FunctionContext::FRAGMENT_LOCAL) { |
855 | 768k | RETURN_IF_ERROR(function->open(fn_ctx, FunctionContext::FRAGMENT_LOCAL)); |
856 | 768k | } |
857 | 2.13M | RETURN_IF_ERROR(function->open(fn_ctx, FunctionContext::THREAD_LOCAL)); |
858 | 2.13M | return Status::OK(); |
859 | 2.13M | } |
860 | | |
861 | | void VExpr::close_function_context(VExprContext* context, FunctionContext::FunctionStateScope scope, |
862 | 2.14M | const FunctionBasePtr& function) const { |
863 | 2.14M | if (_fn_context_index != -1) { |
864 | 2.14M | 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.14M | static_cast<void>(function->close(fn_ctx, FunctionContext::THREAD_LOCAL)); |
867 | 2.14M | if (scope == FunctionContext::FRAGMENT_LOCAL) { |
868 | 767k | static_cast<void>(function->close(fn_ctx, FunctionContext::FRAGMENT_LOCAL)); |
869 | 767k | } |
870 | 2.14M | } |
871 | 2.14M | } |
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 | 397k | uint64_t VExpr::get_digest(uint64_t seed) const { |
881 | 397k | auto digest = seed; |
882 | 785k | for (auto child : _children) { |
883 | 785k | digest = child->get_digest(digest); |
884 | 785k | if (digest == 0) { |
885 | 294 | return 0; |
886 | 294 | } |
887 | 785k | } |
888 | | |
889 | 396k | auto& fn_name = _fn.name.function_name; |
890 | 396k | if (!fn_name.empty()) { |
891 | 393k | digest = HashUtil::hash64(fn_name.c_str(), fn_name.size(), digest); |
892 | 393k | } else { |
893 | 2.92k | digest = HashUtil::hash64((const char*)&_node_type, sizeof(_node_type), digest); |
894 | 2.92k | digest = HashUtil::hash64((const char*)&_opcode, sizeof(_opcode), digest); |
895 | 2.92k | } |
896 | 396k | return digest; |
897 | 397k | } |
898 | | |
899 | 82.2k | ColumnPtr VExpr::get_result_from_const(size_t count) const { |
900 | 82.2k | return ColumnConst::create(_constant_col->column_ptr, count); |
901 | 82.2k | } |
902 | | |
903 | | Status VExpr::_evaluate_inverted_index(VExprContext* context, const FunctionBasePtr& function, |
904 | 19.6k | uint32_t segment_num_rows) { |
905 | | // Pre-allocate vectors based on an estimated or known size |
906 | 19.6k | std::vector<segment_v2::IndexIterator*> iterators; |
907 | 19.6k | std::vector<IndexFieldNameAndTypePair> data_type_with_names; |
908 | 19.6k | std::vector<int> column_ids; |
909 | 19.6k | ColumnsWithTypeAndName arguments; |
910 | 19.6k | VExprSPtrs children_exprs; |
911 | | |
912 | | // Reserve space to avoid multiple reallocations |
913 | 19.6k | const size_t estimated_size = get_num_children(); |
914 | 19.6k | iterators.reserve(estimated_size); |
915 | 19.6k | data_type_with_names.reserve(estimated_size); |
916 | 19.6k | column_ids.reserve(estimated_size); |
917 | 19.6k | children_exprs.reserve(estimated_size); |
918 | | |
919 | 19.6k | 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.2k | for (const auto& child : children()) { |
925 | 40.2k | if (child->node_type() == TExprNodeType::CAST_EXPR) { |
926 | 5.10k | auto* cast_expr = assert_cast<VCastExpr*>(child.get()); |
927 | 5.10k | DCHECK_EQ(cast_expr->get_num_children(), 1); |
928 | 5.10k | if (cast_expr->get_child(0)->is_slot_ref()) { |
929 | 4.96k | auto* column_slot_ref = assert_cast<VSlotRef*>(cast_expr->get_child(0).get()); |
930 | 4.96k | auto column_id = column_slot_ref->column_id(); |
931 | 4.96k | const auto* storage_name_type = |
932 | 4.96k | context->get_index_context()->get_storage_name_and_type_by_column_id( |
933 | 4.96k | column_id); |
934 | 4.96k | auto storage_type = remove_nullable(storage_name_type->second); |
935 | 4.96k | auto target_type = remove_nullable(cast_expr->get_target_type()); |
936 | 4.96k | auto origin_primitive_type = storage_type->get_primitive_type(); |
937 | 4.96k | auto target_primitive_type = target_type->get_primitive_type(); |
938 | 4.96k | if (is_complex_type(storage_type->get_primitive_type())) { |
939 | 537 | if (storage_type->get_primitive_type() == TYPE_ARRAY && |
940 | 537 | target_type->get_primitive_type() == TYPE_ARRAY) { |
941 | 525 | auto nested_storage_type = |
942 | 525 | (assert_cast<const DataTypeArray*>(storage_type.get())) |
943 | 525 | ->get_nested_type(); |
944 | 525 | origin_primitive_type = nested_storage_type->get_primitive_type(); |
945 | 525 | auto nested_target_type = |
946 | 525 | (assert_cast<const DataTypeArray*>(target_type.get())) |
947 | 525 | ->get_nested_type(); |
948 | 525 | target_primitive_type = nested_target_type->get_primitive_type(); |
949 | 525 | } else { |
950 | 12 | continue; |
951 | 12 | } |
952 | 537 | } |
953 | 4.94k | if (origin_primitive_type != TYPE_VARIANT && |
954 | 4.94k | (storage_type->equals(*target_type) || |
955 | 4.13k | (is_string_type(target_primitive_type) && |
956 | 2.77k | is_string_type(origin_primitive_type)))) { |
957 | 1.36k | children_exprs.emplace_back(expr_without_cast(child)); |
958 | 1.36k | } |
959 | 4.94k | } else { |
960 | 145 | return Status::OK(); // for example: cast("abc") as ipv4 case |
961 | 145 | } |
962 | 35.1k | } else { |
963 | 35.1k | children_exprs.emplace_back(child); |
964 | 35.1k | } |
965 | 40.2k | } |
966 | | |
967 | 19.4k | if (children_exprs.empty()) { |
968 | 532 | return Status::OK(); // Early exit if no children to process |
969 | 532 | } |
970 | | |
971 | 33.5k | for (const auto& child : children_exprs) { |
972 | 33.5k | if (child->is_slot_ref()) { |
973 | 14.2k | auto* column_slot_ref = assert_cast<VSlotRef*>(child.get()); |
974 | 14.2k | auto column_id = column_slot_ref->column_id(); |
975 | 14.2k | auto* iter = context->get_index_context()->get_inverted_index_iterator_by_column_id( |
976 | 14.2k | column_id); |
977 | | //column does not have inverted index |
978 | 14.2k | if (iter == nullptr) { |
979 | 4.49k | continue; |
980 | 4.49k | } |
981 | 9.79k | const auto* storage_name_type = |
982 | 9.79k | context->get_index_context()->get_storage_name_and_type_by_column_id(column_id); |
983 | 9.79k | 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.79k | iterators.emplace_back(iter); |
992 | 9.79k | data_type_with_names.emplace_back(*storage_name_type); |
993 | 9.79k | column_ids.emplace_back(column_id); |
994 | 19.2k | } 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.80k | } else { |
1001 | 2.80k | return Status::OK(); // others cases |
1002 | 2.80k | } |
1003 | 33.5k | } |
1004 | | |
1005 | | // is null or is not null has no arguments |
1006 | 16.1k | if (iterators.empty() || (arguments.empty() && !(function->get_name() == "is_not_null_pred" || |
1007 | 6.79k | function->get_name() == "is_null_pred"))) { |
1008 | 6.79k | return Status::OK(); // Nothing to evaluate or no literals to compare against |
1009 | 6.79k | } |
1010 | | |
1011 | 9.33k | const InvertedIndexAnalyzerCtx* analyzer_ctx = nullptr; |
1012 | 9.47k | if (auto index_ctx = context->get_index_context(); index_ctx != nullptr) { |
1013 | 9.47k | analyzer_ctx = index_ctx->get_analyzer_ctx_for_expr(this); |
1014 | 9.47k | } |
1015 | | |
1016 | 9.33k | auto result_bitmap = segment_v2::InvertedIndexResultBitmap(); |
1017 | | // Pass analyzer_key to function (used by match predicates for multi-analyzer index selection) |
1018 | 9.33k | auto res = function->evaluate_inverted_index(arguments, data_type_with_names, iterators, |
1019 | 9.33k | segment_num_rows, analyzer_ctx, result_bitmap); |
1020 | 9.33k | if (!res.ok()) { |
1021 | 339 | return res; |
1022 | 339 | } |
1023 | 9.00k | if (!result_bitmap.is_empty()) { |
1024 | 7.41k | index_context->set_index_result_for_expr(this, result_bitmap); |
1025 | 7.51k | for (int column_id : column_ids) { |
1026 | 7.51k | index_context->set_true_for_index_status(this, column_id); |
1027 | 7.51k | } |
1028 | 7.41k | } |
1029 | 9.00k | return Status::OK(); |
1030 | 9.33k | } |
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 | 7.73M | size_t count, ColumnPtr& result_column) const { |
1052 | 7.73M | RETURN_IF_ERROR(execute_column_impl(context, block, selector, count, result_column)); |
1053 | 7.73M | 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 | 7.73M | DCHECK(selector == nullptr || selector->size() == count); |
1058 | | // Validate type match. ColumnNothing is exempt (used as a placeholder in tests/stubs). |
1059 | 7.73M | if (!check_and_get_column<ColumnNothing>(result_column.get())) { |
1060 | 7.73M | auto result_type = execute_type(block); |
1061 | 7.73M | if (result_type != nullptr) { |
1062 | 7.73M | Status st = result_type->check_column(*result_column); |
1063 | 7.73M | 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 | 7.73M | 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 | 7.73M | } |
1078 | 7.73M | } |
1079 | 7.73M | return Status::OK(); |
1080 | 7.73M | } |
1081 | | |
1082 | | bool VExpr::fast_execute(VExprContext* context, const Selector* selector, size_t count, |
1083 | 698k | ColumnPtr& result_column) const { |
1084 | 698k | if (context->get_index_context() && |
1085 | 698k | context->get_index_context()->get_index_result_column().contains(this)) { |
1086 | | // prepare a column to save result |
1087 | 789 | result_column = filter_column_with_selector( |
1088 | 789 | context->get_index_context()->get_index_result_column()[this], selector, count); |
1089 | 789 | if (_data_type->is_nullable()) { |
1090 | 680 | result_column = make_nullable(result_column); |
1091 | 680 | } |
1092 | 789 | return true; |
1093 | 789 | } |
1094 | 698k | return false; |
1095 | 698k | } |
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.83k | bool enable_result_cache, AnnRangeSearchEvaluationResult& result) { |
1108 | 6.83k | result = {}; |
1109 | 6.83k | return Status::OK(); |
1110 | 6.83k | } |
1111 | | |
1112 | | void VExpr::prepare_ann_range_search(const doris::VectorSearchUserParams& params, |
1113 | | segment_v2::AnnRangeSearchRuntime& range_search_runtime, |
1114 | 22.8k | bool& suitable_for_ann_index) { |
1115 | 22.8k | if (!suitable_for_ann_index) { |
1116 | 0 | return; |
1117 | 0 | } |
1118 | 22.8k | for (auto& child : _children) { |
1119 | 14.3k | child->prepare_ann_range_search(params, range_search_runtime, suitable_for_ann_index); |
1120 | 14.3k | if (!suitable_for_ann_index) { |
1121 | 619 | return; |
1122 | 619 | } |
1123 | 14.3k | } |
1124 | 22.8k | } |
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 | 409k | bool* can_filter_all) const { |
1129 | 409k | ColumnPtr filter_column; |
1130 | 409k | RETURN_IF_ERROR(execute_column(context, block, nullptr, rows, filter_column)); |
1131 | 409k | if (const auto* const_column = check_and_get_column<ColumnConst>(*filter_column)) { |
1132 | | // const(nullable) or const(bool) |
1133 | 44.5k | const bool result = accept_null |
1134 | 44.5k | ? (const_column->is_null_at(0) || const_column->get_bool(0)) |
1135 | 44.5k | : (!const_column->is_null_at(0) && const_column->get_bool(0)); |
1136 | 44.5k | if (!result) { |
1137 | | // filter all |
1138 | 8.62k | *can_filter_all = true; |
1139 | 8.62k | memset(result_filter_data, 0, rows); |
1140 | 8.62k | return Status::OK(); |
1141 | 8.62k | } |
1142 | 365k | } else if (const auto* nullable_column = check_and_get_column<ColumnNullable>(*filter_column)) { |
1143 | | // nullable(bool) |
1144 | 247k | const ColumnPtr& nested_column = nullable_column->get_nested_column_ptr(); |
1145 | 247k | const IColumn::Filter& filter = assert_cast<const ColumnUInt8&>(*nested_column).get_data(); |
1146 | 247k | const auto* __restrict filter_data = filter.data(); |
1147 | 247k | const auto* __restrict null_map_data = nullable_column->get_null_map_data().data(); |
1148 | | |
1149 | 247k | 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 | 247k | } else { |
1154 | 96.2M | for (size_t i = 0; i < rows; ++i) { |
1155 | 96.0M | result_filter_data[i] &= (!null_map_data[i]) & filter_data[i]; |
1156 | 96.0M | } |
1157 | 247k | } |
1158 | | |
1159 | 247k | if (!simd::contain_one(result_filter_data, rows)) { |
1160 | 108k | *can_filter_all = true; |
1161 | 108k | return Status::OK(); |
1162 | 108k | } |
1163 | 247k | } else { |
1164 | | // bool |
1165 | 117k | const IColumn::Filter& filter = assert_cast<const ColumnUInt8&>(*filter_column).get_data(); |
1166 | 117k | const auto* __restrict filter_data = filter.data(); |
1167 | | |
1168 | 77.9M | for (size_t i = 0; i < rows; ++i) { |
1169 | 77.8M | result_filter_data[i] &= filter_data[i]; |
1170 | 77.8M | } |
1171 | | |
1172 | 117k | if (!simd::contain_one(result_filter_data, rows)) { |
1173 | 8.84k | *can_filter_all = true; |
1174 | 8.84k | return Status::OK(); |
1175 | 8.84k | } |
1176 | 117k | } |
1177 | | |
1178 | 284k | return Status::OK(); |
1179 | 409k | } |
1180 | | |
1181 | | } // namespace doris |