/root/doris/be/src/vec/exprs/vexpr.cpp
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 "vec/exprs/vexpr.h" |
19 | | |
20 | | #include <fmt/format.h> |
21 | | #include <gen_cpp/Exprs_types.h> |
22 | | #include <gen_cpp/FrontendService_types.h> |
23 | | #include <thrift/protocol/TDebugProtocol.h> |
24 | | |
25 | | #include <algorithm> |
26 | | #include <boost/algorithm/string/split.hpp> |
27 | | #include <boost/iterator/iterator_facade.hpp> |
28 | | #include <cstdint> |
29 | | #include <memory> |
30 | | #include <stack> |
31 | | #include <utility> |
32 | | |
33 | | #include "common/config.h" |
34 | | #include "common/exception.h" |
35 | | #include "common/status.h" |
36 | | #include "olap/rowset/segment_v2/ann_index/ann_search_params.h" |
37 | | #include "olap/rowset/segment_v2/ann_index/ann_topn_runtime.h" |
38 | | #include "pipeline/pipeline_task.h" |
39 | | #include "runtime/define_primitive_type.h" |
40 | | #include "vec/columns/column_vector.h" |
41 | | #include "vec/core/field.h" |
42 | | #include "vec/data_types/data_type_array.h" |
43 | | #include "vec/data_types/data_type_decimal.h" |
44 | | #include "vec/data_types/data_type_factory.hpp" |
45 | | #include "vec/data_types/data_type_nullable.h" |
46 | | #include "vec/data_types/data_type_number.h" |
47 | | #include "vec/exprs/varray_literal.h" |
48 | | #include "vec/exprs/vcase_expr.h" |
49 | | #include "vec/exprs/vcast_expr.h" |
50 | | #include "vec/exprs/vcolumn_ref.h" |
51 | | #include "vec/exprs/vcompound_pred.h" |
52 | | #include "vec/exprs/vcondition_expr.h" |
53 | | #include "vec/exprs/vectorized_fn_call.h" |
54 | | #include "vec/exprs/vexpr_context.h" |
55 | | #include "vec/exprs/vexpr_fwd.h" |
56 | | #include "vec/exprs/vin_predicate.h" |
57 | | #include "vec/exprs/vinfo_func.h" |
58 | | #include "vec/exprs/virtual_slot_ref.h" |
59 | | #include "vec/exprs/vlambda_function_call_expr.h" |
60 | | #include "vec/exprs/vlambda_function_expr.h" |
61 | | #include "vec/exprs/vliteral.h" |
62 | | #include "vec/exprs/vmap_literal.h" |
63 | | #include "vec/exprs/vmatch_predicate.h" |
64 | | #include "vec/exprs/vsearch.h" |
65 | | #include "vec/exprs/vslot_ref.h" |
66 | | #include "vec/exprs/vstruct_literal.h" |
67 | | #include "vec/utils/util.hpp" |
68 | | |
69 | | namespace doris { |
70 | | #include "common/compile_check_begin.h" |
71 | | |
72 | | class RowDescriptor; |
73 | | class RuntimeState; |
74 | | |
75 | | // NOLINTBEGIN(readability-function-cognitive-complexity) |
76 | | // NOLINTBEGIN(readability-function-size) |
77 | | TExprNode create_texpr_node_from(const void* data, const PrimitiveType& type, int precision, |
78 | 16 | int scale) { |
79 | 16 | TExprNode node; |
80 | | |
81 | 16 | switch (type) { |
82 | 0 | case TYPE_BOOLEAN: { |
83 | 0 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_BOOLEAN>(data, &node)); |
84 | 0 | break; |
85 | 0 | } |
86 | 0 | case TYPE_TINYINT: { |
87 | 0 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_TINYINT>(data, &node)); |
88 | 0 | break; |
89 | 0 | } |
90 | 0 | case TYPE_SMALLINT: { |
91 | 0 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_SMALLINT>(data, &node)); |
92 | 0 | break; |
93 | 0 | } |
94 | 5 | case TYPE_INT: { |
95 | 5 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_INT>(data, &node)); |
96 | 5 | break; |
97 | 5 | } |
98 | 11 | case TYPE_BIGINT: { |
99 | 11 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_BIGINT>(data, &node)); |
100 | 11 | break; |
101 | 11 | } |
102 | 11 | case TYPE_LARGEINT: { |
103 | 0 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_LARGEINT>(data, &node)); |
104 | 0 | break; |
105 | 0 | } |
106 | 0 | case TYPE_FLOAT: { |
107 | 0 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_FLOAT>(data, &node)); |
108 | 0 | break; |
109 | 0 | } |
110 | 0 | case TYPE_DOUBLE: { |
111 | 0 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_DOUBLE>(data, &node)); |
112 | 0 | break; |
113 | 0 | } |
114 | 0 | case TYPE_DATEV2: { |
115 | 0 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_DATEV2>(data, &node)); |
116 | 0 | break; |
117 | 0 | } |
118 | 0 | case TYPE_DATETIMEV2: { |
119 | 0 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_DATETIMEV2>(data, &node, precision, scale)); |
120 | 0 | break; |
121 | 0 | } |
122 | 0 | case TYPE_DATE: { |
123 | 0 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_DATE>(data, &node)); |
124 | 0 | break; |
125 | 0 | } |
126 | 0 | case TYPE_DATETIME: { |
127 | 0 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_DATETIME>(data, &node)); |
128 | 0 | break; |
129 | 0 | } |
130 | 0 | case TYPE_DECIMALV2: { |
131 | 0 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_DECIMALV2>(data, &node, precision, scale)); |
132 | 0 | break; |
133 | 0 | } |
134 | 0 | case TYPE_DECIMAL32: { |
135 | 0 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_DECIMAL32>(data, &node, precision, scale)); |
136 | 0 | break; |
137 | 0 | } |
138 | 0 | case TYPE_DECIMAL64: { |
139 | 0 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_DECIMAL64>(data, &node, precision, scale)); |
140 | 0 | break; |
141 | 0 | } |
142 | 0 | case TYPE_DECIMAL128I: { |
143 | 0 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_DECIMAL128I>(data, &node, precision, scale)); |
144 | 0 | break; |
145 | 0 | } |
146 | 0 | case TYPE_DECIMAL256: { |
147 | 0 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_DECIMAL256>(data, &node, precision, scale)); |
148 | 0 | break; |
149 | 0 | } |
150 | 0 | case TYPE_CHAR: { |
151 | 0 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_CHAR>(data, &node)); |
152 | 0 | break; |
153 | 0 | } |
154 | 0 | case TYPE_VARCHAR: { |
155 | 0 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_VARCHAR>(data, &node)); |
156 | 0 | break; |
157 | 0 | } |
158 | 0 | case TYPE_STRING: { |
159 | 0 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_STRING>(data, &node)); |
160 | 0 | break; |
161 | 0 | } |
162 | 0 | case TYPE_IPV4: { |
163 | 0 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_IPV4>(data, &node)); |
164 | 0 | break; |
165 | 0 | } |
166 | 0 | case TYPE_IPV6: { |
167 | 0 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_IPV6>(data, &node)); |
168 | 0 | break; |
169 | 0 | } |
170 | 0 | case TYPE_TIMEV2: { |
171 | 0 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_TIMEV2>(data, &node)); |
172 | 0 | break; |
173 | 0 | } |
174 | 0 | default: |
175 | 0 | throw Exception(ErrorCode::INTERNAL_ERROR, "runtime filter meet invalid type {}", |
176 | 0 | int(type)); |
177 | 16 | } |
178 | 16 | return node; |
179 | 16 | } |
180 | | |
181 | | TExprNode create_texpr_node_from(const vectorized::Field& field, const PrimitiveType& type, |
182 | 22 | int precision, int scale) { |
183 | 22 | TExprNode node; |
184 | 22 | switch (type) { |
185 | 1 | case TYPE_BOOLEAN: { |
186 | 1 | const auto& storage = static_cast<bool>( |
187 | 1 | field.get<typename PrimitiveTypeTraits<TYPE_BOOLEAN>::NearestFieldType>()); |
188 | 1 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_BOOLEAN>(&storage, &node)); |
189 | 1 | break; |
190 | 1 | } |
191 | 1 | case TYPE_TINYINT: { |
192 | 0 | const auto& storage = static_cast<int8_t>( |
193 | 0 | field.get<typename PrimitiveTypeTraits<TYPE_TINYINT>::NearestFieldType>()); |
194 | 0 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_TINYINT>(&storage, &node)); |
195 | 0 | break; |
196 | 0 | } |
197 | 1 | case TYPE_SMALLINT: { |
198 | 1 | const auto& storage = static_cast<int16_t>( |
199 | 1 | field.get<typename PrimitiveTypeTraits<TYPE_SMALLINT>::NearestFieldType>()); |
200 | 1 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_SMALLINT>(&storage, &node)); |
201 | 1 | break; |
202 | 1 | } |
203 | 1 | case TYPE_INT: { |
204 | 1 | const auto& storage = static_cast<int32_t>( |
205 | 1 | field.get<typename PrimitiveTypeTraits<TYPE_INT>::NearestFieldType>()); |
206 | 1 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_INT>(&storage, &node)); |
207 | 1 | break; |
208 | 1 | } |
209 | 1 | case TYPE_BIGINT: { |
210 | 1 | const auto& storage = static_cast<int64_t>( |
211 | 1 | field.get<typename PrimitiveTypeTraits<TYPE_BIGINT>::NearestFieldType>()); |
212 | 1 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_BIGINT>(&storage, &node)); |
213 | 1 | break; |
214 | 1 | } |
215 | 1 | case TYPE_LARGEINT: { |
216 | 1 | const auto& storage = static_cast<int128_t>( |
217 | 1 | field.get<typename PrimitiveTypeTraits<TYPE_LARGEINT>::NearestFieldType>()); |
218 | 1 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_LARGEINT>(&storage, &node)); |
219 | 1 | break; |
220 | 1 | } |
221 | 1 | case TYPE_FLOAT: { |
222 | 1 | const auto& storage = static_cast<float>( |
223 | 1 | field.get<typename PrimitiveTypeTraits<TYPE_FLOAT>::NearestFieldType>()); |
224 | 1 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_FLOAT>(&storage, &node)); |
225 | 1 | break; |
226 | 1 | } |
227 | 1 | case TYPE_DOUBLE: { |
228 | 1 | const auto& storage = static_cast<double>( |
229 | 1 | field.get<typename PrimitiveTypeTraits<TYPE_DOUBLE>::NearestFieldType>()); |
230 | 1 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_DOUBLE>(&storage, &node)); |
231 | 1 | break; |
232 | 1 | } |
233 | 1 | case TYPE_DATEV2: { |
234 | 1 | DateV2Value<DateV2ValueType> storage = |
235 | 1 | binary_cast<uint32_t, DateV2Value<DateV2ValueType>>(static_cast<uint32_t>( |
236 | 1 | field.get<typename PrimitiveTypeTraits<TYPE_DATEV2>::NearestFieldType>())); |
237 | | |
238 | 1 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_DATEV2>(&storage, &node)); |
239 | 1 | break; |
240 | 1 | } |
241 | 1 | case TYPE_DATETIMEV2: { |
242 | 1 | DateV2Value<DateTimeV2ValueType> storage = binary_cast<uint64_t, |
243 | 1 | DateV2Value<DateTimeV2ValueType>>( |
244 | 1 | field.get<typename PrimitiveTypeTraits<TYPE_DATETIMEV2>::NearestFieldType>()); |
245 | | |
246 | 1 | THROW_IF_ERROR( |
247 | 1 | create_texpr_literal_node<TYPE_DATETIMEV2>(&storage, &node, precision, scale)); |
248 | 1 | break; |
249 | 1 | } |
250 | 1 | case TYPE_DATE: { |
251 | 1 | VecDateTimeValue storage = binary_cast<int64_t, doris::VecDateTimeValue>( |
252 | 1 | field.get<typename PrimitiveTypeTraits<TYPE_DATE>::NearestFieldType>()); |
253 | 1 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_DATE>(&storage, &node)); |
254 | 1 | break; |
255 | 1 | } |
256 | 1 | case TYPE_DATETIME: { |
257 | 1 | VecDateTimeValue storage = binary_cast<int64_t, doris::VecDateTimeValue>( |
258 | 1 | field.get<typename PrimitiveTypeTraits<TYPE_DATETIME>::NearestFieldType>()); |
259 | 1 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_DATETIME>(&storage, &node)); |
260 | 1 | break; |
261 | 1 | } |
262 | 1 | case TYPE_DECIMALV2: { |
263 | 1 | const auto& storage = |
264 | 1 | field.get<typename PrimitiveTypeTraits<TYPE_DECIMALV2>::NearestFieldType>() |
265 | 1 | .get_value(); |
266 | | |
267 | 1 | THROW_IF_ERROR( |
268 | 1 | create_texpr_literal_node<TYPE_DECIMALV2>(&storage, &node, precision, scale)); |
269 | 1 | break; |
270 | 1 | } |
271 | 2 | case TYPE_DECIMAL32: { |
272 | 2 | const auto& storage = |
273 | 2 | field.get<typename PrimitiveTypeTraits<TYPE_DECIMAL32>::NearestFieldType>() |
274 | 2 | .get_value(); |
275 | 2 | THROW_IF_ERROR( |
276 | 2 | create_texpr_literal_node<TYPE_DECIMAL32>(&storage, &node, precision, scale)); |
277 | 2 | break; |
278 | 2 | } |
279 | 2 | case TYPE_DECIMAL64: { |
280 | 2 | const auto& storage = |
281 | 2 | field.get<typename PrimitiveTypeTraits<TYPE_DECIMAL64>::NearestFieldType>() |
282 | 2 | .get_value(); |
283 | 2 | THROW_IF_ERROR( |
284 | 2 | create_texpr_literal_node<TYPE_DECIMAL64>(&storage, &node, precision, scale)); |
285 | 2 | break; |
286 | 2 | } |
287 | 2 | case TYPE_DECIMAL128I: { |
288 | 2 | const auto& storage = |
289 | 2 | field.get<typename PrimitiveTypeTraits<TYPE_DECIMAL128I>::NearestFieldType>() |
290 | 2 | .get_value(); |
291 | 2 | THROW_IF_ERROR( |
292 | 2 | create_texpr_literal_node<TYPE_DECIMAL128I>(&storage, &node, precision, scale)); |
293 | 2 | break; |
294 | 2 | } |
295 | 2 | case TYPE_DECIMAL256: { |
296 | 2 | const auto& storage = |
297 | 2 | field.get<typename PrimitiveTypeTraits<TYPE_DECIMAL256>::NearestFieldType>() |
298 | 2 | .get_value(); |
299 | 2 | THROW_IF_ERROR( |
300 | 2 | create_texpr_literal_node<TYPE_DECIMAL256>(&storage, &node, precision, scale)); |
301 | 2 | break; |
302 | 2 | } |
303 | 2 | case TYPE_CHAR: { |
304 | 0 | const auto& storage = |
305 | 0 | field.get<typename PrimitiveTypeTraits<TYPE_CHAR>::NearestFieldType>(); |
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 = |
311 | 0 | field.get<typename PrimitiveTypeTraits<TYPE_VARCHAR>::NearestFieldType>(); |
312 | 0 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_VARCHAR>(&storage, &node)); |
313 | 0 | break; |
314 | 0 | } |
315 | 1 | case TYPE_STRING: { |
316 | 1 | const auto& storage = |
317 | 1 | field.get<typename PrimitiveTypeTraits<TYPE_STRING>::NearestFieldType>(); |
318 | 1 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_STRING>(&storage, &node)); |
319 | 1 | break; |
320 | 1 | } |
321 | 1 | case TYPE_IPV4: { |
322 | 0 | const auto& storage = |
323 | 0 | field.get<typename PrimitiveTypeTraits<TYPE_IPV4>::NearestFieldType>(); |
324 | 0 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_IPV4>(&storage, &node)); |
325 | 0 | break; |
326 | 0 | } |
327 | 0 | case TYPE_IPV6: { |
328 | 0 | const auto& storage = |
329 | 0 | field.get<typename PrimitiveTypeTraits<TYPE_IPV6>::NearestFieldType>(); |
330 | 0 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_IPV6>(&storage, &node)); |
331 | 0 | break; |
332 | 0 | } |
333 | 1 | case TYPE_TIMEV2: { |
334 | 1 | const auto& storage = |
335 | 1 | field.get<typename PrimitiveTypeTraits<TYPE_TIMEV2>::NearestFieldType>(); |
336 | 1 | THROW_IF_ERROR(create_texpr_literal_node<TYPE_TIMEV2>(&storage, &node)); |
337 | 1 | break; |
338 | 1 | } |
339 | 1 | default: |
340 | 0 | throw Exception(ErrorCode::INTERNAL_ERROR, "runtime filter meet invalid type {}", |
341 | 0 | int(type)); |
342 | 22 | } |
343 | 22 | return node; |
344 | 22 | } |
345 | | |
346 | | // NOLINTEND(readability-function-size) |
347 | | // NOLINTEND(readability-function-cognitive-complexity) |
348 | | } // namespace doris |
349 | | |
350 | | namespace doris::vectorized { |
351 | | |
352 | 0 | bool VExpr::is_acting_on_a_slot(const VExpr& expr) { |
353 | 0 | const auto& children = expr.children(); |
354 | |
|
355 | 0 | auto is_a_slot = std::any_of(children.begin(), children.end(), |
356 | 0 | [](const auto& child) { return is_acting_on_a_slot(*child); }); |
357 | |
|
358 | 0 | return is_a_slot ? true |
359 | 0 | : (expr.node_type() == TExprNodeType::SLOT_REF || |
360 | 0 | expr.node_type() == TExprNodeType::VIRTUAL_SLOT_REF); |
361 | 0 | } |
362 | | |
363 | | VExpr::VExpr(const TExprNode& node) |
364 | 400k | : _node_type(node.node_type), |
365 | 400k | _opcode(node.__isset.opcode ? node.opcode : TExprOpcode::INVALID_OPCODE) { |
366 | 400k | if (node.__isset.fn) { |
367 | 72 | _fn = node.fn; |
368 | 72 | } |
369 | | |
370 | 400k | bool is_nullable = true; |
371 | 400k | if (node.__isset.is_nullable) { |
372 | 396k | is_nullable = node.is_nullable; |
373 | 396k | } |
374 | | // If we define null literal ,should make nullable data type to get correct field instead of undefined ptr |
375 | 400k | if (node.node_type == TExprNodeType::NULL_LITERAL) { |
376 | 1 | CHECK(is_nullable); |
377 | 1 | } |
378 | 400k | _data_type = get_data_type_with_default_argument( |
379 | 400k | DataTypeFactory::instance().create_data_type(node.type, is_nullable)); |
380 | 400k | } |
381 | | |
382 | 0 | VExpr::VExpr(const VExpr& vexpr) = default; |
383 | | |
384 | | VExpr::VExpr(DataTypePtr type, bool is_slotref) |
385 | 9 | : _opcode(TExprOpcode::INVALID_OPCODE), |
386 | 9 | _data_type(get_data_type_with_default_argument(type)) { |
387 | 9 | if (is_slotref) { |
388 | 0 | _node_type = TExprNodeType::SLOT_REF; |
389 | 0 | } |
390 | 9 | } |
391 | | |
392 | 258k | Status VExpr::prepare(RuntimeState* state, const RowDescriptor& row_desc, VExprContext* context) { |
393 | 258k | ++context->_depth_num; |
394 | 258k | if (context->_depth_num > config::max_depth_of_expr_tree) { |
395 | 0 | return Status::Error<ErrorCode::EXCEEDED_LIMIT>( |
396 | 0 | "The depth of the expression tree is too big, make it less than {}", |
397 | 0 | config::max_depth_of_expr_tree); |
398 | 0 | } |
399 | | |
400 | 258k | for (auto& i : _children) { |
401 | 232 | RETURN_IF_ERROR(i->prepare(state, row_desc, context)); |
402 | 232 | } |
403 | 258k | --context->_depth_num; |
404 | | #ifndef BE_TEST |
405 | | _enable_inverted_index_query = state->query_options().enable_inverted_index_query; |
406 | | #endif |
407 | 258k | return Status::OK(); |
408 | 258k | } |
409 | | |
410 | | Status VExpr::open(RuntimeState* state, VExprContext* context, |
411 | 512k | FunctionContext::FunctionStateScope scope) { |
412 | 512k | for (auto& i : _children) { |
413 | 66 | RETURN_IF_ERROR(i->open(state, context, scope)); |
414 | 66 | } |
415 | 512k | if (scope == FunctionContext::FRAGMENT_LOCAL) { |
416 | 258k | RETURN_IF_ERROR(VExpr::get_const_col(context, nullptr)); |
417 | 258k | } |
418 | 512k | return Status::OK(); |
419 | 512k | } |
420 | | |
421 | 514k | void VExpr::close(VExprContext* context, FunctionContext::FunctionStateScope scope) { |
422 | 514k | for (auto& i : _children) { |
423 | 251 | i->close(context, scope); |
424 | 251 | } |
425 | 514k | } |
426 | | |
427 | | // NOLINTBEGIN(readability-function-size) |
428 | 400k | Status VExpr::create_expr(const TExprNode& expr_node, VExprSPtr& expr) { |
429 | 400k | try { |
430 | 400k | switch (expr_node.node_type) { |
431 | 0 | case TExprNodeType::BOOL_LITERAL: |
432 | 31 | case TExprNodeType::INT_LITERAL: |
433 | 31 | case TExprNodeType::LARGE_INT_LITERAL: |
434 | 32 | case TExprNodeType::IPV4_LITERAL: |
435 | 32 | case TExprNodeType::IPV6_LITERAL: |
436 | 143 | case TExprNodeType::FLOAT_LITERAL: |
437 | 144 | case TExprNodeType::DECIMAL_LITERAL: |
438 | 147 | case TExprNodeType::DATE_LITERAL: |
439 | 147 | case TExprNodeType::TIMEV2_LITERAL: |
440 | 153 | case TExprNodeType::STRING_LITERAL: |
441 | 153 | case TExprNodeType::JSON_LITERAL: |
442 | 153 | case TExprNodeType::VARBINARY_LITERAL: |
443 | 154 | case TExprNodeType::NULL_LITERAL: { |
444 | 154 | expr = VLiteral::create_shared(expr_node); |
445 | 154 | break; |
446 | 153 | } |
447 | 13 | case TExprNodeType::ARRAY_LITERAL: { |
448 | 13 | expr = VArrayLiteral::create_shared(expr_node); |
449 | 13 | break; |
450 | 153 | } |
451 | 0 | case TExprNodeType::MAP_LITERAL: { |
452 | 0 | expr = VMapLiteral::create_shared(expr_node); |
453 | 0 | break; |
454 | 153 | } |
455 | 0 | case TExprNodeType::STRUCT_LITERAL: { |
456 | 0 | expr = VStructLiteral::create_shared(expr_node); |
457 | 0 | break; |
458 | 153 | } |
459 | 400k | case TExprNodeType::SLOT_REF: { |
460 | 400k | if (expr_node.slot_ref.__isset.is_virtual_slot && expr_node.slot_ref.is_virtual_slot) { |
461 | 11 | expr = VirtualSlotRef::create_shared(expr_node); |
462 | 11 | expr->_node_type = TExprNodeType::VIRTUAL_SLOT_REF; |
463 | 400k | } else { |
464 | 400k | expr = VSlotRef::create_shared(expr_node); |
465 | 400k | } |
466 | 400k | break; |
467 | 153 | } |
468 | 0 | case TExprNodeType::COLUMN_REF: { |
469 | 0 | expr = VColumnRef::create_shared(expr_node); |
470 | 0 | break; |
471 | 153 | } |
472 | 17 | case TExprNodeType::COMPOUND_PRED: { |
473 | 17 | expr = VCompoundPred::create_shared(expr_node); |
474 | 17 | break; |
475 | 153 | } |
476 | 0 | case TExprNodeType::LAMBDA_FUNCTION_EXPR: { |
477 | 0 | expr = VLambdaFunctionExpr::create_shared(expr_node); |
478 | 0 | break; |
479 | 153 | } |
480 | 0 | case TExprNodeType::LAMBDA_FUNCTION_CALL_EXPR: { |
481 | 0 | expr = VLambdaFunctionCallExpr::create_shared(expr_node); |
482 | 0 | break; |
483 | 153 | } |
484 | 0 | case TExprNodeType::ARITHMETIC_EXPR: |
485 | 44 | case TExprNodeType::BINARY_PRED: |
486 | 45 | case TExprNodeType::NULL_AWARE_BINARY_PRED: |
487 | 45 | case TExprNodeType::COMPUTE_FUNCTION_CALL: { |
488 | 45 | expr = VectorizedFnCall::create_shared(expr_node); |
489 | 45 | break; |
490 | 45 | } |
491 | 21 | case TExprNodeType::FUNCTION_CALL: { |
492 | 21 | if (expr_node.fn.name.function_name == "if") { |
493 | 0 | expr = VectorizedIfExpr::create_shared(expr_node); |
494 | 0 | break; |
495 | 21 | } else if (expr_node.fn.name.function_name == "ifnull" || |
496 | 21 | expr_node.fn.name.function_name == "nvl") { |
497 | 0 | expr = VectorizedIfNullExpr::create_shared(expr_node); |
498 | 0 | break; |
499 | 21 | } else if (expr_node.fn.name.function_name == "coalesce") { |
500 | 0 | expr = VectorizedCoalesceExpr::create_shared(expr_node); |
501 | 0 | break; |
502 | 0 | } |
503 | 21 | expr = VectorizedFnCall::create_shared(expr_node); |
504 | 21 | break; |
505 | 21 | } |
506 | 0 | case TExprNodeType::MATCH_PRED: { |
507 | 0 | expr = VMatchPredicate::create_shared(expr_node); |
508 | 0 | break; |
509 | 21 | } |
510 | 2 | case TExprNodeType::CAST_EXPR: { |
511 | 2 | expr = VCastExpr::create_shared(expr_node); |
512 | 2 | break; |
513 | 21 | } |
514 | 0 | case TExprNodeType::TRY_CAST_EXPR: { |
515 | 0 | expr = TryCastExpr::create_shared(expr_node); |
516 | 0 | break; |
517 | 21 | } |
518 | 5 | case TExprNodeType::IN_PRED: { |
519 | 5 | expr = VInPredicate::create_shared(expr_node); |
520 | 5 | break; |
521 | 21 | } |
522 | 0 | case TExprNodeType::CASE_EXPR: { |
523 | 0 | if (!expr_node.__isset.case_expr) { |
524 | 0 | return Status::InternalError("Case expression not set in thrift node"); |
525 | 0 | } |
526 | 0 | expr = VCaseExpr::create_shared(expr_node); |
527 | 0 | break; |
528 | 0 | } |
529 | 0 | case TExprNodeType::INFO_FUNC: { |
530 | 0 | expr = VInfoFunc::create_shared(expr_node); |
531 | 0 | break; |
532 | 0 | } |
533 | 0 | case TExprNodeType::SEARCH_EXPR: { |
534 | 0 | expr = VSearchExpr::create_shared(expr_node); |
535 | 0 | break; |
536 | 0 | } |
537 | 0 | default: |
538 | 0 | return Status::InternalError("Unknown expr node type: {}", expr_node.node_type); |
539 | 400k | } |
540 | 400k | } catch (const Exception& e) { |
541 | 0 | if (e.code() == ErrorCode::INTERNAL_ERROR) { |
542 | 0 | return Status::InternalError("Create Expr failed because {}\nTExprNode={}", e.what(), |
543 | 0 | apache::thrift::ThriftDebugString(expr_node)); |
544 | 0 | } |
545 | 0 | return Status::Error<false>(e.code(), "Create Expr failed because {}", e.what()); |
546 | 0 | LOG(WARNING) << "create expr failed, TExprNode={}, reason={}" |
547 | 0 | << apache::thrift::ThriftDebugString(expr_node) << e.what(); |
548 | 0 | } |
549 | 400k | if (!expr->data_type()) { |
550 | 0 | return Status::InvalidArgument("Unknown expr type: {}", expr_node.node_type); |
551 | 0 | } |
552 | 400k | return Status::OK(); |
553 | 400k | } |
554 | | // NOLINTEND(readability-function-size) |
555 | | |
556 | | Status VExpr::create_tree_from_thrift(const std::vector<TExprNode>& nodes, int* node_idx, |
557 | 400k | VExprSPtr& root_expr, VExprContextSPtr& ctx) { |
558 | | // propagate error case |
559 | 400k | if (*node_idx >= nodes.size()) { |
560 | 0 | return Status::InternalError("Failed to reconstruct expression tree from thrift."); |
561 | 0 | } |
562 | | |
563 | | // create root expr |
564 | 400k | int root_children = nodes[*node_idx].num_children; |
565 | 400k | VExprSPtr root; |
566 | 400k | RETURN_IF_ERROR(create_expr(nodes[*node_idx], root)); |
567 | 400k | DCHECK(root != nullptr); |
568 | 400k | root_expr = root; |
569 | 400k | ctx = std::make_shared<VExprContext>(root); |
570 | | // short path for leaf node |
571 | 400k | if (root_children <= 0) { |
572 | 399k | return Status::OK(); |
573 | 399k | } |
574 | | |
575 | | // non-recursive traversal |
576 | 45 | using VExprSPtrCountPair = std::pair<VExprSPtr, int>; |
577 | 45 | std::stack<std::shared_ptr<VExprSPtrCountPair>> s; |
578 | 45 | s.emplace(std::make_shared<VExprSPtrCountPair>(root, root_children)); |
579 | 312 | while (!s.empty()) { |
580 | | // copy the shared ptr resource to avoid dangling reference |
581 | 267 | auto parent = s.top(); |
582 | | // Decrement or pop |
583 | 267 | if (parent->second > 1) { |
584 | 173 | parent->second -= 1; |
585 | 173 | } else { |
586 | 94 | s.pop(); |
587 | 94 | } |
588 | | |
589 | 267 | DCHECK(parent->first != nullptr); |
590 | 267 | if (++*node_idx >= nodes.size()) { |
591 | 0 | return Status::InternalError("Failed to reconstruct expression tree from thrift."); |
592 | 0 | } |
593 | | |
594 | 267 | VExprSPtr expr; |
595 | 267 | RETURN_IF_ERROR(create_expr(nodes[*node_idx], expr)); |
596 | 267 | DCHECK(expr != nullptr); |
597 | 267 | parent->first->add_child(expr); |
598 | | // push to stack if has children |
599 | 267 | int num_children = nodes[*node_idx].num_children; |
600 | 267 | if (num_children > 0) { |
601 | 49 | s.emplace(std::make_shared<VExprSPtrCountPair>(expr, num_children)); |
602 | 49 | } |
603 | 267 | } |
604 | 45 | return Status::OK(); |
605 | 45 | } |
606 | | |
607 | 400k | Status VExpr::create_expr_tree(const TExpr& texpr, VExprContextSPtr& ctx) { |
608 | 400k | if (texpr.nodes.empty()) { |
609 | 7 | ctx = nullptr; |
610 | 7 | return Status::OK(); |
611 | 7 | } |
612 | 400k | int node_idx = 0; |
613 | 400k | VExprSPtr e; |
614 | 400k | Status status = create_tree_from_thrift(texpr.nodes, &node_idx, e, ctx); |
615 | 400k | if (status.ok() && node_idx + 1 != texpr.nodes.size()) { |
616 | 0 | status = Status::InternalError( |
617 | 0 | "Expression tree only partially reconstructed. Not all thrift nodes were " |
618 | 0 | "used."); |
619 | 0 | } |
620 | 400k | if (!status.ok()) { |
621 | 0 | LOG(ERROR) << "Could not construct expr tree.\n" |
622 | 0 | << status << "\n" |
623 | 0 | << apache::thrift::ThriftDebugString(texpr); |
624 | 0 | } |
625 | 400k | return status; |
626 | 400k | } |
627 | | |
628 | 28.1k | Status VExpr::create_expr_trees(const std::vector<TExpr>& texprs, VExprContextSPtrs& ctxs) { |
629 | 28.1k | ctxs.clear(); |
630 | 70.0k | for (const auto& texpr : texprs) { |
631 | 70.0k | VExprContextSPtr ctx; |
632 | 70.0k | RETURN_IF_ERROR(create_expr_tree(texpr, ctx)); |
633 | 70.0k | ctxs.push_back(ctx); |
634 | 70.0k | } |
635 | 28.1k | return Status::OK(); |
636 | 28.1k | } |
637 | | |
638 | | Status VExpr::check_expr_output_type(const VExprContextSPtrs& ctxs, |
639 | 24.0k | const RowDescriptor& output_row_desc) { |
640 | 24.0k | if (ctxs.empty()) { |
641 | 2 | return Status::OK(); |
642 | 2 | } |
643 | 24.0k | auto name_and_types = VectorizedUtils::create_name_and_data_types(output_row_desc); |
644 | 24.0k | if (ctxs.size() != name_and_types.size()) { |
645 | 0 | return Status::InternalError( |
646 | 0 | "output type size not match expr size {} , expected output size {} ", ctxs.size(), |
647 | 0 | name_and_types.size()); |
648 | 0 | } |
649 | 65.9k | auto check_type_can_be_converted = [](DataTypePtr& from, DataTypePtr& to) -> bool { |
650 | 65.9k | if (to->equals(*from)) { |
651 | 65.9k | return true; |
652 | 65.9k | } |
653 | 0 | if (to->is_nullable() && !from->is_nullable()) { |
654 | 0 | return remove_nullable(to)->equals(*from); |
655 | 0 | } |
656 | 0 | return false; |
657 | 0 | }; |
658 | 89.9k | for (int i = 0; i < ctxs.size(); i++) { |
659 | 65.9k | auto real_expr_type = get_data_type_with_default_argument(ctxs[i]->root()->data_type()); |
660 | 65.9k | auto&& [name, expected_type] = name_and_types[i]; |
661 | 65.9k | if (!check_type_can_be_converted(real_expr_type, expected_type)) { |
662 | 0 | return Status::InternalError( |
663 | 0 | "output type not match expr type, col name {} , expected type {} , real type " |
664 | 0 | "{}", |
665 | 0 | name, expected_type->get_name(), real_expr_type->get_name()); |
666 | 0 | } |
667 | 65.9k | } |
668 | 24.0k | return Status::OK(); |
669 | 24.0k | } |
670 | | |
671 | | Status VExpr::prepare(const VExprContextSPtrs& ctxs, RuntimeState* state, |
672 | 148k | const RowDescriptor& row_desc) { |
673 | 258k | for (auto ctx : ctxs) { |
674 | 258k | RETURN_IF_ERROR(ctx->prepare(state, row_desc)); |
675 | 258k | } |
676 | 148k | return Status::OK(); |
677 | 148k | } |
678 | | |
679 | 148k | Status VExpr::open(const VExprContextSPtrs& ctxs, RuntimeState* state) { |
680 | 258k | for (const auto& ctx : ctxs) { |
681 | 258k | RETURN_IF_ERROR(ctx->open(state)); |
682 | 258k | } |
683 | 148k | return Status::OK(); |
684 | 148k | } |
685 | | |
686 | | Status VExpr::clone_if_not_exists(const VExprContextSPtrs& ctxs, RuntimeState* state, |
687 | 0 | VExprContextSPtrs& new_ctxs) { |
688 | 0 | if (!new_ctxs.empty()) { |
689 | | // 'ctxs' was already cloned into '*new_ctxs', nothing to do. |
690 | 0 | DCHECK_EQ(new_ctxs.size(), ctxs.size()); |
691 | 0 | for (auto& new_ctx : new_ctxs) { |
692 | 0 | DCHECK(new_ctx->_is_clone); |
693 | 0 | } |
694 | 0 | return Status::OK(); |
695 | 0 | } |
696 | 0 | new_ctxs.resize(ctxs.size()); |
697 | 0 | for (int i = 0; i < ctxs.size(); ++i) { |
698 | 0 | RETURN_IF_ERROR(ctxs[i]->clone(state, new_ctxs[i])); |
699 | 0 | } |
700 | 0 | return Status::OK(); |
701 | 0 | } |
702 | | |
703 | 7 | std::string VExpr::debug_string() const { |
704 | | // TODO: implement partial debug string for member vars |
705 | 7 | std::stringstream out; |
706 | 7 | out << " type=" << _data_type->get_name(); |
707 | | |
708 | 7 | if (!_children.empty()) { |
709 | 0 | out << " children=" << debug_string(_children); |
710 | 0 | } |
711 | | |
712 | 7 | return out.str(); |
713 | 7 | } |
714 | | |
715 | 0 | std::string VExpr::debug_string(const VExprSPtrs& exprs) { |
716 | 0 | std::stringstream out; |
717 | 0 | out << "["; |
718 | |
|
719 | 0 | for (int i = 0; i < exprs.size(); ++i) { |
720 | 0 | out << (i == 0 ? "" : " ") << exprs[i]->debug_string(); |
721 | 0 | } |
722 | |
|
723 | 0 | out << "]"; |
724 | 0 | return out.str(); |
725 | 0 | } |
726 | | |
727 | 0 | std::string VExpr::debug_string(const VExprContextSPtrs& ctxs) { |
728 | 0 | VExprSPtrs exprs; |
729 | 0 | for (const auto& ctx : ctxs) { |
730 | 0 | exprs.push_back(ctx->root()); |
731 | 0 | } |
732 | 0 | return debug_string(exprs); |
733 | 0 | } |
734 | | |
735 | 510 | bool VExpr::is_constant() const { |
736 | 510 | return std::all_of(_children.begin(), _children.end(), |
737 | 510 | [](const VExprSPtr& expr) { return expr->is_constant(); }); |
738 | 510 | } |
739 | | |
740 | | Status VExpr::get_const_col(VExprContext* context, |
741 | 258k | std::shared_ptr<ColumnPtrWrapper>* column_wrapper) { |
742 | 258k | if (!is_constant()) { |
743 | 258k | return Status::OK(); |
744 | 258k | } |
745 | | |
746 | 112 | if (_constant_col != nullptr) { |
747 | 27 | DCHECK(column_wrapper != nullptr); |
748 | 27 | *column_wrapper = _constant_col; |
749 | 27 | return Status::OK(); |
750 | 27 | } |
751 | | |
752 | 85 | int result = -1; |
753 | 85 | Block block; |
754 | | // If block is empty, some functions will produce no result. So we insert a column with |
755 | | // single value here. |
756 | 85 | block.insert({ColumnUInt8::create(1), std::make_shared<DataTypeUInt8>(), ""}); |
757 | | |
758 | 85 | _getting_const_col = true; |
759 | 85 | RETURN_IF_ERROR(execute(context, &block, &result)); |
760 | 85 | _getting_const_col = false; |
761 | | |
762 | 85 | DCHECK(result != -1); |
763 | 85 | const auto& column = block.get_by_position(result).column; |
764 | 85 | _constant_col = std::make_shared<ColumnPtrWrapper>(column); |
765 | 85 | if (column_wrapper != nullptr) { |
766 | 0 | *column_wrapper = _constant_col; |
767 | 0 | } |
768 | | |
769 | 85 | return Status::OK(); |
770 | 85 | } |
771 | | |
772 | 79 | void VExpr::register_function_context(RuntimeState* state, VExprContext* context) { |
773 | 79 | std::vector<DataTypePtr> arg_types; |
774 | 156 | for (auto& i : _children) { |
775 | 156 | arg_types.push_back(i->data_type()); |
776 | 156 | } |
777 | | |
778 | 79 | _fn_context_index = context->register_function_context(state, _data_type, arg_types); |
779 | 79 | } |
780 | | |
781 | | Status VExpr::init_function_context(RuntimeState* state, VExprContext* context, |
782 | | FunctionContext::FunctionStateScope scope, |
783 | 39 | const FunctionBasePtr& function) const { |
784 | 39 | FunctionContext* fn_ctx = context->fn_context(_fn_context_index); |
785 | 39 | if (scope == FunctionContext::FRAGMENT_LOCAL) { |
786 | 27 | std::vector<std::shared_ptr<ColumnPtrWrapper>> constant_cols; |
787 | 52 | for (auto c : _children) { |
788 | 52 | std::shared_ptr<ColumnPtrWrapper> const_col; |
789 | 52 | RETURN_IF_ERROR(c->get_const_col(context, &const_col)); |
790 | 52 | constant_cols.push_back(const_col); |
791 | 52 | } |
792 | 27 | fn_ctx->set_constant_cols(constant_cols); |
793 | 27 | } |
794 | | |
795 | 39 | if (scope == FunctionContext::FRAGMENT_LOCAL) { |
796 | 27 | RETURN_IF_ERROR(function->open(fn_ctx, FunctionContext::FRAGMENT_LOCAL)); |
797 | 27 | } |
798 | 39 | RETURN_IF_ERROR(function->open(fn_ctx, FunctionContext::THREAD_LOCAL)); |
799 | 39 | return Status::OK(); |
800 | 39 | } |
801 | | |
802 | | void VExpr::close_function_context(VExprContext* context, FunctionContext::FunctionStateScope scope, |
803 | 135 | const FunctionBasePtr& function) const { |
804 | 135 | if (_fn_context_index != -1) { |
805 | 39 | FunctionContext* fn_ctx = context->fn_context(_fn_context_index); |
806 | | // `close_function_context` is called in VExprContext's destructor so do not throw exceptions here. |
807 | 39 | static_cast<void>(function->close(fn_ctx, FunctionContext::THREAD_LOCAL)); |
808 | 39 | if (scope == FunctionContext::FRAGMENT_LOCAL) { |
809 | 27 | static_cast<void>(function->close(fn_ctx, FunctionContext::FRAGMENT_LOCAL)); |
810 | 27 | } |
811 | 39 | } |
812 | 135 | } |
813 | | |
814 | 0 | Status VExpr::check_constant(const Block& block, ColumnNumbers arguments) const { |
815 | 0 | if (is_constant() && !VectorizedUtils::all_arguments_are_constant(block, arguments)) { |
816 | 0 | return Status::InternalError("const check failed, expr={}", debug_string()); |
817 | 0 | } |
818 | 0 | return Status::OK(); |
819 | 0 | } |
820 | | |
821 | 0 | uint64_t VExpr::get_digest(uint64_t seed) const { |
822 | 0 | auto digest = seed; |
823 | 0 | for (auto child : _children) { |
824 | 0 | digest = child->get_digest(digest); |
825 | 0 | if (digest == 0) { |
826 | 0 | return 0; |
827 | 0 | } |
828 | 0 | } |
829 | | |
830 | 0 | auto& fn_name = _fn.name.function_name; |
831 | 0 | if (!fn_name.empty()) { |
832 | 0 | digest = HashUtil::hash64(fn_name.c_str(), fn_name.size(), digest); |
833 | 0 | } else { |
834 | 0 | digest = HashUtil::hash64((const char*)&_node_type, sizeof(_node_type), digest); |
835 | 0 | digest = HashUtil::hash64((const char*)&_opcode, sizeof(_opcode), digest); |
836 | 0 | } |
837 | 0 | return digest; |
838 | 0 | } |
839 | | |
840 | 0 | ColumnPtr VExpr::get_result_from_const(size_t count) const { |
841 | 0 | return ColumnConst::create(_constant_col->column_ptr, count); |
842 | 0 | } |
843 | | |
844 | | Status VExpr::_evaluate_inverted_index(VExprContext* context, const FunctionBasePtr& function, |
845 | 1 | uint32_t segment_num_rows) { |
846 | | // Pre-allocate vectors based on an estimated or known size |
847 | 1 | std::vector<segment_v2::IndexIterator*> iterators; |
848 | 1 | std::vector<vectorized::IndexFieldNameAndTypePair> data_type_with_names; |
849 | 1 | std::vector<int> column_ids; |
850 | 1 | vectorized::ColumnsWithTypeAndName arguments; |
851 | 1 | VExprSPtrs children_exprs; |
852 | | |
853 | | // Reserve space to avoid multiple reallocations |
854 | 1 | const size_t estimated_size = get_num_children(); |
855 | 1 | iterators.reserve(estimated_size); |
856 | 1 | data_type_with_names.reserve(estimated_size); |
857 | 1 | column_ids.reserve(estimated_size); |
858 | 1 | children_exprs.reserve(estimated_size); |
859 | | |
860 | 1 | auto index_context = context->get_index_context(); |
861 | | |
862 | | // if child is cast expr, we need to ensure target data type is the same with storage data type. |
863 | | // or they are all string type |
864 | | // and if data type is array, we need to get the nested data type to ensure that. |
865 | 2 | for (const auto& child : children()) { |
866 | 2 | if (child->node_type() == TExprNodeType::CAST_EXPR) { |
867 | 1 | auto* cast_expr = assert_cast<VCastExpr*>(child.get()); |
868 | 1 | DCHECK_EQ(cast_expr->get_num_children(), 1); |
869 | 1 | if (cast_expr->get_child(0)->is_slot_ref()) { |
870 | 0 | auto* column_slot_ref = assert_cast<VSlotRef*>(cast_expr->get_child(0).get()); |
871 | 0 | auto column_id = column_slot_ref->column_id(); |
872 | 0 | const auto* storage_name_type = |
873 | 0 | context->get_index_context()->get_storage_name_and_type_by_column_id( |
874 | 0 | column_id); |
875 | 0 | auto storage_type = remove_nullable(storage_name_type->second); |
876 | 0 | auto target_type = remove_nullable(cast_expr->get_target_type()); |
877 | 0 | auto origin_primitive_type = storage_type->get_primitive_type(); |
878 | 0 | auto target_primitive_type = target_type->get_primitive_type(); |
879 | 0 | if (is_complex_type(storage_type->get_primitive_type())) { |
880 | 0 | if (storage_type->get_primitive_type() == TYPE_ARRAY && |
881 | 0 | target_type->get_primitive_type() == TYPE_ARRAY) { |
882 | 0 | auto nested_storage_type = |
883 | 0 | (assert_cast<const DataTypeArray*>(storage_type.get())) |
884 | 0 | ->get_nested_type(); |
885 | 0 | origin_primitive_type = nested_storage_type->get_primitive_type(); |
886 | 0 | auto nested_target_type = |
887 | 0 | (assert_cast<const DataTypeArray*>(target_type.get())) |
888 | 0 | ->get_nested_type(); |
889 | 0 | target_primitive_type = nested_target_type->get_primitive_type(); |
890 | 0 | } else { |
891 | 0 | continue; |
892 | 0 | } |
893 | 0 | } |
894 | 0 | if (origin_primitive_type != TYPE_VARIANT && |
895 | 0 | (storage_type->equals(*target_type) || |
896 | 0 | (is_string_type(target_primitive_type) && |
897 | 0 | is_string_type(origin_primitive_type)))) { |
898 | 0 | children_exprs.emplace_back(expr_without_cast(child)); |
899 | 0 | } |
900 | 1 | } else { |
901 | 1 | return Status::OK(); // for example: cast("abc") as ipv4 case |
902 | 1 | } |
903 | 1 | } else { |
904 | 1 | children_exprs.emplace_back(child); |
905 | 1 | } |
906 | 2 | } |
907 | | |
908 | 0 | if (children_exprs.empty()) { |
909 | 0 | return Status::OK(); // Early exit if no children to process |
910 | 0 | } |
911 | | |
912 | 0 | for (const auto& child : children_exprs) { |
913 | 0 | if (child->is_slot_ref()) { |
914 | 0 | auto* column_slot_ref = assert_cast<VSlotRef*>(child.get()); |
915 | 0 | auto column_id = column_slot_ref->column_id(); |
916 | 0 | auto* iter = context->get_index_context()->get_inverted_index_iterator_by_column_id( |
917 | 0 | column_id); |
918 | | //column does not have inverted index |
919 | 0 | if (iter == nullptr) { |
920 | 0 | continue; |
921 | 0 | } |
922 | 0 | const auto* storage_name_type = |
923 | 0 | context->get_index_context()->get_storage_name_and_type_by_column_id(column_id); |
924 | 0 | if (storage_name_type == nullptr) { |
925 | 0 | auto err_msg = fmt::format( |
926 | 0 | "storage_name_type cannot be found for column {} while in {} " |
927 | 0 | "evaluate_inverted_index", |
928 | 0 | column_id, expr_name()); |
929 | 0 | LOG(ERROR) << err_msg; |
930 | 0 | return Status::InternalError(err_msg); |
931 | 0 | } |
932 | 0 | iterators.emplace_back(iter); |
933 | 0 | data_type_with_names.emplace_back(*storage_name_type); |
934 | 0 | column_ids.emplace_back(column_id); |
935 | 0 | } else if (child->is_literal()) { |
936 | 0 | auto* column_literal = assert_cast<VLiteral*>(child.get()); |
937 | 0 | arguments.emplace_back(column_literal->get_column_ptr(), |
938 | 0 | column_literal->get_data_type(), column_literal->expr_name()); |
939 | 0 | } else if (child->can_push_down_to_index()) { |
940 | 0 | RETURN_IF_ERROR(child->evaluate_inverted_index(context, segment_num_rows)); |
941 | 0 | } else { |
942 | 0 | return Status::OK(); // others cases |
943 | 0 | } |
944 | 0 | } |
945 | | |
946 | | // is null or is not null has no arguments |
947 | 0 | if (iterators.empty() || (arguments.empty() && !(function->get_name() == "is_not_null_pred" || |
948 | 0 | function->get_name() == "is_null_pred"))) { |
949 | 0 | return Status::OK(); // Nothing to evaluate or no literals to compare against |
950 | 0 | } |
951 | | |
952 | 0 | auto result_bitmap = segment_v2::InvertedIndexResultBitmap(); |
953 | 0 | auto res = function->evaluate_inverted_index(arguments, data_type_with_names, iterators, |
954 | 0 | segment_num_rows, result_bitmap); |
955 | 0 | if (!res.ok()) { |
956 | 0 | return res; |
957 | 0 | } |
958 | 0 | if (!result_bitmap.is_empty()) { |
959 | 0 | index_context->set_index_result_for_expr(this, result_bitmap); |
960 | 0 | for (int column_id : column_ids) { |
961 | 0 | index_context->set_true_for_index_status(this, column_id); |
962 | 0 | } |
963 | 0 | } |
964 | 0 | return Status::OK(); |
965 | 0 | } |
966 | | |
967 | 0 | size_t VExpr::estimate_memory(const size_t rows) { |
968 | 0 | if (is_const_and_have_executed()) { |
969 | 0 | return 0; |
970 | 0 | } |
971 | | |
972 | 0 | size_t estimate_size = 0; |
973 | 0 | for (auto& child : _children) { |
974 | 0 | estimate_size += child->estimate_memory(rows); |
975 | 0 | } |
976 | |
|
977 | 0 | if (_data_type->have_maximum_size_of_value()) { |
978 | 0 | estimate_size += rows * _data_type->get_size_of_value_in_memory(); |
979 | 0 | } else { |
980 | 0 | estimate_size += rows * 64; /// TODO: need a more reasonable value |
981 | 0 | } |
982 | 0 | return estimate_size; |
983 | 0 | } |
984 | | |
985 | 11 | bool VExpr::fast_execute(VExprContext* context, ColumnPtr& result_column) const { |
986 | 11 | if (context->get_index_context() && |
987 | 11 | context->get_index_context()->get_index_result_column().contains(this)) { |
988 | | // prepare a column to save result |
989 | 1 | result_column = context->get_index_context()->get_index_result_column()[this]; |
990 | 1 | if (_data_type->is_nullable()) { |
991 | 1 | result_column = make_nullable(result_column); |
992 | 1 | } |
993 | 1 | return true; |
994 | 1 | } |
995 | 10 | return false; |
996 | 11 | } |
997 | | |
998 | 0 | bool VExpr::equals(const VExpr& other) { |
999 | 0 | return false; |
1000 | 0 | } |
1001 | | |
1002 | | Status VExpr::evaluate_ann_range_search( |
1003 | | const segment_v2::AnnRangeSearchRuntime& runtime, |
1004 | | const std::vector<std::unique_ptr<segment_v2::IndexIterator>>& index_iterators, |
1005 | | const std::vector<ColumnId>& idx_to_cid, |
1006 | | const std::vector<std::unique_ptr<segment_v2::ColumnIterator>>& column_iterators, |
1007 | 0 | roaring::Roaring& row_bitmap, AnnIndexStats& ann_index_stats) { |
1008 | 0 | return Status::OK(); |
1009 | 0 | } |
1010 | | |
1011 | | void VExpr::prepare_ann_range_search(const doris::VectorSearchUserParams& params, |
1012 | | segment_v2::AnnRangeSearchRuntime& range_search_runtime, |
1013 | 0 | bool& suitable_for_ann_index) { |
1014 | 0 | if (!suitable_for_ann_index) { |
1015 | 0 | return; |
1016 | 0 | } |
1017 | 0 | for (auto& child : _children) { |
1018 | 0 | child->prepare_ann_range_search(params, range_search_runtime, suitable_for_ann_index); |
1019 | 0 | if (!suitable_for_ann_index) { |
1020 | 0 | return; |
1021 | 0 | } |
1022 | 0 | } |
1023 | 0 | } |
1024 | | |
1025 | 2 | bool VExpr::ann_range_search_executedd() { |
1026 | 2 | return _has_been_executed; |
1027 | 2 | } |
1028 | | |
1029 | 2 | bool VExpr::ann_dist_is_fulfilled() const { |
1030 | 2 | return _virtual_column_is_fulfilled; |
1031 | 2 | } |
1032 | | |
1033 | | #include "common/compile_check_end.h" |
1034 | | } // namespace doris::vectorized |