Coverage Report

Created: 2026-07-18 03:00

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