Coverage Report

Created: 2026-06-09 07:29

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