Coverage Report

Created: 2026-03-12 03:42

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