Coverage Report

Created: 2026-04-15 09:50

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