Coverage Report

Created: 2026-07-08 18:16

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
167k
                                 int scale) {
98
167k
    TExprNode node;
99
100
167k
    switch (type) {
101
19
    case TYPE_BOOLEAN: {
102
19
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_BOOLEAN>(data, &node));
103
19
        break;
104
19
    }
105
1.35k
    case TYPE_TINYINT: {
106
1.35k
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_TINYINT>(data, &node));
107
1.35k
        break;
108
1.35k
    }
109
1.35k
    case TYPE_SMALLINT: {
110
1.04k
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_SMALLINT>(data, &node));
111
1.04k
        break;
112
1.04k
    }
113
158k
    case TYPE_INT: {
114
158k
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_INT>(data, &node));
115
158k
        break;
116
158k
    }
117
158k
    case TYPE_BIGINT: {
118
3.63k
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_BIGINT>(data, &node));
119
3.63k
        break;
120
3.63k
    }
121
3.63k
    case TYPE_LARGEINT: {
122
114
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_LARGEINT>(data, &node));
123
114
        break;
124
114
    }
125
114
    case TYPE_FLOAT: {
126
6
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_FLOAT>(data, &node));
127
6
        break;
128
6
    }
129
49
    case TYPE_DOUBLE: {
130
49
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_DOUBLE>(data, &node));
131
49
        break;
132
49
    }
133
451
    case TYPE_DATEV2: {
134
451
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_DATEV2>(data, &node));
135
451
        break;
136
451
    }
137
451
    case TYPE_DATETIMEV2: {
138
213
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_DATETIMEV2>(data, &node, precision, scale));
139
213
        break;
140
213
    }
141
213
    case TYPE_DATE: {
142
47
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_DATE>(data, &node));
143
47
        break;
144
47
    }
145
47
    case TYPE_DATETIME: {
146
47
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_DATETIME>(data, &node));
147
47
        break;
148
47
    }
149
47
    case TYPE_DECIMALV2: {
150
18
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_DECIMALV2>(data, &node, precision, scale));
151
18
        break;
152
18
    }
153
36
    case TYPE_DECIMAL32: {
154
36
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_DECIMAL32>(data, &node, precision, scale));
155
36
        break;
156
36
    }
157
333
    case TYPE_DECIMAL64: {
158
333
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_DECIMAL64>(data, &node, precision, scale));
159
333
        break;
160
333
    }
161
333
    case TYPE_DECIMAL128I: {
162
117
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_DECIMAL128I>(data, &node, precision, scale));
163
117
        break;
164
117
    }
165
436
    case TYPE_DECIMAL256: {
166
436
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_DECIMAL256>(data, &node, precision, scale));
167
436
        break;
168
436
    }
169
436
    case TYPE_CHAR: {
170
48
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_CHAR>(data, &node));
171
48
        break;
172
48
    }
173
507
    case TYPE_VARCHAR: {
174
507
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_VARCHAR>(data, &node));
175
507
        break;
176
507
    }
177
507
    case TYPE_STRING: {
178
29
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_STRING>(data, &node));
179
29
        break;
180
29
    }
181
29
    case TYPE_VARBINARY: {
182
0
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_VARBINARY>(data, &node));
183
0
        break;
184
0
    }
185
376
    case TYPE_IPV4: {
186
376
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_IPV4>(data, &node));
187
376
        break;
188
376
    }
189
376
    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
91
    case TYPE_TIMESTAMPTZ: {
198
91
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_TIMESTAMPTZ>(data, &node, precision, scale));
199
91
        break;
200
91
    }
201
91
    default:
202
0
        throw Exception(ErrorCode::INTERNAL_ERROR, "runtime filter meet invalid type {}",
203
0
                        int(type));
204
167k
    }
205
166k
    return node;
206
167k
}
207
208
TExprNode create_texpr_node_from(const Field& field, const PrimitiveType& type, int precision,
209
1.06k
                                 int scale) {
210
1.06k
    TExprNode node;
211
1.06k
    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
44
    case TYPE_INT: {
228
44
        const auto& storage = static_cast<int32_t>(field.get<TYPE_INT>());
229
44
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_INT>(&storage, &node));
230
44
        break;
231
44
    }
232
44
    case TYPE_BIGINT: {
233
1
        const auto& storage = static_cast<int64_t>(field.get<TYPE_BIGINT>());
234
1
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_BIGINT>(&storage, &node));
235
1
        break;
236
1
    }
237
1
    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
259
    case TYPE_CHAR: {
313
259
        const auto& storage = field.get<TYPE_CHAR>();
314
259
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_CHAR>(&storage, &node));
315
259
        break;
316
259
    }
317
628
    case TYPE_VARCHAR: {
318
628
        const auto& storage = field.get<TYPE_VARCHAR>();
319
628
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_VARCHAR>(&storage, &node));
320
628
        break;
321
628
    }
322
628
    case TYPE_STRING: {
323
114
        const auto& storage = field.get<TYPE_STRING>();
324
114
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_STRING>(&storage, &node));
325
114
        break;
326
114
    }
327
114
    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
1.06k
    }
351
1.06k
    return node;
352
1.06k
}
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
6.61M
        : _node_type(node.node_type),
362
6.61M
          _opcode(node.__isset.opcode ? node.opcode : TExprOpcode::INVALID_OPCODE) {
363
6.61M
    if (node.__isset.fn) {
364
496k
        _fn = node.fn;
365
496k
    }
366
367
6.61M
    bool is_nullable = true;
368
6.61M
    if (node.__isset.is_nullable) {
369
6.42M
        is_nullable = node.is_nullable;
370
6.42M
    }
371
    // If we define null literal ,should make nullable data type to get correct field instead of undefined ptr
372
6.61M
    if (node.node_type == TExprNodeType::NULL_LITERAL) {
373
108k
        CHECK(is_nullable);
374
108k
    }
375
6.61M
    _data_type = get_data_type_with_default_argument(
376
6.61M
            DataTypeFactory::instance().create_data_type(node.type, is_nullable));
377
6.61M
}
378
379
81
VExpr::VExpr(const VExpr& vexpr) = default;
380
381
VExpr::VExpr(DataTypePtr type, bool is_slotref)
382
4.18k
        : _opcode(TExprOpcode::INVALID_OPCODE),
383
4.18k
          _data_type(get_data_type_with_default_argument(type)) {
384
4.18k
    if (is_slotref) {
385
3.57k
        _node_type = TExprNodeType::SLOT_REF;
386
3.57k
    }
387
4.18k
}
388
389
17
TExprNode VExpr::clone_texpr_node() const {
390
17
    TExprNode node;
391
17
    node.__set_node_type(_node_type);
392
17
    node.__set_opcode(_opcode);
393
17
    node.__set_type(create_type_desc(remove_nullable(_data_type)->get_primitive_type(),
394
17
                                     static_cast<int>(_data_type->get_precision()),
395
17
                                     static_cast<int>(_data_type->get_scale())));
396
17
    node.__set_is_nullable(_data_type->is_nullable());
397
17
    node.__set_num_children(get_num_children());
398
17
    node.__set_fn(_fn);
399
17
    return node;
400
17
}
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
453
                         const VExprCloneNodeOverride& clone_node_override) const {
409
453
    DORIS_CHECK(cloned_expr != nullptr);
410
411
453
    VExprSPtr cloned;
412
453
    if (clone_node_override) {
413
441
        RETURN_IF_ERROR(clone_node_override(*this, &cloned));
414
441
    }
415
453
    if (cloned == nullptr) {
416
452
        RETURN_IF_ERROR(clone_node(&cloned));
417
452
    }
418
453
    DORIS_CHECK(cloned != nullptr);
419
420
453
    VExprSPtrs cloned_children;
421
453
    cloned_children.reserve(_children.size());
422
453
    for (const auto& child : _children) {
423
323
        DORIS_CHECK(child != nullptr);
424
323
        VExprSPtr cloned_child;
425
323
        RETURN_IF_ERROR(child->deep_clone(&cloned_child, clone_node_override));
426
323
        cloned_children.push_back(std::move(cloned_child));
427
323
    }
428
453
    cloned->set_children(std::move(cloned_children));
429
453
    cloned->reset_prepare_state();
430
453
    *cloned_expr = std::move(cloned);
431
453
    return Status::OK();
432
453
}
433
434
6.21M
Status VExpr::prepare(RuntimeState* state, const RowDescriptor& row_desc, VExprContext* context) {
435
6.21M
    ++context->_depth_num;
436
6.21M
    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
6.21M
    for (auto& i : _children) {
443
1.23M
        RETURN_IF_ERROR(i->prepare(state, row_desc, context));
444
1.23M
    }
445
6.21M
    --context->_depth_num;
446
6.21M
#ifndef BE_TEST
447
6.21M
    _enable_inverted_index_query = state->query_options().enable_inverted_index_query;
448
6.21M
#endif
449
6.21M
    return Status::OK();
450
6.21M
}
451
452
Status VExpr::open(RuntimeState* state, VExprContext* context,
453
20.3M
                   FunctionContext::FunctionStateScope scope) {
454
20.3M
    for (auto& i : _children) {
455
368k
        RETURN_IF_ERROR(i->open(state, context, scope));
456
368k
    }
457
20.3M
    if (scope == FunctionContext::FRAGMENT_LOCAL) {
458
5.61M
        RETURN_IF_ERROR(VExpr::get_const_col(context, nullptr));
459
5.61M
    }
460
20.3M
    return Status::OK();
461
20.3M
}
462
463
914
void VExpr::reset_prepare_state() {
464
914
    _prepared = false;
465
914
    _prepare_finished = false;
466
914
    _open_finished = false;
467
914
    for (auto& child : _children) {
468
461
        child->reset_prepare_state();
469
461
    }
470
914
}
471
472
22.1M
void VExpr::close(VExprContext* context, FunctionContext::FunctionStateScope scope) {
473
22.1M
    for (auto& i : _children) {
474
3.45M
        i->close(context, scope);
475
3.45M
    }
476
22.1M
}
477
478
// NOLINTBEGIN(readability-function-size)
479
6.41M
Status VExpr::create_expr(const TExprNode& expr_node, VExprSPtr& expr) {
480
6.41M
    try {
481
6.41M
        switch (expr_node.node_type) {
482
22.2k
        case TExprNodeType::BOOL_LITERAL:
483
705k
        case TExprNodeType::INT_LITERAL:
484
753k
        case TExprNodeType::LARGE_INT_LITERAL:
485
755k
        case TExprNodeType::IPV4_LITERAL:
486
757k
        case TExprNodeType::IPV6_LITERAL:
487
800k
        case TExprNodeType::FLOAT_LITERAL:
488
856k
        case TExprNodeType::DECIMAL_LITERAL:
489
1.02M
        case TExprNodeType::DATE_LITERAL:
490
1.02M
        case TExprNodeType::TIMEV2_LITERAL:
491
1.61M
        case TExprNodeType::STRING_LITERAL:
492
1.61M
        case TExprNodeType::JSON_LITERAL:
493
1.61M
        case TExprNodeType::VARBINARY_LITERAL:
494
1.72M
        case TExprNodeType::NULL_LITERAL: {
495
1.72M
            expr = VLiteral::create_shared(expr_node);
496
1.72M
            break;
497
1.61M
        }
498
12.2k
        case TExprNodeType::ARRAY_LITERAL: {
499
12.2k
            expr = VArrayLiteral::create_shared(expr_node);
500
12.2k
            break;
501
1.61M
        }
502
11.9k
        case TExprNodeType::MAP_LITERAL: {
503
11.9k
            expr = VMapLiteral::create_shared(expr_node);
504
11.9k
            break;
505
1.61M
        }
506
1.46k
        case TExprNodeType::STRUCT_LITERAL: {
507
1.46k
            expr = VStructLiteral::create_shared(expr_node);
508
1.46k
            break;
509
1.61M
        }
510
4.05M
        case TExprNodeType::SLOT_REF: {
511
4.05M
            if (expr_node.slot_ref.__isset.is_virtual_slot && expr_node.slot_ref.is_virtual_slot) {
512
212
                expr = VirtualSlotRef::create_shared(expr_node);
513
212
                expr->_node_type = TExprNodeType::VIRTUAL_SLOT_REF;
514
4.05M
            } else {
515
4.05M
                expr = VSlotRef::create_shared(expr_node);
516
4.05M
            }
517
4.05M
            break;
518
1.61M
        }
519
1.33k
        case TExprNodeType::COLUMN_REF: {
520
1.33k
            expr = VColumnRef::create_shared(expr_node);
521
1.33k
            break;
522
1.61M
        }
523
4.64k
        case TExprNodeType::COMPOUND_PRED: {
524
4.64k
            expr = VCompoundPred::create_shared(expr_node);
525
4.64k
            break;
526
1.61M
        }
527
1.01k
        case TExprNodeType::LAMBDA_FUNCTION_EXPR: {
528
1.01k
            expr = VLambdaFunctionExpr::create_shared(expr_node);
529
1.01k
            break;
530
1.61M
        }
531
1.01k
        case TExprNodeType::LAMBDA_FUNCTION_CALL_EXPR: {
532
1.01k
            expr = VLambdaFunctionCallExpr::create_shared(expr_node);
533
1.01k
            break;
534
1.61M
        }
535
78.9k
        case TExprNodeType::ARITHMETIC_EXPR:
536
342k
        case TExprNodeType::BINARY_PRED:
537
342k
        case TExprNodeType::NULL_AWARE_BINARY_PRED:
538
342k
        case TExprNodeType::COMPUTE_FUNCTION_CALL: {
539
342k
            expr = VectorizedFnCall::create_shared(expr_node);
540
342k
            break;
541
342k
        }
542
141k
        case TExprNodeType::FUNCTION_CALL: {
543
141k
            if (expr_node.fn.name.function_name == "if") {
544
1.51k
                if (expr_node.__isset.short_circuit_evaluation &&
545
1.51k
                    expr_node.short_circuit_evaluation) {
546
989
                    expr = ShortCircuitIfExpr::create_shared(expr_node);
547
989
                } else {
548
524
                    expr = VectorizedIfExpr::create_shared(expr_node);
549
524
                }
550
1.51k
                break;
551
139k
            } else if (expr_node.fn.name.function_name == "ifnull" ||
552
139k
                       expr_node.fn.name.function_name == "nvl") {
553
475
                if (expr_node.__isset.short_circuit_evaluation &&
554
475
                    expr_node.short_circuit_evaluation) {
555
230
                    expr = ShortCircuitIfNullExpr::create_shared(expr_node);
556
245
                } else {
557
245
                    expr = VectorizedIfNullExpr::create_shared(expr_node);
558
245
                }
559
475
                break;
560
139k
            } else if (expr_node.fn.name.function_name == "coalesce") {
561
155
                if (expr_node.__isset.short_circuit_evaluation &&
562
155
                    expr_node.short_circuit_evaluation) {
563
64
                    expr = ShortCircuitCoalesceExpr::create_shared(expr_node);
564
91
                } else {
565
91
                    expr = VectorizedCoalesceExpr::create_shared(expr_node);
566
91
                }
567
155
                break;
568
155
            }
569
139k
            expr = VectorizedFnCall::create_shared(expr_node);
570
139k
            break;
571
141k
        }
572
1.64k
        case TExprNodeType::MATCH_PRED: {
573
1.64k
            expr = VMatchPredicate::create_shared(expr_node);
574
1.64k
            break;
575
141k
        }
576
117k
        case TExprNodeType::CAST_EXPR: {
577
117k
            expr = VCastExpr::create_shared(expr_node);
578
117k
            break;
579
141k
        }
580
11
        case TExprNodeType::TRY_CAST_EXPR: {
581
11
            expr = TryCastExpr::create_shared(expr_node);
582
11
            break;
583
141k
        }
584
1.25k
        case TExprNodeType::IN_PRED: {
585
1.25k
            expr = VInPredicate::create_shared(expr_node);
586
1.25k
            break;
587
141k
        }
588
274
        case TExprNodeType::CASE_EXPR: {
589
274
            if (!expr_node.__isset.case_expr) {
590
0
                return Status::InternalError("Case expression not set in thrift node");
591
0
            }
592
274
            if (expr_node.__isset.short_circuit_evaluation && expr_node.short_circuit_evaluation) {
593
105
                expr = ShortCircuitCaseExpr::create_shared(expr_node);
594
169
            } else {
595
169
                expr = VCaseExpr::create_shared(expr_node);
596
169
            }
597
274
            break;
598
274
        }
599
0
        case TExprNodeType::INFO_FUNC: {
600
0
            expr = VInfoFunc::create_shared(expr_node);
601
0
            break;
602
274
        }
603
463
        case TExprNodeType::SEARCH_EXPR: {
604
463
            expr = VSearchExpr::create_shared(expr_node);
605
463
            break;
606
274
        }
607
0
        default:
608
0
            return Status::InternalError("Unknown expr node type: {}", expr_node.node_type);
609
6.41M
        }
610
6.41M
    } 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
6.42M
    if (!expr->data_type()) {
620
0
        return Status::InvalidArgument("Unknown expr type: {}", expr_node.node_type);
621
0
    }
622
6.42M
    return Status::OK();
623
6.42M
}
624
// NOLINTEND(readability-function-size)
625
626
Status VExpr::create_tree_from_thrift(const std::vector<TExprNode>& nodes, int* node_idx,
627
5.20M
                                      VExprSPtr& root_expr, VExprContextSPtr& ctx) {
628
    // propagate error case
629
5.20M
    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
5.20M
    int root_children = nodes[*node_idx].num_children;
635
5.20M
    VExprSPtr root;
636
5.20M
    RETURN_IF_ERROR(create_expr(nodes[*node_idx], root));
637
5.20M
    DCHECK(root != nullptr);
638
5.20M
    root_expr = root;
639
5.20M
    ctx = std::make_shared<VExprContext>(root);
640
    // short path for leaf node
641
5.20M
    if (root_children <= 0) {
642
4.70M
        return Status::OK();
643
4.70M
    }
644
645
    // non-recursive traversal
646
494k
    using VExprSPtrCountPair = std::pair<VExprSPtr, int>;
647
494k
    std::stack<std::shared_ptr<VExprSPtrCountPair>> s;
648
494k
    s.emplace(std::make_shared<VExprSPtrCountPair>(root, root_children));
649
1.70M
    while (!s.empty()) {
650
        // copy the shared ptr resource to avoid dangling reference
651
1.20M
        auto parent = s.top();
652
        // Decrement or pop
653
1.20M
        if (parent->second > 1) {
654
584k
            parent->second -= 1;
655
624k
        } else {
656
624k
            s.pop();
657
624k
        }
658
659
1.20M
        DCHECK(parent->first != nullptr);
660
1.20M
        if (++*node_idx >= nodes.size()) {
661
0
            return Status::InternalError("Failed to reconstruct expression tree from thrift.");
662
0
        }
663
664
1.20M
        VExprSPtr expr;
665
1.20M
        RETURN_IF_ERROR(create_expr(nodes[*node_idx], expr));
666
1.20M
        DCHECK(expr != nullptr);
667
1.20M
        parent->first->add_child(expr);
668
        // push to stack if has children
669
1.20M
        int num_children = nodes[*node_idx].num_children;
670
1.20M
        if (num_children > 0) {
671
127k
            s.emplace(std::make_shared<VExprSPtrCountPair>(expr, num_children));
672
127k
        }
673
1.20M
    }
674
494k
    return Status::OK();
675
494k
}
676
677
5.11M
Status VExpr::create_expr_tree(const TExpr& texpr, VExprContextSPtr& ctx) {
678
5.11M
    if (texpr.nodes.empty()) {
679
9
        ctx = nullptr;
680
9
        return Status::OK();
681
9
    }
682
5.11M
    int node_idx = 0;
683
5.11M
    VExprSPtr e;
684
5.11M
    Status status = create_tree_from_thrift(texpr.nodes, &node_idx, e, ctx);
685
5.12M
    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
5.11M
    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
5.11M
    return status;
696
5.11M
}
697
698
1.09M
Status VExpr::create_expr_trees(const std::vector<TExpr>& texprs, VExprContextSPtrs& ctxs) {
699
1.09M
    ctxs.clear();
700
4.37M
    for (const auto& texpr : texprs) {
701
4.37M
        VExprContextSPtr ctx;
702
4.37M
        RETURN_IF_ERROR(create_expr_tree(texpr, ctx));
703
4.37M
        ctxs.push_back(ctx);
704
4.37M
    }
705
1.09M
    return Status::OK();
706
1.09M
}
707
708
Status VExpr::check_expr_output_type(const VExprContextSPtrs& ctxs,
709
235k
                                     const RowDescriptor& output_row_desc) {
710
235k
    if (ctxs.empty()) {
711
6
        return Status::OK();
712
6
    }
713
235k
    auto name_and_types = VectorizedUtils::create_name_and_data_types(output_row_desc);
714
235k
    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.39M
    auto check_type_can_be_converted = [](DataTypePtr& from, DataTypePtr& to) -> bool {
720
1.39M
        if (to->equals(*from)) {
721
1.39M
            return true;
722
1.39M
        }
723
913
        if (to->is_nullable() && !from->is_nullable()) {
724
87
            return remove_nullable(to)->equals(*from);
725
87
        }
726
826
        return false;
727
913
    };
728
1.63M
    for (int i = 0; i < ctxs.size(); i++) {
729
1.39M
        auto real_expr_type = get_data_type_with_default_argument(ctxs[i]->root()->data_type());
730
1.39M
        auto&& [name, expected_type] = name_and_types[i];
731
1.39M
        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.39M
    }
738
235k
    return Status::OK();
739
235k
}
740
741
Status VExpr::prepare(const VExprContextSPtrs& ctxs, RuntimeState* state,
742
1.72M
                      const RowDescriptor& row_desc) {
743
4.66M
    for (auto ctx : ctxs) {
744
4.66M
        RETURN_IF_ERROR(ctx->prepare(state, row_desc));
745
4.66M
    }
746
1.72M
    return Status::OK();
747
1.72M
}
748
749
1.73M
Status VExpr::open(const VExprContextSPtrs& ctxs, RuntimeState* state) {
750
4.66M
    for (const auto& ctx : ctxs) {
751
4.66M
        RETURN_IF_ERROR(ctx->open(state));
752
4.66M
    }
753
1.73M
    return Status::OK();
754
1.73M
}
755
756
1.30M
bool VExpr::contains_blockable_function(const VExprContextSPtrs& ctxs) {
757
1.64M
    return std::any_of(ctxs.begin(), ctxs.end(), [](const VExprContextSPtr& ctx) {
758
1.64M
        return ctx != nullptr && ctx->root() != nullptr && ctx->root()->is_blockable();
759
1.64M
    });
760
1.30M
}
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
14
std::string VExpr::debug_string() const {
780
    // TODO: implement partial debug string for member vars
781
14
    std::stringstream out;
782
14
    out << " type=" << _data_type->get_name();
783
784
14
    if (!_children.empty()) {
785
0
        out << " children=" << debug_string(_children);
786
0
    }
787
788
14
    return out.str();
789
14
}
790
791
0
std::string VExpr::debug_string(const VExprSPtrs& exprs) {
792
0
    std::stringstream out;
793
0
    out << "[";
794
795
0
    for (int i = 0; i < exprs.size(); ++i) {
796
0
        out << (i == 0 ? "" : " ") << exprs[i]->debug_string();
797
0
    }
798
799
0
    out << "]";
800
0
    return out.str();
801
0
}
802
803
0
std::string VExpr::debug_string(const VExprContextSPtrs& ctxs) {
804
0
    VExprSPtrs exprs;
805
0
    for (const auto& ctx : ctxs) {
806
0
        exprs.push_back(ctx->root());
807
0
    }
808
0
    return debug_string(exprs);
809
0
}
810
811
5.18M
bool VExpr::is_constant() const {
812
5.18M
    return std::all_of(_children.begin(), _children.end(),
813
5.18M
                       [](const VExprSPtr& expr) { return expr->is_constant(); });
814
5.18M
}
815
816
Status VExpr::get_const_col(VExprContext* context,
817
7.55M
                            std::shared_ptr<ColumnPtrWrapper>* column_wrapper) {
818
7.55M
    if (!is_constant()) {
819
4.94M
        return Status::OK();
820
4.94M
    }
821
822
2.61M
    if (_constant_col != nullptr && column_wrapper == nullptr) {
823
0
        return Status::OK();
824
2.61M
    } else if (_constant_col != nullptr) {
825
777k
        *column_wrapper = _constant_col;
826
777k
        return Status::OK();
827
777k
    }
828
829
1.83M
    ColumnPtr result;
830
1.83M
    RETURN_IF_ERROR(execute_column(context, nullptr, nullptr, 1, result));
831
1.83M
    _constant_col = std::make_shared<ColumnPtrWrapper>(result);
832
1.83M
    if (column_wrapper != nullptr) {
833
0
        *column_wrapper = _constant_col;
834
0
    }
835
836
1.83M
    return Status::OK();
837
1.83M
}
838
839
605k
void VExpr::register_function_context(RuntimeState* state, VExprContext* context) {
840
605k
    std::vector<DataTypePtr> arg_types;
841
1.06M
    for (auto& i : _children) {
842
1.06M
        arg_types.push_back(i->data_type());
843
1.06M
    }
844
845
605k
    _fn_context_index = context->register_function_context(state, _data_type, arg_types);
846
605k
}
847
848
Status VExpr::init_function_context(RuntimeState* state, VExprContext* context,
849
                                    FunctionContext::FunctionStateScope scope,
850
1.77M
                                    const FunctionBasePtr& function) const {
851
1.77M
    FunctionContext* fn_ctx = context->fn_context(_fn_context_index);
852
1.77M
    if (scope == FunctionContext::FRAGMENT_LOCAL) {
853
606k
        std::vector<std::shared_ptr<ColumnPtrWrapper>> constant_cols;
854
1.06M
        for (auto c : _children) {
855
1.06M
            std::shared_ptr<ColumnPtrWrapper> const_col;
856
1.06M
            RETURN_IF_ERROR(c->get_const_col(context, &const_col));
857
1.06M
            constant_cols.push_back(const_col);
858
1.06M
        }
859
606k
        fn_ctx->set_constant_cols(constant_cols);
860
606k
    }
861
862
1.77M
    if (scope == FunctionContext::FRAGMENT_LOCAL) {
863
606k
        RETURN_IF_ERROR(function->open(fn_ctx, FunctionContext::FRAGMENT_LOCAL));
864
606k
    }
865
1.77M
    RETURN_IF_ERROR(function->open(fn_ctx, FunctionContext::THREAD_LOCAL));
866
1.77M
    return Status::OK();
867
1.77M
}
868
869
void VExpr::close_function_context(VExprContext* context, FunctionContext::FunctionStateScope scope,
870
1.77M
                                   const FunctionBasePtr& function) const {
871
1.78M
    if (_fn_context_index != -1) {
872
1.78M
        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
1.78M
        static_cast<void>(function->close(fn_ctx, FunctionContext::THREAD_LOCAL));
875
1.78M
        if (scope == FunctionContext::FRAGMENT_LOCAL) {
876
605k
            static_cast<void>(function->close(fn_ctx, FunctionContext::FRAGMENT_LOCAL));
877
605k
        }
878
1.78M
    }
879
1.77M
}
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
289k
uint64_t VExpr::get_digest(uint64_t seed) const {
889
289k
    auto digest = seed;
890
573k
    for (auto child : _children) {
891
573k
        digest = child->get_digest(digest);
892
573k
        if (digest == 0) {
893
292
            return 0;
894
292
        }
895
573k
    }
896
897
289k
    auto& fn_name = _fn.name.function_name;
898
289k
    if (!fn_name.empty()) {
899
286k
        digest = HashUtil::hash64(fn_name.c_str(), fn_name.size(), digest);
900
286k
    } else {
901
2.35k
        digest = HashUtil::hash64((const char*)&_node_type, sizeof(_node_type), digest);
902
2.35k
        digest = HashUtil::hash64((const char*)&_opcode, sizeof(_opcode), digest);
903
2.35k
    }
904
289k
    return digest;
905
289k
}
906
907
75.9k
ColumnPtr VExpr::get_result_from_const(size_t count) const {
908
75.9k
    return ColumnConst::create(_constant_col->column_ptr, count);
909
75.9k
}
910
911
Status VExpr::_evaluate_inverted_index(VExprContext* context, const FunctionBasePtr& function,
912
18.5k
                                       uint32_t segment_num_rows) {
913
    // Pre-allocate vectors based on an estimated or known size
914
18.5k
    std::vector<segment_v2::IndexIterator*> iterators;
915
18.5k
    std::vector<IndexFieldNameAndTypePair> data_type_with_names;
916
18.5k
    std::vector<int> column_ids;
917
18.5k
    ColumnsWithTypeAndName arguments;
918
18.5k
    VExprSPtrs children_exprs;
919
920
    // Reserve space to avoid multiple reallocations
921
18.5k
    const size_t estimated_size = get_num_children();
922
18.5k
    iterators.reserve(estimated_size);
923
18.5k
    data_type_with_names.reserve(estimated_size);
924
18.5k
    column_ids.reserve(estimated_size);
925
18.5k
    children_exprs.reserve(estimated_size);
926
927
18.5k
    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
38.4k
    for (const auto& child : children()) {
933
38.4k
        if (child->node_type() == TExprNodeType::CAST_EXPR) {
934
3.74k
            auto* cast_expr = assert_cast<VCastExpr*>(child.get());
935
3.74k
            DCHECK_EQ(cast_expr->get_num_children(), 1);
936
3.74k
            if (cast_expr->get_child(0)->is_slot_ref()) {
937
3.58k
                auto* column_slot_ref = assert_cast<VSlotRef*>(cast_expr->get_child(0).get());
938
3.58k
                auto column_id = column_slot_ref->column_id();
939
3.58k
                const auto* storage_name_type =
940
3.58k
                        context->get_index_context()->get_storage_name_and_type_by_column_id(
941
3.58k
                                column_id);
942
3.58k
                auto storage_type = remove_nullable(storage_name_type->second);
943
3.58k
                auto target_type = remove_nullable(cast_expr->get_target_type());
944
3.58k
                auto origin_primitive_type = storage_type->get_primitive_type();
945
3.58k
                auto target_primitive_type = target_type->get_primitive_type();
946
3.58k
                if (is_complex_type(storage_type->get_primitive_type())) {
947
93
                    if (storage_type->get_primitive_type() == TYPE_ARRAY &&
948
94
                        target_type->get_primitive_type() == TYPE_ARRAY) {
949
82
                        auto nested_storage_type =
950
82
                                (assert_cast<const DataTypeArray*>(storage_type.get()))
951
82
                                        ->get_nested_type();
952
82
                        origin_primitive_type = nested_storage_type->get_primitive_type();
953
82
                        auto nested_target_type =
954
82
                                (assert_cast<const DataTypeArray*>(target_type.get()))
955
82
                                        ->get_nested_type();
956
82
                        target_primitive_type = nested_target_type->get_primitive_type();
957
82
                    } else {
958
11
                        continue;
959
11
                    }
960
93
                }
961
3.57k
                if (origin_primitive_type != TYPE_VARIANT &&
962
3.57k
                    (storage_type->equals(*target_type) ||
963
2.91k
                     (is_string_type(target_primitive_type) &&
964
2.20k
                      is_string_type(origin_primitive_type)))) {
965
707
                    children_exprs.emplace_back(expr_without_cast(child));
966
707
                }
967
3.57k
            } else {
968
161
                return Status::OK(); // for example: cast("abc") as ipv4 case
969
161
            }
970
34.7k
        } else {
971
34.7k
            children_exprs.emplace_back(child);
972
34.7k
        }
973
38.4k
    }
974
975
18.4k
    if (children_exprs.empty()) {
976
443
        return Status::OK(); // Early exit if no children to process
977
443
    }
978
979
32.6k
    for (const auto& child : children_exprs) {
980
32.6k
        if (child->is_slot_ref()) {
981
14.0k
            auto* column_slot_ref = assert_cast<VSlotRef*>(child.get());
982
14.0k
            auto column_id = column_slot_ref->column_id();
983
14.0k
            auto* iter = context->get_index_context()->get_inverted_index_iterator_by_column_id(
984
14.0k
                    column_id);
985
            //column does not have inverted index
986
14.0k
            if (iter == nullptr) {
987
4.60k
                continue;
988
4.60k
            }
989
9.41k
            const auto* storage_name_type =
990
9.41k
                    context->get_index_context()->get_storage_name_and_type_by_column_id(column_id);
991
9.41k
            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
9.41k
            iterators.emplace_back(iter);
1000
9.41k
            data_type_with_names.emplace_back(*storage_name_type);
1001
9.41k
            column_ids.emplace_back(column_id);
1002
18.5k
        } else if (child->is_literal()) {
1003
15.7k
            auto* column_literal = assert_cast<VLiteral*>(child.get());
1004
15.7k
            arguments.emplace_back(column_literal->get_column_ptr(),
1005
15.7k
                                   column_literal->get_data_type(), column_literal->expr_name());
1006
15.7k
        } else if (child->can_push_down_to_index()) {
1007
0
            RETURN_IF_ERROR(child->evaluate_inverted_index(context, segment_num_rows));
1008
2.81k
        } else {
1009
2.81k
            return Status::OK(); // others cases
1010
2.81k
        }
1011
32.6k
    }
1012
1013
    // is null or is not null has no arguments
1014
15.1k
    if (iterators.empty() || (arguments.empty() && !(function->get_name() == "is_not_null_pred" ||
1015
6.29k
                                                     function->get_name() == "is_null_pred"))) {
1016
6.29k
        return Status::OK(); // Nothing to evaluate or no literals to compare against
1017
6.29k
    }
1018
1019
8.86k
    const InvertedIndexAnalyzerCtx* analyzer_ctx = nullptr;
1020
9.10k
    if (auto index_ctx = context->get_index_context(); index_ctx != nullptr) {
1021
9.10k
        analyzer_ctx = index_ctx->get_analyzer_ctx_for_expr(this);
1022
9.10k
    }
1023
1024
8.86k
    auto result_bitmap = segment_v2::InvertedIndexResultBitmap();
1025
    // Pass analyzer_key to function (used by match predicates for multi-analyzer index selection)
1026
8.86k
    auto res = function->evaluate_inverted_index(arguments, data_type_with_names, iterators,
1027
8.86k
                                                 segment_num_rows, analyzer_ctx, result_bitmap);
1028
8.86k
    if (!res.ok()) {
1029
352
        return res;
1030
352
    }
1031
8.51k
    if (!result_bitmap.is_empty()) {
1032
7.35k
        index_context->set_index_result_for_expr(this, result_bitmap);
1033
7.40k
        for (int column_id : column_ids) {
1034
7.40k
            index_context->set_true_for_index_status(this, column_id);
1035
7.40k
        }
1036
7.35k
    }
1037
8.51k
    return Status::OK();
1038
8.86k
}
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
6.16M
                             size_t count, ColumnPtr& result_column) const {
1060
6.16M
    RETURN_IF_ERROR(execute_column_impl(context, block, selector, count, result_column));
1061
6.16M
    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
6.16M
    DCHECK(selector == nullptr || selector->size() == count);
1066
    // Validate type match. ColumnNothing is exempt (used as a placeholder in tests/stubs).
1067
6.16M
    if (!check_and_get_column<ColumnNothing>(result_column.get())) {
1068
6.16M
        auto result_type = execute_type(block);
1069
6.16M
        if (result_type != nullptr) {
1070
6.16M
            Status st = result_type->check_column(*result_column);
1071
6.16M
            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
6.16M
            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
6.16M
        }
1086
6.16M
    }
1087
6.16M
    return Status::OK();
1088
6.16M
}
1089
1090
bool VExpr::fast_execute(VExprContext* context, const Selector* selector, size_t count,
1091
437k
                         ColumnPtr& result_column) const {
1092
437k
    if (context->get_index_context() &&
1093
437k
        context->get_index_context()->get_index_result_column().contains(this)) {
1094
        // prepare a column to save result
1095
752
        result_column = filter_column_with_selector(
1096
752
                context->get_index_context()->get_index_result_column()[this], selector, count);
1097
752
        if (_data_type->is_nullable()) {
1098
643
            result_column = make_nullable(result_column);
1099
643
        }
1100
752
        return true;
1101
752
    }
1102
436k
    return false;
1103
437k
}
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.31k
        bool enable_result_cache, AnnRangeSearchEvaluationResult& result) {
1116
6.31k
    result = {};
1117
6.31k
    return Status::OK();
1118
6.31k
}
1119
1120
void VExpr::prepare_ann_range_search(const doris::VectorSearchUserParams& params,
1121
                                     segment_v2::AnnRangeSearchRuntime& range_search_runtime,
1122
19.2k
                                     bool& suitable_for_ann_index) {
1123
19.2k
    if (!suitable_for_ann_index) {
1124
0
        return;
1125
0
    }
1126
19.2k
    for (auto& child : _children) {
1127
11.9k
        child->prepare_ann_range_search(params, range_search_runtime, suitable_for_ann_index);
1128
11.9k
        if (!suitable_for_ann_index) {
1129
481
            return;
1130
481
        }
1131
11.9k
    }
1132
19.2k
}
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
159k
                             bool* can_filter_all) const {
1137
159k
    ColumnPtr filter_column;
1138
159k
    RETURN_IF_ERROR(execute_column(context, block, nullptr, rows, filter_column));
1139
159k
    if (const auto* const_column = check_and_get_column<ColumnConst>(*filter_column)) {
1140
        // const(nullable) or const(bool)
1141
34.2k
        const bool result = accept_null
1142
34.2k
                                    ? (const_column->is_null_at(0) || const_column->get_bool(0))
1143
34.2k
                                    : (!const_column->is_null_at(0) && const_column->get_bool(0));
1144
34.2k
        if (!result) {
1145
            // filter all
1146
6.83k
            *can_filter_all = true;
1147
6.83k
            memset(result_filter_data, 0, rows);
1148
6.83k
            return Status::OK();
1149
6.83k
        }
1150
125k
    } else if (const auto* nullable_column = check_and_get_column<ColumnNullable>(*filter_column)) {
1151
        // nullable(bool)
1152
97.3k
        const ColumnPtr& nested_column = nullable_column->get_nested_column_ptr();
1153
97.3k
        const IColumn::Filter& filter = assert_cast<const ColumnUInt8&>(*nested_column).get_data();
1154
97.3k
        const auto* __restrict filter_data = filter.data();
1155
97.3k
        const auto* __restrict null_map_data = nullable_column->get_null_map_data().data();
1156
1157
97.3k
        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
97.3k
        } else {
1162
20.5M
            for (size_t i = 0; i < rows; ++i) {
1163
20.4M
                result_filter_data[i] &= (!null_map_data[i]) & filter_data[i];
1164
20.4M
            }
1165
97.3k
        }
1166
1167
97.3k
        if (!simd::contain_one(result_filter_data, rows)) {
1168
19.2k
            *can_filter_all = true;
1169
19.2k
            return Status::OK();
1170
19.2k
        }
1171
97.3k
    } else {
1172
        // bool
1173
28.3k
        const IColumn::Filter& filter = assert_cast<const ColumnUInt8&>(*filter_column).get_data();
1174
28.3k
        const auto* __restrict filter_data = filter.data();
1175
1176
58.8M
        for (size_t i = 0; i < rows; ++i) {
1177
58.8M
            result_filter_data[i] &= filter_data[i];
1178
58.8M
        }
1179
1180
28.3k
        if (!simd::contain_one(result_filter_data, rows)) {
1181
6.62k
            *can_filter_all = true;
1182
6.62k
            return Status::OK();
1183
6.62k
        }
1184
28.3k
    }
1185
1186
127k
    return Status::OK();
1187
159k
}
1188
1189
} // namespace doris