Coverage Report

Created: 2026-10-29 07:12

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