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