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 | 58.7k | static inline int _fast_to_buffer(T value, char* buffer) { |
93 | 58.7k | char* end = nullptr; |
94 | | // output NaN and Infinity to be compatible with most of the implementations |
95 | 58.7k | if (std::isnan(value)) { |
96 | 14 | static constexpr char nan_str[] = "NaN"; |
97 | 14 | static constexpr int nan_str_len = sizeof(nan_str) - 1; |
98 | 14 | memcpy(buffer, nan_str, nan_str_len); |
99 | 14 | end = buffer + nan_str_len; |
100 | 58.7k | } else if (std::isinf(value)) { |
101 | 27 | static constexpr char inf_str[] = "Infinity"; |
102 | 27 | static constexpr int inf_str_len = sizeof(inf_str) - 1; |
103 | 27 | static constexpr char neg_inf_str[] = "-Infinity"; |
104 | 27 | static constexpr int neg_inf_str_len = sizeof(neg_inf_str) - 1; |
105 | 27 | if (value > 0) { |
106 | 14 | memcpy(buffer, inf_str, inf_str_len); |
107 | 14 | end = buffer + inf_str_len; |
108 | 14 | } else { |
109 | 13 | memcpy(buffer, neg_inf_str, neg_inf_str_len); |
110 | 13 | end = buffer + neg_inf_str_len; |
111 | 13 | } |
112 | 58.7k | } else { |
113 | 58.7k | if constexpr (std::is_same_v<T, float>) { |
114 | 27.6k | end = fmt::format_to(buffer, FMT_COMPILE("{:.{}g}"), value, |
115 | 27.6k | std::numeric_limits<float>::digits10 + 1); |
116 | 31.0k | } else { |
117 | 31.0k | end = fmt::format_to(buffer, FMT_COMPILE("{:.{}g}"), value, |
118 | 31.0k | std::numeric_limits<double>::digits10 + 1); |
119 | 31.0k | } |
120 | 58.7k | } |
121 | 58.7k | *end = '\0'; |
122 | 58.7k | return int(end - buffer); |
123 | 58.7k | } _ZN5doris12CastToString15_fast_to_bufferIfQoosr3stdE9is_same_vIT_fEsr3stdE9is_same_vIS2_dEEEiS2_Pc Line | Count | Source | 92 | 27.6k | static inline int _fast_to_buffer(T value, char* buffer) { | 93 | 27.6k | char* end = nullptr; | 94 | | // output NaN and Infinity to be compatible with most of the implementations | 95 | 27.6k | if (std::isnan(value)) { | 96 | 6 | static constexpr char nan_str[] = "NaN"; | 97 | 6 | static constexpr int nan_str_len = sizeof(nan_str) - 1; | 98 | 6 | memcpy(buffer, nan_str, nan_str_len); | 99 | 6 | end = buffer + nan_str_len; | 100 | 27.6k | } else if (std::isinf(value)) { | 101 | 12 | static constexpr char inf_str[] = "Infinity"; | 102 | 12 | static constexpr int inf_str_len = sizeof(inf_str) - 1; | 103 | 12 | static constexpr char neg_inf_str[] = "-Infinity"; | 104 | 12 | static constexpr int neg_inf_str_len = sizeof(neg_inf_str) - 1; | 105 | 12 | if (value > 0) { | 106 | 6 | memcpy(buffer, inf_str, inf_str_len); | 107 | 6 | end = buffer + inf_str_len; | 108 | 6 | } else { | 109 | 6 | memcpy(buffer, neg_inf_str, neg_inf_str_len); | 110 | 6 | end = buffer + neg_inf_str_len; | 111 | 6 | } | 112 | 27.6k | } else { | 113 | 27.6k | if constexpr (std::is_same_v<T, float>) { | 114 | 27.6k | end = fmt::format_to(buffer, FMT_COMPILE("{:.{}g}"), value, | 115 | 27.6k | 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 | 27.6k | } | 121 | 27.6k | *end = '\0'; | 122 | 27.6k | return int(end - buffer); | 123 | 27.6k | } |
_ZN5doris12CastToString15_fast_to_bufferIdQoosr3stdE9is_same_vIT_fEsr3stdE9is_same_vIS2_dEEEiS2_Pc Line | Count | Source | 92 | 31.0k | static inline int _fast_to_buffer(T value, char* buffer) { | 93 | 31.0k | char* end = nullptr; | 94 | | // output NaN and Infinity to be compatible with most of the implementations | 95 | 31.0k | if (std::isnan(value)) { | 96 | 8 | static constexpr char nan_str[] = "NaN"; | 97 | 8 | static constexpr int nan_str_len = sizeof(nan_str) - 1; | 98 | 8 | memcpy(buffer, nan_str, nan_str_len); | 99 | 8 | end = buffer + nan_str_len; | 100 | 31.0k | } else if (std::isinf(value)) { | 101 | 15 | static constexpr char inf_str[] = "Infinity"; | 102 | 15 | static constexpr int inf_str_len = sizeof(inf_str) - 1; | 103 | 15 | static constexpr char neg_inf_str[] = "-Infinity"; | 104 | 15 | static constexpr int neg_inf_str_len = sizeof(neg_inf_str) - 1; | 105 | 15 | if (value > 0) { | 106 | 8 | memcpy(buffer, inf_str, inf_str_len); | 107 | 8 | end = buffer + inf_str_len; | 108 | 8 | } else { | 109 | 7 | memcpy(buffer, neg_inf_str, neg_inf_str_len); | 110 | 7 | end = buffer + neg_inf_str_len; | 111 | 7 | } | 112 | 31.0k | } 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 | 31.0k | } else { | 117 | | end = fmt::format_to(buffer, FMT_COMPILE("{:.{}g}"), value, | 118 | 31.0k | std::numeric_limits<double>::digits10 + 1); | 119 | 31.0k | } | 120 | 31.0k | } | 121 | 31.0k | *end = '\0'; | 122 | 31.0k | return int(end - buffer); | 123 | 31.0k | } |
|
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 | 1 | inline std::string CastToString::from_number(const UInt8& num) { |
172 | 1 | auto f = fmt::format_int(num); |
173 | 1 | return std::string(f.data(), f.data() + f.size()); |
174 | 1 | } |
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 | 351 | inline void CastToString::push_number(const UInt8& num, BufferWritable& bw) { |
184 | 351 | auto f = fmt::format_int(num); |
185 | 351 | bw.write(f.data(), f.size()); |
186 | 351 | } |
187 | | |
188 | | // TINYINT |
189 | | template <> |
190 | 2.46k | inline std::string CastToString::from_number(const Int8& num) { |
191 | 2.46k | auto f = fmt::format_int(num); |
192 | 2.46k | return std::string(f.data(), f.data() + f.size()); |
193 | 2.46k | } |
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 | 6.66k | inline void CastToString::push_number(const Int8& num, BufferWritable& bw) { |
203 | 6.66k | auto f = fmt::format_int(num); |
204 | 6.66k | bw.write(f.data(), f.size()); |
205 | 6.66k | } |
206 | | |
207 | | // SMALLINT |
208 | | template <> |
209 | 30 | inline std::string CastToString::from_number(const Int16& num) { |
210 | 30 | auto f = fmt::format_int(num); |
211 | 30 | return std::string(f.data(), f.data() + f.size()); |
212 | 30 | } |
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 | 6.07k | inline void CastToString::push_number(const Int16& num, BufferWritable& bw) { |
222 | 6.07k | auto f = fmt::format_int(num); |
223 | 6.07k | bw.write(f.data(), f.size()); |
224 | 6.07k | } |
225 | | |
226 | | // INT |
227 | | template <> |
228 | 48.8k | inline std::string CastToString::from_number(const Int32& num) { |
229 | 48.8k | auto f = fmt::format_int(num); |
230 | 48.8k | return std::string(f.data(), f.data() + f.size()); |
231 | 48.8k | } |
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 | 9.41k | inline void CastToString::push_number(const Int32& num, BufferWritable& bw) { |
241 | 9.41k | auto f = fmt::format_int(num); |
242 | 9.41k | bw.write(f.data(), f.size()); |
243 | 9.41k | } |
244 | | |
245 | | // BIGINT |
246 | | template <> |
247 | 1.56k | inline std::string CastToString::from_number(const Int64& num) { |
248 | 1.56k | auto f = fmt::format_int(num); |
249 | 1.56k | return std::string(f.data(), f.data() + f.size()); |
250 | 1.56k | } |
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 | 15.4k | inline void CastToString::push_number(const Int64& num, BufferWritable& bw) { |
260 | 15.4k | auto f = fmt::format_int(num); |
261 | 15.4k | bw.write(f.data(), f.size()); |
262 | 15.4k | } |
263 | | |
264 | | // LARGEINT |
265 | | template <> |
266 | 1 | inline std::string CastToString::from_number(const Int128& num) { |
267 | 1 | fmt::memory_buffer buffer; |
268 | 1 | fmt::format_to(buffer, "{}", num); |
269 | 1 | return std::string(buffer.data(), buffer.size()); |
270 | 1 | } |
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 | 4.77k | inline void CastToString::push_number(const Int128& num, BufferWritable& bw) { |
281 | 4.77k | fmt::memory_buffer buffer; |
282 | 4.77k | fmt::format_to(buffer, "{}", num); |
283 | 4.77k | bw.write(buffer.data(), buffer.size()); |
284 | 4.77k | } |
285 | | |
286 | | // FLOAT |
287 | | |
288 | | template <> |
289 | 22.2k | inline std::string CastToString::from_number(const Float32& num) { |
290 | 22.2k | char buf[MAX_FLOAT_STR_LENGTH + 2]; |
291 | 22.2k | int len = _fast_to_buffer(num, buf); |
292 | 22.2k | return std::string(buf, buf + len); |
293 | 22.2k | } |
294 | | |
295 | | template <typename T> |
296 | | requires(std::is_same_v<T, Float32> || std::is_same_v<T, Float64>) |
297 | 81 | inline int CastToString::from_number(const T& from, char* buffer) { |
298 | 81 | return _fast_to_buffer(from, buffer); |
299 | 81 | } _ZN5doris12CastToString11from_numberIfQoosr3stdE9is_same_vIT_fEsr3stdE9is_same_vIS2_dEEEiRKS2_Pc Line | Count | Source | 297 | 38 | inline int CastToString::from_number(const T& from, char* buffer) { | 298 | 38 | return _fast_to_buffer(from, buffer); | 299 | 38 | } |
_ZN5doris12CastToString11from_numberIdQoosr3stdE9is_same_vIT_fEsr3stdE9is_same_vIS2_dEEEiRKS2_Pc Line | Count | Source | 297 | 43 | inline int CastToString::from_number(const T& from, char* buffer) { | 298 | 43 | return _fast_to_buffer(from, buffer); | 299 | 43 | } |
|
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 | 5.42k | inline void CastToString::push_number(const Float32& num, BufferWritable& bw) { |
310 | 5.42k | char buf[MAX_FLOAT_STR_LENGTH + 2]; |
311 | 5.42k | int len = _fast_to_buffer(num, buf); |
312 | 5.42k | bw.write(buf, len); |
313 | 5.42k | } |
314 | | |
315 | | // DOUBLE |
316 | | template <> |
317 | 25.0k | inline std::string CastToString::from_number(const Float64& num) { |
318 | 25.0k | char buf[MAX_DOUBLE_STR_LENGTH + 2]; |
319 | 25.0k | int len = _fast_to_buffer(num, buf); |
320 | 25.0k | return std::string(buf, len); |
321 | 25.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 | 6.02k | inline void CastToString::push_number(const Float64& num, BufferWritable& bw) { |
332 | 6.02k | char buf[MAX_DOUBLE_STR_LENGTH + 2]; |
333 | 6.02k | int len = _fast_to_buffer(num, buf); |
334 | 6.02k | bw.write(buf, len); |
335 | 6.02k | } |
336 | | |
337 | | // DECIMAL32 |
338 | | template <> |
339 | 1 | inline std::string CastToString::from_decimal(const Decimal32& from, UInt32 scale) { |
340 | 1 | return from.to_string(scale); |
341 | 1 | } |
342 | | |
343 | | template <> |
344 | 4.84k | inline void CastToString::push_decimal(const Decimal32& from, UInt32 scale, BufferWritable& bw) { |
345 | 4.84k | std::string str = from.to_string(scale); |
346 | 4.84k | bw.write(str.data(), str.size()); |
347 | 4.84k | } |
348 | | |
349 | | // DECIMAL64 |
350 | | template <> |
351 | 1 | inline std::string CastToString::from_decimal(const Decimal64& from, UInt32 scale) { |
352 | 1 | return from.to_string(scale); |
353 | 1 | } |
354 | | |
355 | | template <> |
356 | 12.6k | inline void CastToString::push_decimal(const Decimal64& from, UInt32 scale, BufferWritable& bw) { |
357 | 12.6k | std::string str = from.to_string(scale); |
358 | 12.6k | bw.write(str.data(), str.size()); |
359 | 12.6k | } |
360 | | |
361 | | // DECIMAL128 |
362 | | template <> |
363 | 1 | inline std::string CastToString::from_decimal(const Decimal128V3& from, UInt32 scale) { |
364 | 1 | return from.to_string(scale); |
365 | 1 | } |
366 | | |
367 | | template <> |
368 | 13.9k | inline void CastToString::push_decimal(const Decimal128V3& from, UInt32 scale, BufferWritable& bw) { |
369 | 13.9k | std::string str = from.to_string(scale); |
370 | 13.9k | bw.write(str.data(), str.size()); |
371 | 13.9k | } |
372 | | |
373 | | // DECIMAL256 |
374 | | template <> |
375 | 1 | inline std::string CastToString::from_decimal(const Decimal256& from, UInt32 scale) { |
376 | 1 | return from.to_string(scale); |
377 | 1 | } |
378 | | |
379 | | template <> |
380 | 14.7k | inline void CastToString::push_decimal(const Decimal256& from, UInt32 scale, BufferWritable& bw) { |
381 | 14.7k | std::string str = from.to_string(scale); |
382 | 14.7k | bw.write(str.data(), str.size()); |
383 | 14.7k | } |
384 | | |
385 | | // DECIMALV2 |
386 | | template <> |
387 | 1 | inline std::string CastToString::from_decimal(const Decimal128V2& from, UInt32 scale) { |
388 | 1 | auto value = (DecimalV2Value)from; |
389 | 1 | auto str = value.to_string(scale); |
390 | 1 | return str; |
391 | 1 | } |
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 | 8.14k | BufferWritable& bw) { |
403 | 8.14k | std::string str = from.to_string(scale); |
404 | 8.14k | bw.write(str.data(), str.size()); |
405 | 8.14k | } |
406 | | |
407 | | // DATEV1 DATETIMEV1 |
408 | 2 | inline std::string CastToString::from_date_or_datetime(const VecDateTimeValue& from) { |
409 | 2 | char buf[64]; |
410 | 2 | char* pos = from.to_string(buf); |
411 | | // DateTime to_string the end is /0 |
412 | 2 | return std::string(buf, pos - 1); |
413 | 2 | } |
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 | 6.55k | inline void CastToString::push_date_or_datetime(const VecDateTimeValue& from, BufferWritable& bw) { |
424 | 6.55k | char buf[64]; |
425 | 6.55k | char* pos = from.to_string(buf); |
426 | | // DateTime to_string the end is /0 |
427 | 6.55k | bw.write(buf, pos - buf - 1); |
428 | 6.55k | } |
429 | | |
430 | | // DATEV2 |
431 | 22 | inline std::string CastToString::from_datev2(const DateV2Value<DateV2ValueType>& from) { |
432 | 22 | char buf[64]; |
433 | 22 | char* pos = from.to_string(buf); |
434 | | // DateTime to_string the end is /0 |
435 | 22 | return std::string(buf, pos - 1); |
436 | 22 | } |
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 | 2.72k | BufferWritable& bw) { |
448 | 2.72k | char buf[64]; |
449 | 2.72k | char* pos = from.to_string(buf); |
450 | | // DateTime to_string the end is /0 |
451 | 2.72k | bw.write(buf, pos - buf - 1); |
452 | 2.72k | } |
453 | | |
454 | | // DATETIMEV2 |
455 | | inline std::string CastToString::from_datetimev2(const DateV2Value<DateTimeV2ValueType>& from, |
456 | 53 | UInt32 scale) { |
457 | 53 | char buf[64]; |
458 | 53 | char* pos = from.to_string(buf, scale); |
459 | | // DateTime to_string the end is /0 |
460 | 53 | return std::string(buf, pos - 1); |
461 | 53 | } |
462 | | |
463 | | inline std::string CastToString::from_timestamptz(const TimestampTzValue& from, UInt32 scale, |
464 | 23 | const cctz::time_zone* timezone) { |
465 | 23 | cctz::time_zone tz; |
466 | 23 | if (timezone == nullptr) { |
467 | 23 | tz = cctz::utc_time_zone(); |
468 | 23 | } else { |
469 | 0 | tz = *timezone; |
470 | 0 | } |
471 | 23 | return from.to_string(tz, scale); |
472 | 23 | } |
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 | 14.3k | UInt32 scale, BufferWritable& bw) { |
483 | 14.3k | char buf[64]; |
484 | 14.3k | char* pos = from.to_string(buf, scale); |
485 | | // DateTime to_string the end is /0 |
486 | 14.3k | bw.write(buf, pos - buf - 1); |
487 | 14.3k | } |
488 | | |
489 | | inline void CastToString::push_timestamptz(const TimestampTzValue& from, UInt32 scale, |
490 | | BufferWritable& bw, |
491 | 3 | const DataTypeSerDe::FormatOptions& options) { |
492 | 3 | auto str = from.to_string(*options.timezone, scale); |
493 | 3 | bw.write(str.data(), str.size()); |
494 | 3 | } |
495 | | |
496 | | // IPv4 |
497 | | template <> |
498 | 8 | inline std::string CastToString::from_ip(const IPv4& from) { |
499 | 8 | auto value = IPv4Value(from); |
500 | 8 | return value.to_string(); |
501 | 8 | } |
502 | | |
503 | | template <> |
504 | 10.1k | inline void CastToString::push_ip(const IPv4& from, BufferWritable& bw) { |
505 | 10.1k | auto value = IPv4Value(from); |
506 | 10.1k | std::string str = value.to_string(); |
507 | 10.1k | bw.write(str.data(), str.size()); |
508 | 10.1k | } |
509 | | |
510 | | //IPv6 |
511 | | |
512 | | template <> |
513 | 4 | inline std::string CastToString::from_ip(const IPv6& from) { |
514 | 4 | auto value = IPv6Value(from); |
515 | 4 | return value.to_string(); |
516 | 4 | } |
517 | | |
518 | | template <> |
519 | 7.17k | inline void CastToString::push_ip(const IPv6& from, BufferWritable& bw) { |
520 | 7.17k | auto value = IPv6Value(from); |
521 | 7.17k | std::string str = value.to_string(); |
522 | 7.17k | bw.write(str.data(), str.size()); |
523 | 7.17k | } |
524 | | |
525 | | // Time |
526 | 1 | inline std::string CastToString::from_time(const TimeValue::TimeType& from, UInt32 scale) { |
527 | 1 | return timev2_to_buffer_from_double(from, scale); |
528 | 1 | } |
529 | | |
530 | | inline void CastToString::push_time(const TimeValue::TimeType& from, UInt32 scale, |
531 | 258 | BufferWritable& bw) { |
532 | 258 | std::string str = timev2_to_buffer_from_double(from, scale); |
533 | 258 | bw.write(str.data(), str.size()); |
534 | 258 | } |
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 | 16 | const NullMap::value_type* null_map = nullptr) { |
542 | 16 | const auto& col_with_type_and_name = block.get_by_position(arguments[0]); |
543 | 16 | const IDataType& type = *col_with_type_and_name.type; |
544 | 16 | const IColumn& col_from = *col_with_type_and_name.column; |
545 | | |
546 | 16 | auto col_to = ColumnString::create(); |
547 | | |
548 | 16 | DataTypeSerDe::FormatOptions options; |
549 | 16 | auto time_zone = cctz::utc_time_zone(); |
550 | 16 | options.timezone = |
551 | 16 | (context && context->state()) ? &context->state()->timezone_obj() : &time_zone; |
552 | 16 | type.get_serde()->to_string_batch(col_from, *col_to, options); |
553 | | |
554 | 16 | block.replace_by_position(result, std::move(col_to)); |
555 | 16 | return Status::OK(); |
556 | 16 | } |
557 | | }; |
558 | | |
559 | | namespace CastWrapper { |
560 | | |
561 | 14 | inline WrapperType create_string_wrapper(const DataTypePtr& from_type) { |
562 | 14 | return [](FunctionContext* context, Block& block, const ColumnNumbers& arguments, |
563 | 14 | uint32_t result, size_t input_rows_count, |
564 | 14 | const NullMap::value_type* null_map = nullptr) { |
565 | 14 | return CastToStringFunction::execute_impl(context, block, arguments, result, |
566 | 14 | input_rows_count, null_map); |
567 | 14 | }; |
568 | 14 | } |
569 | | |
570 | | }; // namespace CastWrapper |
571 | | } // namespace doris |