Coverage Report

Created: 2026-06-23 16:02

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