Coverage Report

Created: 2026-05-18 13:36

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