Coverage Report

Created: 2026-07-20 20:20

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