Coverage Report

Created: 2026-05-26 02:10

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