Coverage Report

Created: 2026-10-15 11:46

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