Coverage Report

Created: 2026-06-25 05:47

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