Coverage Report

Created: 2026-07-28 23:41

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