Coverage Report

Created: 2026-07-01 07:02

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