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