Coverage Report

Created: 2025-07-27 01:13

/root/doris/be/src/vec/exprs/vexpr.cpp
Line
Count
Source (jump to first uncovered line)
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 "vec/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 <memory>
29
#include <stack>
30
#include <utility>
31
32
#include "common/config.h"
33
#include "common/exception.h"
34
#include "common/status.h"
35
#include "pipeline/pipeline_task.h"
36
#include "runtime/define_primitive_type.h"
37
#include "vec/columns/column_vector.h"
38
#include "vec/columns/columns_number.h"
39
#include "vec/data_types/data_type_array.h"
40
#include "vec/data_types/data_type_factory.hpp"
41
#include "vec/data_types/data_type_nullable.h"
42
#include "vec/data_types/data_type_number.h"
43
#include "vec/exprs/varray_literal.h"
44
#include "vec/exprs/vcase_expr.h"
45
#include "vec/exprs/vcast_expr.h"
46
#include "vec/exprs/vcolumn_ref.h"
47
#include "vec/exprs/vcompound_pred.h"
48
#include "vec/exprs/vectorized_fn_call.h"
49
#include "vec/exprs/vexpr_context.h"
50
#include "vec/exprs/vin_predicate.h"
51
#include "vec/exprs/vinfo_func.h"
52
#include "vec/exprs/vlambda_function_call_expr.h"
53
#include "vec/exprs/vlambda_function_expr.h"
54
#include "vec/exprs/vliteral.h"
55
#include "vec/exprs/vmap_literal.h"
56
#include "vec/exprs/vmatch_predicate.h"
57
#include "vec/exprs/vslot_ref.h"
58
#include "vec/exprs/vstruct_literal.h"
59
#include "vec/exprs/vtuple_is_null_predicate.h"
60
#include "vec/utils/util.hpp"
61
62
namespace doris {
63
class RowDescriptor;
64
class RuntimeState;
65
66
// NOLINTBEGIN(readability-function-cognitive-complexity)
67
// NOLINTBEGIN(readability-function-size)
68
TExprNode create_texpr_node_from(const void* data, const PrimitiveType& type, int precision,
69
0
                                 int scale) {
70
0
    TExprNode node;
71
72
0
    switch (type) {
73
0
    case TYPE_BOOLEAN: {
74
0
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_BOOLEAN>(data, &node));
75
0
        break;
76
0
    }
77
0
    case TYPE_TINYINT: {
78
0
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_TINYINT>(data, &node));
79
0
        break;
80
0
    }
81
0
    case TYPE_SMALLINT: {
82
0
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_SMALLINT>(data, &node));
83
0
        break;
84
0
    }
85
0
    case TYPE_INT: {
86
0
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_INT>(data, &node));
87
0
        break;
88
0
    }
89
0
    case TYPE_BIGINT: {
90
0
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_BIGINT>(data, &node));
91
0
        break;
92
0
    }
93
0
    case TYPE_LARGEINT: {
94
0
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_LARGEINT>(data, &node));
95
0
        break;
96
0
    }
97
0
    case TYPE_FLOAT: {
98
0
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_FLOAT>(data, &node));
99
0
        break;
100
0
    }
101
0
    case TYPE_DOUBLE: {
102
0
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_DOUBLE>(data, &node));
103
0
        break;
104
0
    }
105
0
    case TYPE_DATEV2: {
106
0
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_DATEV2>(data, &node));
107
0
        break;
108
0
    }
109
0
    case TYPE_DATETIMEV2: {
110
0
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_DATETIMEV2>(data, &node, precision, scale));
111
0
        break;
112
0
    }
113
0
    case TYPE_DATE: {
114
0
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_DATE>(data, &node));
115
0
        break;
116
0
    }
117
0
    case TYPE_DATETIME: {
118
0
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_DATETIME>(data, &node));
119
0
        break;
120
0
    }
121
0
    case TYPE_DECIMALV2: {
122
0
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_DECIMALV2>(data, &node, precision, scale));
123
0
        break;
124
0
    }
125
0
    case TYPE_DECIMAL32: {
126
0
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_DECIMAL32>(data, &node, precision, scale));
127
0
        break;
128
0
    }
129
0
    case TYPE_DECIMAL64: {
130
0
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_DECIMAL64>(data, &node, precision, scale));
131
0
        break;
132
0
    }
133
0
    case TYPE_DECIMAL128I: {
134
0
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_DECIMAL128I>(data, &node, precision, scale));
135
0
        break;
136
0
    }
137
0
    case TYPE_DECIMAL256: {
138
0
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_DECIMAL256>(data, &node, precision, scale));
139
0
        break;
140
0
    }
141
0
    case TYPE_CHAR: {
142
0
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_CHAR>(data, &node));
143
0
        break;
144
0
    }
145
0
    case TYPE_VARCHAR: {
146
0
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_VARCHAR>(data, &node));
147
0
        break;
148
0
    }
149
0
    case TYPE_STRING: {
150
0
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_STRING>(data, &node));
151
0
        break;
152
0
    }
153
0
    case TYPE_IPV4: {
154
0
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_IPV4>(data, &node));
155
0
        break;
156
0
    }
157
0
    case TYPE_IPV6: {
158
0
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_IPV6>(data, &node));
159
0
        break;
160
0
    }
161
0
    default:
162
0
        throw Exception(ErrorCode::INTERNAL_ERROR, "runtime filter meet invalid type {}",
163
0
                        int(type));
164
0
    }
165
0
    return node;
166
0
}
167
// NOLINTEND(readability-function-size)
168
// NOLINTEND(readability-function-cognitive-complexity)
169
} // namespace doris
170
171
namespace doris::vectorized {
172
173
0
bool VExpr::is_acting_on_a_slot(const VExpr& expr) {
174
0
    const auto& children = expr.children();
175
176
0
    auto is_a_slot = std::any_of(children.begin(), children.end(),
177
0
                                 [](const auto& child) { return is_acting_on_a_slot(*child); });
178
179
0
    return is_a_slot ? true : (expr.node_type() == TExprNodeType::SLOT_REF);
180
0
}
181
182
VExpr::VExpr(const TExprNode& node)
183
        : _node_type(node.node_type),
184
          _opcode(node.__isset.opcode ? node.opcode : TExprOpcode::INVALID_OPCODE),
185
30
          _type(TypeDescriptor::from_thrift(node.type)) {
186
30
    if (node.__isset.fn) {
187
3
        _fn = node.fn;
188
3
    }
189
190
30
    bool is_nullable = true;
191
30
    if (node.__isset.is_nullable) {
192
4
        is_nullable = node.is_nullable;
193
4
    }
194
    // If we define null literal ,should make nullable data type to get correct field instead of undefined ptr
195
30
    if (node.node_type == TExprNodeType::NULL_LITERAL) {
196
0
        CHECK(is_nullable);
197
0
    }
198
30
    _data_type = DataTypeFactory::instance().create_data_type(_type, is_nullable);
199
30
}
200
201
0
VExpr::VExpr(const VExpr& vexpr) = default;
202
203
VExpr::VExpr(TypeDescriptor type, bool is_slotref, bool is_nullable)
204
0
        : _opcode(TExprOpcode::INVALID_OPCODE), _type(std::move(type)) {
205
0
    if (is_slotref) {
206
0
        _node_type = TExprNodeType::SLOT_REF;
207
0
    }
208
209
0
    _data_type = DataTypeFactory::instance().create_data_type(_type, is_nullable);
210
0
}
211
212
16
Status VExpr::prepare(RuntimeState* state, const RowDescriptor& row_desc, VExprContext* context) {
213
16
    ++context->_depth_num;
214
16
    if (context->_depth_num > config::max_depth_of_expr_tree) {
215
0
        return Status::Error<ErrorCode::EXCEEDED_LIMIT>(
216
0
                "The depth of the expression tree is too big, make it less than {}",
217
0
                config::max_depth_of_expr_tree);
218
0
    }
219
220
16
    for (auto& i : _children) {
221
5
        RETURN_IF_ERROR(i->prepare(state, row_desc, context));
222
5
    }
223
16
    --context->_depth_num;
224
16
    return Status::OK();
225
16
}
226
227
Status VExpr::open(RuntimeState* state, VExprContext* context,
228
2
                   FunctionContext::FunctionStateScope scope) {
229
2
    for (auto& i : _children) {
230
0
        RETURN_IF_ERROR(i->open(state, context, scope));
231
0
    }
232
2
    if (scope == FunctionContext::FRAGMENT_LOCAL) {
233
2
        RETURN_IF_ERROR(VExpr::get_const_col(context, nullptr));
234
2
    }
235
2
    return Status::OK();
236
2
}
237
238
4
void VExpr::close(VExprContext* context, FunctionContext::FunctionStateScope scope) {
239
4
    for (auto& i : _children) {
240
2
        i->close(context, scope);
241
2
    }
242
4
}
243
244
// NOLINTBEGIN(readability-function-size)
245
16
Status VExpr::create_expr(const TExprNode& expr_node, VExprSPtr& expr) {
246
16
    try {
247
16
        switch (expr_node.node_type) {
248
0
        case TExprNodeType::BOOL_LITERAL:
249
0
        case TExprNodeType::INT_LITERAL:
250
0
        case TExprNodeType::LARGE_INT_LITERAL:
251
1
        case TExprNodeType::IPV4_LITERAL:
252
1
        case TExprNodeType::IPV6_LITERAL:
253
1
        case TExprNodeType::FLOAT_LITERAL:
254
1
        case TExprNodeType::DECIMAL_LITERAL:
255
1
        case TExprNodeType::DATE_LITERAL:
256
2
        case TExprNodeType::STRING_LITERAL:
257
2
        case TExprNodeType::JSON_LITERAL:
258
2
        case TExprNodeType::NULL_LITERAL: {
259
2
            expr = VLiteral::create_shared(expr_node);
260
2
            break;
261
2
        }
262
0
        case TExprNodeType::ARRAY_LITERAL: {
263
0
            expr = VArrayLiteral::create_shared(expr_node);
264
0
            break;
265
2
        }
266
0
        case TExprNodeType::MAP_LITERAL: {
267
0
            expr = VMapLiteral::create_shared(expr_node);
268
0
            break;
269
2
        }
270
0
        case TExprNodeType::STRUCT_LITERAL: {
271
0
            expr = VStructLiteral::create_shared(expr_node);
272
0
            break;
273
2
        }
274
10
        case TExprNodeType::SLOT_REF: {
275
10
            expr = VSlotRef::create_shared(expr_node);
276
10
            break;
277
2
        }
278
0
        case TExprNodeType::COLUMN_REF: {
279
0
            expr = VColumnRef::create_shared(expr_node);
280
0
            break;
281
2
        }
282
0
        case TExprNodeType::COMPOUND_PRED: {
283
0
            expr = VCompoundPred::create_shared(expr_node);
284
0
            break;
285
2
        }
286
0
        case TExprNodeType::LAMBDA_FUNCTION_EXPR: {
287
0
            expr = VLambdaFunctionExpr::create_shared(expr_node);
288
0
            break;
289
2
        }
290
0
        case TExprNodeType::LAMBDA_FUNCTION_CALL_EXPR: {
291
0
            expr = VLambdaFunctionCallExpr::create_shared(expr_node);
292
0
            break;
293
2
        }
294
0
        case TExprNodeType::ARITHMETIC_EXPR:
295
0
        case TExprNodeType::BINARY_PRED:
296
0
        case TExprNodeType::NULL_AWARE_BINARY_PRED:
297
2
        case TExprNodeType::FUNCTION_CALL:
298
2
        case TExprNodeType::COMPUTE_FUNCTION_CALL: {
299
2
            expr = VectorizedFnCall::create_shared(expr_node);
300
2
            break;
301
2
        }
302
0
        case TExprNodeType::MATCH_PRED: {
303
0
            expr = VMatchPredicate::create_shared(expr_node);
304
0
            break;
305
2
        }
306
1
        case TExprNodeType::CAST_EXPR: {
307
1
            expr = VCastExpr::create_shared(expr_node);
308
1
            break;
309
2
        }
310
1
        case TExprNodeType::IN_PRED: {
311
1
            expr = VInPredicate::create_shared(expr_node);
312
1
            break;
313
2
        }
314
0
        case TExprNodeType::CASE_EXPR: {
315
0
            if (!expr_node.__isset.case_expr) {
316
0
                return Status::InternalError("Case expression not set in thrift node");
317
0
            }
318
0
            expr = VCaseExpr::create_shared(expr_node);
319
0
            break;
320
0
        }
321
0
        case TExprNodeType::INFO_FUNC: {
322
0
            expr = VInfoFunc::create_shared(expr_node);
323
0
            break;
324
0
        }
325
0
        case TExprNodeType::TUPLE_IS_NULL_PRED: {
326
0
            expr = VTupleIsNullPredicate::create_shared(expr_node);
327
0
            break;
328
0
        }
329
0
        default:
330
0
            return Status::InternalError("Unknown expr node type: {}", expr_node.node_type);
331
16
        }
332
16
    } catch (const Exception& e) {
333
0
        if (e.code() == ErrorCode::INTERNAL_ERROR) {
334
0
            return Status::InternalError("Create Expr failed because {}\nTExprNode={}", e.what(),
335
0
                                         apache::thrift::ThriftDebugString(expr_node));
336
0
        }
337
0
        return Status::Error<false>(e.code(), "Create Expr failed because {}", e.what());
338
0
        LOG(WARNING) << "create expr failed, TExprNode={}, reason={}"
339
0
                     << apache::thrift::ThriftDebugString(expr_node) << e.what();
340
0
    }
341
16
    if (!expr->data_type()) {
342
0
        return Status::InvalidArgument("Unknown expr type: {}", expr_node.node_type);
343
0
    }
344
16
    return Status::OK();
345
16
}
346
// NOLINTEND(readability-function-size)
347
348
Status VExpr::create_tree_from_thrift(const std::vector<TExprNode>& nodes, int* node_idx,
349
10
                                      VExprSPtr& root_expr, VExprContextSPtr& ctx) {
350
    // propagate error case
351
10
    if (*node_idx >= nodes.size()) {
352
0
        return Status::InternalError("Failed to reconstruct expression tree from thrift.");
353
0
    }
354
355
    // create root expr
356
10
    int root_children = nodes[*node_idx].num_children;
357
10
    VExprSPtr root;
358
10
    RETURN_IF_ERROR(create_expr(nodes[*node_idx], root));
359
10
    DCHECK(root != nullptr);
360
10
    root_expr = root;
361
10
    ctx = std::make_shared<VExprContext>(root);
362
    // short path for leaf node
363
10
    if (root_children <= 0) {
364
8
        return Status::OK();
365
8
    }
366
367
    // non-recursive traversal
368
2
    using VExprSPtrCountPair = std::pair<VExprSPtr, int>;
369
2
    std::stack<std::shared_ptr<VExprSPtrCountPair>> s;
370
2
    s.emplace(std::make_shared<VExprSPtrCountPair>(root, root_children));
371
4
    while (!s.empty()) {
372
        // copy the shared ptr resource to avoid dangling reference
373
2
        auto parent = s.top();
374
        // Decrement or pop
375
2
        if (parent->second > 1) {
376
0
            parent->second -= 1;
377
2
        } else {
378
2
            s.pop();
379
2
        }
380
381
2
        DCHECK(parent->first != nullptr);
382
2
        if (++*node_idx >= nodes.size()) {
383
0
            return Status::InternalError("Failed to reconstruct expression tree from thrift.");
384
0
        }
385
386
2
        VExprSPtr expr;
387
2
        RETURN_IF_ERROR(create_expr(nodes[*node_idx], expr));
388
2
        DCHECK(expr != nullptr);
389
2
        parent->first->add_child(expr);
390
        // push to stack if has children
391
2
        int num_children = nodes[*node_idx].num_children;
392
2
        if (num_children > 0) {
393
0
            s.emplace(std::make_shared<VExprSPtrCountPair>(expr, num_children));
394
0
        }
395
2
    }
396
2
    return Status::OK();
397
2
}
398
399
10
Status VExpr::create_expr_tree(const TExpr& texpr, VExprContextSPtr& ctx) {
400
10
    if (texpr.nodes.empty()) {
401
0
        ctx = nullptr;
402
0
        return Status::OK();
403
0
    }
404
10
    int node_idx = 0;
405
10
    VExprSPtr e;
406
10
    Status status = create_tree_from_thrift(texpr.nodes, &node_idx, e, ctx);
407
10
    if (status.ok() && node_idx + 1 != texpr.nodes.size()) {
408
0
        status = Status::InternalError(
409
0
                "Expression tree only partially reconstructed. Not all thrift nodes were "
410
0
                "used.");
411
0
    }
412
10
    if (!status.ok()) {
413
0
        LOG(ERROR) << "Could not construct expr tree.\n"
414
0
                   << status << "\n"
415
0
                   << apache::thrift::ThriftDebugString(texpr);
416
0
    }
417
10
    return status;
418
10
}
419
420
0
Status VExpr::create_expr_trees(const std::vector<TExpr>& texprs, VExprContextSPtrs& ctxs) {
421
0
    ctxs.clear();
422
0
    for (const auto& texpr : texprs) {
423
0
        VExprContextSPtr ctx;
424
0
        RETURN_IF_ERROR(create_expr_tree(texpr, ctx));
425
0
        ctxs.push_back(ctx);
426
0
    }
427
0
    return Status::OK();
428
0
}
429
430
Status VExpr::check_expr_output_type(const VExprContextSPtrs& ctxs,
431
0
                                     const RowDescriptor& output_row_desc) {
432
0
    if (ctxs.empty()) {
433
0
        return Status::OK();
434
0
    }
435
0
    auto name_and_types = VectorizedUtils::create_name_and_data_types(output_row_desc);
436
0
    if (ctxs.size() != name_and_types.size()) {
437
0
        return Status::InternalError(
438
0
                "output type size not match expr size {} , expected output size {} ", ctxs.size(),
439
0
                name_and_types.size());
440
0
    }
441
0
    auto check_type_can_be_converted = [](DataTypePtr& from, DataTypePtr& to) -> bool {
442
0
        if (to->equals(*from)) {
443
0
            return true;
444
0
        }
445
0
        if (to->is_nullable() && !from->is_nullable()) {
446
0
            return remove_nullable(to)->equals(*from);
447
0
        }
448
0
        return false;
449
0
    };
450
0
    for (int i = 0; i < ctxs.size(); i++) {
451
0
        auto real_expr_type = ctxs[i]->root()->data_type();
452
0
        auto&& [name, expected_type] = name_and_types[i];
453
0
        if (!check_type_can_be_converted(real_expr_type, expected_type)) {
454
0
            return Status::InternalError(
455
0
                    "output type not match expr type  , col name {} , expected type {} , real type "
456
0
                    "{}",
457
0
                    name, expected_type->get_name(), real_expr_type->get_name());
458
0
        }
459
0
    }
460
0
    return Status::OK();
461
0
}
462
463
Status VExpr::prepare(const VExprContextSPtrs& ctxs, RuntimeState* state,
464
4
                      const RowDescriptor& row_desc) {
465
4
    for (auto ctx : ctxs) {
466
0
        RETURN_IF_ERROR(ctx->prepare(state, row_desc));
467
0
    }
468
4
    return Status::OK();
469
4
}
470
471
4
Status VExpr::open(const VExprContextSPtrs& ctxs, RuntimeState* state) {
472
4
    for (const auto& ctx : ctxs) {
473
0
        RETURN_IF_ERROR(ctx->open(state));
474
0
    }
475
4
    return Status::OK();
476
4
}
477
478
Status VExpr::clone_if_not_exists(const VExprContextSPtrs& ctxs, RuntimeState* state,
479
0
                                  VExprContextSPtrs& new_ctxs) {
480
0
    if (!new_ctxs.empty()) {
481
        // 'ctxs' was already cloned into '*new_ctxs', nothing to do.
482
0
        DCHECK_EQ(new_ctxs.size(), ctxs.size());
483
0
        for (auto& new_ctx : new_ctxs) {
484
0
            DCHECK(new_ctx->_is_clone);
485
0
        }
486
0
        return Status::OK();
487
0
    }
488
0
    new_ctxs.resize(ctxs.size());
489
0
    for (int i = 0; i < ctxs.size(); ++i) {
490
0
        RETURN_IF_ERROR(ctxs[i]->clone(state, new_ctxs[i]));
491
0
    }
492
0
    return Status::OK();
493
0
}
494
495
0
std::string VExpr::debug_string() const {
496
    // TODO: implement partial debug string for member vars
497
0
    std::stringstream out;
498
0
    out << " type=" << _type.debug_string();
499
500
0
    if (!_children.empty()) {
501
0
        out << " children=" << debug_string(_children);
502
0
    }
503
504
0
    return out.str();
505
0
}
506
507
0
std::string VExpr::debug_string(const VExprSPtrs& exprs) {
508
0
    std::stringstream out;
509
0
    out << "[";
510
511
0
    for (int i = 0; i < exprs.size(); ++i) {
512
0
        out << (i == 0 ? "" : " ") << exprs[i]->debug_string();
513
0
    }
514
515
0
    out << "]";
516
0
    return out.str();
517
0
}
518
519
0
std::string VExpr::debug_string(const VExprContextSPtrs& ctxs) {
520
0
    VExprSPtrs exprs;
521
0
    for (const auto& ctx : ctxs) {
522
0
        exprs.push_back(ctx->root());
523
0
    }
524
0
    return debug_string(exprs);
525
0
}
526
527
2
bool VExpr::is_constant() const {
528
2
    return std::all_of(_children.begin(), _children.end(),
529
2
                       [](const VExprSPtr& expr) { return expr->is_constant(); });
530
2
}
531
532
Status VExpr::get_const_col(VExprContext* context,
533
6
                            std::shared_ptr<ColumnPtrWrapper>* column_wrapper) {
534
6
    if (!is_constant()) {
535
6
        return Status::OK();
536
6
    }
537
538
0
    if (_constant_col != nullptr) {
539
0
        DCHECK(column_wrapper != nullptr);
540
0
        *column_wrapper = _constant_col;
541
0
        return Status::OK();
542
0
    }
543
544
0
    int result = -1;
545
0
    Block block;
546
    // If block is empty, some functions will produce no result. So we insert a column with
547
    // single value here.
548
0
    block.insert({ColumnUInt8::create(1), std::make_shared<DataTypeUInt8>(), ""});
549
550
0
    _getting_const_col = true;
551
0
    RETURN_IF_ERROR(execute(context, &block, &result));
552
0
    _getting_const_col = false;
553
554
0
    DCHECK(result != -1);
555
0
    const auto& column = block.get_by_position(result).column;
556
0
    _constant_col = std::make_shared<ColumnPtrWrapper>(column);
557
0
    if (column_wrapper != nullptr) {
558
0
        *column_wrapper = _constant_col;
559
0
    }
560
561
0
    return Status::OK();
562
0
}
563
564
4
void VExpr::register_function_context(RuntimeState* state, VExprContext* context) {
565
4
    std::vector<TypeDescriptor> arg_types;
566
5
    for (auto& i : _children) {
567
5
        arg_types.push_back(i->type());
568
5
    }
569
570
4
    _fn_context_index = context->register_function_context(state, _type, arg_types);
571
4
}
572
573
Status VExpr::init_function_context(RuntimeState* state, VExprContext* context,
574
                                    FunctionContext::FunctionStateScope scope,
575
2
                                    const FunctionBasePtr& function) const {
576
2
    FunctionContext* fn_ctx = context->fn_context(_fn_context_index);
577
2
    if (scope == FunctionContext::FRAGMENT_LOCAL) {
578
2
        std::vector<std::shared_ptr<ColumnPtrWrapper>> constant_cols;
579
2
        for (auto c : _children) {
580
2
            std::shared_ptr<ColumnPtrWrapper> const_col;
581
2
            RETURN_IF_ERROR(c->get_const_col(context, &const_col));
582
2
            constant_cols.push_back(const_col);
583
2
        }
584
2
        fn_ctx->set_constant_cols(constant_cols);
585
2
    } else {
586
0
        if (function->is_udf_function()) {
587
0
            auto* timer = ADD_TIMER(state->get_task()->get_task_profile(),
588
0
                                    "UDF[" + function->get_name() + "]");
589
0
            fn_ctx->set_udf_execute_timer(timer);
590
0
        }
591
0
    }
592
593
2
    if (scope == FunctionContext::FRAGMENT_LOCAL) {
594
2
        RETURN_IF_ERROR(function->open(fn_ctx, FunctionContext::FRAGMENT_LOCAL));
595
2
    }
596
2
    RETURN_IF_ERROR(function->open(fn_ctx, FunctionContext::THREAD_LOCAL));
597
2
    return Status::OK();
598
2
}
599
600
void VExpr::close_function_context(VExprContext* context, FunctionContext::FunctionStateScope scope,
601
2
                                   const FunctionBasePtr& function) const {
602
2
    if (_fn_context_index != -1) {
603
2
        FunctionContext* fn_ctx = context->fn_context(_fn_context_index);
604
        // `close_function_context` is called in VExprContext's destructor so do not throw exceptions here.
605
2
        static_cast<void>(function->close(fn_ctx, FunctionContext::THREAD_LOCAL));
606
2
        if (scope == FunctionContext::FRAGMENT_LOCAL) {
607
2
            static_cast<void>(function->close(fn_ctx, FunctionContext::FRAGMENT_LOCAL));
608
2
        }
609
2
    }
610
2
}
611
612
0
Status VExpr::check_constant(const Block& block, ColumnNumbers arguments) const {
613
0
    if (is_constant() && !VectorizedUtils::all_arguments_are_constant(block, arguments)) {
614
0
        return Status::InternalError("const check failed, expr={}", debug_string());
615
0
    }
616
0
    return Status::OK();
617
0
}
618
619
Status VExpr::get_result_from_const(vectorized::Block* block, const std::string& expr_name,
620
0
                                    int* result_column_id) {
621
0
    *result_column_id = block->columns();
622
0
    auto column = ColumnConst::create(_constant_col->column_ptr, block->rows());
623
0
    block->insert({std::move(column), _data_type, expr_name});
624
0
    return Status::OK();
625
0
}
626
627
Status VExpr::_evaluate_inverted_index(VExprContext* context, const FunctionBasePtr& function,
628
1
                                       uint32_t segment_num_rows) {
629
    // Pre-allocate vectors based on an estimated or known size
630
1
    std::vector<segment_v2::InvertedIndexIterator*> iterators;
631
1
    std::vector<vectorized::IndexFieldNameAndTypePair> data_type_with_names;
632
1
    std::vector<int> column_ids;
633
1
    vectorized::ColumnsWithTypeAndName arguments;
634
1
    VExprSPtrs children_exprs;
635
636
    // Reserve space to avoid multiple reallocations
637
1
    const size_t estimated_size = children().size();
638
1
    iterators.reserve(estimated_size);
639
1
    data_type_with_names.reserve(estimated_size);
640
1
    column_ids.reserve(estimated_size);
641
1
    children_exprs.reserve(estimated_size);
642
643
1
    auto index_context = context->get_inverted_index_context();
644
645
    // if child is cast expr, we need to ensure target data type is the same with storage data type.
646
    // or they are all string type
647
    // and if data type is array, we need to get the nested data type to ensure that.
648
2
    for (const auto& child : children()) {
649
2
        if (child->node_type() == TExprNodeType::CAST_EXPR) {
650
1
            auto* cast_expr = assert_cast<VCastExpr*>(child.get());
651
1
            DCHECK_EQ(cast_expr->children().size(), 1);
652
1
            if (cast_expr->get_child(0)->is_slot_ref()) {
653
0
                auto* column_slot_ref = assert_cast<VSlotRef*>(cast_expr->get_child(0).get());
654
0
                auto column_id = column_slot_ref->column_id();
655
0
                const auto* storage_name_type =
656
0
                        context->get_inverted_index_context()
657
0
                                ->get_storage_name_and_type_by_column_id(column_id);
658
0
                auto storage_type = remove_nullable(storage_name_type->second);
659
0
                auto target_type = remove_nullable(cast_expr->get_target_type());
660
0
                auto origin_primitive_type = storage_type->get_type_as_type_descriptor().type;
661
0
                auto target_primitive_type = target_type->get_type_as_type_descriptor().type;
662
0
                if (is_complex_type(storage_type)) {
663
0
                    if (is_array(storage_type) && is_array(target_type)) {
664
0
                        auto nested_storage_type =
665
0
                                (assert_cast<const DataTypeArray*>(storage_type.get()))
666
0
                                        ->get_nested_type();
667
0
                        origin_primitive_type =
668
0
                                nested_storage_type->get_type_as_type_descriptor().type;
669
0
                        auto nested_target_type =
670
0
                                (assert_cast<const DataTypeArray*>(target_type.get()))
671
0
                                        ->get_nested_type();
672
0
                        target_primitive_type =
673
0
                                nested_target_type->get_type_as_type_descriptor().type;
674
0
                    } else {
675
0
                        continue;
676
0
                    }
677
0
                }
678
0
                if (origin_primitive_type != TYPE_VARIANT &&
679
0
                    (storage_type->equals(*target_type) ||
680
0
                     (is_string_type(target_primitive_type) &&
681
0
                      is_string_type(origin_primitive_type)))) {
682
0
                    children_exprs.emplace_back(expr_without_cast(child));
683
0
                }
684
1
            } else {
685
1
                return Status::OK(); // for example: cast("abc") as ipv4 case
686
1
            }
687
1
        } else {
688
1
            children_exprs.emplace_back(child);
689
1
        }
690
2
    }
691
692
0
    if (children_exprs.empty()) {
693
0
        return Status::OK(); // Early exit if no children to process
694
0
    }
695
696
0
    for (const auto& child : children_exprs) {
697
0
        if (child->is_slot_ref()) {
698
0
            auto* column_slot_ref = assert_cast<VSlotRef*>(child.get());
699
0
            auto column_id = column_slot_ref->column_id();
700
0
            auto* iter =
701
0
                    context->get_inverted_index_context()->get_inverted_index_iterator_by_column_id(
702
0
                            column_id);
703
            //column does not have inverted index
704
0
            if (iter == nullptr) {
705
0
                continue;
706
0
            }
707
0
            const auto* storage_name_type =
708
0
                    context->get_inverted_index_context()->get_storage_name_and_type_by_column_id(
709
0
                            column_id);
710
0
            if (storage_name_type == nullptr) {
711
0
                auto err_msg = fmt::format(
712
0
                        "storage_name_type cannot be found for column {} while in {} "
713
0
                        "evaluate_inverted_index",
714
0
                        column_id, expr_name());
715
0
                LOG(ERROR) << err_msg;
716
0
                return Status::InternalError(err_msg);
717
0
            }
718
0
            iterators.emplace_back(iter);
719
0
            data_type_with_names.emplace_back(*storage_name_type);
720
0
            column_ids.emplace_back(column_id);
721
0
        } else if (child->is_literal()) {
722
0
            auto* column_literal = assert_cast<VLiteral*>(child.get());
723
0
            arguments.emplace_back(column_literal->get_column_ptr(),
724
0
                                   column_literal->get_data_type(), column_literal->expr_name());
725
0
        } else {
726
0
            return Status::OK(); // others cases
727
0
        }
728
0
    }
729
730
0
    if (iterators.empty() || arguments.empty()) {
731
0
        return Status::OK(); // Nothing to evaluate or no literals to compare against
732
0
    }
733
734
0
    auto result_bitmap = segment_v2::InvertedIndexResultBitmap();
735
0
    auto res = function->evaluate_inverted_index(arguments, data_type_with_names, iterators,
736
0
                                                 segment_num_rows, result_bitmap);
737
0
    if (!res.ok()) {
738
0
        return res;
739
0
    }
740
0
    if (!result_bitmap.is_empty()) {
741
0
        index_context->set_inverted_index_result_for_expr(this, result_bitmap);
742
0
        for (int column_id : column_ids) {
743
0
            index_context->set_true_for_inverted_index_status(this, column_id);
744
0
        }
745
0
    }
746
0
    return Status::OK();
747
0
}
748
749
bool VExpr::fast_execute(doris::vectorized::VExprContext* context, doris::vectorized::Block* block,
750
0
                         int* result_column_id) {
751
0
    if (context->get_inverted_index_context() &&
752
0
        context->get_inverted_index_context()->get_inverted_index_result_column().contains(this)) {
753
0
        size_t num_columns_without_result = block->columns();
754
        // prepare a column to save result
755
0
        auto result_column =
756
0
                context->get_inverted_index_context()->get_inverted_index_result_column()[this];
757
0
        if (_data_type->is_nullable()) {
758
0
            block->insert(
759
0
                    {ColumnNullable::create(result_column, ColumnUInt8::create(block->rows(), 0)),
760
0
                     _data_type, expr_name()});
761
0
        } else {
762
0
            block->insert({result_column, _data_type, expr_name()});
763
0
        }
764
0
        *result_column_id = num_columns_without_result;
765
0
        return true;
766
0
    }
767
0
    return false;
768
0
}
769
770
0
bool VExpr::equals(const VExpr& other) {
771
0
    return false;
772
0
}
773
774
} // namespace doris::vectorized