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