Coverage Report

Created: 2026-06-09 11:50

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
48.6k
                                 int scale) {
84
48.6k
    TExprNode node;
85
86
48.6k
    switch (type) {
87
56
    case TYPE_BOOLEAN: {
88
56
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_BOOLEAN>(data, &node));
89
56
        break;
90
56
    }
91
1.23k
    case TYPE_TINYINT: {
92
1.23k
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_TINYINT>(data, &node));
93
1.23k
        break;
94
1.23k
    }
95
1.23k
    case TYPE_SMALLINT: {
96
740
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_SMALLINT>(data, &node));
97
740
        break;
98
740
    }
99
9.69k
    case TYPE_INT: {
100
9.69k
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_INT>(data, &node));
101
9.69k
        break;
102
9.69k
    }
103
34.6k
    case TYPE_BIGINT: {
104
34.6k
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_BIGINT>(data, &node));
105
34.6k
        break;
106
34.6k
    }
107
34.6k
    case TYPE_LARGEINT: {
108
82
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_LARGEINT>(data, &node));
109
82
        break;
110
82
    }
111
82
    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
2
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_DOUBLE>(data, &node));
117
2
        break;
118
2
    }
119
424
    case TYPE_DATEV2: {
120
424
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_DATEV2>(data, &node));
121
424
        break;
122
424
    }
123
424
    case TYPE_DATETIMEV2: {
124
385
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_DATETIMEV2>(data, &node, precision, scale));
125
385
        break;
126
385
    }
127
385
    case TYPE_DATE: {
128
6
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_DATE>(data, &node));
129
6
        break;
130
6
    }
131
12
    case TYPE_DATETIME: {
132
12
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_DATETIME>(data, &node));
133
12
        break;
134
12
    }
135
12
    case TYPE_DECIMALV2: {
136
4
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_DECIMALV2>(data, &node, precision, scale));
137
4
        break;
138
4
    }
139
30
    case TYPE_DECIMAL32: {
140
30
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_DECIMAL32>(data, &node, precision, scale));
141
30
        break;
142
30
    }
143
252
    case TYPE_DECIMAL64: {
144
252
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_DECIMAL64>(data, &node, precision, scale));
145
252
        break;
146
252
    }
147
252
    case TYPE_DECIMAL128I: {
148
47
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_DECIMAL128I>(data, &node, precision, scale));
149
47
        break;
150
47
    }
151
47
    case TYPE_DECIMAL256: {
152
38
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_DECIMAL256>(data, &node, precision, scale));
153
38
        break;
154
38
    }
155
142
    case TYPE_CHAR: {
156
142
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_CHAR>(data, &node));
157
142
        break;
158
142
    }
159
171
    case TYPE_VARCHAR: {
160
171
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_VARCHAR>(data, &node));
161
171
        break;
162
171
    }
163
751
    case TYPE_STRING: {
164
751
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_STRING>(data, &node));
165
751
        break;
166
751
    }
167
751
    case TYPE_IPV4: {
168
16
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_IPV4>(data, &node));
169
16
        break;
170
16
    }
171
16
    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
5
    case TYPE_TIMESTAMPTZ: {
180
5
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_TIMESTAMPTZ>(data, &node, precision, scale));
181
5
        break;
182
5
    }
183
5
    default:
184
0
        throw Exception(ErrorCode::INTERNAL_ERROR, "runtime filter meet invalid type {}",
185
0
                        int(type));
186
48.6k
    }
187
48.5k
    return node;
188
48.6k
}
189
190
TExprNode create_texpr_node_from(const Field& field, const PrimitiveType& type, int precision,
191
117
                                 int scale) {
192
117
    TExprNode node;
193
117
    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
95
    case TYPE_INT: {
210
95
        const auto& storage = static_cast<int32_t>(field.get<TYPE_INT>());
211
95
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_INT>(&storage, &node));
212
95
        break;
213
95
    }
214
95
    case TYPE_BIGINT: {
215
1
        const auto& storage = static_cast<int64_t>(field.get<TYPE_BIGINT>());
216
1
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_BIGINT>(&storage, &node));
217
1
        break;
218
1
    }
219
1
    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
117
    }
333
117
    return node;
334
117
}
335
336
// NOLINTEND(readability-function-size)
337
// NOLINTEND(readability-function-cognitive-complexity)
338
} // namespace doris
339
340
namespace doris {
341
342
13.9k
bool VExpr::is_acting_on_a_slot(const VExpr& expr) {
343
13.9k
    const auto& children = expr.children();
344
345
13.9k
    auto is_a_slot = std::any_of(children.begin(), children.end(),
346
13.9k
                                 [](const auto& child) { return is_acting_on_a_slot(*child); });
347
348
13.9k
    return is_a_slot ? true
349
13.9k
                     : (expr.node_type() == TExprNodeType::SLOT_REF ||
350
5.40k
                        expr.node_type() == TExprNodeType::VIRTUAL_SLOT_REF);
351
13.9k
}
352
353
VExpr::VExpr(const TExprNode& node)
354
10.3M
        : _node_type(node.node_type),
355
10.3M
          _opcode(node.__isset.opcode ? node.opcode : TExprOpcode::INVALID_OPCODE) {
356
10.3M
    if (node.__isset.fn) {
357
791k
        _fn = node.fn;
358
791k
    }
359
360
10.3M
    bool is_nullable = true;
361
10.3M
    if (node.__isset.is_nullable) {
362
10.2M
        is_nullable = node.is_nullable;
363
10.2M
    }
364
    // If we define null literal ,should make nullable data type to get correct field instead of undefined ptr
365
10.3M
    if (node.node_type == TExprNodeType::NULL_LITERAL) {
366
770k
        CHECK(is_nullable);
367
770k
    }
368
10.3M
    _data_type = get_data_type_with_default_argument(
369
10.3M
            DataTypeFactory::instance().create_data_type(node.type, is_nullable));
370
10.3M
}
371
372
0
VExpr::VExpr(const VExpr& vexpr) = default;
373
374
VExpr::VExpr(DataTypePtr type, bool is_slotref)
375
548
        : _opcode(TExprOpcode::INVALID_OPCODE),
376
548
          _data_type(get_data_type_with_default_argument(type)) {
377
548
    if (is_slotref) {
378
539
        _node_type = TExprNodeType::SLOT_REF;
379
539
    }
380
548
}
381
382
9.90M
Status VExpr::prepare(RuntimeState* state, const RowDescriptor& row_desc, VExprContext* context) {
383
9.90M
    ++context->_depth_num;
384
9.90M
    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
9.90M
    for (auto& i : _children) {
391
1.86M
        RETURN_IF_ERROR(i->prepare(state, row_desc, context));
392
1.86M
    }
393
9.90M
    --context->_depth_num;
394
9.90M
#ifndef BE_TEST
395
9.90M
    _enable_inverted_index_query = state->query_options().enable_inverted_index_query;
396
9.90M
#endif
397
9.90M
    return Status::OK();
398
9.90M
}
399
400
Status VExpr::open(RuntimeState* state, VExprContext* context,
401
33.5M
                   FunctionContext::FunctionStateScope scope) {
402
33.5M
    for (auto& i : _children) {
403
529k
        RETURN_IF_ERROR(i->open(state, context, scope));
404
529k
    }
405
33.5M
    if (scope == FunctionContext::FRAGMENT_LOCAL) {
406
8.99M
        RETURN_IF_ERROR(VExpr::get_const_col(context, nullptr));
407
8.99M
    }
408
33.5M
    return Status::OK();
409
33.5M
}
410
411
36.4M
void VExpr::close(VExprContext* context, FunctionContext::FunctionStateScope scope) {
412
36.4M
    for (auto& i : _children) {
413
5.21M
        i->close(context, scope);
414
5.21M
    }
415
36.4M
}
416
417
// NOLINTBEGIN(readability-function-size)
418
10.1M
Status VExpr::create_expr(const TExprNode& expr_node, VExprSPtr& expr) {
419
10.1M
    try {
420
10.1M
        switch (expr_node.node_type) {
421
24.5k
        case TExprNodeType::BOOL_LITERAL:
422
847k
        case TExprNodeType::INT_LITERAL:
423
896k
        case TExprNodeType::LARGE_INT_LITERAL:
424
898k
        case TExprNodeType::IPV4_LITERAL:
425
899k
        case TExprNodeType::IPV6_LITERAL:
426
950k
        case TExprNodeType::FLOAT_LITERAL:
427
1.01M
        case TExprNodeType::DECIMAL_LITERAL:
428
1.20M
        case TExprNodeType::DATE_LITERAL:
429
1.20M
        case TExprNodeType::TIMEV2_LITERAL:
430
2.02M
        case TExprNodeType::STRING_LITERAL:
431
2.02M
        case TExprNodeType::JSON_LITERAL:
432
2.02M
        case TExprNodeType::VARBINARY_LITERAL:
433
2.79M
        case TExprNodeType::NULL_LITERAL: {
434
2.79M
            expr = VLiteral::create_shared(expr_node);
435
2.79M
            break;
436
2.02M
        }
437
20.4k
        case TExprNodeType::ARRAY_LITERAL: {
438
20.4k
            expr = VArrayLiteral::create_shared(expr_node);
439
20.4k
            break;
440
2.02M
        }
441
11.9k
        case TExprNodeType::MAP_LITERAL: {
442
11.9k
            expr = VMapLiteral::create_shared(expr_node);
443
11.9k
            break;
444
2.02M
        }
445
1.57k
        case TExprNodeType::STRUCT_LITERAL: {
446
1.57k
            expr = VStructLiteral::create_shared(expr_node);
447
1.57k
            break;
448
2.02M
        }
449
6.41M
        case TExprNodeType::SLOT_REF: {
450
6.41M
            if (expr_node.slot_ref.__isset.is_virtual_slot && expr_node.slot_ref.is_virtual_slot) {
451
275
                expr = VirtualSlotRef::create_shared(expr_node);
452
275
                expr->_node_type = TExprNodeType::VIRTUAL_SLOT_REF;
453
6.41M
            } else {
454
6.41M
                expr = VSlotRef::create_shared(expr_node);
455
6.41M
            }
456
6.41M
            break;
457
2.02M
        }
458
1.29k
        case TExprNodeType::COLUMN_REF: {
459
1.29k
            expr = VColumnRef::create_shared(expr_node);
460
1.29k
            break;
461
2.02M
        }
462
6.60k
        case TExprNodeType::COMPOUND_PRED: {
463
6.60k
            expr = VCompoundPred::create_shared(expr_node);
464
6.60k
            break;
465
2.02M
        }
466
978
        case TExprNodeType::LAMBDA_FUNCTION_EXPR: {
467
978
            expr = VLambdaFunctionExpr::create_shared(expr_node);
468
978
            break;
469
2.02M
        }
470
978
        case TExprNodeType::LAMBDA_FUNCTION_CALL_EXPR: {
471
978
            expr = VLambdaFunctionCallExpr::create_shared(expr_node);
472
978
            break;
473
2.02M
        }
474
94.4k
        case TExprNodeType::ARITHMETIC_EXPR:
475
567k
        case TExprNodeType::BINARY_PRED:
476
567k
        case TExprNodeType::NULL_AWARE_BINARY_PRED:
477
568k
        case TExprNodeType::COMPUTE_FUNCTION_CALL: {
478
568k
            expr = VectorizedFnCall::create_shared(expr_node);
479
568k
            break;
480
567k
        }
481
173k
        case TExprNodeType::FUNCTION_CALL: {
482
173k
            if (expr_node.fn.name.function_name == "if") {
483
3.90k
                if (expr_node.__isset.short_circuit_evaluation &&
484
3.90k
                    expr_node.short_circuit_evaluation) {
485
726
                    expr = ShortCircuitIfExpr::create_shared(expr_node);
486
3.17k
                } else {
487
3.17k
                    expr = VectorizedIfExpr::create_shared(expr_node);
488
3.17k
                }
489
3.90k
                break;
490
169k
            } else if (expr_node.fn.name.function_name == "ifnull" ||
491
169k
                       expr_node.fn.name.function_name == "nvl") {
492
470
                if (expr_node.__isset.short_circuit_evaluation &&
493
470
                    expr_node.short_circuit_evaluation) {
494
77
                    expr = ShortCircuitIfNullExpr::create_shared(expr_node);
495
393
                } else {
496
393
                    expr = VectorizedIfNullExpr::create_shared(expr_node);
497
393
                }
498
470
                break;
499
168k
            } else if (expr_node.fn.name.function_name == "coalesce") {
500
163
                if (expr_node.__isset.short_circuit_evaluation &&
501
163
                    expr_node.short_circuit_evaluation) {
502
61
                    expr = ShortCircuitCoalesceExpr::create_shared(expr_node);
503
102
                } else {
504
102
                    expr = VectorizedCoalesceExpr::create_shared(expr_node);
505
102
                }
506
163
                break;
507
163
            }
508
168k
            expr = VectorizedFnCall::create_shared(expr_node);
509
168k
            break;
510
173k
        }
511
1.60k
        case TExprNodeType::MATCH_PRED: {
512
1.60k
            expr = VMatchPredicate::create_shared(expr_node);
513
1.60k
            break;
514
173k
        }
515
164k
        case TExprNodeType::CAST_EXPR: {
516
164k
            expr = VCastExpr::create_shared(expr_node);
517
164k
            break;
518
173k
        }
519
11
        case TExprNodeType::TRY_CAST_EXPR: {
520
11
            expr = TryCastExpr::create_shared(expr_node);
521
11
            break;
522
173k
        }
523
2.20k
        case TExprNodeType::IN_PRED: {
524
2.20k
            expr = VInPredicate::create_shared(expr_node);
525
2.20k
            break;
526
173k
        }
527
293
        case TExprNodeType::CASE_EXPR: {
528
293
            if (!expr_node.__isset.case_expr) {
529
0
                return Status::InternalError("Case expression not set in thrift node");
530
0
            }
531
293
            if (expr_node.__isset.short_circuit_evaluation && expr_node.short_circuit_evaluation) {
532
93
                expr = ShortCircuitCaseExpr::create_shared(expr_node);
533
200
            } else {
534
200
                expr = VCaseExpr::create_shared(expr_node);
535
200
            }
536
293
            break;
537
293
        }
538
0
        case TExprNodeType::INFO_FUNC: {
539
0
            expr = VInfoFunc::create_shared(expr_node);
540
0
            break;
541
293
        }
542
462
        case TExprNodeType::SEARCH_EXPR: {
543
462
            expr = VSearchExpr::create_shared(expr_node);
544
462
            break;
545
293
        }
546
0
        default:
547
0
            return Status::InternalError("Unknown expr node type: {}", expr_node.node_type);
548
10.1M
        }
549
10.1M
    } 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.1M
    if (!expr->data_type()) {
559
0
        return Status::InvalidArgument("Unknown expr type: {}", expr_node.node_type);
560
0
    }
561
10.1M
    return Status::OK();
562
10.1M
}
563
// NOLINTEND(readability-function-size)
564
565
Status VExpr::create_tree_from_thrift(const std::vector<TExprNode>& nodes, int* node_idx,
566
8.39M
                                      VExprSPtr& root_expr, VExprContextSPtr& ctx) {
567
    // propagate error case
568
8.39M
    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.39M
    int root_children = nodes[*node_idx].num_children;
574
8.39M
    VExprSPtr root;
575
8.39M
    RETURN_IF_ERROR(create_expr(nodes[*node_idx], root));
576
8.39M
    DCHECK(root != nullptr);
577
8.39M
    root_expr = root;
578
8.39M
    ctx = std::make_shared<VExprContext>(root);
579
    // short path for leaf node
580
8.39M
    if (root_children <= 0) {
581
7.67M
        return Status::OK();
582
7.67M
    }
583
584
    // non-recursive traversal
585
723k
    using VExprSPtrCountPair = std::pair<VExprSPtr, int>;
586
723k
    std::stack<std::shared_ptr<VExprSPtrCountPair>> s;
587
723k
    s.emplace(std::make_shared<VExprSPtrCountPair>(root, root_children));
588
2.44M
    while (!s.empty()) {
589
        // copy the shared ptr resource to avoid dangling reference
590
1.72M
        auto parent = s.top();
591
        // Decrement or pop
592
1.72M
        if (parent->second > 1) {
593
817k
            parent->second -= 1;
594
903k
        } else {
595
903k
            s.pop();
596
903k
        }
597
598
1.72M
        DCHECK(parent->first != nullptr);
599
1.72M
        if (++*node_idx >= nodes.size()) {
600
0
            return Status::InternalError("Failed to reconstruct expression tree from thrift.");
601
0
        }
602
603
1.72M
        VExprSPtr expr;
604
1.72M
        RETURN_IF_ERROR(create_expr(nodes[*node_idx], expr));
605
1.72M
        DCHECK(expr != nullptr);
606
1.72M
        parent->first->add_child(expr);
607
        // push to stack if has children
608
1.72M
        int num_children = nodes[*node_idx].num_children;
609
1.72M
        if (num_children > 0) {
610
174k
            s.emplace(std::make_shared<VExprSPtrCountPair>(expr, num_children));
611
174k
        }
612
1.72M
    }
613
723k
    return Status::OK();
614
723k
}
615
616
8.18M
Status VExpr::create_expr_tree(const TExpr& texpr, VExprContextSPtr& ctx) {
617
8.18M
    if (texpr.nodes.empty()) {
618
7
        ctx = nullptr;
619
7
        return Status::OK();
620
7
    }
621
8.18M
    int node_idx = 0;
622
8.18M
    VExprSPtr e;
623
8.18M
    Status status = create_tree_from_thrift(texpr.nodes, &node_idx, e, ctx);
624
8.19M
    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.18M
    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.18M
    return status;
635
8.18M
}
636
637
1.45M
Status VExpr::create_expr_trees(const std::vector<TExpr>& texprs, VExprContextSPtrs& ctxs) {
638
1.45M
    ctxs.clear();
639
6.40M
    for (const auto& texpr : texprs) {
640
6.40M
        VExprContextSPtr ctx;
641
6.40M
        RETURN_IF_ERROR(create_expr_tree(texpr, ctx));
642
6.40M
        ctxs.push_back(ctx);
643
6.40M
    }
644
1.45M
    return Status::OK();
645
1.45M
}
646
647
Status VExpr::check_expr_output_type(const VExprContextSPtrs& ctxs,
648
320k
                                     const RowDescriptor& output_row_desc) {
649
320k
    if (ctxs.empty()) {
650
6
        return Status::OK();
651
6
    }
652
320k
    auto name_and_types = VectorizedUtils::create_name_and_data_types(output_row_desc);
653
320k
    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.19M
    auto check_type_can_be_converted = [](DataTypePtr& from, DataTypePtr& to) -> bool {
659
2.19M
        if (to->equals_ignore_precision(*from)) {
660
2.18M
            return true;
661
2.18M
        }
662
1.76k
        if (to->is_nullable() && !from->is_nullable()) {
663
99
            return remove_nullable(to)->equals_ignore_precision(*from);
664
99
        }
665
1.67k
        return false;
666
1.76k
    };
667
2.51M
    for (int i = 0; i < ctxs.size(); i++) {
668
2.19M
        auto real_expr_type = get_data_type_with_default_argument(ctxs[i]->root()->data_type());
669
2.19M
        auto&& [name, expected_type] = name_and_types[i];
670
2.19M
        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.19M
    }
677
320k
    return Status::OK();
678
320k
}
679
680
Status VExpr::prepare(const VExprContextSPtrs& ctxs, RuntimeState* state,
681
2.26M
                      const RowDescriptor& row_desc) {
682
6.82M
    for (auto ctx : ctxs) {
683
6.82M
        RETURN_IF_ERROR(ctx->prepare(state, row_desc));
684
6.82M
    }
685
2.26M
    return Status::OK();
686
2.26M
}
687
688
2.28M
Status VExpr::open(const VExprContextSPtrs& ctxs, RuntimeState* state) {
689
6.82M
    for (const auto& ctx : ctxs) {
690
6.82M
        RETURN_IF_ERROR(ctx->open(state));
691
6.82M
    }
692
2.27M
    return Status::OK();
693
2.28M
}
694
695
1.55M
bool VExpr::contains_blockable_function(const VExprContextSPtrs& ctxs) {
696
2.60M
    return std::any_of(ctxs.begin(), ctxs.end(), [](const VExprContextSPtr& ctx) {
697
2.60M
        return ctx != nullptr && ctx->root() != nullptr && ctx->root()->is_blockable();
698
2.60M
    });
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
778
std::string VExpr::debug_string() const {
719
    // TODO: implement partial debug string for member vars
720
778
    std::stringstream out;
721
778
    out << " type=" << _data_type->get_name();
722
723
778
    if (!_children.empty()) {
724
108
        out << " children=" << debug_string(_children);
725
108
    }
726
727
778
    return out.str();
728
778
}
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.27M
bool VExpr::is_constant() const {
751
9.27M
    return std::all_of(_children.begin(), _children.end(),
752
9.27M
                       [](const VExprSPtr& expr) { return expr->is_constant(); });
753
9.27M
}
754
755
Status VExpr::get_const_col(VExprContext* context,
756
12.0M
                            std::shared_ptr<ColumnPtrWrapper>* column_wrapper) {
757
12.0M
    if (!is_constant()) {
758
7.77M
        return Status::OK();
759
7.77M
    }
760
761
4.26M
    if (_constant_col != nullptr) {
762
1.29M
        DCHECK(column_wrapper != nullptr);
763
1.29M
        *column_wrapper = _constant_col;
764
1.29M
        return Status::OK();
765
1.29M
    }
766
767
2.96M
    ColumnPtr result;
768
2.96M
    RETURN_IF_ERROR(execute_column(context, nullptr, nullptr, 1, result));
769
2.96M
    _constant_col = std::make_shared<ColumnPtrWrapper>(result);
770
2.96M
    if (column_wrapper != nullptr) {
771
0
        *column_wrapper = _constant_col;
772
0
    }
773
774
2.96M
    return Status::OK();
775
2.96M
}
776
777
910k
void VExpr::register_function_context(RuntimeState* state, VExprContext* context) {
778
910k
    std::vector<DataTypePtr> arg_types;
779
1.62M
    for (auto& i : _children) {
780
1.62M
        arg_types.push_back(i->data_type());
781
1.62M
    }
782
783
910k
    _fn_context_index = context->register_function_context(state, _data_type, arg_types);
784
910k
}
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
910k
        std::vector<std::shared_ptr<ColumnPtrWrapper>> constant_cols;
792
1.62M
        for (auto c : _children) {
793
1.62M
            std::shared_ptr<ColumnPtrWrapper> const_col;
794
1.62M
            RETURN_IF_ERROR(c->get_const_col(context, &const_col));
795
1.62M
            constant_cols.push_back(const_col);
796
1.62M
        }
797
910k
        fn_ctx->set_constant_cols(constant_cols);
798
910k
    }
799
800
2.65M
    if (scope == FunctionContext::FRAGMENT_LOCAL) {
801
910k
        RETURN_IF_ERROR(function->open(fn_ctx, FunctionContext::FRAGMENT_LOCAL));
802
910k
    }
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
911k
            static_cast<void>(function->close(fn_ctx, FunctionContext::FRAGMENT_LOCAL));
815
911k
        }
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
554k
uint64_t VExpr::get_digest(uint64_t seed) const {
827
554k
    auto digest = seed;
828
1.10M
    for (auto child : _children) {
829
1.10M
        digest = child->get_digest(digest);
830
1.10M
        if (digest == 0) {
831
325
            return 0;
832
325
        }
833
1.10M
    }
834
835
554k
    auto& fn_name = _fn.name.function_name;
836
554k
    if (!fn_name.empty()) {
837
551k
        digest = HashUtil::hash64(fn_name.c_str(), fn_name.size(), digest);
838
551k
    } else {
839
3.38k
        digest = HashUtil::hash64((const char*)&_node_type, sizeof(_node_type), digest);
840
3.38k
        digest = HashUtil::hash64((const char*)&_opcode, sizeof(_opcode), digest);
841
3.38k
    }
842
554k
    return digest;
843
554k
}
844
845
97.4k
ColumnPtr VExpr::get_result_from_const(size_t count) const {
846
97.4k
    return ColumnConst::create(_constant_col->column_ptr, count);
847
97.4k
}
848
849
Status VExpr::_evaluate_inverted_index(VExprContext* context, const FunctionBasePtr& function,
850
13.8k
                                       uint32_t segment_num_rows) {
851
    // Pre-allocate vectors based on an estimated or known size
852
13.8k
    std::vector<segment_v2::IndexIterator*> iterators;
853
13.8k
    std::vector<IndexFieldNameAndTypePair> data_type_with_names;
854
13.8k
    std::vector<int> column_ids;
855
13.8k
    ColumnsWithTypeAndName arguments;
856
13.8k
    VExprSPtrs children_exprs;
857
858
    // Reserve space to avoid multiple reallocations
859
13.8k
    const size_t estimated_size = get_num_children();
860
13.8k
    iterators.reserve(estimated_size);
861
13.8k
    data_type_with_names.reserve(estimated_size);
862
13.8k
    column_ids.reserve(estimated_size);
863
13.8k
    children_exprs.reserve(estimated_size);
864
865
13.8k
    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.1k
    for (const auto& child : children()) {
871
29.1k
        if (child->node_type() == TExprNodeType::CAST_EXPR) {
872
2.30k
            auto* cast_expr = assert_cast<VCastExpr*>(child.get());
873
2.30k
            DCHECK_EQ(cast_expr->get_num_children(), 1);
874
2.30k
            if (cast_expr->get_child(0)->is_slot_ref()) {
875
2.20k
                auto* column_slot_ref = assert_cast<VSlotRef*>(cast_expr->get_child(0).get());
876
2.20k
                auto column_id = column_slot_ref->column_id();
877
2.20k
                const auto* storage_name_type =
878
2.20k
                        context->get_index_context()->get_storage_name_and_type_by_column_id(
879
2.20k
                                column_id);
880
2.20k
                auto storage_type = remove_nullable(storage_name_type->second);
881
2.20k
                auto target_type = remove_nullable(cast_expr->get_target_type());
882
2.20k
                auto origin_primitive_type = storage_type->get_primitive_type();
883
2.20k
                auto target_primitive_type = target_type->get_primitive_type();
884
2.20k
                if (is_complex_type(storage_type->get_primitive_type())) {
885
261
                    if (storage_type->get_primitive_type() == TYPE_ARRAY &&
886
261
                        target_type->get_primitive_type() == TYPE_ARRAY) {
887
257
                        auto nested_storage_type =
888
257
                                (assert_cast<const DataTypeArray*>(storage_type.get()))
889
257
                                        ->get_nested_type();
890
257
                        origin_primitive_type = nested_storage_type->get_primitive_type();
891
257
                        auto nested_target_type =
892
257
                                (assert_cast<const DataTypeArray*>(target_type.get()))
893
257
                                        ->get_nested_type();
894
257
                        target_primitive_type = nested_target_type->get_primitive_type();
895
257
                    } else {
896
4
                        continue;
897
4
                    }
898
261
                }
899
2.20k
                if (origin_primitive_type != TYPE_VARIANT &&
900
2.20k
                    (storage_type->equals(*target_type) ||
901
1.92k
                     (is_string_type(target_primitive_type) &&
902
1.41k
                      is_string_type(origin_primitive_type)))) {
903
504
                    children_exprs.emplace_back(expr_without_cast(child));
904
504
                }
905
2.20k
            } else {
906
104
                return Status::OK(); // for example: cast("abc") as ipv4 case
907
104
            }
908
26.8k
        } else {
909
26.8k
            children_exprs.emplace_back(child);
910
26.8k
        }
911
29.1k
    }
912
913
13.7k
    if (children_exprs.empty()) {
914
109
        return Status::OK(); // Early exit if no children to process
915
109
    }
916
917
26.4k
    for (const auto& child : children_exprs) {
918
26.4k
        if (child->is_slot_ref()) {
919
11.8k
            auto* column_slot_ref = assert_cast<VSlotRef*>(child.get());
920
11.8k
            auto column_id = column_slot_ref->column_id();
921
11.8k
            auto* iter = context->get_index_context()->get_inverted_index_iterator_by_column_id(
922
11.8k
                    column_id);
923
            //column does not have inverted index
924
11.8k
            if (iter == nullptr) {
925
2.86k
                continue;
926
2.86k
            }
927
9.00k
            const auto* storage_name_type =
928
9.00k
                    context->get_index_context()->get_storage_name_and_type_by_column_id(column_id);
929
9.00k
            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.00k
            iterators.emplace_back(iter);
938
9.00k
            data_type_with_names.emplace_back(*storage_name_type);
939
9.00k
            column_ids.emplace_back(column_id);
940
14.6k
        } else if (child->is_literal()) {
941
13.7k
            auto* column_literal = assert_cast<VLiteral*>(child.get());
942
13.7k
            arguments.emplace_back(column_literal->get_column_ptr(),
943
13.7k
                                   column_literal->get_data_type(), column_literal->expr_name());
944
13.7k
        } else if (child->can_push_down_to_index()) {
945
0
            RETURN_IF_ERROR(child->evaluate_inverted_index(context, segment_num_rows));
946
890
        } else {
947
890
            return Status::OK(); // others cases
948
890
        }
949
26.4k
    }
950
951
    // is null or is not null has no arguments
952
12.7k
    if (iterators.empty() || (arguments.empty() && !(function->get_name() == "is_not_null_pred" ||
953
4.32k
                                                     function->get_name() == "is_null_pred"))) {
954
4.32k
        return Status::OK(); // Nothing to evaluate or no literals to compare against
955
4.32k
    }
956
957
8.46k
    const InvertedIndexAnalyzerCtx* analyzer_ctx = nullptr;
958
8.72k
    if (auto index_ctx = context->get_index_context(); index_ctx != nullptr) {
959
8.72k
        analyzer_ctx = index_ctx->get_analyzer_ctx_for_expr(this);
960
8.72k
    }
961
962
8.46k
    auto result_bitmap = segment_v2::InvertedIndexResultBitmap();
963
    // Pass analyzer_key to function (used by match predicates for multi-analyzer index selection)
964
8.46k
    auto res = function->evaluate_inverted_index(arguments, data_type_with_names, iterators,
965
8.46k
                                                 segment_num_rows, analyzer_ctx, result_bitmap);
966
8.46k
    if (!res.ok()) {
967
341
        return res;
968
341
    }
969
8.12k
    if (!result_bitmap.is_empty()) {
970
6.61k
        index_context->set_index_result_for_expr(this, result_bitmap);
971
6.70k
        for (int column_id : column_ids) {
972
6.70k
            index_context->set_true_for_index_status(this, column_id);
973
6.70k
        }
974
6.61k
    }
975
8.12k
    return Status::OK();
976
8.46k
}
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.10M
                         ColumnPtr& result_column) const {
998
1.10M
    if (context->get_index_context() &&
999
1.10M
        context->get_index_context()->get_index_result_column().contains(this)) {
1000
        // prepare a column to save result
1001
965
        result_column = filter_column_with_selector(
1002
965
                context->get_index_context()->get_index_result_column()[this], selector, count);
1003
965
        if (_data_type->is_nullable()) {
1004
776
            result_column = make_nullable(result_column);
1005
776
        }
1006
965
        return true;
1007
965
    }
1008
1.09M
    return false;
1009
1.10M
}
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
        roaring::Roaring& row_bitmap, AnnIndexStats& ann_index_stats,
1021
4.90k
        AnnRangeSearchEvaluationResult& result) {
1022
4.90k
    result = {};
1023
4.90k
    return Status::OK();
1024
4.90k
}
1025
1026
void VExpr::prepare_ann_range_search(const doris::VectorSearchUserParams& params,
1027
                                     segment_v2::AnnRangeSearchRuntime& range_search_runtime,
1028
15.6k
                                     bool& suitable_for_ann_index) {
1029
15.6k
    if (!suitable_for_ann_index) {
1030
0
        return;
1031
0
    }
1032
15.6k
    for (auto& child : _children) {
1033
10.3k
        child->prepare_ann_range_search(params, range_search_runtime, suitable_for_ann_index);
1034
10.3k
        if (!suitable_for_ann_index) {
1035
607
            return;
1036
607
        }
1037
10.3k
    }
1038
15.6k
}
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
466k
                             bool* can_filter_all) const {
1043
466k
    ColumnPtr filter_column;
1044
466k
    RETURN_IF_ERROR(execute_column(context, block, nullptr, rows, filter_column));
1045
466k
    if (const auto* const_column = check_and_get_column<ColumnConst>(*filter_column)) {
1046
        // const(nullable) or const(bool)
1047
21.3k
        const bool result = accept_null
1048
21.3k
                                    ? (const_column->is_null_at(0) || const_column->get_bool(0))
1049
21.3k
                                    : (!const_column->is_null_at(0) && const_column->get_bool(0));
1050
21.3k
        if (!result) {
1051
            // filter all
1052
2.65k
            *can_filter_all = true;
1053
2.65k
            memset(result_filter_data, 0, rows);
1054
2.65k
            return Status::OK();
1055
2.65k
        }
1056
445k
    } else if (const auto* nullable_column = check_and_get_column<ColumnNullable>(*filter_column)) {
1057
        // nullable(bool)
1058
398k
        const ColumnPtr& nested_column = nullable_column->get_nested_column_ptr();
1059
398k
        const IColumn::Filter& filter = assert_cast<const ColumnUInt8&>(*nested_column).get_data();
1060
398k
        const auto* __restrict filter_data = filter.data();
1061
398k
        const auto* __restrict null_map_data = nullable_column->get_null_map_data().data();
1062
1063
398k
        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
398k
        } else {
1068
2.53G
            for (size_t i = 0; i < rows; ++i) {
1069
2.53G
                result_filter_data[i] &= (!null_map_data[i]) & filter_data[i];
1070
2.53G
            }
1071
398k
        }
1072
1073
398k
        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
398k
    } else {
1078
        // bool
1079
46.7k
        const IColumn::Filter& filter = assert_cast<const ColumnUInt8&>(*filter_column).get_data();
1080
46.7k
        const auto* __restrict filter_data = filter.data();
1081
1082
83.4M
        for (size_t i = 0; i < rows; ++i) {
1083
83.4M
            result_filter_data[i] &= filter_data[i];
1084
83.4M
        }
1085
1086
46.7k
        if (memchr(result_filter_data, 0x1, rows) == nullptr) {
1087
8.25k
            *can_filter_all = true;
1088
8.25k
            return Status::OK();
1089
8.25k
        }
1090
46.7k
    }
1091
1092
418k
    return Status::OK();
1093
466k
}
1094
1095
#include "common/compile_check_end.h"
1096
} // namespace doris