Coverage Report

Created: 2026-06-09 13:53

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/format_v2/expr/cast.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 "format_v2/expr/cast.h"
19
20
#include <fmt/format.h>
21
#include <gen_cpp/Exprs_types.h>
22
#include <glog/logging.h>
23
24
#include <ostream>
25
26
#include "common/status.h"
27
#include "core/block/block.h"
28
#include "core/block/column_with_type_and_name.h"
29
#include "core/block/columns_with_type_and_name.h"
30
#include "exprs/function/simple_function_factory.h"
31
#include "exprs/vexpr_context.h"
32
#include "exprs/vliteral.h"
33
34
namespace doris {
35
36
0
Status Cast::prepare(RuntimeState* state, const RowDescriptor& desc, VExprContext* context) {
37
0
    RETURN_IF_ERROR_OR_PREPARED(VExpr::prepare(state, desc, context));
38
0
    if (_children.size() != 1) {
39
0
        return Status::InternalError(
40
0
                fmt::format("Cast should have exactly 1 child expr, but got {}", _children.size()));
41
0
    }
42
0
    ColumnsWithTypeAndName argument_template;
43
0
    argument_template.reserve(_children.size());
44
0
    if (_children[0]->is_literal()) {
45
        // For some functions, he needs some literal columns to derive the return type.
46
0
        auto literal_node = std::dynamic_pointer_cast<VLiteral>(_children[0]);
47
0
        argument_template.emplace_back(literal_node->get_column_ptr(), _children[0]->data_type(),
48
0
                                       _children[0]->expr_name());
49
0
    } else {
50
0
        argument_template.emplace_back(nullptr, _children[0]->data_type(),
51
0
                                       _children[0]->expr_name());
52
0
    }
53
54
0
    _expr_name = fmt::format("CAST(arguments={},return={})", _children[0]->data_type()->get_name(),
55
0
                             _data_type->get_name());
56
    // get the function. won't prepare function.
57
0
    _function = SimpleFunctionFactory::instance().get_function(
58
0
            "CAST", argument_template, _data_type,
59
0
            {.new_version_unix_timestamp = state->query_options().new_version_unix_timestamp},
60
0
            state->be_exec_version());
61
0
    if (_function == nullptr) {
62
0
        return Status::InternalError("Could not find function {} ", _expr_name);
63
0
    }
64
0
    VExpr::register_function_context(state, context);
65
0
    _prepare_finished = true;
66
0
    return Status::OK();
67
0
}
68
69
Status Cast::open(RuntimeState* state, VExprContext* context,
70
0
                  FunctionContext::FunctionStateScope scope) {
71
0
    DCHECK(_prepare_finished);
72
0
    for (auto& i : _children) {
73
0
        RETURN_IF_ERROR(i->open(state, context, scope));
74
0
    }
75
0
    RETURN_IF_ERROR(VExpr::init_function_context(state, context, scope, _function));
76
0
    if (scope == FunctionContext::FRAGMENT_LOCAL) {
77
0
        RETURN_IF_ERROR(VExpr::get_const_col(context, nullptr));
78
0
    }
79
0
    _open_finished = true;
80
0
    return Status::OK();
81
0
}
82
83
0
void Cast::close(VExprContext* context, FunctionContext::FunctionStateScope scope) {
84
0
    VExpr::close_function_context(context, scope, _function);
85
0
    VExpr::close(context, scope);
86
0
}
87
88
Status Cast::execute_column_impl(VExprContext* context, const Block* block,
89
                                 const Selector* selector, size_t count,
90
0
                                 ColumnPtr& result_column) const {
91
0
    return _do_execute(context, block, selector, count, result_column);
92
0
}
93
94
0
std::string Cast::debug_string() const {
95
0
    return _expr_name;
96
0
}
97
98
Status Cast::_do_execute(VExprContext* context, const Block* block, const Selector* selector,
99
0
                         size_t count, ColumnPtr& result_column) const {
100
0
    DCHECK(_open_finished || block == nullptr) << debug_string();
101
0
    if (_children.size() != 1) {
102
0
        return Status::InternalError(
103
0
                fmt::format("Cast should have exactly 1 child expr, but got {}", _children.size()));
104
0
    }
105
0
    if (is_const_and_have_executed()) { // const have executed in open function
106
0
        result_column = get_result_from_const(count);
107
0
        return Status::OK();
108
0
    }
109
110
0
    Block temp_block;
111
0
    ColumnNumbers args(1);
112
113
0
    ColumnPtr tmp_arg_column;
114
0
    RETURN_IF_ERROR(_children[0]->execute_column(context, block, selector, count, tmp_arg_column));
115
0
    auto arg_type = _children[0]->execute_type(block);
116
0
    temp_block.insert({tmp_arg_column, arg_type, _children[0]->expr_name()});
117
0
    args[0] = 0;
118
119
0
    uint32_t num_columns_without_result = temp_block.columns();
120
    // prepare a column to save result
121
0
    temp_block.insert({nullptr, _data_type, _expr_name});
122
123
0
    RETURN_IF_ERROR(_function->execute(context->fn_context(_fn_context_index), temp_block, args,
124
0
                                       num_columns_without_result, count));
125
0
    result_column = temp_block.get_by_position(num_columns_without_result).column;
126
0
    DCHECK_EQ(result_column->size(), count);
127
0
    RETURN_IF_ERROR(result_column->column_self_check());
128
0
    return Status::OK();
129
0
}
130
131
} // namespace doris