Coverage Report

Created: 2026-06-05 05:38

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