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