Coverage Report

Created: 2025-09-12 17:50

/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: {
  Branch (73:5): [True: 0, False: 0]
74
0
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_BOOLEAN>(data, &node));
Line
Count
Source
652
0
    do {                                \
653
0
        Status _status_ = (stmt);       \
654
0
        if (UNLIKELY(!_status_.ok())) { \
Line
Count
Source
36
0
#define UNLIKELY(expr) __builtin_expect(!!(expr), 0)
  Branch (36:24): [True: 0, False: 0]
655
0
            throw Exception(_status_);  \
656
0
        }                               \
657
0
    } while (false)
  Branch (657:14): [Folded - Ignored]
75
0
        break;
76
0
    }
77
0
    case TYPE_TINYINT: {
  Branch (77:5): [True: 0, False: 0]
78
0
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_TINYINT>(data, &node));
Line
Count
Source
652
0
    do {                                \
653
0
        Status _status_ = (stmt);       \
654
0
        if (UNLIKELY(!_status_.ok())) { \
Line
Count
Source
36
0
#define UNLIKELY(expr) __builtin_expect(!!(expr), 0)
  Branch (36:24): [True: 0, False: 0]
655
0
            throw Exception(_status_);  \
656
0
        }                               \
657
0
    } while (false)
  Branch (657:14): [Folded - Ignored]
79
0
        break;
80
0
    }
81
0
    case TYPE_SMALLINT: {
  Branch (81:5): [True: 0, False: 0]
82
0
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_SMALLINT>(data, &node));
Line
Count
Source
652
0
    do {                                \
653
0
        Status _status_ = (stmt);       \
654
0
        if (UNLIKELY(!_status_.ok())) { \
Line
Count
Source
36
0
#define UNLIKELY(expr) __builtin_expect(!!(expr), 0)
  Branch (36:24): [True: 0, False: 0]
655
0
            throw Exception(_status_);  \
656
0
        }                               \
657
0
    } while (false)
  Branch (657:14): [Folded - Ignored]
83
0
        break;
84
0
    }
85
0
    case TYPE_INT: {
  Branch (85:5): [True: 0, False: 0]
86
0
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_INT>(data, &node));
Line
Count
Source
652
0
    do {                                \
653
0
        Status _status_ = (stmt);       \
654
0
        if (UNLIKELY(!_status_.ok())) { \
Line
Count
Source
36
0
#define UNLIKELY(expr) __builtin_expect(!!(expr), 0)
  Branch (36:24): [True: 0, False: 0]
655
0
            throw Exception(_status_);  \
656
0
        }                               \
657
0
    } while (false)
  Branch (657:14): [Folded - Ignored]
87
0
        break;
88
0
    }
89
0
    case TYPE_BIGINT: {
  Branch (89:5): [True: 0, False: 0]
90
0
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_BIGINT>(data, &node));
Line
Count
Source
652
0
    do {                                \
653
0
        Status _status_ = (stmt);       \
654
0
        if (UNLIKELY(!_status_.ok())) { \
Line
Count
Source
36
0
#define UNLIKELY(expr) __builtin_expect(!!(expr), 0)
  Branch (36:24): [True: 0, False: 0]
655
0
            throw Exception(_status_);  \
656
0
        }                               \
657
0
    } while (false)
  Branch (657:14): [Folded - Ignored]
91
0
        break;
92
0
    }
93
0
    case TYPE_LARGEINT: {
  Branch (93:5): [True: 0, False: 0]
94
0
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_LARGEINT>(data, &node));
Line
Count
Source
652
0
    do {                                \
653
0
        Status _status_ = (stmt);       \
654
0
        if (UNLIKELY(!_status_.ok())) { \
Line
Count
Source
36
0
#define UNLIKELY(expr) __builtin_expect(!!(expr), 0)
  Branch (36:24): [True: 0, False: 0]
655
0
            throw Exception(_status_);  \
656
0
        }                               \
657
0
    } while (false)
  Branch (657:14): [Folded - Ignored]
95
0
        break;
96
0
    }
97
0
    case TYPE_FLOAT: {
  Branch (97:5): [True: 0, False: 0]
98
0
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_FLOAT>(data, &node));
Line
Count
Source
652
0
    do {                                \
653
0
        Status _status_ = (stmt);       \
654
0
        if (UNLIKELY(!_status_.ok())) { \
Line
Count
Source
36
0
#define UNLIKELY(expr) __builtin_expect(!!(expr), 0)
  Branch (36:24): [True: 0, False: 0]
655
0
            throw Exception(_status_);  \
656
0
        }                               \
657
0
    } while (false)
  Branch (657:14): [Folded - Ignored]
99
0
        break;
100
0
    }
101
0
    case TYPE_DOUBLE: {
  Branch (101:5): [True: 0, False: 0]
102
0
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_DOUBLE>(data, &node));
Line
Count
Source
652
0
    do {                                \
653
0
        Status _status_ = (stmt);       \
654
0
        if (UNLIKELY(!_status_.ok())) { \
Line
Count
Source
36
0
#define UNLIKELY(expr) __builtin_expect(!!(expr), 0)
  Branch (36:24): [True: 0, False: 0]
655
0
            throw Exception(_status_);  \
656
0
        }                               \
657
0
    } while (false)
  Branch (657:14): [Folded - Ignored]
103
0
        break;
104
0
    }
105
0
    case TYPE_DATEV2: {
  Branch (105:5): [True: 0, False: 0]
106
0
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_DATEV2>(data, &node));
Line
Count
Source
652
0
    do {                                \
653
0
        Status _status_ = (stmt);       \
654
0
        if (UNLIKELY(!_status_.ok())) { \
Line
Count
Source
36
0
#define UNLIKELY(expr) __builtin_expect(!!(expr), 0)
  Branch (36:24): [True: 0, False: 0]
655
0
            throw Exception(_status_);  \
656
0
        }                               \
657
0
    } while (false)
  Branch (657:14): [Folded - Ignored]
107
0
        break;
108
0
    }
109
0
    case TYPE_DATETIMEV2: {
  Branch (109:5): [True: 0, False: 0]
110
0
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_DATETIMEV2>(data, &node, precision, scale));
Line
Count
Source
652
0
    do {                                \
653
0
        Status _status_ = (stmt);       \
654
0
        if (UNLIKELY(!_status_.ok())) { \
Line
Count
Source
36
0
#define UNLIKELY(expr) __builtin_expect(!!(expr), 0)
  Branch (36:24): [True: 0, False: 0]
655
0
            throw Exception(_status_);  \
656
0
        }                               \
657
0
    } while (false)
  Branch (657:14): [Folded - Ignored]
111
0
        break;
112
0
    }
113
0
    case TYPE_DATE: {
  Branch (113:5): [True: 0, False: 0]
114
0
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_DATE>(data, &node));
Line
Count
Source
652
0
    do {                                \
653
0
        Status _status_ = (stmt);       \
654
0
        if (UNLIKELY(!_status_.ok())) { \
Line
Count
Source
36
0
#define UNLIKELY(expr) __builtin_expect(!!(expr), 0)
  Branch (36:24): [True: 0, False: 0]
655
0
            throw Exception(_status_);  \
656
0
        }                               \
657
0
    } while (false)
  Branch (657:14): [Folded - Ignored]
115
0
        break;
116
0
    }
117
0
    case TYPE_DATETIME: {
  Branch (117:5): [True: 0, False: 0]
118
0
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_DATETIME>(data, &node));
Line
Count
Source
652
0
    do {                                \
653
0
        Status _status_ = (stmt);       \
654
0
        if (UNLIKELY(!_status_.ok())) { \
Line
Count
Source
36
0
#define UNLIKELY(expr) __builtin_expect(!!(expr), 0)
  Branch (36:24): [True: 0, False: 0]
655
0
            throw Exception(_status_);  \
656
0
        }                               \
657
0
    } while (false)
  Branch (657:14): [Folded - Ignored]
119
0
        break;
120
0
    }
121
0
    case TYPE_DECIMALV2: {
  Branch (121:5): [True: 0, False: 0]
122
0
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_DECIMALV2>(data, &node, precision, scale));
Line
Count
Source
652
0
    do {                                \
653
0
        Status _status_ = (stmt);       \
654
0
        if (UNLIKELY(!_status_.ok())) { \
Line
Count
Source
36
0
#define UNLIKELY(expr) __builtin_expect(!!(expr), 0)
  Branch (36:24): [True: 0, False: 0]
655
0
            throw Exception(_status_);  \
656
0
        }                               \
657
0
    } while (false)
  Branch (657:14): [Folded - Ignored]
123
0
        break;
124
0
    }
125
0
    case TYPE_DECIMAL32: {
  Branch (125:5): [True: 0, False: 0]
126
0
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_DECIMAL32>(data, &node, precision, scale));
Line
Count
Source
652
0
    do {                                \
653
0
        Status _status_ = (stmt);       \
654
0
        if (UNLIKELY(!_status_.ok())) { \
Line
Count
Source
36
0
#define UNLIKELY(expr) __builtin_expect(!!(expr), 0)
  Branch (36:24): [True: 0, False: 0]
655
0
            throw Exception(_status_);  \
656
0
        }                               \
657
0
    } while (false)
  Branch (657:14): [Folded - Ignored]
127
0
        break;
128
0
    }
129
0
    case TYPE_DECIMAL64: {
  Branch (129:5): [True: 0, False: 0]
130
0
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_DECIMAL64>(data, &node, precision, scale));
Line
Count
Source
652
0
    do {                                \
653
0
        Status _status_ = (stmt);       \
654
0
        if (UNLIKELY(!_status_.ok())) { \
Line
Count
Source
36
0
#define UNLIKELY(expr) __builtin_expect(!!(expr), 0)
  Branch (36:24): [True: 0, False: 0]
655
0
            throw Exception(_status_);  \
656
0
        }                               \
657
0
    } while (false)
  Branch (657:14): [Folded - Ignored]
131
0
        break;
132
0
    }
133
0
    case TYPE_DECIMAL128I: {
  Branch (133:5): [True: 0, False: 0]
134
0
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_DECIMAL128I>(data, &node, precision, scale));
Line
Count
Source
652
0
    do {                                \
653
0
        Status _status_ = (stmt);       \
654
0
        if (UNLIKELY(!_status_.ok())) { \
Line
Count
Source
36
0
#define UNLIKELY(expr) __builtin_expect(!!(expr), 0)
  Branch (36:24): [True: 0, False: 0]
655
0
            throw Exception(_status_);  \
656
0
        }                               \
657
0
    } while (false)
  Branch (657:14): [Folded - Ignored]
135
0
        break;
136
0
    }
137
0
    case TYPE_DECIMAL256: {
  Branch (137:5): [True: 0, False: 0]
138
0
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_DECIMAL256>(data, &node, precision, scale));
Line
Count
Source
652
0
    do {                                \
653
0
        Status _status_ = (stmt);       \
654
0
        if (UNLIKELY(!_status_.ok())) { \
Line
Count
Source
36
0
#define UNLIKELY(expr) __builtin_expect(!!(expr), 0)
  Branch (36:24): [True: 0, False: 0]
655
0
            throw Exception(_status_);  \
656
0
        }                               \
657
0
    } while (false)
  Branch (657:14): [Folded - Ignored]
139
0
        break;
140
0
    }
141
0
    case TYPE_CHAR: {
  Branch (141:5): [True: 0, False: 0]
142
0
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_CHAR>(data, &node));
Line
Count
Source
652
0
    do {                                \
653
0
        Status _status_ = (stmt);       \
654
0
        if (UNLIKELY(!_status_.ok())) { \
Line
Count
Source
36
0
#define UNLIKELY(expr) __builtin_expect(!!(expr), 0)
  Branch (36:24): [True: 0, False: 0]
655
0
            throw Exception(_status_);  \
656
0
        }                               \
657
0
    } while (false)
  Branch (657:14): [Folded - Ignored]
143
0
        break;
144
0
    }
145
0
    case TYPE_VARCHAR: {
  Branch (145:5): [True: 0, False: 0]
146
0
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_VARCHAR>(data, &node));
Line
Count
Source
652
0
    do {                                \
653
0
        Status _status_ = (stmt);       \
654
0
        if (UNLIKELY(!_status_.ok())) { \
Line
Count
Source
36
0
#define UNLIKELY(expr) __builtin_expect(!!(expr), 0)
  Branch (36:24): [True: 0, False: 0]
655
0
            throw Exception(_status_);  \
656
0
        }                               \
657
0
    } while (false)
  Branch (657:14): [Folded - Ignored]
147
0
        break;
148
0
    }
149
0
    case TYPE_STRING: {
  Branch (149:5): [True: 0, False: 0]
150
0
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_STRING>(data, &node));
Line
Count
Source
652
0
    do {                                \
653
0
        Status _status_ = (stmt);       \
654
0
        if (UNLIKELY(!_status_.ok())) { \
Line
Count
Source
36
0
#define UNLIKELY(expr) __builtin_expect(!!(expr), 0)
  Branch (36:24): [True: 0, False: 0]
655
0
            throw Exception(_status_);  \
656
0
        }                               \
657
0
    } while (false)
  Branch (657:14): [Folded - Ignored]
151
0
        break;
152
0
    }
153
0
    case TYPE_IPV4: {
  Branch (153:5): [True: 0, False: 0]
154
0
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_IPV4>(data, &node));
Line
Count
Source
652
0
    do {                                \
653
0
        Status _status_ = (stmt);       \
654
0
        if (UNLIKELY(!_status_.ok())) { \
Line
Count
Source
36
0
#define UNLIKELY(expr) __builtin_expect(!!(expr), 0)
  Branch (36:24): [True: 0, False: 0]
655
0
            throw Exception(_status_);  \
656
0
        }                               \
657
0
    } while (false)
  Branch (657:14): [Folded - Ignored]
155
0
        break;
156
0
    }
157
0
    case TYPE_IPV6: {
  Branch (157:5): [True: 0, False: 0]
158
0
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_IPV6>(data, &node));
Line
Count
Source
652
0
    do {                                \
653
0
        Status _status_ = (stmt);       \
654
0
        if (UNLIKELY(!_status_.ok())) { \
Line
Count
Source
36
0
#define UNLIKELY(expr) __builtin_expect(!!(expr), 0)
  Branch (36:24): [True: 0, False: 0]
655
0
            throw Exception(_status_);  \
656
0
        }                               \
657
0
    } while (false)
  Branch (657:14): [Folded - Ignored]
159
0
        break;
160
0
    }
161
0
    default:
  Branch (161:5): [True: 0, False: 0]
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);
  Branch (179:12): [True: 0, False: 0]
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
146
          _type(TypeDescriptor::from_thrift(node.type)) {
186
146
    if (node.__isset.fn) {
  Branch (186:9): [True: 33, False: 113]
187
33
        _fn = node.fn;
188
33
    }
189
190
146
    bool is_nullable = true;
191
146
    if (node.__isset.is_nullable) {
  Branch (191:9): [True: 120, False: 26]
192
120
        is_nullable = node.is_nullable;
193
120
    }
194
    // If we define null literal ,should make nullable data type to get correct field instead of undefined ptr
195
146
    if (node.node_type == TExprNodeType::NULL_LITERAL) {
  Branch (195:9): [True: 1, False: 145]
196
1
        CHECK(is_nullable);
197
1
    }
198
146
    _data_type = DataTypeFactory::instance().create_data_type(_type, is_nullable);
199
146
}
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) {
  Branch (205:9): [True: 0, False: 0]
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
132
Status VExpr::prepare(RuntimeState* state, const RowDescriptor& row_desc, VExprContext* context) {
213
132
    ++context->_depth_num;
214
132
    if (context->_depth_num > config::max_depth_of_expr_tree) {
  Branch (214:9): [True: 0, False: 132]
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
132
    for (auto& i : _children) {
  Branch (220:18): [True: 108, False: 132]
221
108
        RETURN_IF_ERROR(i->prepare(state, row_desc, context));
Line
Count
Source
637
108
    do {                                \
638
108
        Status _status_ = (stmt);       \
639
108
        if (UNLIKELY(!_status_.ok())) { \
Line
Count
Source
36
108
#define UNLIKELY(expr) __builtin_expect(!!(expr), 0)
  Branch (36:24): [True: 0, False: 108]
640
0
            return _status_;            \
641
0
        }                               \
642
108
    } while (false)
  Branch (642:14): [Folded - Ignored]
222
108
    }
223
132
    --context->_depth_num;
224
132
    return Status::OK();
225
132
}
226
227
Status VExpr::open(RuntimeState* state, VExprContext* context,
228
2
                   FunctionContext::FunctionStateScope scope) {
229
2
    for (auto& i : _children) {
  Branch (229:18): [True: 0, False: 2]
230
0
        RETURN_IF_ERROR(i->open(state, context, scope));
Line
Count
Source
637
0
    do {                                \
638
0
        Status _status_ = (stmt);       \
639
0
        if (UNLIKELY(!_status_.ok())) { \
Line
Count
Source
36
0
#define UNLIKELY(expr) __builtin_expect(!!(expr), 0)
  Branch (36:24): [True: 0, False: 0]
640
0
            return _status_;            \
641
0
        }                               \
642
0
    } while (false)
  Branch (642:14): [Folded - Ignored]
231
0
    }
232
2
    if (scope == FunctionContext::FRAGMENT_LOCAL) {
  Branch (232:9): [True: 2, False: 0]
233
2
        RETURN_IF_ERROR(VExpr::get_const_col(context, nullptr));
Line
Count
Source
637
2
    do {                                \
638
2
        Status _status_ = (stmt);       \
639
2
        if (UNLIKELY(!_status_.ok())) { \
Line
Count
Source
36
2
#define UNLIKELY(expr) __builtin_expect(!!(expr), 0)
  Branch (36:24): [True: 0, False: 2]
640
0
            return _status_;            \
641
0
        }                               \
642
2
    } while (false)
  Branch (642:14): [Folded - Ignored]
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) {
  Branch (239:18): [True: 2, False: 4]
240
2
        i->close(context, scope);
241
2
    }
242
4
}
243
244
// NOLINTBEGIN(readability-function-size)
245
132
Status VExpr::create_expr(const TExprNode& expr_node, VExprSPtr& expr) {
246
132
    try {
247
132
        switch (expr_node.node_type) {
248
0
        case TExprNodeType::BOOL_LITERAL:
  Branch (248:9): [True: 0, False: 132]
249
25
        case TExprNodeType::INT_LITERAL:
  Branch (249:9): [True: 25, False: 107]
250
25
        case TExprNodeType::LARGE_INT_LITERAL:
  Branch (250:9): [True: 0, False: 132]
251
26
        case TExprNodeType::IPV4_LITERAL:
  Branch (251:9): [True: 1, False: 131]
252
26
        case TExprNodeType::IPV6_LITERAL:
  Branch (252:9): [True: 0, False: 132]
253
27
        case TExprNodeType::FLOAT_LITERAL:
  Branch (253:9): [True: 1, False: 131]
254
28
        case TExprNodeType::DECIMAL_LITERAL:
  Branch (254:9): [True: 1, False: 131]
255
31
        case TExprNodeType::DATE_LITERAL:
  Branch (255:9): [True: 3, False: 129]
256
37
        case TExprNodeType::STRING_LITERAL:
  Branch (256:9): [True: 6, False: 126]
257
37
        case TExprNodeType::JSON_LITERAL:
  Branch (257:9): [True: 0, False: 132]
258
38
        case TExprNodeType::NULL_LITERAL: {
  Branch (258:9): [True: 1, False: 131]
259
38
            expr = VLiteral::create_shared(expr_node);
260
38
            break;
261
37
        }
262
0
        case TExprNodeType::ARRAY_LITERAL: {
  Branch (262:9): [True: 0, False: 132]
263
0
            expr = VArrayLiteral::create_shared(expr_node);
264
0
            break;
265
37
        }
266
0
        case TExprNodeType::MAP_LITERAL: {
  Branch (266:9): [True: 0, False: 132]
267
0
            expr = VMapLiteral::create_shared(expr_node);
268
0
            break;
269
37
        }
270
0
        case TExprNodeType::STRUCT_LITERAL: {
  Branch (270:9): [True: 0, False: 132]
271
0
            expr = VStructLiteral::create_shared(expr_node);
272
0
            break;
273
37
        }
274
39
        case TExprNodeType::SLOT_REF: {
  Branch (274:9): [True: 39, False: 93]
275
39
            expr = VSlotRef::create_shared(expr_node);
276
39
            break;
277
37
        }
278
0
        case TExprNodeType::COLUMN_REF: {
  Branch (278:9): [True: 0, False: 132]
279
0
            expr = VColumnRef::create_shared(expr_node);
280
0
            break;
281
37
        }
282
17
        case TExprNodeType::COMPOUND_PRED: {
  Branch (282:9): [True: 17, False: 115]
283
17
            expr = VCompoundPred::create_shared(expr_node);
284
17
            break;
285
37
        }
286
0
        case TExprNodeType::LAMBDA_FUNCTION_EXPR: {
  Branch (286:9): [True: 0, False: 132]
287
0
            expr = VLambdaFunctionExpr::create_shared(expr_node);
288
0
            break;
289
37
        }
290
0
        case TExprNodeType::LAMBDA_FUNCTION_CALL_EXPR: {
  Branch (290:9): [True: 0, False: 132]
291
0
            expr = VLambdaFunctionCallExpr::create_shared(expr_node);
292
0
            break;
293
37
        }
294
1
        case TExprNodeType::ARITHMETIC_EXPR:
  Branch (294:9): [True: 1, False: 131]
295
23
        case TExprNodeType::BINARY_PRED:
  Branch (295:9): [True: 22, False: 110]
296
23
        case TExprNodeType::NULL_AWARE_BINARY_PRED:
  Branch (296:9): [True: 0, False: 132]
297
31
        case TExprNodeType::FUNCTION_CALL:
  Branch (297:9): [True: 8, False: 124]
298
31
        case TExprNodeType::COMPUTE_FUNCTION_CALL: {
  Branch (298:9): [True: 0, False: 132]
299
31
            expr = VectorizedFnCall::create_shared(expr_node);
300
31
            break;
301
31
        }
302
0
        case TExprNodeType::MATCH_PRED: {
  Branch (302:9): [True: 0, False: 132]
303
0
            expr = VMatchPredicate::create_shared(expr_node);
304
0
            break;
305
31
        }
306
2
        case TExprNodeType::CAST_EXPR: {
  Branch (306:9): [True: 2, False: 130]
307
2
            expr = VCastExpr::create_shared(expr_node);
308
2
            break;
309
31
        }
310
5
        case TExprNodeType::IN_PRED: {
  Branch (310:9): [True: 5, False: 127]
311
5
            expr = VInPredicate::create_shared(expr_node);
312
5
            break;
313
31
        }
314
0
        case TExprNodeType::CASE_EXPR: {
  Branch (314:9): [True: 0, False: 132]
315
0
            if (!expr_node.__isset.case_expr) {
  Branch (315:17): [True: 0, False: 0]
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: {
  Branch (321:9): [True: 0, False: 132]
322
0
            expr = VInfoFunc::create_shared(expr_node);
323
0
            break;
324
0
        }
325
0
        case TExprNodeType::TUPLE_IS_NULL_PRED: {
  Branch (325:9): [True: 0, False: 132]
326
0
            expr = VTupleIsNullPredicate::create_shared(expr_node);
327
0
            break;
328
0
        }
329
0
        default:
  Branch (329:9): [True: 0, False: 132]
330
0
            return Status::InternalError("Unknown expr node type: {}", expr_node.node_type);
331
132
        }
332
132
    } catch (const Exception& e) {
333
0
        if (e.code() == ErrorCode::INTERNAL_ERROR) {
  Branch (333:13): [True: 0, False: 0]
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
132
    if (!expr->data_type()) {
  Branch (341:9): [True: 0, False: 132]
342
0
        return Status::InvalidArgument("Unknown expr type: {}", expr_node.node_type);
343
0
    }
344
132
    return Status::OK();
345
132
}
346
// NOLINTEND(readability-function-size)
347
348
Status VExpr::create_tree_from_thrift(const std::vector<TExprNode>& nodes, int* node_idx,
349
23
                                      VExprSPtr& root_expr, VExprContextSPtr& ctx) {
350
    // propagate error case
351
23
    if (*node_idx >= nodes.size()) {
  Branch (351:9): [True: 0, False: 23]
352
0
        return Status::InternalError("Failed to reconstruct expression tree from thrift.");
353
0
    }
354
355
    // create root expr
356
23
    int root_children = nodes[*node_idx].num_children;
357
23
    VExprSPtr root;
358
23
    RETURN_IF_ERROR(create_expr(nodes[*node_idx], root));
Line
Count
Source
637
23
    do {                                \
638
23
        Status _status_ = (stmt);       \
639
23
        if (UNLIKELY(!_status_.ok())) { \
Line
Count
Source
36
23
#define UNLIKELY(expr) __builtin_expect(!!(expr), 0)
  Branch (36:24): [True: 0, False: 23]
640
0
            return _status_;            \
641
0
        }                               \
642
23
    } while (false)
  Branch (642:14): [Folded - Ignored]
359
23
    DCHECK(root != nullptr);
360
23
    root_expr = root;
361
23
    ctx = std::make_shared<VExprContext>(root);
362
    // short path for leaf node
363
23
    if (root_children <= 0) {
  Branch (363:9): [True: 8, False: 15]
364
8
        return Status::OK();
365
8
    }
366
367
    // non-recursive traversal
368
15
    using VExprSPtrCountPair = std::pair<VExprSPtr, int>;
369
15
    std::stack<std::shared_ptr<VExprSPtrCountPair>> s;
370
15
    s.emplace(std::make_shared<VExprSPtrCountPair>(root, root_children));
371
120
    while (!s.empty()) {
  Branch (371:12): [True: 105, False: 15]
372
        // copy the shared ptr resource to avoid dangling reference
373
105
        auto parent = s.top();
374
        // Decrement or pop
375
105
        if (parent->second > 1) {
  Branch (375:13): [True: 53, False: 52]
376
53
            parent->second -= 1;
377
53
        } else {
378
52
            s.pop();
379
52
        }
380
381
105
        DCHECK(parent->first != nullptr);
382
105
        if (++*node_idx >= nodes.size()) {
  Branch (382:13): [True: 0, False: 105]
383
0
            return Status::InternalError("Failed to reconstruct expression tree from thrift.");
384
0
        }
385
386
105
        VExprSPtr expr;
387
105
        RETURN_IF_ERROR(create_expr(nodes[*node_idx], expr));
Line
Count
Source
637
105
    do {                                \
638
105
        Status _status_ = (stmt);       \
639
105
        if (UNLIKELY(!_status_.ok())) { \
Line
Count
Source
36
105
#define UNLIKELY(expr) __builtin_expect(!!(expr), 0)
  Branch (36:24): [True: 0, False: 105]
640
0
            return _status_;            \
641
0
        }                               \
642
105
    } while (false)
  Branch (642:14): [Folded - Ignored]
388
105
        DCHECK(expr != nullptr);
389
105
        parent->first->add_child(expr);
390
        // push to stack if has children
391
105
        int num_children = nodes[*node_idx].num_children;
392
105
        if (num_children > 0) {
  Branch (392:13): [True: 37, False: 68]
393
37
            s.emplace(std::make_shared<VExprSPtrCountPair>(expr, num_children));
394
37
        }
395
105
    }
396
15
    return Status::OK();
397
15
}
398
399
23
Status VExpr::create_expr_tree(const TExpr& texpr, VExprContextSPtr& ctx) {
400
23
    if (texpr.nodes.empty()) {
  Branch (400:9): [True: 0, False: 23]
401
0
        ctx = nullptr;
402
0
        return Status::OK();
403
0
    }
404
23
    int node_idx = 0;
405
23
    VExprSPtr e;
406
23
    Status status = create_tree_from_thrift(texpr.nodes, &node_idx, e, ctx);
407
23
    if (status.ok() && node_idx + 1 != texpr.nodes.size()) {
  Branch (407:9): [True: 23, False: 0]
  Branch (407:24): [True: 0, False: 23]
408
0
        status = Status::InternalError(
409
0
                "Expression tree only partially reconstructed. Not all thrift nodes were "
410
0
                "used.");
411
0
    }
412
23
    if (!status.ok()) {
  Branch (412:9): [True: 0, False: 23]
413
0
        LOG(ERROR) << "Could not construct expr tree.\n"
414
0
                   << status << "\n"
415
0
                   << apache::thrift::ThriftDebugString(texpr);
416
0
    }
417
23
    return status;
418
23
}
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) {
  Branch (422:28): [True: 0, False: 0]
423
0
        VExprContextSPtr ctx;
424
0
        RETURN_IF_ERROR(create_expr_tree(texpr, ctx));
Line
Count
Source
637
0
    do {                                \
638
0
        Status _status_ = (stmt);       \
639
0
        if (UNLIKELY(!_status_.ok())) { \
Line
Count
Source
36
0
#define UNLIKELY(expr) __builtin_expect(!!(expr), 0)
  Branch (36:24): [True: 0, False: 0]
640
0
            return _status_;            \
641
0
        }                               \
642
0
    } while (false)
  Branch (642:14): [Folded - Ignored]
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()) {
  Branch (432:9): [True: 0, False: 0]
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()) {
  Branch (436:9): [True: 0, False: 0]
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)) {
  Branch (442:13): [True: 0, False: 0]
443
0
            return true;
444
0
        }
445
0
        if (to->is_nullable() && !from->is_nullable()) {
  Branch (445:13): [True: 0, False: 0]
  Branch (445:34): [True: 0, False: 0]
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++) {
  Branch (450:21): [True: 0, False: 0]
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)) {
  Branch (453:13): [True: 0, False: 0]
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) {
  Branch (465:19): [True: 0, False: 4]
466
0
        RETURN_IF_ERROR(ctx->prepare(state, row_desc));
Line
Count
Source
637
0
    do {                                \
638
0
        Status _status_ = (stmt);       \
639
0
        if (UNLIKELY(!_status_.ok())) { \
Line
Count
Source
36
0
#define UNLIKELY(expr) __builtin_expect(!!(expr), 0)
  Branch (36:24): [True: 0, False: 0]
640
0
            return _status_;            \
641
0
        }                               \
642
0
    } while (false)
  Branch (642:14): [Folded - Ignored]
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) {
  Branch (472:26): [True: 0, False: 4]
473
0
        RETURN_IF_ERROR(ctx->open(state));
Line
Count
Source
637
0
    do {                                \
638
0
        Status _status_ = (stmt);       \
639
0
        if (UNLIKELY(!_status_.ok())) { \
Line
Count
Source
36
0
#define UNLIKELY(expr) __builtin_expect(!!(expr), 0)
  Branch (36:24): [True: 0, False: 0]
640
0
            return _status_;            \
641
0
        }                               \
642
0
    } while (false)
  Branch (642:14): [Folded - Ignored]
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()) {
  Branch (480:9): [True: 0, False: 0]
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) {
  Branch (483:28): [True: 0, False: 0]
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) {
  Branch (489:21): [True: 0, False: 0]
490
0
        RETURN_IF_ERROR(ctxs[i]->clone(state, new_ctxs[i]));
Line
Count
Source
637
0
    do {                                \
638
0
        Status _status_ = (stmt);       \
639
0
        if (UNLIKELY(!_status_.ok())) { \
Line
Count
Source
36
0
#define UNLIKELY(expr) __builtin_expect(!!(expr), 0)
  Branch (36:24): [True: 0, False: 0]
640
0
            return _status_;            \
641
0
        }                               \
642
0
    } while (false)
  Branch (642:14): [Folded - Ignored]
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()) {
  Branch (500:9): [True: 0, False: 0]
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) {
  Branch (511:21): [True: 0, False: 0]
512
0
        out << (i == 0 ? "" : " ") << exprs[i]->debug_string();
  Branch (512:17): [True: 0, False: 0]
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) {
  Branch (521:26): [True: 0, False: 0]
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()) {
  Branch (534:9): [True: 6, False: 0]
535
6
        return Status::OK();
536
6
    }
537
538
0
    if (_constant_col != nullptr) {
  Branch (538:9): [True: 0, False: 0]
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));
Line
Count
Source
637
0
    do {                                \
638
0
        Status _status_ = (stmt);       \
639
0
        if (UNLIKELY(!_status_.ok())) { \
Line
Count
Source
36
0
#define UNLIKELY(expr) __builtin_expect(!!(expr), 0)
  Branch (36:24): [True: 0, False: 0]
640
0
            return _status_;            \
641
0
        }                               \
642
0
    } while (false)
  Branch (642:14): [Folded - Ignored]
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) {
  Branch (557:9): [True: 0, False: 0]
558
0
        *column_wrapper = _constant_col;
559
0
    }
560
561
0
    return Status::OK();
562
0
}
563
564
55
void VExpr::register_function_context(RuntimeState* state, VExprContext* context) {
565
55
    std::vector<TypeDescriptor> arg_types;
566
108
    for (auto& i : _children) {
  Branch (566:18): [True: 108, False: 55]
567
108
        arg_types.push_back(i->type());
568
108
    }
569
570
55
    _fn_context_index = context->register_function_context(state, _type, arg_types);
571
55
}
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) {
  Branch (577:9): [True: 2, False: 0]
578
2
        std::vector<std::shared_ptr<ColumnPtrWrapper>> constant_cols;
579
2
        for (auto c : _children) {
  Branch (579:21): [True: 2, False: 2]
580
2
            std::shared_ptr<ColumnPtrWrapper> const_col;
581
2
            RETURN_IF_ERROR(c->get_const_col(context, &const_col));
Line
Count
Source
637
2
    do {                                \
638
2
        Status _status_ = (stmt);       \
639
2
        if (UNLIKELY(!_status_.ok())) { \
Line
Count
Source
36
2
#define UNLIKELY(expr) __builtin_expect(!!(expr), 0)
  Branch (36:24): [True: 0, False: 2]
640
0
            return _status_;            \
641
0
        }                               \
642
2
    } while (false)
  Branch (642:14): [Folded - Ignored]
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()) {
  Branch (586:13): [True: 0, False: 0]
587
0
            auto* timer = ADD_TIMER(state->get_task()->get_task_profile(),
Line
Count
Source
60
0
#define ADD_TIMER(profile, name) (profile)->add_counter(name, TUnit::TIME_NS)
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) {
  Branch (593:9): [True: 2, False: 0]
594
2
        RETURN_IF_ERROR(function->open(fn_ctx, FunctionContext::FRAGMENT_LOCAL));
Line
Count
Source
637
2
    do {                                \
638
2
        Status _status_ = (stmt);       \
639
2
        if (UNLIKELY(!_status_.ok())) { \
Line
Count
Source
36
2
#define UNLIKELY(expr) __builtin_expect(!!(expr), 0)
  Branch (36:24): [True: 0, False: 2]
640
0
            return _status_;            \
641
0
        }                               \
642
2
    } while (false)
  Branch (642:14): [Folded - Ignored]
595
2
    }
596
2
    RETURN_IF_ERROR(function->open(fn_ctx, FunctionContext::THREAD_LOCAL));
Line
Count
Source
637
2
    do {                                \
638
2
        Status _status_ = (stmt);       \
639
2
        if (UNLIKELY(!_status_.ok())) { \
Line
Count
Source
36
2
#define UNLIKELY(expr) __builtin_expect(!!(expr), 0)
  Branch (36:24): [True: 0, False: 2]
640
0
            return _status_;            \
641
0
        }                               \
642
2
    } while (false)
  Branch (642:14): [Folded - Ignored]
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) {
  Branch (602:9): [True: 2, False: 0]
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) {
  Branch (606:13): [True: 2, False: 0]
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)) {
  Branch (613:9): [True: 0, False: 0]
  Branch (613:26): [True: 0, False: 0]
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()) {
  Branch (648:28): [True: 2, False: 0]
649
2
        if (child->node_type() == TExprNodeType::CAST_EXPR) {
  Branch (649:13): [True: 1, False: 1]
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()) {
  Branch (652:17): [True: 0, False: 1]
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)) {
  Branch (662:21): [True: 0, False: 0]
663
0
                    if (is_array(storage_type) && is_array(target_type)) {
  Branch (663:25): [True: 0, False: 0]
  Branch (663:51): [True: 0, False: 0]
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 &&
  Branch (678:21): [True: 0, False: 0]
679
0
                    (storage_type->equals(*target_type) ||
  Branch (679:22): [True: 0, False: 0]
680
0
                     (is_string_type(target_primitive_type) &&
  Branch (680:23): [True: 0, False: 0]
681
0
                      is_string_type(origin_primitive_type)))) {
  Branch (681:23): [True: 0, False: 0]
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()) {
  Branch (692:9): [True: 0, False: 0]
693
0
        return Status::OK(); // Early exit if no children to process
694
0
    }
695
696
0
    for (const auto& child : children_exprs) {
  Branch (696:28): [True: 0, False: 0]
697
0
        if (child->is_slot_ref()) {
  Branch (697:13): [True: 0, False: 0]
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) {
  Branch (704:17): [True: 0, False: 0]
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) {
  Branch (710:17): [True: 0, False: 0]
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()) {
  Branch (721:20): [True: 0, False: 0]
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
    // is null or is not null has no arguments
731
0
    if (iterators.empty() || (arguments.empty() && !(function->get_name() == "is_not_null_pred" ||
  Branch (731:9): [True: 0, False: 0]
  Branch (731:9): [True: 0, False: 0]
  Branch (731:31): [True: 0, False: 0]
  Branch (731:54): [True: 0, False: 0]
732
0
                                                     function->get_name() == "is_null_pred"))) {
  Branch (732:54): [True: 0, False: 0]
733
0
        return Status::OK(); // Nothing to evaluate or no literals to compare against
734
0
    }
735
736
0
    auto result_bitmap = segment_v2::InvertedIndexResultBitmap();
737
0
    auto res = function->evaluate_inverted_index(arguments, data_type_with_names, iterators,
738
0
                                                 segment_num_rows, result_bitmap);
739
0
    if (!res.ok()) {
  Branch (739:9): [True: 0, False: 0]
740
0
        return res;
741
0
    }
742
0
    if (!result_bitmap.is_empty()) {
  Branch (742:9): [True: 0, False: 0]
743
0
        index_context->set_inverted_index_result_for_expr(this, result_bitmap);
744
0
        for (int column_id : column_ids) {
  Branch (744:28): [True: 0, False: 0]
745
0
            index_context->set_true_for_inverted_index_status(this, column_id);
746
0
        }
747
0
    }
748
0
    return Status::OK();
749
0
}
750
751
bool VExpr::fast_execute(doris::vectorized::VExprContext* context, doris::vectorized::Block* block,
752
0
                         int* result_column_id) {
753
0
    if (context->get_inverted_index_context() &&
  Branch (753:9): [True: 0, False: 0]
  Branch (753:9): [True: 0, False: 0]
754
0
        context->get_inverted_index_context()->get_inverted_index_result_column().contains(this)) {
  Branch (754:9): [True: 0, False: 0]
755
0
        size_t num_columns_without_result = block->columns();
756
        // prepare a column to save result
757
0
        auto result_column =
758
0
                context->get_inverted_index_context()->get_inverted_index_result_column()[this];
759
0
        if (_data_type->is_nullable()) {
  Branch (759:13): [True: 0, False: 0]
760
0
            block->insert(
761
0
                    {ColumnNullable::create(result_column, ColumnUInt8::create(block->rows(), 0)),
762
0
                     _data_type, expr_name()});
763
0
        } else {
764
0
            block->insert({result_column, _data_type, expr_name()});
765
0
        }
766
0
        *result_column_id = num_columns_without_result;
767
0
        return true;
768
0
    }
769
0
    return false;
770
0
}
771
772
0
bool VExpr::equals(const VExpr& other) {
773
0
    return false;
774
0
}
775
776
} // namespace doris::vectorized