Coverage Report

Created: 2026-08-02 13:15

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