Coverage Report

Created: 2026-06-18 11:53

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