Coverage Report

Created: 2026-10-31 08:21

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