Coverage Report

Created: 2026-03-13 05:13

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/exprs/vexpr.cpp
Line
Count
Source
1
// Licensed to the Apache Software Foundation (ASF) under one
2
// or more contributor license agreements.  See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership.  The ASF licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License.  You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
18
#include "exprs/vexpr.h"
19
20
#include <fmt/format.h>
21
#include <gen_cpp/Exprs_types.h>
22
#include <gen_cpp/FrontendService_types.h>
23
#include <thrift/protocol/TDebugProtocol.h>
24
25
#include <algorithm>
26
#include <boost/algorithm/string/split.hpp>
27
#include <boost/iterator/iterator_facade.hpp>
28
#include <cstdint>
29
#include <memory>
30
#include <stack>
31
#include <string_view>
32
#include <utility>
33
34
#include "common/config.h"
35
#include "common/exception.h"
36
#include "common/status.h"
37
#include "core/column/column_vector.h"
38
#include "core/data_type/data_type_array.h"
39
#include "core/data_type/data_type_decimal.h"
40
#include "core/data_type/data_type_factory.hpp"
41
#include "core/data_type/data_type_nullable.h"
42
#include "core/data_type/data_type_number.h"
43
#include "core/data_type/define_primitive_type.h"
44
#include "core/field.h"
45
#include "core/value/timestamptz_value.h"
46
#include "exec/common/util.hpp"
47
#include "exec/pipeline/pipeline_task.h"
48
#include "exprs/short_circuit_evaluation_expr.h"
49
#include "exprs/varray_literal.h"
50
#include "exprs/vcase_expr.h"
51
#include "exprs/vcast_expr.h"
52
#include "exprs/vcolumn_ref.h"
53
#include "exprs/vcompound_pred.h"
54
#include "exprs/vcondition_expr.h"
55
#include "exprs/vectorized_fn_call.h"
56
#include "exprs/vexpr_context.h"
57
#include "exprs/vexpr_fwd.h"
58
#include "exprs/vin_predicate.h"
59
#include "exprs/vinfo_func.h"
60
#include "exprs/virtual_slot_ref.h"
61
#include "exprs/vlambda_function_call_expr.h"
62
#include "exprs/vlambda_function_expr.h"
63
#include "exprs/vliteral.h"
64
#include "exprs/vmap_literal.h"
65
#include "exprs/vmatch_predicate.h"
66
#include "exprs/vsearch.h"
67
#include "exprs/vslot_ref.h"
68
#include "exprs/vstruct_literal.h"
69
#include "storage/index/ann/ann_search_params.h"
70
#include "storage/index/ann/ann_topn_runtime.h"
71
#include "storage/index/inverted/inverted_index_parser.h"
72
#include "storage/segment/column_reader.h"
73
74
namespace doris {
75
#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
60.1k
                                 int scale) {
84
60.1k
    TExprNode node;
85
86
60.1k
    switch (type) {
87
58
    case TYPE_BOOLEAN: {
88
58
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_BOOLEAN>(data, &node));
89
58
        break;
90
58
    }
91
354
    case TYPE_TINYINT: {
92
354
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_TINYINT>(data, &node));
93
354
        break;
94
354
    }
95
354
    case TYPE_SMALLINT: {
96
249
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_SMALLINT>(data, &node));
97
249
        break;
98
249
    }
99
8.74k
    case TYPE_INT: {
100
8.74k
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_INT>(data, &node));
101
8.74k
        break;
102
8.74k
    }
103
48.4k
    case TYPE_BIGINT: {
104
48.4k
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_BIGINT>(data, &node));
105
48.4k
        break;
106
48.4k
    }
107
48.4k
    case TYPE_LARGEINT: {
108
54
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_LARGEINT>(data, &node));
109
54
        break;
110
54
    }
111
64
    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
5
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_DOUBLE>(data, &node));
117
5
        break;
118
5
    }
119
452
    case TYPE_DATEV2: {
120
452
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_DATEV2>(data, &node));
121
452
        break;
122
452
    }
123
897
    case TYPE_DATETIMEV2: {
124
897
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_DATETIMEV2>(data, &node, precision, scale));
125
897
        break;
126
897
    }
127
897
    case TYPE_DATE: {
128
0
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_DATE>(data, &node));
129
0
        break;
130
0
    }
131
0
    case TYPE_DATETIME: {
132
0
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_DATETIME>(data, &node));
133
0
        break;
134
0
    }
135
0
    case TYPE_DECIMALV2: {
136
0
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_DECIMALV2>(data, &node, precision, scale));
137
0
        break;
138
0
    }
139
112
    case TYPE_DECIMAL32: {
140
112
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_DECIMAL32>(data, &node, precision, scale));
141
112
        break;
142
112
    }
143
216
    case TYPE_DECIMAL64: {
144
216
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_DECIMAL64>(data, &node, precision, scale));
145
216
        break;
146
216
    }
147
216
    case TYPE_DECIMAL128I: {
148
55
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_DECIMAL128I>(data, &node, precision, scale));
149
55
        break;
150
55
    }
151
55
    case TYPE_DECIMAL256: {
152
32
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_DECIMAL256>(data, &node, precision, scale));
153
32
        break;
154
32
    }
155
72
    case TYPE_CHAR: {
156
72
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_CHAR>(data, &node));
157
72
        break;
158
72
    }
159
186
    case TYPE_VARCHAR: {
160
186
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_VARCHAR>(data, &node));
161
186
        break;
162
186
    }
163
186
    case TYPE_STRING: {
164
143
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_STRING>(data, &node));
165
143
        break;
166
143
    }
167
143
    case TYPE_IPV4: {
168
12
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_IPV4>(data, &node));
169
12
        break;
170
12
    }
171
12
    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
1
    case TYPE_TIMESTAMPTZ: {
180
1
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_TIMESTAMPTZ>(data, &node, precision, scale));
181
1
        break;
182
1
    }
183
1
    default:
184
0
        throw Exception(ErrorCode::INTERNAL_ERROR, "runtime filter meet invalid type {}",
185
0
                        int(type));
186
60.1k
    }
187
60.1k
    return node;
188
60.1k
}
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
67
    case TYPE_INT: {
210
67
        const auto& storage = static_cast<int32_t>(field.get<TYPE_INT>());
211
67
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_INT>(&storage, &node));
212
67
        break;
213
67
    }
214
67
    case TYPE_BIGINT: {
215
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
30
    case TYPE_DECIMAL64: {
277
30
        const auto& storage = field.get<TYPE_DECIMAL64>();
278
30
        THROW_IF_ERROR(
279
30
                create_texpr_literal_node<TYPE_DECIMAL64>(&storage, &node, precision, scale));
280
30
        break;
281
30
    }
282
30
    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
15.6k
bool VExpr::is_acting_on_a_slot(const VExpr& expr) {
343
15.6k
    if (expr.node_type() == TExprNodeType::SEARCH_EXPR) {
344
574
        return true;
345
574
    }
346
15.1k
    const auto& children = expr.children();
347
348
15.1k
    auto is_a_slot = std::any_of(children.begin(), children.end(),
349
15.1k
                                 [](const auto& child) { return is_acting_on_a_slot(*child); });
350
351
15.1k
    return is_a_slot ? true
352
15.1k
                     : (expr.node_type() == TExprNodeType::SLOT_REF ||
353
5.63k
                        expr.node_type() == TExprNodeType::VIRTUAL_SLOT_REF);
354
15.6k
}
355
356
VExpr::VExpr(const TExprNode& node)
357
10.1M
        : _node_type(node.node_type),
358
10.1M
          _opcode(node.__isset.opcode ? node.opcode : TExprOpcode::INVALID_OPCODE) {
359
10.1M
    if (node.__isset.fn) {
360
802k
        _fn = node.fn;
361
802k
    }
362
363
10.1M
    bool is_nullable = true;
364
10.1M
    if (node.__isset.is_nullable) {
365
9.98M
        is_nullable = node.is_nullable;
366
9.98M
    }
367
    // If we define null literal ,should make nullable data type to get correct field instead of undefined ptr
368
10.1M
    if (node.node_type == TExprNodeType::NULL_LITERAL) {
369
573k
        CHECK(is_nullable);
370
573k
    }
371
10.1M
    _data_type = get_data_type_with_default_argument(
372
10.1M
            DataTypeFactory::instance().create_data_type(node.type, is_nullable));
373
10.1M
}
374
375
0
VExpr::VExpr(const VExpr& vexpr) = default;
376
377
VExpr::VExpr(DataTypePtr type, bool is_slotref)
378
696
        : _opcode(TExprOpcode::INVALID_OPCODE),
379
696
          _data_type(get_data_type_with_default_argument(type)) {
380
696
    if (is_slotref) {
381
687
        _node_type = TExprNodeType::SLOT_REF;
382
687
    }
383
696
}
384
385
9.68M
Status VExpr::prepare(RuntimeState* state, const RowDescriptor& row_desc, VExprContext* context) {
386
9.68M
    ++context->_depth_num;
387
9.68M
    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
9.68M
    for (auto& i : _children) {
394
1.81M
        RETURN_IF_ERROR(i->prepare(state, row_desc, context));
395
1.81M
    }
396
9.68M
    --context->_depth_num;
397
9.68M
#ifndef BE_TEST
398
9.68M
    _enable_inverted_index_query = state->query_options().enable_inverted_index_query;
399
9.68M
#endif
400
9.68M
    return Status::OK();
401
9.68M
}
402
403
Status VExpr::open(RuntimeState* state, VExprContext* context,
404
33.4M
                   FunctionContext::FunctionStateScope scope) {
405
33.4M
    for (auto& i : _children) {
406
485k
        RETURN_IF_ERROR(i->open(state, context, scope));
407
485k
    }
408
33.4M
    if (scope == FunctionContext::FRAGMENT_LOCAL) {
409
8.81M
        RETURN_IF_ERROR(VExpr::get_const_col(context, nullptr));
410
8.81M
    }
411
33.4M
    return Status::OK();
412
33.4M
}
413
414
35.8M
void VExpr::close(VExprContext* context, FunctionContext::FunctionStateScope scope) {
415
35.8M
    for (auto& i : _children) {
416
4.70M
        i->close(context, scope);
417
4.70M
    }
418
35.8M
}
419
420
// NOLINTBEGIN(readability-function-size)
421
9.95M
Status VExpr::create_expr(const TExprNode& expr_node, VExprSPtr& expr) {
422
9.95M
    try {
423
9.95M
        switch (expr_node.node_type) {
424
24.1k
        case TExprNodeType::BOOL_LITERAL:
425
799k
        case TExprNodeType::INT_LITERAL:
426
847k
        case TExprNodeType::LARGE_INT_LITERAL:
427
849k
        case TExprNodeType::IPV4_LITERAL:
428
850k
        case TExprNodeType::IPV6_LITERAL:
429
897k
        case TExprNodeType::FLOAT_LITERAL:
430
962k
        case TExprNodeType::DECIMAL_LITERAL:
431
1.14M
        case TExprNodeType::DATE_LITERAL:
432
1.14M
        case TExprNodeType::TIMEV2_LITERAL:
433
1.94M
        case TExprNodeType::STRING_LITERAL:
434
1.94M
        case TExprNodeType::JSON_LITERAL:
435
1.94M
        case TExprNodeType::VARBINARY_LITERAL:
436
2.51M
        case TExprNodeType::NULL_LITERAL: {
437
2.51M
            expr = VLiteral::create_shared(expr_node);
438
2.51M
            break;
439
1.94M
        }
440
17.8k
        case TExprNodeType::ARRAY_LITERAL: {
441
17.8k
            expr = VArrayLiteral::create_shared(expr_node);
442
17.8k
            break;
443
1.94M
        }
444
11.9k
        case TExprNodeType::MAP_LITERAL: {
445
11.9k
            expr = VMapLiteral::create_shared(expr_node);
446
11.9k
            break;
447
1.94M
        }
448
1.57k
        case TExprNodeType::STRUCT_LITERAL: {
449
1.57k
            expr = VStructLiteral::create_shared(expr_node);
450
1.57k
            break;
451
1.94M
        }
452
6.52M
        case TExprNodeType::SLOT_REF: {
453
6.52M
            if (expr_node.slot_ref.__isset.is_virtual_slot && expr_node.slot_ref.is_virtual_slot) {
454
239
                expr = VirtualSlotRef::create_shared(expr_node);
455
239
                expr->_node_type = TExprNodeType::VIRTUAL_SLOT_REF;
456
6.52M
            } else {
457
6.52M
                expr = VSlotRef::create_shared(expr_node);
458
6.52M
            }
459
6.52M
            break;
460
1.94M
        }
461
1.29k
        case TExprNodeType::COLUMN_REF: {
462
1.29k
            expr = VColumnRef::create_shared(expr_node);
463
1.29k
            break;
464
1.94M
        }
465
3.59k
        case TExprNodeType::COMPOUND_PRED: {
466
3.59k
            expr = VCompoundPred::create_shared(expr_node);
467
3.59k
            break;
468
1.94M
        }
469
976
        case TExprNodeType::LAMBDA_FUNCTION_EXPR: {
470
976
            expr = VLambdaFunctionExpr::create_shared(expr_node);
471
976
            break;
472
1.94M
        }
473
976
        case TExprNodeType::LAMBDA_FUNCTION_CALL_EXPR: {
474
976
            expr = VLambdaFunctionCallExpr::create_shared(expr_node);
475
976
            break;
476
1.94M
        }
477
93.3k
        case TExprNodeType::ARITHMETIC_EXPR:
478
588k
        case TExprNodeType::BINARY_PRED:
479
589k
        case TExprNodeType::NULL_AWARE_BINARY_PRED:
480
589k
        case TExprNodeType::COMPUTE_FUNCTION_CALL: {
481
589k
            expr = VectorizedFnCall::create_shared(expr_node);
482
589k
            break;
483
589k
        }
484
150k
        case TExprNodeType::FUNCTION_CALL: {
485
150k
            if (expr_node.fn.name.function_name == "if") {
486
2.59k
                if (expr_node.__isset.short_circuit_evaluation &&
487
2.59k
                    expr_node.short_circuit_evaluation) {
488
875
                    expr = ShortCircuitIfExpr::create_shared(expr_node);
489
1.72k
                } else {
490
1.72k
                    expr = VectorizedIfExpr::create_shared(expr_node);
491
1.72k
                }
492
2.59k
                break;
493
148k
            } else if (expr_node.fn.name.function_name == "ifnull" ||
494
148k
                       expr_node.fn.name.function_name == "nvl") {
495
317
                if (expr_node.__isset.short_circuit_evaluation &&
496
317
                    expr_node.short_circuit_evaluation) {
497
108
                    expr = ShortCircuitIfNullExpr::create_shared(expr_node);
498
209
                } else {
499
209
                    expr = VectorizedIfNullExpr::create_shared(expr_node);
500
209
                }
501
317
                break;
502
148k
            } else if (expr_node.fn.name.function_name == "coalesce") {
503
203
                if (expr_node.__isset.short_circuit_evaluation &&
504
203
                    expr_node.short_circuit_evaluation) {
505
50
                    expr = ShortCircuitCoalesceExpr::create_shared(expr_node);
506
153
                } else {
507
153
                    expr = VectorizedCoalesceExpr::create_shared(expr_node);
508
153
                }
509
203
                break;
510
203
            }
511
147k
            expr = VectorizedFnCall::create_shared(expr_node);
512
147k
            break;
513
150k
        }
514
1.58k
        case TExprNodeType::MATCH_PRED: {
515
1.58k
            expr = VMatchPredicate::create_shared(expr_node);
516
1.58k
            break;
517
150k
        }
518
130k
        case TExprNodeType::CAST_EXPR: {
519
130k
            expr = VCastExpr::create_shared(expr_node);
520
130k
            break;
521
150k
        }
522
7
        case TExprNodeType::TRY_CAST_EXPR: {
523
7
            expr = TryCastExpr::create_shared(expr_node);
524
7
            break;
525
150k
        }
526
1.89k
        case TExprNodeType::IN_PRED: {
527
1.89k
            expr = VInPredicate::create_shared(expr_node);
528
1.89k
            break;
529
150k
        }
530
314
        case TExprNodeType::CASE_EXPR: {
531
314
            if (!expr_node.__isset.case_expr) {
532
0
                return Status::InternalError("Case expression not set in thrift node");
533
0
            }
534
314
            if (expr_node.__isset.short_circuit_evaluation && expr_node.short_circuit_evaluation) {
535
93
                expr = ShortCircuitCaseExpr::create_shared(expr_node);
536
221
            } else {
537
221
                expr = VCaseExpr::create_shared(expr_node);
538
221
            }
539
314
            break;
540
314
        }
541
0
        case TExprNodeType::INFO_FUNC: {
542
0
            expr = VInfoFunc::create_shared(expr_node);
543
0
            break;
544
314
        }
545
440
        case TExprNodeType::SEARCH_EXPR: {
546
440
            expr = VSearchExpr::create_shared(expr_node);
547
440
            break;
548
314
        }
549
0
        default:
550
0
            return Status::InternalError("Unknown expr node type: {}", expr_node.node_type);
551
9.95M
        }
552
9.95M
    } 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
9.96M
    if (!expr->data_type()) {
562
0
        return Status::InvalidArgument("Unknown expr type: {}", expr_node.node_type);
563
0
    }
564
9.96M
    return Status::OK();
565
9.96M
}
566
// NOLINTEND(readability-function-size)
567
568
Status VExpr::create_tree_from_thrift(const std::vector<TExprNode>& nodes, int* node_idx,
569
8.25M
                                      VExprSPtr& root_expr, VExprContextSPtr& ctx) {
570
    // propagate error case
571
8.25M
    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
8.25M
    int root_children = nodes[*node_idx].num_children;
577
8.25M
    VExprSPtr root;
578
8.25M
    RETURN_IF_ERROR(create_expr(nodes[*node_idx], root));
579
8.25M
    DCHECK(root != nullptr);
580
8.25M
    root_expr = root;
581
8.25M
    ctx = std::make_shared<VExprContext>(root);
582
    // short path for leaf node
583
8.25M
    if (root_children <= 0) {
584
7.54M
        return Status::OK();
585
7.54M
    }
586
587
    // non-recursive traversal
588
703k
    using VExprSPtrCountPair = std::pair<VExprSPtr, int>;
589
703k
    std::stack<std::shared_ptr<VExprSPtrCountPair>> s;
590
703k
    s.emplace(std::make_shared<VExprSPtrCountPair>(root, root_children));
591
2.35M
    while (!s.empty()) {
592
        // copy the shared ptr resource to avoid dangling reference
593
1.64M
        auto parent = s.top();
594
        // Decrement or pop
595
1.64M
        if (parent->second > 1) {
596
797k
            parent->second -= 1;
597
850k
        } else {
598
850k
            s.pop();
599
850k
        }
600
601
1.64M
        DCHECK(parent->first != nullptr);
602
1.64M
        if (++*node_idx >= nodes.size()) {
603
0
            return Status::InternalError("Failed to reconstruct expression tree from thrift.");
604
0
        }
605
606
1.64M
        VExprSPtr expr;
607
1.64M
        RETURN_IF_ERROR(create_expr(nodes[*node_idx], expr));
608
1.64M
        DCHECK(expr != nullptr);
609
1.64M
        parent->first->add_child(expr);
610
        // push to stack if has children
611
1.64M
        int num_children = nodes[*node_idx].num_children;
612
1.64M
        if (num_children > 0) {
613
143k
            s.emplace(std::make_shared<VExprSPtrCountPair>(expr, num_children));
614
143k
        }
615
1.64M
    }
616
703k
    return Status::OK();
617
703k
}
618
619
8.05M
Status VExpr::create_expr_tree(const TExpr& texpr, VExprContextSPtr& ctx) {
620
8.05M
    if (texpr.nodes.empty()) {
621
7
        ctx = nullptr;
622
7
        return Status::OK();
623
7
    }
624
8.05M
    int node_idx = 0;
625
8.05M
    VExprSPtr e;
626
8.05M
    Status status = create_tree_from_thrift(texpr.nodes, &node_idx, e, ctx);
627
8.06M
    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
8.05M
    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
8.05M
    return status;
638
8.05M
}
639
640
1.40M
Status VExpr::create_expr_trees(const std::vector<TExpr>& texprs, VExprContextSPtrs& ctxs) {
641
1.40M
    ctxs.clear();
642
6.44M
    for (const auto& texpr : texprs) {
643
6.44M
        VExprContextSPtr ctx;
644
6.44M
        RETURN_IF_ERROR(create_expr_tree(texpr, ctx));
645
6.44M
        ctxs.push_back(ctx);
646
6.44M
    }
647
1.40M
    return Status::OK();
648
1.40M
}
649
650
Status VExpr::check_expr_output_type(const VExprContextSPtrs& ctxs,
651
309k
                                     const RowDescriptor& output_row_desc) {
652
309k
    if (ctxs.empty()) {
653
2
        return Status::OK();
654
2
    }
655
309k
    auto name_and_types = VectorizedUtils::create_name_and_data_types(output_row_desc);
656
309k
    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
2.24M
    auto check_type_can_be_converted = [](DataTypePtr& from, DataTypePtr& to) -> bool {
662
2.24M
        if (to->equals(*from)) {
663
2.24M
            return true;
664
2.24M
        }
665
1.14k
        if (to->is_nullable() && !from->is_nullable()) {
666
99
            return remove_nullable(to)->equals(*from);
667
99
        }
668
1.04k
        return false;
669
1.14k
    };
670
2.55M
    for (int i = 0; i < ctxs.size(); i++) {
671
2.24M
        auto real_expr_type = get_data_type_with_default_argument(ctxs[i]->root()->data_type());
672
2.24M
        auto&& [name, expected_type] = name_and_types[i];
673
2.24M
        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
2.24M
    }
680
309k
    return Status::OK();
681
309k
}
682
683
Status VExpr::prepare(const VExprContextSPtrs& ctxs, RuntimeState* state,
684
2.14M
                      const RowDescriptor& row_desc) {
685
6.84M
    for (auto ctx : ctxs) {
686
6.84M
        RETURN_IF_ERROR(ctx->prepare(state, row_desc));
687
6.84M
    }
688
2.14M
    return Status::OK();
689
2.14M
}
690
691
2.15M
Status VExpr::open(const VExprContextSPtrs& ctxs, RuntimeState* state) {
692
6.84M
    for (const auto& ctx : ctxs) {
693
6.84M
        RETURN_IF_ERROR(ctx->open(state));
694
6.84M
    }
695
2.15M
    return Status::OK();
696
2.15M
}
697
698
1.45M
bool VExpr::contains_blockable_function(const VExprContextSPtrs& ctxs) {
699
2.67M
    return std::any_of(ctxs.begin(), ctxs.end(), [](const VExprContextSPtr& ctx) {
700
2.67M
        return ctx != nullptr && ctx->root() != nullptr && ctx->root()->is_blockable();
701
2.67M
    });
702
1.45M
}
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
1.35k
std::string VExpr::debug_string() const {
722
    // TODO: implement partial debug string for member vars
723
1.35k
    std::stringstream out;
724
1.35k
    out << " type=" << _data_type->get_name();
725
726
1.35k
    if (!_children.empty()) {
727
240
        out << " children=" << debug_string(_children);
728
240
    }
729
730
1.35k
    return out.str();
731
1.35k
}
732
733
240
std::string VExpr::debug_string(const VExprSPtrs& exprs) {
734
240
    std::stringstream out;
735
240
    out << "[";
736
737
480
    for (int i = 0; i < exprs.size(); ++i) {
738
240
        out << (i == 0 ? "" : " ") << exprs[i]->debug_string();
739
240
    }
740
741
240
    out << "]";
742
240
    return out.str();
743
240
}
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
7.60M
bool VExpr::is_constant() const {
754
7.60M
    return std::all_of(_children.begin(), _children.end(),
755
7.60M
                       [](const VExprSPtr& expr) { return expr->is_constant(); });
756
7.60M
}
757
758
Status VExpr::get_const_col(VExprContext* context,
759
11.8M
                            std::shared_ptr<ColumnPtrWrapper>* column_wrapper) {
760
11.8M
    if (!is_constant()) {
761
7.83M
        return Status::OK();
762
7.83M
    }
763
764
3.97M
    if (_constant_col != nullptr) {
765
1.29M
        DCHECK(column_wrapper != nullptr);
766
1.29M
        *column_wrapper = _constant_col;
767
1.29M
        return Status::OK();
768
1.29M
    }
769
770
2.68M
    ColumnPtr result;
771
2.68M
    RETURN_IF_ERROR(execute_column(context, nullptr, nullptr, 1, result));
772
2.67M
    _constant_col = std::make_shared<ColumnPtrWrapper>(result);
773
2.67M
    if (column_wrapper != nullptr) {
774
0
        *column_wrapper = _constant_col;
775
0
    }
776
777
2.67M
    return Status::OK();
778
2.68M
}
779
780
873k
void VExpr::register_function_context(RuntimeState* state, VExprContext* context) {
781
873k
    std::vector<DataTypePtr> arg_types;
782
1.59M
    for (auto& i : _children) {
783
1.59M
        arg_types.push_back(i->data_type());
784
1.59M
    }
785
786
873k
    _fn_context_index = context->register_function_context(state, _data_type, arg_types);
787
873k
}
788
789
Status VExpr::init_function_context(RuntimeState* state, VExprContext* context,
790
                                    FunctionContext::FunctionStateScope scope,
791
2.36M
                                    const FunctionBasePtr& function) const {
792
2.36M
    FunctionContext* fn_ctx = context->fn_context(_fn_context_index);
793
2.36M
    if (scope == FunctionContext::FRAGMENT_LOCAL) {
794
874k
        std::vector<std::shared_ptr<ColumnPtrWrapper>> constant_cols;
795
1.59M
        for (auto c : _children) {
796
1.59M
            std::shared_ptr<ColumnPtrWrapper> const_col;
797
1.59M
            RETURN_IF_ERROR(c->get_const_col(context, &const_col));
798
1.59M
            constant_cols.push_back(const_col);
799
1.59M
        }
800
874k
        fn_ctx->set_constant_cols(constant_cols);
801
874k
    }
802
803
2.36M
    if (scope == FunctionContext::FRAGMENT_LOCAL) {
804
874k
        RETURN_IF_ERROR(function->open(fn_ctx, FunctionContext::FRAGMENT_LOCAL));
805
874k
    }
806
2.36M
    RETURN_IF_ERROR(function->open(fn_ctx, FunctionContext::THREAD_LOCAL));
807
2.36M
    return Status::OK();
808
2.36M
}
809
810
void VExpr::close_function_context(VExprContext* context, FunctionContext::FunctionStateScope scope,
811
2.36M
                                   const FunctionBasePtr& function) const {
812
2.36M
    if (_fn_context_index != -1) {
813
2.36M
        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
2.36M
        static_cast<void>(function->close(fn_ctx, FunctionContext::THREAD_LOCAL));
816
2.36M
        if (scope == FunctionContext::FRAGMENT_LOCAL) {
817
874k
            static_cast<void>(function->close(fn_ctx, FunctionContext::FRAGMENT_LOCAL));
818
874k
        }
819
2.36M
    }
820
2.36M
}
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
574k
uint64_t VExpr::get_digest(uint64_t seed) const {
830
574k
    auto digest = seed;
831
1.13M
    for (auto child : _children) {
832
1.13M
        digest = child->get_digest(digest);
833
1.13M
        if (digest == 0) {
834
508
            return 0;
835
508
        }
836
1.13M
    }
837
838
573k
    auto& fn_name = _fn.name.function_name;
839
573k
    if (!fn_name.empty()) {
840
569k
        digest = HashUtil::hash64(fn_name.c_str(), fn_name.size(), digest);
841
569k
    } else {
842
4.22k
        digest = HashUtil::hash64((const char*)&_node_type, sizeof(_node_type), digest);
843
4.22k
        digest = HashUtil::hash64((const char*)&_opcode, sizeof(_opcode), digest);
844
4.22k
    }
845
573k
    return digest;
846
574k
}
847
848
73.6k
ColumnPtr VExpr::get_result_from_const(size_t count) const {
849
73.6k
    return ColumnConst::create(_constant_col->column_ptr, count);
850
73.6k
}
851
852
Status VExpr::_evaluate_inverted_index(VExprContext* context, const FunctionBasePtr& function,
853
14.9k
                                       uint32_t segment_num_rows) {
854
    // Pre-allocate vectors based on an estimated or known size
855
14.9k
    std::vector<segment_v2::IndexIterator*> iterators;
856
14.9k
    std::vector<IndexFieldNameAndTypePair> data_type_with_names;
857
14.9k
    std::vector<int> column_ids;
858
14.9k
    ColumnsWithTypeAndName arguments;
859
14.9k
    VExprSPtrs children_exprs;
860
861
    // Reserve space to avoid multiple reallocations
862
14.9k
    const size_t estimated_size = get_num_children();
863
14.9k
    iterators.reserve(estimated_size);
864
14.9k
    data_type_with_names.reserve(estimated_size);
865
14.9k
    column_ids.reserve(estimated_size);
866
14.9k
    children_exprs.reserve(estimated_size);
867
868
14.9k
    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
30.8k
    for (const auto& child : children()) {
874
30.8k
        if (child->node_type() == TExprNodeType::CAST_EXPR) {
875
3.12k
            auto* cast_expr = assert_cast<VCastExpr*>(child.get());
876
3.12k
            DCHECK_EQ(cast_expr->get_num_children(), 1);
877
3.12k
            if (cast_expr->get_child(0)->is_slot_ref()) {
878
3.00k
                auto* column_slot_ref = assert_cast<VSlotRef*>(cast_expr->get_child(0).get());
879
3.00k
                auto column_id = column_slot_ref->column_id();
880
3.00k
                const auto* storage_name_type =
881
3.00k
                        context->get_index_context()->get_storage_name_and_type_by_column_id(
882
3.00k
                                column_id);
883
3.00k
                auto storage_type = remove_nullable(storage_name_type->second);
884
3.00k
                auto target_type = remove_nullable(cast_expr->get_target_type());
885
3.00k
                auto origin_primitive_type = storage_type->get_primitive_type();
886
3.00k
                auto target_primitive_type = target_type->get_primitive_type();
887
3.00k
                if (is_complex_type(storage_type->get_primitive_type())) {
888
318
                    if (storage_type->get_primitive_type() == TYPE_ARRAY &&
889
318
                        target_type->get_primitive_type() == TYPE_ARRAY) {
890
309
                        auto nested_storage_type =
891
309
                                (assert_cast<const DataTypeArray*>(storage_type.get()))
892
309
                                        ->get_nested_type();
893
309
                        origin_primitive_type = nested_storage_type->get_primitive_type();
894
309
                        auto nested_target_type =
895
309
                                (assert_cast<const DataTypeArray*>(target_type.get()))
896
309
                                        ->get_nested_type();
897
309
                        target_primitive_type = nested_target_type->get_primitive_type();
898
309
                    } else {
899
9
                        continue;
900
9
                    }
901
318
                }
902
2.99k
                if (origin_primitive_type != TYPE_VARIANT &&
903
2.99k
                    (storage_type->equals(*target_type) ||
904
2.36k
                     (is_string_type(target_primitive_type) &&
905
1.62k
                      is_string_type(origin_primitive_type)))) {
906
733
                    children_exprs.emplace_back(expr_without_cast(child));
907
733
                }
908
2.99k
            } else {
909
123
                return Status::OK(); // for example: cast("abc") as ipv4 case
910
123
            }
911
27.7k
        } else {
912
27.7k
            children_exprs.emplace_back(child);
913
27.7k
        }
914
30.8k
    }
915
916
14.8k
    if (children_exprs.empty()) {
917
301
        return Status::OK(); // Early exit if no children to process
918
301
    }
919
920
27.5k
    for (const auto& child : children_exprs) {
921
27.5k
        if (child->is_slot_ref()) {
922
12.2k
            auto* column_slot_ref = assert_cast<VSlotRef*>(child.get());
923
12.2k
            auto column_id = column_slot_ref->column_id();
924
12.2k
            auto* iter = context->get_index_context()->get_inverted_index_iterator_by_column_id(
925
12.2k
                    column_id);
926
            //column does not have inverted index
927
12.2k
            if (iter == nullptr) {
928
2.96k
                continue;
929
2.96k
            }
930
9.32k
            const auto* storage_name_type =
931
9.32k
                    context->get_index_context()->get_storage_name_and_type_by_column_id(column_id);
932
9.32k
            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
9.32k
            iterators.emplace_back(iter);
941
9.32k
            data_type_with_names.emplace_back(*storage_name_type);
942
9.32k
            column_ids.emplace_back(column_id);
943
15.2k
        } else if (child->is_literal()) {
944
14.2k
            auto* column_literal = assert_cast<VLiteral*>(child.get());
945
14.2k
            arguments.emplace_back(column_literal->get_column_ptr(),
946
14.2k
                                   column_literal->get_data_type(), column_literal->expr_name());
947
14.2k
        } else if (child->can_push_down_to_index()) {
948
0
            RETURN_IF_ERROR(child->evaluate_inverted_index(context, segment_num_rows));
949
982
        } else {
950
982
            return Status::OK(); // others cases
951
982
        }
952
27.5k
    }
953
954
    // is null or is not null has no arguments
955
13.5k
    if (iterators.empty() || (arguments.empty() && !(function->get_name() == "is_not_null_pred" ||
956
4.69k
                                                     function->get_name() == "is_null_pred"))) {
957
4.69k
        return Status::OK(); // Nothing to evaluate or no literals to compare against
958
4.69k
    }
959
960
8.88k
    const InvertedIndexAnalyzerCtx* analyzer_ctx = nullptr;
961
9.00k
    if (auto index_ctx = context->get_index_context(); index_ctx != nullptr) {
962
9.00k
        analyzer_ctx = index_ctx->get_analyzer_ctx_for_expr(this);
963
9.00k
    }
964
965
8.88k
    auto result_bitmap = segment_v2::InvertedIndexResultBitmap();
966
    // Pass analyzer_key to function (used by match predicates for multi-analyzer index selection)
967
8.88k
    auto res = function->evaluate_inverted_index(arguments, data_type_with_names, iterators,
968
8.88k
                                                 segment_num_rows, analyzer_ctx, result_bitmap);
969
8.88k
    if (!res.ok()) {
970
339
        return res;
971
339
    }
972
8.54k
    if (!result_bitmap.is_empty()) {
973
6.87k
        index_context->set_index_result_for_expr(this, result_bitmap);
974
6.96k
        for (int column_id : column_ids) {
975
6.96k
            index_context->set_true_for_index_status(this, column_id);
976
6.96k
        }
977
6.87k
    }
978
8.54k
    return Status::OK();
979
8.88k
}
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
506k
                         ColumnPtr& result_column) const {
1001
506k
    if (context->get_index_context() &&
1002
506k
        context->get_index_context()->get_index_result_column().contains(this)) {
1003
        // prepare a column to save result
1004
1.00k
        result_column = filter_column_with_selector(
1005
1.00k
                context->get_index_context()->get_index_result_column()[this], selector, count);
1006
1.00k
        if (_data_type->is_nullable()) {
1007
810
            result_column = make_nullable(result_column);
1008
810
        }
1009
1.00k
        return true;
1010
1.00k
    }
1011
505k
    return false;
1012
506k
}
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.84k
        roaring::Roaring& row_bitmap, AnnIndexStats& ann_index_stats) {
1024
4.84k
    return Status::OK();
1025
4.84k
}
1026
1027
void VExpr::prepare_ann_range_search(const doris::VectorSearchUserParams& params,
1028
                                     segment_v2::AnnRangeSearchRuntime& range_search_runtime,
1029
15.8k
                                     bool& suitable_for_ann_index) {
1030
15.8k
    if (!suitable_for_ann_index) {
1031
0
        return;
1032
0
    }
1033
15.8k
    for (auto& child : _children) {
1034
10.0k
        child->prepare_ann_range_search(params, range_search_runtime, suitable_for_ann_index);
1035
10.0k
        if (!suitable_for_ann_index) {
1036
41
            return;
1037
41
        }
1038
10.0k
    }
1039
15.8k
}
1040
1041
26.6k
bool VExpr::ann_range_search_executedd() {
1042
26.6k
    return _has_been_executed;
1043
26.6k
}
1044
1045
53
bool VExpr::ann_dist_is_fulfilled() const {
1046
53
    return _virtual_column_is_fulfilled;
1047
53
}
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
148k
                             bool* can_filter_all) const {
1052
148k
    ColumnPtr filter_column;
1053
148k
    RETURN_IF_ERROR(execute_column(context, block, nullptr, rows, filter_column));
1054
148k
    if (const auto* const_column = check_and_get_column<ColumnConst>(*filter_column)) {
1055
        // const(nullable) or const(bool)
1056
14.8k
        const bool result = accept_null
1057
14.8k
                                    ? (const_column->is_null_at(0) || const_column->get_bool(0))
1058
14.8k
                                    : (!const_column->is_null_at(0) && const_column->get_bool(0));
1059
14.8k
        if (!result) {
1060
            // filter all
1061
2.63k
            *can_filter_all = true;
1062
2.63k
            memset(result_filter_data, 0, rows);
1063
2.63k
            return Status::OK();
1064
2.63k
        }
1065
133k
    } else if (const auto* nullable_column = check_and_get_column<ColumnNullable>(*filter_column)) {
1066
        // nullable(bool)
1067
89.8k
        const ColumnPtr& nested_column = nullable_column->get_nested_column_ptr();
1068
89.8k
        const IColumn::Filter& filter = assert_cast<const ColumnUInt8&>(*nested_column).get_data();
1069
89.8k
        const auto* __restrict filter_data = filter.data();
1070
89.8k
        const auto* __restrict null_map_data = nullable_column->get_null_map_data().data();
1071
1072
89.8k
        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
89.8k
        } else {
1077
115M
            for (size_t i = 0; i < rows; ++i) {
1078
115M
                result_filter_data[i] &= (!null_map_data[i]) & filter_data[i];
1079
115M
            }
1080
89.8k
        }
1081
1082
89.8k
        if (!simd::contain_one(result_filter_data, rows)) {
1083
36.7k
            *can_filter_all = true;
1084
36.7k
            return Status::OK();
1085
36.7k
        }
1086
89.8k
    } else {
1087
        // bool
1088
43.9k
        const IColumn::Filter& filter = assert_cast<const ColumnUInt8&>(*filter_column).get_data();
1089
43.9k
        const auto* __restrict filter_data = filter.data();
1090
1091
98.6M
        for (size_t i = 0; i < rows; ++i) {
1092
98.5M
            result_filter_data[i] &= filter_data[i];
1093
98.5M
        }
1094
1095
43.9k
        if (!simd::contain_one(result_filter_data, rows)) {
1096
7.87k
            *can_filter_all = true;
1097
7.87k
            return Status::OK();
1098
7.87k
        }
1099
43.9k
    }
1100
1101
101k
    return Status::OK();
1102
148k
}
1103
1104
#include "common/compile_check_end.h"
1105
} // namespace doris