Coverage Report

Created: 2026-10-19 18:14

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/root/doris/be/src/exprs/vcast_expr.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/vcast_expr.h"
19
20
#include <fmt/format.h>
21
#include <gen_cpp/Types_types.h>
22
#include <glog/logging.h>
23
24
#include <cstddef>
25
#include <memory>
26
#include <ostream>
27
28
#include "common/exception.h"
29
#include "common/status.h"
30
#include "core/assert_cast.h"
31
#include "core/block/block.h"
32
#include "core/block/column_with_type_and_name.h"
33
#include "core/block/columns_with_type_and_name.h"
34
#include "core/column/column.h"
35
#include "core/column/column_nullable.h"
36
#include "core/data_type/data_type_nullable.h"
37
#include "exprs/function/simple_function_factory.h"
38
#include "exprs/vexpr.h"
39
#include "exprs/vexpr_context.h"
40
#include "runtime/runtime_state.h"
41
42
namespace doris {
43
class RowDescriptor;
44
class RuntimeState;
45
} // namespace doris
46
47
namespace doris {
48
#include "common/compile_check_begin.h"
49
50
doris::Status VCastExpr::prepare(doris::RuntimeState* state, const doris::RowDescriptor& desc,
51
2
                                 VExprContext* context) {
52
2
    RETURN_IF_ERROR_OR_PREPARED(VExpr::prepare(state, desc, context));
53
54
2
    DCHECK_EQ(_children.size(), 1);
55
2
    auto child = _children[0];
56
2
    const auto& child_name = child->expr_name();
57
58
    // create a const string column
59
2
    _target_data_type = _data_type;
60
    // TODO(xy): support return struct type name
61
2
    _target_data_type_name = _target_data_type->get_name();
62
    // Using typeindex to indicate the datatype, not using type name because
63
    // type name is not stable, but type index is stable and immutable
64
2
    _cast_param_data_type = _target_data_type;
65
66
2
    ColumnsWithTypeAndName argument_template;
67
2
    argument_template.reserve(2);
68
2
    argument_template.emplace_back(nullptr, child->data_type(), child_name);
69
2
    argument_template.emplace_back(nullptr, _cast_param_data_type, _target_data_type_name);
70
2
    _function = SimpleFunctionFactory::instance().get_function(function_name, argument_template,
71
2
                                                               _data_type, {});
72
73
2
    if (_function == nullptr) {
74
0
        return Status::NotSupported("Cast from {} to {} is not implemented",
75
0
                                    child->data_type()->get_name(), _target_data_type_name);
76
0
    }
77
2
    VExpr::register_function_context(state, context);
78
2
    _expr_name = fmt::format("({} {}({}) TO {})", cast_name(), child_name,
79
2
                             child->data_type()->get_name(), _target_data_type_name);
80
2
    _prepare_finished = true;
81
2
    return Status::OK();
82
2
}
83
84
0
const DataTypePtr& VCastExpr::get_target_type() const {
85
0
    return _target_data_type;
86
0
}
87
88
doris::Status VCastExpr::open(doris::RuntimeState* state, VExprContext* context,
89
0
                              FunctionContext::FunctionStateScope scope) {
90
0
    DCHECK(_prepare_finished);
91
0
    for (auto& i : _children) {
92
0
        RETURN_IF_ERROR(i->open(state, context, scope));
93
0
    }
94
0
    if (_is_strict_cast) {
95
0
        context->fn_context(_fn_context_index)->set_enable_strict_mode(true);
96
0
    }
97
0
    RETURN_IF_ERROR(VExpr::init_function_context(state, context, scope, _function));
98
0
    if (scope == FunctionContext::FRAGMENT_LOCAL) {
99
0
        RETURN_IF_ERROR(VExpr::get_const_col(context, nullptr));
100
0
    }
101
0
    _open_finished = true;
102
0
    return Status::OK();
103
0
}
104
105
0
void VCastExpr::close(VExprContext* context, FunctionContext::FunctionStateScope scope) {
106
0
    VExpr::close_function_context(context, scope, _function);
107
0
    VExpr::close(context, scope);
108
0
}
109
110
Status VCastExpr::execute_column(VExprContext* context, const Block* block, Selector* selector,
111
0
                                 size_t count, ColumnPtr& result_column) const {
112
0
    DCHECK(_open_finished || block == nullptr) << _open_finished << _expr_name;
113
0
    if (is_const_and_have_executed()) { // const have executed in open function
114
0
        result_column = get_result_from_const(count);
115
0
        return Status::OK();
116
0
    }
117
    // for each child call execute
118
119
0
    ColumnPtr from_column;
120
0
    RETURN_IF_ERROR(_children[0]->execute_column(context, block, selector, count, from_column));
121
122
0
    Block temp_block;
123
0
    temp_block.insert({from_column, _children[0]->execute_type(block), _children[0]->expr_name()});
124
0
    temp_block.insert({nullptr, _data_type, _expr_name});
125
0
    RETURN_IF_ERROR(_function->execute(context->fn_context(_fn_context_index), temp_block, {0}, 1,
126
0
                                       temp_block.rows()));
127
128
0
    result_column = temp_block.get_by_position(1).column;
129
0
    DCHECK_EQ(result_column->size(), count);
130
0
    return Status::OK();
131
0
}
132
133
125
bool cast_error_code(const Status& st) {
134
    // Value conversion failures use INVALID_ARGUMENT (parsing, numeric/date range checks,
135
    // JSONB and complex elements) or ARITHMETIC_OVERFLOW_ERRROR (decimal conversions).
136
    // This predicate relies on producers classifying errors correctly: execution-contract
137
    // violations (including invalid column shapes) must use INTERNAL_ERROR, not these codes.
138
    // Likewise, value failures reported as INTERNAL_ERROR must be corrected at their producer;
139
    // accepting all INTERNAL_ERROR/RUNTIME_ERROR statuses would hide execution defects.
140
125
    return st.is<ErrorCode::INVALID_ARGUMENT>() || st.is<ErrorCode::ARITHMETIC_OVERFLOW_ERRROR>();
141
125
}
142
143
177
DataTypePtr TryCastExpr::original_cast_return_type() const {
144
177
    if (_original_cast_return_is_nullable) {
145
64
        return _data_type;
146
113
    } else {
147
113
        return remove_nullable(_data_type);
148
113
    }
149
177
}
150
151
Status TryCastExpr::execute_column(VExprContext* context, const Block* block, Selector* selector,
152
89
                                   size_t count, ColumnPtr& result_column) const {
153
89
    DCHECK(_open_finished || block == nullptr) << _open_finished << _expr_name;
154
89
    if (is_const_and_have_executed()) { // const have executed in open function
155
0
        result_column = get_result_from_const(count);
156
0
        return Status::OK();
157
0
    }
158
159
    // For try_cast, try to execute it in batches first.
160
161
    // execute child first
162
163
89
    ColumnPtr from_column;
164
89
    RETURN_IF_ERROR(_children[0]->execute_column(context, block, selector, count, from_column));
165
88
    auto from_type = _children[0]->execute_type(block);
166
167
    // prepare block
168
169
88
    Block temp_block;
170
88
    temp_block.insert({from_column, from_type, _children[0]->expr_name()});
171
88
    temp_block.insert({nullptr, original_cast_return_type(), _expr_name});
172
173
    // batch execute
174
88
    auto batch_exec_status = _function->execute(context->fn_context(_fn_context_index), temp_block,
175
88
                                                {0}, 1, temp_block.rows());
176
    // If batch is executed successfully,
177
    // it means that there is no error and it will be returned directly.
178
88
    if (batch_exec_status.ok()) {
179
11
        result_column = temp_block.get_by_position(1).column;
180
11
        result_column = make_nullable(result_column);
181
11
        return batch_exec_status;
182
11
    }
183
184
    // If there is an error that cannot be handled by try cast, it will be returned directly.
185
77
    if (!cast_error_code(batch_exec_status)) {
186
29
        return batch_exec_status;
187
29
    }
188
189
    // If there is an error that can be handled by try cast,
190
    // it will be converted into line execution.
191
48
    ColumnWithTypeAndName input_info {from_column, from_type, _children[0]->expr_name()};
192
48
    RETURN_IF_ERROR(single_row_execute(context, input_info, result_column));
193
48
    DCHECK_EQ(result_column->size(), count);
194
195
19
    return Status::OK();
196
48
}
197
198
Status TryCastExpr::single_row_execute(VExprContext* context,
199
                                       const ColumnWithTypeAndName& input_info,
200
48
                                       ColumnPtr& return_column) const {
201
48
    auto input_column = input_info.column;
202
48
    const auto& input_type = input_info.type;
203
48
    const auto& input_name = input_info.name;
204
48
    auto result_column = _data_type->create_column();
205
206
48
    auto& result_null_column = assert_cast<ColumnNullable&>(*result_column);
207
208
48
    const auto size = input_column->size();
209
108
    for (size_t row = 0; row < size; ++row) {
210
89
        Block single_row_block;
211
89
        single_row_block.insert({input_column->cut(row, 1), input_type, input_name});
212
89
        single_row_block.insert({nullptr, original_cast_return_type(), _expr_name});
213
214
89
        auto single_exec_status = _function->execute(context->fn_context(_fn_context_index),
215
89
                                                     single_row_block, {0}, 1, 1);
216
89
        if (single_exec_status.ok()) {
217
            // FunctionCast uses TRY_CAST's nullable return type even when the original CAST
218
            // is non-nullable. Normalize the actual result before copying the successful row.
219
41
            auto single_exec_column = make_nullable(
220
41
                    single_row_block.get_by_position(1).column->convert_to_full_column_if_const());
221
41
            DCHECK_EQ(single_exec_column->size(), 1);
222
41
            result_null_column.insert_from(*single_exec_column, 0);
223
48
        } else {
224
48
            if (!cast_error_code(single_exec_status)) {
225
29
                return single_exec_status;
226
29
            }
227
19
            result_null_column.insert_default();
228
19
        }
229
89
    }
230
19
    return_column = std::move(result_column);
231
19
    return Status::OK();
232
48
}
233
234
7
const std::string& VCastExpr::expr_name() const {
235
7
    return _expr_name;
236
7
}
237
238
0
std::string VCastExpr::debug_string() const {
239
0
    std::stringstream out;
240
0
    out << cast_name() << " Expr(CAST " << get_child(0)->data_type()->get_name() << " to "
241
0
        << _target_data_type->get_name() << "){";
242
0
    bool first = true;
243
0
    for (const auto& input_expr : children()) {
244
0
        if (first) {
245
0
            first = false;
246
0
        } else {
247
0
            out << ",";
248
0
        }
249
0
        out << input_expr->debug_string();
250
0
    }
251
0
    out << "}";
252
0
    return out.str();
253
0
}
254
255
#include "common/compile_check_end.h"
256
} // namespace doris