Coverage Report

Created: 2026-06-25 13:24

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