Coverage Report

Created: 2026-04-14 12:18

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