Coverage Report

Created: 2026-07-10 12:22

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/exprs/vcast_expr.h
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
#pragma once
19
#include <string>
20
21
#include "common/object_pool.h"
22
#include "common/status.h"
23
#include "core/block/column_with_type_and_name.h"
24
#include "core/data_type/data_type.h"
25
#include "core/data_type/define_primitive_type.h"
26
#include "exprs/aggregate/aggregate_function.h"
27
#include "exprs/function/function.h"
28
#include "exprs/function_context.h"
29
#include "exprs/vexpr.h"
30
31
namespace doris {
32
class RowDescriptor;
33
class RuntimeState;
34
class TExprNode;
35
class Block;
36
class VExprContext;
37
} // namespace doris
38
39
namespace doris {
40
class VCastExpr : public VExpr {
41
    ENABLE_FACTORY_CREATOR(VCastExpr);
42
43
public:
44
#ifdef BE_TEST
45
    VCastExpr() = default;
46
#endif
47
151k
    VCastExpr(const TExprNode& node) : VExpr(node) {}
48
151k
    ~VCastExpr() override = default;
49
    Status execute_column_impl(VExprContext* context, const Block* block, const Selector* selector,
50
                               size_t count, ColumnPtr& result_column) const override;
51
    Status prepare(RuntimeState* state, const RowDescriptor& desc, VExprContext* context) override;
52
    Status open(RuntimeState* state, VExprContext* context,
53
                FunctionContext::FunctionStateScope scope) override;
54
    void close(VExprContext* context, FunctionContext::FunctionStateScope scope) override;
55
    const std::string& expr_name() const override;
56
    std::string debug_string() const override;
57
    const DataTypePtr& get_target_type() const;
58
0
    bool is_safe_to_execute_on_selected_rows() const override { return false; }
59
60
151k
    virtual std::string cast_name() const { return "CAST"; }
61
0
    Status clone_node(VExprSPtr* cloned_expr) const override {
62
0
        DORIS_CHECK(cloned_expr != nullptr);
63
0
        *cloned_expr = VCastExpr::create_shared(clone_texpr_node());
64
0
        return Status::OK();
65
0
    }
66
67
3.60k
    uint64_t get_digest(uint64_t seed) const override {
68
3.60k
        auto res = VExpr::get_digest(seed);
69
3.60k
        if (res) {
70
3.59k
            return HashUtil::hash64(_target_data_type_name.data(), _target_data_type_name.size(),
71
3.59k
                                    res);
72
3.59k
        }
73
6
        return 0;
74
3.60k
    }
75
76
protected:
77
    FunctionBasePtr _function;
78
    std::string _expr_name;
79
80
private:
81
    DataTypePtr _target_data_type;
82
    std::string _target_data_type_name;
83
84
    DataTypePtr _cast_param_data_type;
85
86
    static const constexpr char* function_name = "CAST";
87
};
88
89
class TryCastExpr final : public VCastExpr {
90
    ENABLE_FACTORY_CREATOR(TryCastExpr);
91
92
public:
93
#ifdef BE_TEST
94
    TryCastExpr() = default;
95
#endif
96
97
    TryCastExpr(const TExprNode& node)
98
11
            : VCastExpr(node), _original_cast_return_is_nullable(node.is_cast_nullable) {}
99
    Status execute_column_impl(VExprContext* context, const Block* block, const Selector* selector,
100
                               size_t count, ColumnPtr& result_column) const override;
101
    ~TryCastExpr() override = default;
102
11
    std::string cast_name() const override { return "TRY CAST"; }
103
0
    bool is_safe_to_execute_on_selected_rows() const override {
104
0
        return VExpr::is_safe_to_execute_on_selected_rows();
105
0
    }
106
0
    Status clone_node(VExprSPtr* cloned_expr) const override {
107
0
        DORIS_CHECK(cloned_expr != nullptr);
108
0
        auto node = clone_texpr_node();
109
0
        node.__set_is_cast_nullable(_original_cast_return_is_nullable);
110
0
        *cloned_expr = TryCastExpr::create_shared(node);
111
0
        return Status::OK();
112
0
    }
113
114
private:
115
    DataTypePtr original_cast_return_type() const;
116
    template <bool original_cast_reutrn_is_nullable>
117
    Status single_row_execute(VExprContext* context, const ColumnWithTypeAndName& input_info,
118
                              ColumnPtr& return_column) const;
119
120
    //Try_cast always returns nullable,
121
    // but we also need the information of whether the return value of the original cast is nullable.
122
    bool _original_cast_return_is_nullable = false;
123
};
124
125
} // namespace doris