Coverage Report

Created: 2026-07-07 17:25

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