be/src/exprs/function/cast/cast_to_timestamptz.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 | | |
20 | | #include "core/assert_cast.h" |
21 | | #include "core/data_type/data_type_date_or_datetime_v2.h" |
22 | | #include "core/data_type/data_type_timestamp_ns.h" |
23 | | #include "core/data_type/data_type_timestamptz.h" |
24 | | #include "core/data_type/primitive_type.h" |
25 | | #include "core/types.h" |
26 | | #include "core/value/timestamptz_value.h" |
27 | | #include "core/value/vdatetime_value.h" |
28 | | #include "exprs/function/cast/cast_base.h" |
29 | | #include "exprs/function/cast/cast_to_datetimev2_impl.hpp" |
30 | | #include "exprs/function/cast/cast_to_timestamptz_impl.hpp" |
31 | | #include "runtime/runtime_state.h" |
32 | | |
33 | | namespace doris { |
34 | | |
35 | | template <CastModeType Mode> |
36 | | class CastToImpl<Mode, DataTypeString, DataTypeTimeStampTz> : public CastToBase { |
37 | | public: |
38 | | Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments, |
39 | | uint32_t result, size_t input_rows_count, |
40 | 4 | const NullMap::value_type* null_map = nullptr) const override { |
41 | 4 | const auto& col_from = assert_cast<const DataTypeString::ColumnType&>( |
42 | 4 | *block.get_by_position(arguments[0]).column); |
43 | | |
44 | 4 | auto to_type = block.get_by_position(result).type; |
45 | 4 | auto serde = remove_nullable(to_type)->get_serde(); |
46 | 4 | MutableColumnPtr column_to; |
47 | | |
48 | 4 | DataTypeSerDe::FormatOptions options; |
49 | 4 | options.timezone = &context->state()->timezone_obj(); |
50 | | |
51 | 4 | if constexpr (Mode == CastModeType::NonStrictMode) { |
52 | 2 | auto to_nullable_type = make_nullable(to_type); |
53 | 2 | column_to = to_nullable_type->create_column(); |
54 | 2 | auto& nullable_col_to = assert_cast<ColumnNullable&>(*column_to); |
55 | 2 | RETURN_IF_ERROR(serde->from_string_batch(col_from, nullable_col_to, options)); |
56 | 2 | } else if constexpr (Mode == CastModeType::StrictMode) { |
57 | 2 | column_to = to_type->create_column(); |
58 | 2 | RETURN_IF_ERROR( |
59 | 2 | serde->from_string_strict_mode_batch(col_from, *column_to, options, null_map)); |
60 | 2 | } |
61 | | |
62 | 3 | block.get_by_position(result).column = std::move(column_to); |
63 | 4 | return Status::OK(); |
64 | 4 | } _ZNK5doris10CastToImplILNS_12CastModeTypeE0ENS_14DataTypeStringENS_19DataTypeTimeStampTzEE12execute_implEPNS_15FunctionContextERNS_5BlockERKSt6vectorIjSaIjEEjmPKh Line | Count | Source | 40 | 2 | const NullMap::value_type* null_map = nullptr) const override { | 41 | 2 | const auto& col_from = assert_cast<const DataTypeString::ColumnType&>( | 42 | 2 | *block.get_by_position(arguments[0]).column); | 43 | | | 44 | 2 | auto to_type = block.get_by_position(result).type; | 45 | 2 | auto serde = remove_nullable(to_type)->get_serde(); | 46 | 2 | MutableColumnPtr column_to; | 47 | | | 48 | 2 | DataTypeSerDe::FormatOptions options; | 49 | 2 | options.timezone = &context->state()->timezone_obj(); | 50 | | | 51 | | if constexpr (Mode == CastModeType::NonStrictMode) { | 52 | | auto to_nullable_type = make_nullable(to_type); | 53 | | column_to = to_nullable_type->create_column(); | 54 | | auto& nullable_col_to = assert_cast<ColumnNullable&>(*column_to); | 55 | | RETURN_IF_ERROR(serde->from_string_batch(col_from, nullable_col_to, options)); | 56 | 2 | } else if constexpr (Mode == CastModeType::StrictMode) { | 57 | 2 | column_to = to_type->create_column(); | 58 | 2 | RETURN_IF_ERROR( | 59 | 2 | serde->from_string_strict_mode_batch(col_from, *column_to, options, null_map)); | 60 | 2 | } | 61 | | | 62 | 1 | block.get_by_position(result).column = std::move(column_to); | 63 | 2 | return Status::OK(); | 64 | 2 | } |
_ZNK5doris10CastToImplILNS_12CastModeTypeE1ENS_14DataTypeStringENS_19DataTypeTimeStampTzEE12execute_implEPNS_15FunctionContextERNS_5BlockERKSt6vectorIjSaIjEEjmPKh Line | Count | Source | 40 | 2 | const NullMap::value_type* null_map = nullptr) const override { | 41 | 2 | const auto& col_from = assert_cast<const DataTypeString::ColumnType&>( | 42 | 2 | *block.get_by_position(arguments[0]).column); | 43 | | | 44 | 2 | auto to_type = block.get_by_position(result).type; | 45 | 2 | auto serde = remove_nullable(to_type)->get_serde(); | 46 | 2 | MutableColumnPtr column_to; | 47 | | | 48 | 2 | DataTypeSerDe::FormatOptions options; | 49 | 2 | options.timezone = &context->state()->timezone_obj(); | 50 | | | 51 | 2 | if constexpr (Mode == CastModeType::NonStrictMode) { | 52 | 2 | auto to_nullable_type = make_nullable(to_type); | 53 | 2 | column_to = to_nullable_type->create_column(); | 54 | 2 | auto& nullable_col_to = assert_cast<ColumnNullable&>(*column_to); | 55 | 2 | RETURN_IF_ERROR(serde->from_string_batch(col_from, nullable_col_to, options)); | 56 | | } else if constexpr (Mode == CastModeType::StrictMode) { | 57 | | column_to = to_type->create_column(); | 58 | | RETURN_IF_ERROR( | 59 | | serde->from_string_strict_mode_batch(col_from, *column_to, options, null_map)); | 60 | | } | 61 | | | 62 | 2 | block.get_by_position(result).column = std::move(column_to); | 63 | 2 | return Status::OK(); | 64 | 2 | } |
|
65 | | }; |
66 | | |
67 | | template <> |
68 | | class CastToImpl<CastModeType::StrictMode, DataTypeDateTimeV2, DataTypeTimeStampTz> |
69 | | : public CastToBase { |
70 | | public: |
71 | | Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments, |
72 | | uint32_t result, size_t input_rows_count, |
73 | 2 | const NullMap::value_type* null_map = nullptr) const override { |
74 | 2 | const auto& col_from = |
75 | 2 | assert_cast<const ColumnDateTimeV2&>(*block.get_by_position(arguments[0]).column) |
76 | 2 | .get_data(); |
77 | | |
78 | 2 | auto col_to = ColumnTimeStampTz::create(input_rows_count); |
79 | 2 | auto& col_to_data = col_to->get_data(); |
80 | 2 | const auto& local_time_zone = context->state()->timezone_obj(); |
81 | | |
82 | 2 | const auto dt_scale = block.get_by_position(arguments[0]).type->get_scale(); |
83 | 2 | const auto tz_scale = block.get_by_position(result).type->get_scale(); |
84 | | |
85 | 6 | for (int i = 0; i < input_rows_count; ++i) { |
86 | 5 | if (null_map && null_map[i]) { |
87 | 0 | continue; |
88 | 0 | } |
89 | | |
90 | 5 | DateV2Value<DateTimeV2ValueType> from_dt {col_from[i]}; |
91 | 5 | TimestampTzValue tz_value; |
92 | | |
93 | 5 | if (!tz_value.from_datetime(from_dt, local_time_zone, dt_scale, tz_scale)) { |
94 | 1 | return Status::InternalError( |
95 | 1 | "can not cast from datetime : {} to timestamptz in timezone : {}", |
96 | 1 | from_dt.to_string(), context->state()->timezone()); |
97 | 1 | } |
98 | | |
99 | 4 | col_to_data[i] = tz_value; |
100 | 4 | } |
101 | 1 | block.get_by_position(result).column = std::move(col_to); |
102 | 1 | return Status::OK(); |
103 | 2 | } |
104 | | }; |
105 | | |
106 | | template <> |
107 | | class CastToImpl<CastModeType::NonStrictMode, DataTypeDateTimeV2, DataTypeTimeStampTz> |
108 | | : public CastToBase { |
109 | | public: |
110 | | Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments, |
111 | | uint32_t result, size_t input_rows_count, |
112 | 2 | const NullMap::value_type*) const override { |
113 | 2 | const auto& col_from = |
114 | 2 | assert_cast<const ColumnDateTimeV2&>(*block.get_by_position(arguments[0]).column) |
115 | 2 | .get_data(); |
116 | | |
117 | 2 | auto col_to = ColumnTimeStampTz::create(input_rows_count); |
118 | 2 | auto& col_to_data = col_to->get_data(); |
119 | 2 | auto col_null = ColumnBool::create(input_rows_count, 0); |
120 | 2 | auto& col_null_map = col_null->get_data(); |
121 | 2 | const auto& local_time_zone = context->state()->timezone_obj(); |
122 | | |
123 | 2 | const auto dt_scale = block.get_by_position(arguments[0]).type->get_scale(); |
124 | 2 | const auto tz_scale = block.get_by_position(result).type->get_scale(); |
125 | | |
126 | 10 | for (int i = 0; i < input_rows_count; ++i) { |
127 | 8 | DateV2Value<DateTimeV2ValueType> from_dt {col_from[i]}; |
128 | 8 | TimestampTzValue tz_value {}; |
129 | | |
130 | 8 | if (tz_value.from_datetime(from_dt, local_time_zone, dt_scale, tz_scale)) { |
131 | 7 | col_to_data[i] = tz_value; |
132 | 7 | } else { |
133 | 1 | col_null_map[i] = true; |
134 | 1 | } |
135 | 8 | } |
136 | | |
137 | 2 | block.get_by_position(result).column = |
138 | 2 | ColumnNullable::create(std::move(col_to), std::move(col_null)); |
139 | 2 | return Status::OK(); |
140 | 2 | } |
141 | | }; |
142 | | |
143 | | template <CastModeType CastMode> |
144 | | class CastToImpl<CastMode, DataTypeTimeStampNs, DataTypeTimeStampTz> : public CastToBase { |
145 | | public: |
146 | | Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments, |
147 | | uint32_t result, size_t input_rows_count, |
148 | 1 | const NullMap::value_type* null_map = nullptr) const override { |
149 | 1 | const auto& col_from = |
150 | 1 | assert_cast<const ColumnTimeStampNs&>(*block.get_by_position(arguments[0]).column) |
151 | 1 | .get_data(); |
152 | 1 | auto col_to = ColumnTimeStampTz::create(input_rows_count); |
153 | 1 | auto col_null = ColumnBool::create(input_rows_count, 0); |
154 | 1 | const auto& local_time_zone = context->state()->timezone_obj(); |
155 | 1 | const auto target_scale = block.get_by_position(result).type->get_scale(); |
156 | | |
157 | 6 | for (size_t i = 0; i < input_rows_count; ++i) { |
158 | 5 | if (null_map && null_map[i]) { |
159 | 0 | continue; |
160 | 0 | } |
161 | 5 | DateV2Value<DateTimeV2ValueType> rounded_datetime; |
162 | 5 | TimestampTzValue target; |
163 | 5 | const bool converted = |
164 | 5 | transform_date_scale(target_scale, TimeStampNsValue::FRACTIONAL_DIGITS, |
165 | 5 | rounded_datetime, col_from[i]) && |
166 | 5 | target.from_datetime(rounded_datetime, local_time_zone, target_scale, |
167 | 5 | target_scale); |
168 | 5 | if (!converted) { |
169 | 0 | if constexpr (CastMode == CastModeType::StrictMode) { |
170 | 0 | return Status::InvalidArgument( |
171 | 0 | "can not cast TIMESTAMP_NS {} to timestamptz in timezone {}", |
172 | 0 | col_from[i].to_string(), context->state()->timezone()); |
173 | 0 | } |
174 | 0 | col_null->get_data()[i] = true; |
175 | 0 | continue; |
176 | 0 | } |
177 | 5 | col_to->get_data()[i] = target; |
178 | 5 | } |
179 | | |
180 | 1 | if constexpr (CastMode == CastModeType::StrictMode) { |
181 | 0 | block.get_by_position(result).column = std::move(col_to); |
182 | 1 | } else { |
183 | 1 | block.get_by_position(result).column = |
184 | 1 | ColumnNullable::create(std::move(col_to), std::move(col_null)); |
185 | 1 | } |
186 | 1 | return Status::OK(); |
187 | 1 | } Unexecuted instantiation: _ZNK5doris10CastToImplILNS_12CastModeTypeE0ENS_19DataTypeTimeStampNsENS_19DataTypeTimeStampTzEE12execute_implEPNS_15FunctionContextERNS_5BlockERKSt6vectorIjSaIjEEjmPKh _ZNK5doris10CastToImplILNS_12CastModeTypeE1ENS_19DataTypeTimeStampNsENS_19DataTypeTimeStampTzEE12execute_implEPNS_15FunctionContextERNS_5BlockERKSt6vectorIjSaIjEEjmPKh Line | Count | Source | 148 | 1 | const NullMap::value_type* null_map = nullptr) const override { | 149 | 1 | const auto& col_from = | 150 | 1 | assert_cast<const ColumnTimeStampNs&>(*block.get_by_position(arguments[0]).column) | 151 | 1 | .get_data(); | 152 | 1 | auto col_to = ColumnTimeStampTz::create(input_rows_count); | 153 | 1 | auto col_null = ColumnBool::create(input_rows_count, 0); | 154 | 1 | const auto& local_time_zone = context->state()->timezone_obj(); | 155 | 1 | const auto target_scale = block.get_by_position(result).type->get_scale(); | 156 | | | 157 | 6 | for (size_t i = 0; i < input_rows_count; ++i) { | 158 | 5 | if (null_map && null_map[i]) { | 159 | 0 | continue; | 160 | 0 | } | 161 | 5 | DateV2Value<DateTimeV2ValueType> rounded_datetime; | 162 | 5 | TimestampTzValue target; | 163 | 5 | const bool converted = | 164 | 5 | transform_date_scale(target_scale, TimeStampNsValue::FRACTIONAL_DIGITS, | 165 | 5 | rounded_datetime, col_from[i]) && | 166 | 5 | target.from_datetime(rounded_datetime, local_time_zone, target_scale, | 167 | 5 | target_scale); | 168 | 5 | if (!converted) { | 169 | | if constexpr (CastMode == CastModeType::StrictMode) { | 170 | | return Status::InvalidArgument( | 171 | | "can not cast TIMESTAMP_NS {} to timestamptz in timezone {}", | 172 | | col_from[i].to_string(), context->state()->timezone()); | 173 | | } | 174 | 0 | col_null->get_data()[i] = true; | 175 | 0 | continue; | 176 | 0 | } | 177 | 5 | col_to->get_data()[i] = target; | 178 | 5 | } | 179 | | | 180 | | if constexpr (CastMode == CastModeType::StrictMode) { | 181 | | block.get_by_position(result).column = std::move(col_to); | 182 | 1 | } else { | 183 | 1 | block.get_by_position(result).column = | 184 | 1 | ColumnNullable::create(std::move(col_to), std::move(col_null)); | 185 | 1 | } | 186 | 1 | return Status::OK(); | 187 | 1 | } |
|
188 | | }; |
189 | | |
190 | | template <> |
191 | | class CastToImpl<CastModeType::StrictMode, DataTypeTimeStampTz, DataTypeTimeStampTz> |
192 | | : public CastToBase { |
193 | | public: |
194 | | Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments, |
195 | | uint32_t result, size_t input_rows_count, |
196 | 0 | const NullMap::value_type* null_map = nullptr) const override { |
197 | 0 | const auto& col_from = |
198 | 0 | assert_cast<const ColumnTimeStampTz&>(*block.get_by_position(arguments[0]).column) |
199 | 0 | .get_data(); |
200 | |
|
201 | 0 | auto col_to = ColumnTimeStampTz::create(input_rows_count); |
202 | 0 | auto& col_to_data = col_to->get_data(); |
203 | 0 | const auto& local_time_zone = context->state()->timezone_obj(); |
204 | |
|
205 | 0 | const auto from_scale = block.get_by_position(arguments[0]).type->get_scale(); |
206 | 0 | const auto to_scale = block.get_by_position(result).type->get_scale(); |
207 | |
|
208 | 0 | for (int i = 0; i < input_rows_count; ++i) { |
209 | 0 | if (null_map && null_map[i]) { |
210 | 0 | continue; |
211 | 0 | } |
212 | | |
213 | 0 | const auto& from_tz = col_from[i]; |
214 | 0 | auto& to_tz = col_to_data[i]; |
215 | |
|
216 | 0 | if (!transform_date_scale(to_scale, from_scale, to_tz, from_tz)) { |
217 | 0 | return Status::InternalError( |
218 | 0 | "can not cast from timestamptz : {} to timestamptz in timezone : {}", |
219 | 0 | TimestampTzValue {from_tz}.to_string(local_time_zone, from_scale), |
220 | 0 | context->state()->timezone()); |
221 | 0 | } |
222 | 0 | } |
223 | 0 | block.get_by_position(result).column = std::move(col_to); |
224 | 0 | return Status::OK(); |
225 | 0 | } |
226 | | }; |
227 | | |
228 | | template <> |
229 | | class CastToImpl<CastModeType::NonStrictMode, DataTypeTimeStampTz, DataTypeTimeStampTz> |
230 | | : public CastToBase { |
231 | | public: |
232 | | Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments, |
233 | | uint32_t result, size_t input_rows_count, |
234 | 0 | const NullMap::value_type*) const override { |
235 | 0 | const auto& col_from = |
236 | 0 | assert_cast<const ColumnTimeStampTz&>(*block.get_by_position(arguments[0]).column) |
237 | 0 | .get_data(); |
238 | |
|
239 | 0 | auto col_to = ColumnTimeStampTz::create(input_rows_count); |
240 | 0 | auto& col_to_data = col_to->get_data(); |
241 | 0 | auto col_null = ColumnBool::create(input_rows_count, 0); |
242 | 0 | auto& col_null_map = col_null->get_data(); |
243 | |
|
244 | 0 | const auto from_scale = block.get_by_position(arguments[0]).type->get_scale(); |
245 | 0 | const auto to_scale = block.get_by_position(result).type->get_scale(); |
246 | |
|
247 | 0 | for (int i = 0; i < input_rows_count; ++i) { |
248 | 0 | const auto& from_tz = col_from[i]; |
249 | 0 | auto& to_tz = col_to_data[i]; |
250 | |
|
251 | 0 | if (!transform_date_scale(to_scale, from_scale, to_tz, from_tz)) { |
252 | 0 | col_null_map[i] = true; |
253 | 0 | to_tz = TimestampTzValue(MIN_DATETIME_V2); |
254 | 0 | } |
255 | 0 | } |
256 | |
|
257 | 0 | block.get_by_position(result).column = |
258 | 0 | ColumnNullable::create(std::move(col_to), std::move(col_null)); |
259 | 0 | return Status::OK(); |
260 | 0 | } |
261 | | }; |
262 | | |
263 | | } // namespace doris |