Coverage Report

Created: 2026-07-28 18:03

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