Coverage Report

Created: 2026-07-12 06:41

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