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 <functional> |
20 | | #include <string> |
21 | | |
22 | | #include "common/object_pool.h" |
23 | | #include "common/status.h" |
24 | | #include "core/block/column_with_type_and_name.h" |
25 | | #include "core/data_type/data_type.h" |
26 | | #include "core/data_type/define_primitive_type.h" |
27 | | #include "exprs/aggregate/aggregate_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 | 16 | VCastExpr() = default; |
46 | | #endif |
47 | | VCastExpr(const TExprNode& node) |
48 | 8 | : VExpr(node), _is_strict_cast(node.__isset.is_strict_cast && node.is_strict_cast) {} |
49 | 24 | ~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 | 7 | 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 | uint64_t get_digest(uint64_t seed) const override { |
69 | 2 | auto res = VExpr::get_digest(seed); |
70 | 2 | if (res) { |
71 | 0 | res = HashUtil::hash64(_target_data_type_name.data(), _target_data_type_name.size(), |
72 | 0 | res); |
73 | 0 | return HashUtil::hash64(&_is_strict_cast, sizeof(_is_strict_cast), res); |
74 | 0 | } |
75 | 2 | return 0; |
76 | 2 | } |
77 | | |
78 | | protected: |
79 | | using CastExecutor = |
80 | | std::function<Status(RuntimeState*, const ColumnWithTypeAndName&, |
81 | | ColumnWithTypeAndName&, size_t, const NullMap::value_type*)>; |
82 | | Status execute_cast(RuntimeState* state, const ColumnWithTypeAndName& source, |
83 | | const DataTypePtr& result_type, size_t rows, |
84 | | ColumnPtr& result_column) const; |
85 | | CastExecutor _cast_executor; |
86 | | std::string _expr_name; |
87 | | RuntimeState* _runtime_state = nullptr; |
88 | | |
89 | | private: |
90 | | DataTypePtr _target_data_type; |
91 | | std::string _target_data_type_name; |
92 | | |
93 | | bool _is_strict_cast = false; |
94 | | }; |
95 | | |
96 | | class TryCastExpr final : public VCastExpr { |
97 | | ENABLE_FACTORY_CREATOR(TryCastExpr); |
98 | | |
99 | | public: |
100 | | #ifdef BE_TEST |
101 | 15 | TryCastExpr() = default; |
102 | | #endif |
103 | | |
104 | | TryCastExpr(const TExprNode& node) |
105 | 0 | : VCastExpr(node), _original_cast_return_is_nullable(node.is_cast_nullable) {} |
106 | | Status execute_column_impl(VExprContext* context, const Block* block, const Selector* selector, |
107 | | size_t count, ColumnPtr& result_column) const override; |
108 | 15 | ~TryCastExpr() override = default; |
109 | 0 | std::string cast_name() const override { return "TRY CAST"; } |
110 | 1 | bool is_safe_to_execute_on_selected_rows() const override { |
111 | 1 | return VExpr::is_safe_to_execute_on_selected_rows(); |
112 | 1 | } |
113 | 0 | Status clone_node(VExprSPtr* cloned_expr) const override { |
114 | 0 | DORIS_CHECK(cloned_expr != nullptr); |
115 | 0 | auto node = clone_texpr_node(); |
116 | 0 | node.__set_is_cast_nullable(_original_cast_return_is_nullable); |
117 | 0 | *cloned_expr = TryCastExpr::create_shared(node); |
118 | 0 | return Status::OK(); |
119 | 0 | } |
120 | | |
121 | | private: |
122 | | DataTypePtr original_cast_return_type() const; |
123 | | Status single_row_execute(VExprContext* context, const ColumnWithTypeAndName& input_info, |
124 | | ColumnPtr& return_column) const; |
125 | | |
126 | | //Try_cast always returns nullable, |
127 | | // but we also need the information of whether the return value of the original cast is nullable. |
128 | | bool _original_cast_return_is_nullable = false; |
129 | | }; |
130 | | |
131 | | } // namespace doris |