Coverage Report

Created: 2026-07-16 22:25

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
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
49
doris::Status VCastExpr::prepare(doris::RuntimeState* state, const doris::RowDescriptor& desc,
50
2
                                 VExprContext* context) {
51
2
    RETURN_IF_ERROR_OR_PREPARED(VExpr::prepare(state, desc, context));
52
53
2
    DCHECK_EQ(_children.size(), 1);
54
2
    auto child = _children[0];
55
2
    const auto& child_name = child->expr_name();
56
57
    // create a const string column
58
2
    _target_data_type = _data_type;
59
    // TODO(xy): support return struct type name
60
2
    _target_data_type_name = _target_data_type->get_name();
61
    // Using typeindex to indicate the datatype, not using type name because
62
    // type name is not stable, but type index is stable and immutable
63
2
    _cast_param_data_type = _target_data_type;
64
65
2
    ColumnsWithTypeAndName argument_template;
66
2
    argument_template.reserve(2);
67
2
    argument_template.emplace_back(nullptr, child->data_type(), child_name);
68
2
    argument_template.emplace_back(nullptr, _cast_param_data_type, _target_data_type_name);
69
2
    _function = SimpleFunctionFactory::instance().get_function(function_name, argument_template,
70
2
                                                               _data_type, {});
71
72
2
    if (_function == nullptr) {
73
0
        return Status::NotSupported("Cast from {} to {} is not implemented",
74
0
                                    child->data_type()->get_name(), _target_data_type_name);
75
0
    }
76
2
    VExpr::register_function_context(state, context);
77
2
    context->fn_context(_fn_context_index)
78
2
            ->set_enable_lossless_decimal_cast(_lossless_decimal_cast);
79
2
    _expr_name = fmt::format("({} {}({}) TO {})", cast_name(), child_name,
80
2
                             child->data_type()->get_name(), _target_data_type_name);
81
2
    _prepare_finished = true;
82
2
    return Status::OK();
83
2
}
84
85
0
const DataTypePtr& VCastExpr::get_target_type() const {
86
0
    return _target_data_type;
87
0
}
88
89
doris::Status VCastExpr::open(doris::RuntimeState* state, VExprContext* context,
90
0
                              FunctionContext::FunctionStateScope scope) {
91
0
    DCHECK(_prepare_finished);
92
0
    for (auto& i : _children) {
93
0
        RETURN_IF_ERROR(i->open(state, context, scope));
94
0
    }
95
0
    RETURN_IF_ERROR(VExpr::init_function_context(state, context, scope, _function));
96
0
    if (scope == FunctionContext::FRAGMENT_LOCAL) {
97
0
        RETURN_IF_ERROR(VExpr::get_const_col(context, nullptr));
98
0
    }
99
0
    _open_finished = true;
100
0
    return Status::OK();
101
0
}
102
103
0
void VCastExpr::close(VExprContext* context, FunctionContext::FunctionStateScope scope) {
104
0
    VExpr::close_function_context(context, scope, _function);
105
0
    VExpr::close(context, scope);
106
0
}
107
108
Status VCastExpr::execute_column_impl(VExprContext* context, const Block* block,
109
                                      const Selector* selector, size_t count,
110
0
                                      ColumnPtr& result_column) const {
111
0
    DCHECK(_open_finished || block == nullptr) << _open_finished << _expr_name;
112
0
    if (is_const_and_have_executed()) { // const have executed in open function
113
0
        result_column = get_result_from_const(count);
114
0
        return Status::OK();
115
0
    }
116
    // for each child call execute
117
118
0
    ColumnPtr from_column;
119
0
    RETURN_IF_ERROR(_children[0]->execute_column(context, block, selector, count, from_column));
120
121
0
    Block temp_block;
122
0
    temp_block.insert({from_column, _children[0]->execute_type(block), _children[0]->expr_name()});
123
0
    temp_block.insert({nullptr, _data_type, _expr_name});
124
0
    RETURN_IF_ERROR(_function->execute(context->fn_context(_fn_context_index), temp_block, {0}, 1,
125
0
                                       temp_block.rows()));
126
127
0
    result_column = temp_block.get_by_position(1).column;
128
0
    DCHECK_EQ(result_column->size(), count);
129
0
    return Status::OK();
130
0
}
131
132
7
bool cast_error_code(Status& st) {
133
    //There may be more error codes that need to be captured by try cast in the future.
134
7
    if (st.is<ErrorCode::INVALID_ARGUMENT>()) {
135
5
        return true;
136
5
    } else {
137
2
        return false;
138
2
    }
139
7
}
140
141
13
DataTypePtr TryCastExpr::original_cast_return_type() const {
142
13
    if (_original_cast_return_is_nullable) {
143
7
        return _data_type;
144
7
    } else {
145
6
        return remove_nullable(_data_type);
146
6
    }
147
13
}
148
149
Status TryCastExpr::execute_column_impl(VExprContext* context, const Block* block,
150
                                        const Selector* selector, size_t count,
151
6
                                        ColumnPtr& result_column) const {
152
6
    DCHECK(_open_finished || block == nullptr) << _open_finished << _expr_name;
153
6
    if (is_const_and_have_executed()) { // const have executed in open function
154
0
        result_column = get_result_from_const(count);
155
0
        return Status::OK();
156
0
    }
157
158
    // For try_cast, try to execute it in batches first.
159
160
    // execute child first
161
162
6
    ColumnPtr from_column;
163
6
    RETURN_IF_ERROR(_children[0]->execute_column(context, block, selector, count, from_column));
164
6
    auto from_type = _children[0]->execute_type(block);
165
166
    // prepare block
167
168
6
    Block temp_block;
169
6
    temp_block.insert({from_column, from_type, _children[0]->expr_name()});
170
6
    temp_block.insert({nullptr, original_cast_return_type(), _expr_name});
171
172
    // batch execute
173
6
    auto batch_exec_status = _function->execute(context->fn_context(_fn_context_index), temp_block,
174
6
                                                {0}, 1, temp_block.rows());
175
    // If batch is executed successfully,
176
    // it means that there is no error and it will be returned directly.
177
6
    if (batch_exec_status.ok()) {
178
2
        result_column = temp_block.get_by_position(1).column;
179
2
        result_column = make_nullable(result_column);
180
2
        return batch_exec_status;
181
2
    }
182
183
    // If there is an error that cannot be handled by try cast, it will be returned directly.
184
4
    if (!cast_error_code(batch_exec_status)) {
185
1
        return batch_exec_status;
186
1
    }
187
188
    // If there is an error that can be handled by try cast,
189
    // it will be converted into line execution.
190
3
    ColumnWithTypeAndName input_info {from_column, from_type, _children[0]->expr_name()};
191
    // distinguish whether the return value of the original cast is nullable
192
3
    if (_original_cast_return_is_nullable) {
193
2
        RETURN_IF_ERROR(single_row_execute<true>(context, input_info, result_column));
194
2
    } else {
195
1
        RETURN_IF_ERROR(single_row_execute<false>(context, input_info, result_column));
196
1
    }
197
    // wrap nullable
198
2
    result_column = make_nullable(result_column);
199
2
    DCHECK_EQ(result_column->size(), count);
200
201
2
    return Status::OK();
202
3
}
203
204
template <bool original_cast_reutrn_is_nullable>
205
Status TryCastExpr::single_row_execute(VExprContext* context,
206
                                       const ColumnWithTypeAndName& input_info,
207
3
                                       ColumnPtr& return_column) const {
208
3
    auto input_column = input_info.column;
209
3
    const auto& input_type = input_info.type;
210
3
    const auto& input_name = input_info.name;
211
3
    auto result_column = _data_type->create_column();
212
213
3
    ColumnNullable& result_null_column = assert_cast<ColumnNullable&>(*result_column);
214
215
3
    IColumn& result_nested_column = result_null_column.get_nested_column();
216
3
    auto& result_null_map_data = result_null_column.get_null_map_data();
217
218
4
    auto insert_from_single_row = [&](const IColumn& single_exec_column, size_t row) {
219
4
        DCHECK_EQ(single_exec_column.size(), 1);
220
4
        if constexpr (original_cast_reutrn_is_nullable) {
221
2
            result_null_column.insert_from(single_exec_column, 0);
222
2
        } else {
223
2
            DCHECK(!single_exec_column.is_nullable());
224
2
            result_nested_column.insert_from(single_exec_column, 0);
225
2
            result_null_map_data.push_back(0);
226
2
        }
227
4
    };
_ZZNK5doris11TryCastExpr18single_row_executeILb1EEENS_6StatusEPNS_12VExprContextERKNS_21ColumnWithTypeAndNameERNS_3COWINS_7IColumnEE13immutable_ptrIS9_EEENKUlRKS9_mE_clESF_m
Line
Count
Source
218
2
    auto insert_from_single_row = [&](const IColumn& single_exec_column, size_t row) {
219
2
        DCHECK_EQ(single_exec_column.size(), 1);
220
2
        if constexpr (original_cast_reutrn_is_nullable) {
221
2
            result_null_column.insert_from(single_exec_column, 0);
222
        } else {
223
            DCHECK(!single_exec_column.is_nullable());
224
            result_nested_column.insert_from(single_exec_column, 0);
225
            result_null_map_data.push_back(0);
226
        }
227
2
    };
_ZZNK5doris11TryCastExpr18single_row_executeILb0EEENS_6StatusEPNS_12VExprContextERKNS_21ColumnWithTypeAndNameERNS_3COWINS_7IColumnEE13immutable_ptrIS9_EEENKUlRKS9_mE_clESF_m
Line
Count
Source
218
2
    auto insert_from_single_row = [&](const IColumn& single_exec_column, size_t row) {
219
2
        DCHECK_EQ(single_exec_column.size(), 1);
220
        if constexpr (original_cast_reutrn_is_nullable) {
221
            result_null_column.insert_from(single_exec_column, 0);
222
2
        } else {
223
            DCHECK(!single_exec_column.is_nullable());
224
2
            result_nested_column.insert_from(single_exec_column, 0);
225
2
            result_null_map_data.push_back(0);
226
2
        }
227
2
    };
228
229
3
    auto insert_null = [&](size_t row) { result_null_column.insert_default(); };
_ZZNK5doris11TryCastExpr18single_row_executeILb1EEENS_6StatusEPNS_12VExprContextERKNS_21ColumnWithTypeAndNameERNS_3COWINS_7IColumnEE13immutable_ptrIS9_EEENKUlmE_clEm
Line
Count
Source
229
1
    auto insert_null = [&](size_t row) { result_null_column.insert_default(); };
_ZZNK5doris11TryCastExpr18single_row_executeILb0EEENS_6StatusEPNS_12VExprContextERKNS_21ColumnWithTypeAndNameERNS_3COWINS_7IColumnEE13immutable_ptrIS9_EEENKUlmE_clEm
Line
Count
Source
229
1
    auto insert_null = [&](size_t row) { result_null_column.insert_default(); };
230
231
3
    const auto size = input_column->size();
232
9
    for (size_t row = 0; row < size; ++row) {
233
7
        Block single_row_block;
234
7
        single_row_block.insert({input_column->cut(row, 1), input_type, input_name});
235
7
        single_row_block.insert({nullptr, original_cast_return_type(), _expr_name});
236
237
7
        auto single_exec_status = _function->execute(context->fn_context(_fn_context_index),
238
7
                                                     single_row_block, {0}, 1, 1);
239
7
        if (single_exec_status.ok()) {
240
4
            insert_from_single_row(*single_row_block.get_by_position(1).column, row);
241
4
        } else {
242
3
            if (!cast_error_code(single_exec_status)) {
243
1
                return single_exec_status;
244
1
            }
245
2
            insert_null(row);
246
2
        }
247
7
    }
248
2
    return_column = std::move(result_column);
249
2
    return Status::OK();
250
3
}
_ZNK5doris11TryCastExpr18single_row_executeILb1EEENS_6StatusEPNS_12VExprContextERKNS_21ColumnWithTypeAndNameERNS_3COWINS_7IColumnEE13immutable_ptrIS9_EE
Line
Count
Source
207
2
                                       ColumnPtr& return_column) const {
208
2
    auto input_column = input_info.column;
209
2
    const auto& input_type = input_info.type;
210
2
    const auto& input_name = input_info.name;
211
2
    auto result_column = _data_type->create_column();
212
213
2
    ColumnNullable& result_null_column = assert_cast<ColumnNullable&>(*result_column);
214
215
2
    IColumn& result_nested_column = result_null_column.get_nested_column();
216
2
    auto& result_null_map_data = result_null_column.get_null_map_data();
217
218
2
    auto insert_from_single_row = [&](const IColumn& single_exec_column, size_t row) {
219
2
        DCHECK_EQ(single_exec_column.size(), 1);
220
2
        if constexpr (original_cast_reutrn_is_nullable) {
221
2
            result_null_column.insert_from(single_exec_column, 0);
222
2
        } else {
223
2
            DCHECK(!single_exec_column.is_nullable());
224
2
            result_nested_column.insert_from(single_exec_column, 0);
225
2
            result_null_map_data.push_back(0);
226
2
        }
227
2
    };
228
229
2
    auto insert_null = [&](size_t row) { result_null_column.insert_default(); };
230
231
2
    const auto size = input_column->size();
232
5
    for (size_t row = 0; row < size; ++row) {
233
4
        Block single_row_block;
234
4
        single_row_block.insert({input_column->cut(row, 1), input_type, input_name});
235
4
        single_row_block.insert({nullptr, original_cast_return_type(), _expr_name});
236
237
4
        auto single_exec_status = _function->execute(context->fn_context(_fn_context_index),
238
4
                                                     single_row_block, {0}, 1, 1);
239
4
        if (single_exec_status.ok()) {
240
2
            insert_from_single_row(*single_row_block.get_by_position(1).column, row);
241
2
        } else {
242
2
            if (!cast_error_code(single_exec_status)) {
243
1
                return single_exec_status;
244
1
            }
245
1
            insert_null(row);
246
1
        }
247
4
    }
248
1
    return_column = std::move(result_column);
249
1
    return Status::OK();
250
2
}
_ZNK5doris11TryCastExpr18single_row_executeILb0EEENS_6StatusEPNS_12VExprContextERKNS_21ColumnWithTypeAndNameERNS_3COWINS_7IColumnEE13immutable_ptrIS9_EE
Line
Count
Source
207
1
                                       ColumnPtr& return_column) const {
208
1
    auto input_column = input_info.column;
209
1
    const auto& input_type = input_info.type;
210
1
    const auto& input_name = input_info.name;
211
1
    auto result_column = _data_type->create_column();
212
213
1
    ColumnNullable& result_null_column = assert_cast<ColumnNullable&>(*result_column);
214
215
1
    IColumn& result_nested_column = result_null_column.get_nested_column();
216
1
    auto& result_null_map_data = result_null_column.get_null_map_data();
217
218
1
    auto insert_from_single_row = [&](const IColumn& single_exec_column, size_t row) {
219
1
        DCHECK_EQ(single_exec_column.size(), 1);
220
1
        if constexpr (original_cast_reutrn_is_nullable) {
221
1
            result_null_column.insert_from(single_exec_column, 0);
222
1
        } else {
223
1
            DCHECK(!single_exec_column.is_nullable());
224
1
            result_nested_column.insert_from(single_exec_column, 0);
225
1
            result_null_map_data.push_back(0);
226
1
        }
227
1
    };
228
229
1
    auto insert_null = [&](size_t row) { result_null_column.insert_default(); };
230
231
1
    const auto size = input_column->size();
232
4
    for (size_t row = 0; row < size; ++row) {
233
3
        Block single_row_block;
234
3
        single_row_block.insert({input_column->cut(row, 1), input_type, input_name});
235
3
        single_row_block.insert({nullptr, original_cast_return_type(), _expr_name});
236
237
3
        auto single_exec_status = _function->execute(context->fn_context(_fn_context_index),
238
3
                                                     single_row_block, {0}, 1, 1);
239
3
        if (single_exec_status.ok()) {
240
2
            insert_from_single_row(*single_row_block.get_by_position(1).column, row);
241
2
        } else {
242
1
            if (!cast_error_code(single_exec_status)) {
243
0
                return single_exec_status;
244
0
            }
245
1
            insert_null(row);
246
1
        }
247
3
    }
248
1
    return_column = std::move(result_column);
249
1
    return Status::OK();
250
1
}
251
252
7
const std::string& VCastExpr::expr_name() const {
253
7
    return _expr_name;
254
7
}
255
256
0
std::string VCastExpr::debug_string() const {
257
0
    std::stringstream out;
258
0
    out << cast_name() << " Expr(CAST " << get_child(0)->data_type()->get_name() << " to "
259
0
        << _target_data_type->get_name() << "){";
260
0
    bool first = true;
261
0
    for (const auto& input_expr : children()) {
262
0
        if (first) {
263
0
            first = false;
264
0
        } else {
265
0
            out << ",";
266
0
        }
267
0
        out << input_expr->debug_string();
268
0
    }
269
0
    out << "}";
270
0
    return out.str();
271
0
}
272
273
} // namespace doris