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