Coverage Report

Created: 2026-07-15 10:06

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