Coverage Report

Created: 2026-08-26 11:06

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
    VCastExpr(const TExprNode& node)
48
114k
            : VExpr(node), _is_strict_cast(node.__isset.is_strict_cast && node.is_strict_cast) {}
49
114k
    ~VCastExpr() override = default;
50
    Status execute_column_impl(VExprContext* context, const Block* block, const Selector* selector,
51
                               size_t count, ColumnPtr& result_column) const override;
52
    Status prepare(RuntimeState* state, const RowDescriptor& desc, VExprContext* context) override;
53
    Status open(RuntimeState* state, VExprContext* context,
54
                FunctionContext::FunctionStateScope scope) override;
55
    void close(VExprContext* context, FunctionContext::FunctionStateScope scope) override;
56
    const std::string& expr_name() const override;
57
    std::string debug_string() const override;
58
    const DataTypePtr& get_target_type() const;
59
1
    bool is_safe_to_execute_on_selected_rows() const override { return false; }
60
61
114k
    virtual std::string cast_name() const { return "CAST"; }
62
0
    Status clone_node(VExprSPtr* cloned_expr) const override {
63
0
        DORIS_CHECK(cloned_expr != nullptr);
64
0
        *cloned_expr = VCastExpr::create_shared(clone_texpr_node());
65
0
        return Status::OK();
66
0
    }
67
68
2.83k
    uint64_t get_digest(uint64_t seed) const override {
69
2.83k
        auto res = VExpr::get_digest(seed);
70
2.83k
        if (res) {
71
2.82k
            res = HashUtil::hash64(_target_data_type_name.data(), _target_data_type_name.size(),
72
2.82k
                                   res);
73
2.82k
            return HashUtil::hash64(&_is_strict_cast, sizeof(_is_strict_cast), res);
74
2.82k
        }
75
18
        return 0;
76
2.83k
    }
77
78
protected:
79
    FunctionBasePtr _function;
80
    std::string _expr_name;
81
82
private:
83
    DataTypePtr _target_data_type;
84
    std::string _target_data_type_name;
85
86
    DataTypePtr _cast_param_data_type;
87
88
    bool _is_strict_cast = false;
89
90
    static const constexpr char* function_name = "CAST";
91
};
92
93
class TryCastExpr final : public VCastExpr {
94
    ENABLE_FACTORY_CREATOR(TryCastExpr);
95
96
public:
97
#ifdef BE_TEST
98
    TryCastExpr() = default;
99
#endif
100
101
    TryCastExpr(const TExprNode& node)
102
11
            : VCastExpr(node), _original_cast_return_is_nullable(node.is_cast_nullable) {}
103
    Status execute_column_impl(VExprContext* context, const Block* block, const Selector* selector,
104
                               size_t count, ColumnPtr& result_column) const override;
105
    ~TryCastExpr() override = default;
106
11
    std::string cast_name() const override { return "TRY CAST"; }
107
1
    bool is_safe_to_execute_on_selected_rows() const override {
108
1
        return VExpr::is_safe_to_execute_on_selected_rows();
109
1
    }
110
0
    Status clone_node(VExprSPtr* cloned_expr) const override {
111
0
        DORIS_CHECK(cloned_expr != nullptr);
112
0
        auto node = clone_texpr_node();
113
0
        node.__set_is_cast_nullable(_original_cast_return_is_nullable);
114
0
        *cloned_expr = TryCastExpr::create_shared(node);
115
0
        return Status::OK();
116
0
    }
117
118
private:
119
    DataTypePtr original_cast_return_type() const;
120
    template <bool original_cast_reutrn_is_nullable>
121
    Status single_row_execute(VExprContext* context, const ColumnWithTypeAndName& input_info,
122
                              ColumnPtr& return_column) const;
123
124
    //Try_cast always returns nullable,
125
    // but we also need the information of whether the return value of the original cast is nullable.
126
    bool _original_cast_return_is_nullable = false;
127
};
128
129
} // namespace doris