Coverage Report

Created: 2026-07-28 14:58

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