Coverage Report

Created: 2026-07-16 17:23

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