be/src/exprs/function/cast/cast_to_string.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/data_type_serde/data_type_serde.h" |
21 | | #include "core/types.h" |
22 | | #include "core/value/time_value.h" |
23 | | #include "exprs/function/cast/cast_base.h" |
24 | | #include "util/mysql_global.h" |
25 | | #include "util/to_string.h" |
26 | | namespace doris { |
27 | | struct CastToString { |
28 | | template <class SRC> |
29 | | static inline std::string from_number(const SRC& from); |
30 | | |
31 | | // Caller is responsible for ensuring that `buffer` has enough space. |
32 | | template <class SRC> |
33 | | static inline int from_number(const SRC& from, char* buffer); |
34 | | |
35 | | template <typename T> |
36 | | requires(std::is_same_v<T, Float32> || std::is_same_v<T, Float64>) |
37 | | static inline int from_number(const T& from, char* buffer); |
38 | | |
39 | | template <class SRC> |
40 | | static inline void push_number(const SRC& from, ColumnString::Chars& chars); |
41 | | |
42 | | template <class SRC> |
43 | | static inline void push_number(const SRC& from, BufferWritable& bw); |
44 | | |
45 | | template <class SRC> |
46 | | static inline std::string from_decimal(const SRC& from, UInt32 scale); |
47 | | |
48 | | template <class SRC> |
49 | | static inline void push_decimal(const SRC& from, UInt32 scale, BufferWritable& bw); |
50 | | |
51 | | static inline std::string from_date_or_datetime(const VecDateTimeValue& from); |
52 | | |
53 | | static inline void push_date_or_datetime(const VecDateTimeValue& from, |
54 | | ColumnString::Chars& chars); |
55 | | static inline void push_date_or_datetime(const VecDateTimeValue& from, BufferWritable& bw); |
56 | | |
57 | | static inline std::string from_datev2(const DateV2Value<DateV2ValueType>& from); |
58 | | static inline void push_datev2(const DateV2Value<DateV2ValueType>& from, |
59 | | ColumnString::Chars& chars); |
60 | | static inline void push_datev2(const DateV2Value<DateV2ValueType>& from, BufferWritable& bw); |
61 | | |
62 | | static inline std::string from_datetimev2(const DateV2Value<DateTimeV2ValueType>& from, |
63 | | UInt32 scale); |
64 | | static inline std::string from_timestamptz(const TimestampTzValue& from, UInt32 scale, |
65 | | const cctz::time_zone* timezone = nullptr); |
66 | | static inline void push_datetimev2(const DateV2Value<DateTimeV2ValueType>& from, UInt32 scale, |
67 | | ColumnString::Chars& chars); |
68 | | |
69 | | static inline void push_datetimev2(const DateV2Value<DateTimeV2ValueType>& from, UInt32 scale, |
70 | | BufferWritable& bw); |
71 | | static inline void push_timestamptz(const TimestampTzValue& from, UInt32 scale, |
72 | | BufferWritable& bw, |
73 | | const DataTypeSerDe::FormatOptions& options); |
74 | | |
75 | | template <class SRC> |
76 | | static inline std::string from_ip(const SRC& from); |
77 | | |
78 | | template <class SRC> |
79 | | static inline void push_ip(const SRC& from, BufferWritable& bw); |
80 | | |
81 | | static inline std::string from_time(const TimeValue::TimeType& from, UInt32 scale); |
82 | | |
83 | | static inline void push_time(const TimeValue::TimeType& from, UInt32 scale, BufferWritable& bw); |
84 | | |
85 | | template <PrimitiveType T> |
86 | | static constexpr size_t string_length = 1; |
87 | | |
88 | | private: |
89 | | // refer to: https://en.cppreference.com/w/cpp/types/numeric_limits/max_digits10.html |
90 | | template <typename T> |
91 | | requires(std::is_same_v<T, float> || std::is_same_v<T, double>) |
92 | 117k | static inline int _fast_to_buffer(T value, char* buffer) { |
93 | 117k | char* end = nullptr; |
94 | | // output NaN and Infinity to be compatible with most of the implementations |
95 | 117k | if (std::isnan(value)) { |
96 | 28 | static constexpr char nan_str[] = "NaN"; |
97 | 28 | static constexpr int nan_str_len = sizeof(nan_str) - 1; |
98 | 28 | memcpy(buffer, nan_str, nan_str_len); |
99 | 28 | end = buffer + nan_str_len; |
100 | 117k | } else if (std::isinf(value)) { |
101 | 54 | static constexpr char inf_str[] = "Infinity"; |
102 | 54 | static constexpr int inf_str_len = sizeof(inf_str) - 1; |
103 | 54 | static constexpr char neg_inf_str[] = "-Infinity"; |
104 | 54 | static constexpr int neg_inf_str_len = sizeof(neg_inf_str) - 1; |
105 | 54 | if (value > 0) { |
106 | 28 | memcpy(buffer, inf_str, inf_str_len); |
107 | 28 | end = buffer + inf_str_len; |
108 | 28 | } else { |
109 | 26 | memcpy(buffer, neg_inf_str, neg_inf_str_len); |
110 | 26 | end = buffer + neg_inf_str_len; |
111 | 26 | } |
112 | 117k | } else { |
113 | 117k | if constexpr (std::is_same_v<T, float>) { |
114 | 55.3k | end = fmt::format_to(buffer, FMT_COMPILE("{:.{}g}"), value, |
115 | 55.3k | std::numeric_limits<float>::digits10 + 1); |
116 | 62.1k | } else { |
117 | 62.1k | end = fmt::format_to(buffer, FMT_COMPILE("{:.{}g}"), value, |
118 | 62.1k | std::numeric_limits<double>::digits10 + 1); |
119 | 62.1k | } |
120 | 117k | } |
121 | 117k | *end = '\0'; |
122 | 117k | return int(end - buffer); |
123 | 117k | } _ZN5doris12CastToString15_fast_to_bufferIfQoosr3stdE9is_same_vIT_fEsr3stdE9is_same_vIS2_dEEEiS2_Pc Line | Count | Source | 92 | 55.3k | static inline int _fast_to_buffer(T value, char* buffer) { | 93 | 55.3k | char* end = nullptr; | 94 | | // output NaN and Infinity to be compatible with most of the implementations | 95 | 55.3k | if (std::isnan(value)) { | 96 | 12 | static constexpr char nan_str[] = "NaN"; | 97 | 12 | static constexpr int nan_str_len = sizeof(nan_str) - 1; | 98 | 12 | memcpy(buffer, nan_str, nan_str_len); | 99 | 12 | end = buffer + nan_str_len; | 100 | 55.3k | } else if (std::isinf(value)) { | 101 | 24 | static constexpr char inf_str[] = "Infinity"; | 102 | 24 | static constexpr int inf_str_len = sizeof(inf_str) - 1; | 103 | 24 | static constexpr char neg_inf_str[] = "-Infinity"; | 104 | 24 | static constexpr int neg_inf_str_len = sizeof(neg_inf_str) - 1; | 105 | 24 | if (value > 0) { | 106 | 12 | memcpy(buffer, inf_str, inf_str_len); | 107 | 12 | end = buffer + inf_str_len; | 108 | 12 | } else { | 109 | 12 | memcpy(buffer, neg_inf_str, neg_inf_str_len); | 110 | 12 | end = buffer + neg_inf_str_len; | 111 | 12 | } | 112 | 55.3k | } else { | 113 | 55.3k | if constexpr (std::is_same_v<T, float>) { | 114 | 55.3k | end = fmt::format_to(buffer, FMT_COMPILE("{:.{}g}"), value, | 115 | 55.3k | std::numeric_limits<float>::digits10 + 1); | 116 | | } else { | 117 | | end = fmt::format_to(buffer, FMT_COMPILE("{:.{}g}"), value, | 118 | | std::numeric_limits<double>::digits10 + 1); | 119 | | } | 120 | 55.3k | } | 121 | 55.3k | *end = '\0'; | 122 | 55.3k | return int(end - buffer); | 123 | 55.3k | } |
_ZN5doris12CastToString15_fast_to_bufferIdQoosr3stdE9is_same_vIT_fEsr3stdE9is_same_vIS2_dEEEiS2_Pc Line | Count | Source | 92 | 62.1k | static inline int _fast_to_buffer(T value, char* buffer) { | 93 | 62.1k | char* end = nullptr; | 94 | | // output NaN and Infinity to be compatible with most of the implementations | 95 | 62.1k | if (std::isnan(value)) { | 96 | 16 | static constexpr char nan_str[] = "NaN"; | 97 | 16 | static constexpr int nan_str_len = sizeof(nan_str) - 1; | 98 | 16 | memcpy(buffer, nan_str, nan_str_len); | 99 | 16 | end = buffer + nan_str_len; | 100 | 62.1k | } else if (std::isinf(value)) { | 101 | 30 | static constexpr char inf_str[] = "Infinity"; | 102 | 30 | static constexpr int inf_str_len = sizeof(inf_str) - 1; | 103 | 30 | static constexpr char neg_inf_str[] = "-Infinity"; | 104 | 30 | static constexpr int neg_inf_str_len = sizeof(neg_inf_str) - 1; | 105 | 30 | if (value > 0) { | 106 | 16 | memcpy(buffer, inf_str, inf_str_len); | 107 | 16 | end = buffer + inf_str_len; | 108 | 16 | } else { | 109 | 14 | memcpy(buffer, neg_inf_str, neg_inf_str_len); | 110 | 14 | end = buffer + neg_inf_str_len; | 111 | 14 | } | 112 | 62.1k | } else { | 113 | | if constexpr (std::is_same_v<T, float>) { | 114 | | end = fmt::format_to(buffer, FMT_COMPILE("{:.{}g}"), value, | 115 | | std::numeric_limits<float>::digits10 + 1); | 116 | 62.1k | } else { | 117 | | end = fmt::format_to(buffer, FMT_COMPILE("{:.{}g}"), value, | 118 | 62.1k | std::numeric_limits<double>::digits10 + 1); | 119 | 62.1k | } | 120 | 62.1k | } | 121 | 62.1k | *end = '\0'; | 122 | 62.1k | return int(end - buffer); | 123 | 62.1k | } |
|
124 | | }; |
125 | | |
126 | | template <> |
127 | | constexpr size_t CastToString::string_length<TYPE_BOOLEAN> = 1; |
128 | | template <> |
129 | | constexpr size_t CastToString::string_length<TYPE_TINYINT> = 4; |
130 | | template <> |
131 | | constexpr size_t CastToString::string_length<TYPE_SMALLINT> = 6; |
132 | | template <> |
133 | | constexpr size_t CastToString::string_length<TYPE_INT> = 11; |
134 | | template <> |
135 | | constexpr size_t CastToString::string_length<TYPE_BIGINT> = 20; |
136 | | template <> |
137 | | constexpr size_t CastToString::string_length<TYPE_LARGEINT> = 40; |
138 | | template <> |
139 | | constexpr size_t CastToString::string_length<TYPE_FLOAT> = MAX_FLOAT_STR_LENGTH; |
140 | | template <> |
141 | | constexpr size_t CastToString::string_length<TYPE_DOUBLE> = MAX_DOUBLE_STR_LENGTH; |
142 | | template <> |
143 | | constexpr size_t CastToString::string_length<TYPE_DECIMAL32> = 14; |
144 | | template <> |
145 | | constexpr size_t CastToString::string_length<TYPE_DECIMAL64> = 24; |
146 | | template <> |
147 | | constexpr size_t CastToString::string_length<TYPE_DECIMALV2> = 24; |
148 | | template <> |
149 | | constexpr size_t CastToString::string_length<TYPE_DECIMAL128I> = 39; |
150 | | template <> |
151 | | constexpr size_t CastToString::string_length<TYPE_DECIMAL256> = 78; |
152 | | template <> |
153 | | constexpr size_t CastToString::string_length<TYPE_DATE> = sizeof("YYYY-MM-DD") - 1; |
154 | | template <> |
155 | | constexpr size_t CastToString::string_length<TYPE_DATETIME> = sizeof("YYYY-MM-DD HH:MM:SS") - 1; |
156 | | template <> |
157 | | constexpr size_t CastToString::string_length<TYPE_DATEV2> = sizeof("YYYY-MM-DD") - 1; |
158 | | template <> |
159 | | constexpr size_t CastToString::string_length<TYPE_DATETIMEV2> = |
160 | | sizeof("YYYY-MM-DD HH:MM:SS.ssssss") - 1; |
161 | | template <> |
162 | | constexpr size_t CastToString::string_length<TYPE_TIMEV2> = sizeof("-838:59:59.999999") - 1; |
163 | | template <> |
164 | | constexpr size_t CastToString::string_length<TYPE_IPV4> = sizeof("255.255 .255.255") - 1; |
165 | | template <> |
166 | | constexpr size_t CastToString::string_length<TYPE_IPV6> = |
167 | | sizeof("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff") - 1; |
168 | | |
169 | | // BOOLEAN |
170 | | template <> |
171 | 2 | inline std::string CastToString::from_number(const UInt8& num) { |
172 | 2 | auto f = fmt::format_int(num); |
173 | 2 | return std::string(f.data(), f.data() + f.size()); |
174 | 2 | } |
175 | | |
176 | | template <> |
177 | 0 | inline void CastToString::push_number(const UInt8& num, ColumnString::Chars& chars) { |
178 | 0 | auto f = fmt::format_int(num); |
179 | 0 | chars.insert(f.data(), f.data() + f.size()); |
180 | 0 | } |
181 | | |
182 | | template <> |
183 | 702 | inline void CastToString::push_number(const UInt8& num, BufferWritable& bw) { |
184 | 702 | auto f = fmt::format_int(num); |
185 | 702 | bw.write(f.data(), f.size()); |
186 | 702 | } |
187 | | |
188 | | // TINYINT |
189 | | template <> |
190 | 4.92k | inline std::string CastToString::from_number(const Int8& num) { |
191 | 4.92k | auto f = fmt::format_int(num); |
192 | 4.92k | return std::string(f.data(), f.data() + f.size()); |
193 | 4.92k | } |
194 | | |
195 | | template <> |
196 | 0 | inline void CastToString::push_number(const Int8& num, ColumnString::Chars& chars) { |
197 | 0 | auto f = fmt::format_int(num); |
198 | 0 | chars.insert(f.data(), f.data() + f.size()); |
199 | 0 | } |
200 | | |
201 | | template <> |
202 | 13.3k | inline void CastToString::push_number(const Int8& num, BufferWritable& bw) { |
203 | 13.3k | auto f = fmt::format_int(num); |
204 | 13.3k | bw.write(f.data(), f.size()); |
205 | 13.3k | } |
206 | | |
207 | | // SMALLINT |
208 | | template <> |
209 | 60 | inline std::string CastToString::from_number(const Int16& num) { |
210 | 60 | auto f = fmt::format_int(num); |
211 | 60 | return std::string(f.data(), f.data() + f.size()); |
212 | 60 | } |
213 | | |
214 | | template <> |
215 | 0 | inline void CastToString::push_number(const Int16& num, ColumnString::Chars& chars) { |
216 | 0 | auto f = fmt::format_int(num); |
217 | 0 | chars.insert(f.data(), f.data() + f.size()); |
218 | 0 | } |
219 | | |
220 | | template <> |
221 | 12.1k | inline void CastToString::push_number(const Int16& num, BufferWritable& bw) { |
222 | 12.1k | auto f = fmt::format_int(num); |
223 | 12.1k | bw.write(f.data(), f.size()); |
224 | 12.1k | } |
225 | | |
226 | | // INT |
227 | | template <> |
228 | 97.7k | inline std::string CastToString::from_number(const Int32& num) { |
229 | 97.7k | auto f = fmt::format_int(num); |
230 | 97.7k | return std::string(f.data(), f.data() + f.size()); |
231 | 97.7k | } |
232 | | |
233 | | template <> |
234 | 0 | inline void CastToString::push_number(const Int32& num, ColumnString::Chars& chars) { |
235 | 0 | auto f = fmt::format_int(num); |
236 | 0 | chars.insert(f.data(), f.data() + f.size()); |
237 | 0 | } |
238 | | |
239 | | template <> |
240 | 18.8k | inline void CastToString::push_number(const Int32& num, BufferWritable& bw) { |
241 | 18.8k | auto f = fmt::format_int(num); |
242 | 18.8k | bw.write(f.data(), f.size()); |
243 | 18.8k | } |
244 | | |
245 | | // BIGINT |
246 | | template <> |
247 | 3.13k | inline std::string CastToString::from_number(const Int64& num) { |
248 | 3.13k | auto f = fmt::format_int(num); |
249 | 3.13k | return std::string(f.data(), f.data() + f.size()); |
250 | 3.13k | } |
251 | | |
252 | | template <> |
253 | 0 | inline void CastToString::push_number(const Int64& num, ColumnString::Chars& chars) { |
254 | 0 | auto f = fmt::format_int(num); |
255 | 0 | chars.insert(f.data(), f.data() + f.size()); |
256 | 0 | } |
257 | | |
258 | | template <> |
259 | 30.8k | inline void CastToString::push_number(const Int64& num, BufferWritable& bw) { |
260 | 30.8k | auto f = fmt::format_int(num); |
261 | 30.8k | bw.write(f.data(), f.size()); |
262 | 30.8k | } |
263 | | |
264 | | // LARGEINT |
265 | | template <> |
266 | 2 | inline std::string CastToString::from_number(const Int128& num) { |
267 | 2 | fmt::memory_buffer buffer; |
268 | 2 | fmt::format_to(buffer, "{}", num); |
269 | 2 | return std::string(buffer.data(), buffer.size()); |
270 | 2 | } |
271 | | |
272 | | template <> |
273 | 0 | inline void CastToString::push_number(const Int128& num, ColumnString::Chars& chars) { |
274 | 0 | fmt::memory_buffer buffer; |
275 | 0 | fmt::format_to(buffer, "{}", num); |
276 | 0 | chars.insert(buffer.data(), buffer.data() + buffer.size()); |
277 | 0 | } |
278 | | |
279 | | template <> |
280 | 9.55k | inline void CastToString::push_number(const Int128& num, BufferWritable& bw) { |
281 | 9.55k | fmt::memory_buffer buffer; |
282 | 9.55k | fmt::format_to(buffer, "{}", num); |
283 | 9.55k | bw.write(buffer.data(), buffer.size()); |
284 | 9.55k | } |
285 | | |
286 | | // FLOAT |
287 | | |
288 | | template <> |
289 | 44.4k | inline std::string CastToString::from_number(const Float32& num) { |
290 | 44.4k | char buf[MAX_FLOAT_STR_LENGTH + 2]; |
291 | 44.4k | int len = _fast_to_buffer(num, buf); |
292 | 44.4k | return std::string(buf, buf + len); |
293 | 44.4k | } |
294 | | |
295 | | template <typename T> |
296 | | requires(std::is_same_v<T, Float32> || std::is_same_v<T, Float64>) |
297 | 162 | inline int CastToString::from_number(const T& from, char* buffer) { |
298 | 162 | return _fast_to_buffer(from, buffer); |
299 | 162 | } _ZN5doris12CastToString11from_numberIfQoosr3stdE9is_same_vIT_fEsr3stdE9is_same_vIS2_dEEEiRKS2_Pc Line | Count | Source | 297 | 76 | inline int CastToString::from_number(const T& from, char* buffer) { | 298 | 76 | return _fast_to_buffer(from, buffer); | 299 | 76 | } |
_ZN5doris12CastToString11from_numberIdQoosr3stdE9is_same_vIT_fEsr3stdE9is_same_vIS2_dEEEiRKS2_Pc Line | Count | Source | 297 | 86 | inline int CastToString::from_number(const T& from, char* buffer) { | 298 | 86 | return _fast_to_buffer(from, buffer); | 299 | 86 | } |
|
300 | | |
301 | | template <> |
302 | 0 | inline void CastToString::push_number(const Float32& num, ColumnString::Chars& chars) { |
303 | 0 | char buf[MAX_FLOAT_STR_LENGTH + 2]; |
304 | 0 | int len = _fast_to_buffer(num, buf); |
305 | 0 | chars.insert(buf, buf + len); |
306 | 0 | } |
307 | | |
308 | | template <> |
309 | 10.8k | inline void CastToString::push_number(const Float32& num, BufferWritable& bw) { |
310 | 10.8k | char buf[MAX_FLOAT_STR_LENGTH + 2]; |
311 | 10.8k | int len = _fast_to_buffer(num, buf); |
312 | 10.8k | bw.write(buf, len); |
313 | 10.8k | } |
314 | | |
315 | | // DOUBLE |
316 | | template <> |
317 | 50.0k | inline std::string CastToString::from_number(const Float64& num) { |
318 | 50.0k | char buf[MAX_DOUBLE_STR_LENGTH + 2]; |
319 | 50.0k | int len = _fast_to_buffer(num, buf); |
320 | 50.0k | return std::string(buf, len); |
321 | 50.0k | } |
322 | | |
323 | | template <> |
324 | 0 | inline void CastToString::push_number(const Float64& num, ColumnString::Chars& chars) { |
325 | 0 | char buf[MAX_DOUBLE_STR_LENGTH + 2]; |
326 | 0 | int len = _fast_to_buffer(num, buf); |
327 | 0 | chars.insert(buf, buf + len); |
328 | 0 | } |
329 | | |
330 | | template <> |
331 | 12.0k | inline void CastToString::push_number(const Float64& num, BufferWritable& bw) { |
332 | 12.0k | char buf[MAX_DOUBLE_STR_LENGTH + 2]; |
333 | 12.0k | int len = _fast_to_buffer(num, buf); |
334 | 12.0k | bw.write(buf, len); |
335 | 12.0k | } |
336 | | |
337 | | // DECIMAL32 |
338 | | template <> |
339 | 2 | inline std::string CastToString::from_decimal(const Decimal32& from, UInt32 scale) { |
340 | 2 | return from.to_string(scale); |
341 | 2 | } |
342 | | |
343 | | template <> |
344 | 9.69k | inline void CastToString::push_decimal(const Decimal32& from, UInt32 scale, BufferWritable& bw) { |
345 | 9.69k | std::string str = from.to_string(scale); |
346 | 9.69k | bw.write(str.data(), str.size()); |
347 | 9.69k | } |
348 | | |
349 | | // DECIMAL64 |
350 | | template <> |
351 | 2 | inline std::string CastToString::from_decimal(const Decimal64& from, UInt32 scale) { |
352 | 2 | return from.to_string(scale); |
353 | 2 | } |
354 | | |
355 | | template <> |
356 | 25.2k | inline void CastToString::push_decimal(const Decimal64& from, UInt32 scale, BufferWritable& bw) { |
357 | 25.2k | std::string str = from.to_string(scale); |
358 | 25.2k | bw.write(str.data(), str.size()); |
359 | 25.2k | } |
360 | | |
361 | | // DECIMAL128 |
362 | | template <> |
363 | 2 | inline std::string CastToString::from_decimal(const Decimal128V3& from, UInt32 scale) { |
364 | 2 | return from.to_string(scale); |
365 | 2 | } |
366 | | |
367 | | template <> |
368 | 27.9k | inline void CastToString::push_decimal(const Decimal128V3& from, UInt32 scale, BufferWritable& bw) { |
369 | 27.9k | std::string str = from.to_string(scale); |
370 | 27.9k | bw.write(str.data(), str.size()); |
371 | 27.9k | } |
372 | | |
373 | | // DECIMAL256 |
374 | | template <> |
375 | 2 | inline std::string CastToString::from_decimal(const Decimal256& from, UInt32 scale) { |
376 | 2 | return from.to_string(scale); |
377 | 2 | } |
378 | | |
379 | | template <> |
380 | 29.4k | inline void CastToString::push_decimal(const Decimal256& from, UInt32 scale, BufferWritable& bw) { |
381 | 29.4k | std::string str = from.to_string(scale); |
382 | 29.4k | bw.write(str.data(), str.size()); |
383 | 29.4k | } |
384 | | |
385 | | // DECIMALV2 |
386 | | template <> |
387 | 2 | inline std::string CastToString::from_decimal(const Decimal128V2& from, UInt32 scale) { |
388 | 2 | auto value = (DecimalV2Value)from; |
389 | 2 | auto str = value.to_string(scale); |
390 | 2 | return str; |
391 | 2 | } |
392 | | |
393 | | template <> |
394 | 0 | inline void CastToString::push_decimal(const Decimal128V2& from, UInt32 scale, BufferWritable& bw) { |
395 | 0 | auto value = (DecimalV2Value)from; |
396 | 0 | std::string str = value.to_string(scale); |
397 | 0 | bw.write(str.data(), str.size()); |
398 | 0 | } |
399 | | |
400 | | template <> |
401 | | inline void CastToString::push_decimal(const DecimalV2Value& from, UInt32 scale, |
402 | 16.2k | BufferWritable& bw) { |
403 | 16.2k | std::string str = from.to_string(scale); |
404 | 16.2k | bw.write(str.data(), str.size()); |
405 | 16.2k | } |
406 | | |
407 | | // DATEV1 DATETIMEV1 |
408 | 4 | inline std::string CastToString::from_date_or_datetime(const VecDateTimeValue& from) { |
409 | 4 | char buf[64]; |
410 | 4 | char* pos = from.to_string(buf); |
411 | | // DateTime to_string the end is /0 |
412 | 4 | return std::string(buf, pos - 1); |
413 | 4 | } |
414 | | |
415 | | inline void CastToString::push_date_or_datetime(const VecDateTimeValue& from, |
416 | 0 | ColumnString::Chars& chars) { |
417 | 0 | char buf[64]; |
418 | 0 | char* pos = from.to_string(buf); |
419 | 0 | // DateTime to_string the end is /0 |
420 | 0 | chars.insert(buf, pos - 1); |
421 | 0 | } |
422 | | |
423 | 13.1k | inline void CastToString::push_date_or_datetime(const VecDateTimeValue& from, BufferWritable& bw) { |
424 | 13.1k | char buf[64]; |
425 | 13.1k | char* pos = from.to_string(buf); |
426 | | // DateTime to_string the end is /0 |
427 | 13.1k | bw.write(buf, pos - buf - 1); |
428 | 13.1k | } |
429 | | |
430 | | // DATEV2 |
431 | 44 | inline std::string CastToString::from_datev2(const DateV2Value<DateV2ValueType>& from) { |
432 | 44 | char buf[64]; |
433 | 44 | char* pos = from.to_string(buf); |
434 | | // DateTime to_string the end is /0 |
435 | 44 | return std::string(buf, pos - 1); |
436 | 44 | } |
437 | | |
438 | | inline void CastToString::push_datev2(const DateV2Value<DateV2ValueType>& from, |
439 | 0 | ColumnString::Chars& chars) { |
440 | 0 | char buf[64]; |
441 | 0 | char* pos = from.to_string(buf); |
442 | 0 | // DateTime to_string the end is /0 |
443 | 0 | chars.insert(buf, pos - 1); |
444 | 0 | } |
445 | | |
446 | | inline void CastToString::push_datev2(const DateV2Value<DateV2ValueType>& from, |
447 | 5.44k | BufferWritable& bw) { |
448 | 5.44k | char buf[64]; |
449 | 5.44k | char* pos = from.to_string(buf); |
450 | | // DateTime to_string the end is /0 |
451 | 5.44k | bw.write(buf, pos - buf - 1); |
452 | 5.44k | } |
453 | | |
454 | | // DATETIMEV2 |
455 | | inline std::string CastToString::from_datetimev2(const DateV2Value<DateTimeV2ValueType>& from, |
456 | 106 | UInt32 scale) { |
457 | 106 | char buf[64]; |
458 | 106 | char* pos = from.to_string(buf, scale); |
459 | | // DateTime to_string the end is /0 |
460 | 106 | return std::string(buf, pos - 1); |
461 | 106 | } |
462 | | |
463 | | inline std::string CastToString::from_timestamptz(const TimestampTzValue& from, UInt32 scale, |
464 | 46 | const cctz::time_zone* timezone) { |
465 | 46 | cctz::time_zone tz; |
466 | 46 | if (timezone == nullptr) { |
467 | 46 | tz = cctz::utc_time_zone(); |
468 | 46 | } else { |
469 | 0 | tz = *timezone; |
470 | 0 | } |
471 | 46 | return from.to_string(tz, scale); |
472 | 46 | } |
473 | | inline void CastToString::push_datetimev2(const DateV2Value<DateTimeV2ValueType>& from, |
474 | 0 | UInt32 scale, ColumnString::Chars& chars) { |
475 | 0 | char buf[64]; |
476 | 0 | char* pos = from.to_string(buf, scale); |
477 | 0 | // DateTime to_string the end is /0 |
478 | 0 | chars.insert(buf, pos - 1); |
479 | 0 | } |
480 | | |
481 | | inline void CastToString::push_datetimev2(const DateV2Value<DateTimeV2ValueType>& from, |
482 | 28.6k | UInt32 scale, BufferWritable& bw) { |
483 | 28.6k | char buf[64]; |
484 | 28.6k | char* pos = from.to_string(buf, scale); |
485 | | // DateTime to_string the end is /0 |
486 | 28.6k | bw.write(buf, pos - buf - 1); |
487 | 28.6k | } |
488 | | |
489 | | inline void CastToString::push_timestamptz(const TimestampTzValue& from, UInt32 scale, |
490 | | BufferWritable& bw, |
491 | 6 | const DataTypeSerDe::FormatOptions& options) { |
492 | 6 | auto str = from.to_string(*options.timezone, scale); |
493 | 6 | bw.write(str.data(), str.size()); |
494 | 6 | } |
495 | | |
496 | | // IPv4 |
497 | | template <> |
498 | 16 | inline std::string CastToString::from_ip(const IPv4& from) { |
499 | 16 | auto value = IPv4Value(from); |
500 | 16 | return value.to_string(); |
501 | 16 | } |
502 | | |
503 | | template <> |
504 | 20.3k | inline void CastToString::push_ip(const IPv4& from, BufferWritable& bw) { |
505 | 20.3k | auto value = IPv4Value(from); |
506 | 20.3k | std::string str = value.to_string(); |
507 | 20.3k | bw.write(str.data(), str.size()); |
508 | 20.3k | } |
509 | | |
510 | | //IPv6 |
511 | | |
512 | | template <> |
513 | 8 | inline std::string CastToString::from_ip(const IPv6& from) { |
514 | 8 | auto value = IPv6Value(from); |
515 | 8 | return value.to_string(); |
516 | 8 | } |
517 | | |
518 | | template <> |
519 | 14.3k | inline void CastToString::push_ip(const IPv6& from, BufferWritable& bw) { |
520 | 14.3k | auto value = IPv6Value(from); |
521 | 14.3k | std::string str = value.to_string(); |
522 | 14.3k | bw.write(str.data(), str.size()); |
523 | 14.3k | } |
524 | | |
525 | | // Time |
526 | 2 | inline std::string CastToString::from_time(const TimeValue::TimeType& from, UInt32 scale) { |
527 | 2 | return timev2_to_buffer_from_double(from, scale); |
528 | 2 | } |
529 | | |
530 | | inline void CastToString::push_time(const TimeValue::TimeType& from, UInt32 scale, |
531 | 516 | BufferWritable& bw) { |
532 | 516 | std::string str = timev2_to_buffer_from_double(from, scale); |
533 | 516 | bw.write(str.data(), str.size()); |
534 | 516 | } |
535 | | |
536 | | class CastToStringFunction { |
537 | | public: |
538 | | static Status execute_impl(FunctionContext* context, Block& block, |
539 | | const ColumnNumbers& arguments, uint32_t result, |
540 | | size_t input_rows_count, |
541 | 32 | const NullMap::value_type* null_map = nullptr) { |
542 | 32 | const auto& col_with_type_and_name = block.get_by_position(arguments[0]); |
543 | 32 | const IDataType& type = *col_with_type_and_name.type; |
544 | 32 | const IColumn& col_from = *col_with_type_and_name.column; |
545 | | |
546 | 32 | auto col_to = ColumnString::create(); |
547 | | |
548 | 32 | DataTypeSerDe::FormatOptions options; |
549 | 32 | auto time_zone = cctz::utc_time_zone(); |
550 | 32 | options.timezone = |
551 | 32 | (context && context->state()) ? &context->state()->timezone_obj() : &time_zone; |
552 | 32 | type.get_serde()->to_string_batch(col_from, *col_to, options); |
553 | | |
554 | 32 | block.replace_by_position(result, std::move(col_to)); |
555 | 32 | return Status::OK(); |
556 | 32 | } |
557 | | }; |
558 | | |
559 | | namespace CastWrapper { |
560 | | |
561 | 28 | inline WrapperType create_string_wrapper(const DataTypePtr& from_type) { |
562 | 28 | return [](FunctionContext* context, Block& block, const ColumnNumbers& arguments, |
563 | 28 | uint32_t result, size_t input_rows_count, |
564 | 28 | const NullMap::value_type* null_map = nullptr) { |
565 | 28 | return CastToStringFunction::execute_impl(context, block, arguments, result, |
566 | 28 | input_rows_count, null_map); |
567 | 28 | }; |
568 | 28 | } |
569 | | |
570 | | }; // namespace CastWrapper |
571 | | } // namespace doris |