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