Coverage Report

Created: 2026-09-10 04:38

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 "core/value/timestamp_ns_value.h"
24
#include "exprs/function/cast/cast_base.h"
25
#include "runtime/runtime_state.h"
26
#include "util/mysql_global.h"
27
#include "util/to_string.h"
28
namespace doris {
29
struct CastToString {
30
    static inline std::string from_int128(int128_t value);
31
    static inline std::string from_uint128(uint128_t value);
32
    static inline std::string from_uint128(UInt128 value);
33
34
    template <class SRC>
35
    static inline std::string from_number(const SRC& from);
36
37
    // Caller is responsible for ensuring that `buffer` has enough space.
38
    template <class SRC>
39
    static inline int from_number(const SRC& from, char* buffer);
40
41
    template <typename T>
42
        requires(std::is_same_v<T, Float32> || std::is_same_v<T, Float64>)
43
    static inline int from_number(const T& from, char* buffer);
44
45
    template <class SRC>
46
    static inline void push_number(const SRC& from, ColumnString::Chars& chars);
47
48
    template <class SRC>
49
    static inline void push_number(const SRC& from, BufferWritable& bw);
50
51
    template <class SRC>
52
    static inline std::string from_decimal(const SRC& from, UInt32 scale);
53
54
    template <class SRC>
55
    static inline void push_decimal(const SRC& from, UInt32 scale, BufferWritable& bw);
56
57
    static inline std::string from_date_or_datetime(const VecDateTimeValue& from);
58
59
    static inline void push_date_or_datetime(const VecDateTimeValue& from,
60
                                             ColumnString::Chars& chars);
61
    static inline void push_date_or_datetime(const VecDateTimeValue& from, BufferWritable& bw);
62
63
    static inline std::string from_datev2(const DateV2Value<DateV2ValueType>& from);
64
    static inline void push_datev2(const DateV2Value<DateV2ValueType>& from,
65
                                   ColumnString::Chars& chars);
66
    static inline void push_datev2(const DateV2Value<DateV2ValueType>& from, BufferWritable& bw);
67
68
    static inline std::string from_datetimev2(const DateV2Value<DateTimeV2ValueType>& from,
69
                                              UInt32 scale);
70
    static inline std::string from_timestamp_ns(const TimeStampNsValue& from);
71
    static inline std::string from_timestamptz(const TimestampTzValue& from, UInt32 scale,
72
                                               const cctz::time_zone* timezone = nullptr);
73
    static inline void push_datetimev2(const DateV2Value<DateTimeV2ValueType>& from, UInt32 scale,
74
                                       ColumnString::Chars& chars);
75
76
    static inline void push_datetimev2(const DateV2Value<DateTimeV2ValueType>& from, UInt32 scale,
77
                                       BufferWritable& bw);
78
    static inline void push_timestamp_ns(const TimeStampNsValue& from, BufferWritable& bw);
79
    static inline void push_timestamptz(const TimestampTzValue& from, UInt32 scale,
80
                                        BufferWritable& bw,
81
                                        const DataTypeSerDe::FormatOptions& options);
82
83
    template <class SRC>
84
    static inline std::string from_ip(const SRC& from);
85
86
    template <class SRC>
87
    static inline void push_ip(const SRC& from, BufferWritable& bw);
88
89
    static inline std::string from_time(const TimeValue::TimeType& from, UInt32 scale);
90
91
    static inline void push_time(const TimeValue::TimeType& from, UInt32 scale, BufferWritable& bw);
92
93
    template <PrimitiveType T>
94
    static constexpr size_t string_length = 1;
95
96
private:
97
    template <typename T>
98
        requires(std::is_same_v<T, float> || std::is_same_v<T, double>)
99
58.4k
    static inline int _fast_to_buffer(T value, char* buffer) {
100
58.4k
        char* end = nullptr;
101
        // output NaN and Infinity to be compatible with most of the implementations
102
58.4k
        if (std::isnan(value)) {
103
14
            static constexpr char nan_str[] = "NaN";
104
14
            static constexpr int nan_str_len = sizeof(nan_str) - 1;
105
14
            memcpy(buffer, nan_str, nan_str_len);
106
14
            end = buffer + nan_str_len;
107
58.4k
        } else if (std::isinf(value)) {
108
27
            static constexpr char inf_str[] = "Infinity";
109
27
            static constexpr int inf_str_len = sizeof(inf_str) - 1;
110
27
            static constexpr char neg_inf_str[] = "-Infinity";
111
27
            static constexpr int neg_inf_str_len = sizeof(neg_inf_str) - 1;
112
27
            if (value > 0) {
113
14
                memcpy(buffer, inf_str, inf_str_len);
114
14
                end = buffer + inf_str_len;
115
14
            } else {
116
13
                memcpy(buffer, neg_inf_str, neg_inf_str_len);
117
13
                end = buffer + neg_inf_str_len;
118
13
            }
119
58.3k
        } else {
120
58.3k
            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
121
58.3k
        }
122
58.4k
        *end = '\0';
123
58.4k
        return int(end - buffer);
124
58.4k
    }
_ZN5doris12CastToString15_fast_to_bufferIfQoosr3stdE9is_same_vIT_fEsr3stdE9is_same_vIS2_dEEEiS2_Pc
Line
Count
Source
99
27.5k
    static inline int _fast_to_buffer(T value, char* buffer) {
100
27.5k
        char* end = nullptr;
101
        // output NaN and Infinity to be compatible with most of the implementations
102
27.5k
        if (std::isnan(value)) {
103
6
            static constexpr char nan_str[] = "NaN";
104
6
            static constexpr int nan_str_len = sizeof(nan_str) - 1;
105
6
            memcpy(buffer, nan_str, nan_str_len);
106
6
            end = buffer + nan_str_len;
107
27.4k
        } else if (std::isinf(value)) {
108
12
            static constexpr char inf_str[] = "Infinity";
109
12
            static constexpr int inf_str_len = sizeof(inf_str) - 1;
110
12
            static constexpr char neg_inf_str[] = "-Infinity";
111
12
            static constexpr int neg_inf_str_len = sizeof(neg_inf_str) - 1;
112
12
            if (value > 0) {
113
6
                memcpy(buffer, inf_str, inf_str_len);
114
6
                end = buffer + inf_str_len;
115
6
            } else {
116
6
                memcpy(buffer, neg_inf_str, neg_inf_str_len);
117
6
                end = buffer + neg_inf_str_len;
118
6
            }
119
27.4k
        } else {
120
            end = fmt::format_to(buffer, FMT_COMPILE("{}"), value);
121
27.4k
        }
122
27.5k
        *end = '\0';
123
27.5k
        return int(end - buffer);
124
27.5k
    }
_ZN5doris12CastToString15_fast_to_bufferIdQoosr3stdE9is_same_vIT_fEsr3stdE9is_same_vIS2_dEEEiS2_Pc
Line
Count
Source
99
30.9k
    static inline int _fast_to_buffer(T value, char* buffer) {
100
30.9k
        char* end = nullptr;
101
        // output NaN and Infinity to be compatible with most of the implementations
102
30.9k
        if (std::isnan(value)) {
103
8
            static constexpr char nan_str[] = "NaN";
104
8
            static constexpr int nan_str_len = sizeof(nan_str) - 1;
105
8
            memcpy(buffer, nan_str, nan_str_len);
106
8
            end = buffer + nan_str_len;
107
30.9k
        } else if (std::isinf(value)) {
108
15
            static constexpr char inf_str[] = "Infinity";
109
15
            static constexpr int inf_str_len = sizeof(inf_str) - 1;
110
15
            static constexpr char neg_inf_str[] = "-Infinity";
111
15
            static constexpr int neg_inf_str_len = sizeof(neg_inf_str) - 1;
112
15
            if (value > 0) {
113
8
                memcpy(buffer, inf_str, inf_str_len);
114
8
                end = buffer + inf_str_len;
115
8
            } else {
116
7
                memcpy(buffer, neg_inf_str, neg_inf_str_len);
117
7
                end = buffer + neg_inf_str_len;
118
7
            }
119
30.9k
        } else {
120
            end = fmt::format_to(buffer, FMT_COMPILE("{}"), value);
121
30.9k
        }
122
30.9k
        *end = '\0';
123
30.9k
        return int(end - buffer);
124
30.9k
    }
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
351
inline void CastToString::push_number(const UInt8& num, BufferWritable& bw) {
185
351
    auto f = fmt::format_int(num);
186
351
    bw.write(f.data(), f.size());
187
351
}
188
189
// TINYINT
190
template <>
191
5.73k
inline std::string CastToString::from_number(const Int8& num) {
192
5.73k
    auto f = fmt::format_int(num);
193
5.73k
    return std::string(f.data(), f.data() + f.size());
194
5.73k
}
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
344
inline std::string CastToString::from_number(const Int16& num) {
211
344
    auto f = fmt::format_int(num);
212
344
    return std::string(f.data(), f.data() + f.size());
213
344
}
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
33.0k
inline std::string CastToString::from_number(const Int32& num) {
230
33.0k
    auto f = fmt::format_int(num);
231
33.0k
    return std::string(f.data(), f.data() + f.size());
232
33.0k
}
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.49k
inline void CastToString::push_number(const Int32& num, BufferWritable& bw) {
242
9.49k
    auto f = fmt::format_int(num);
243
9.49k
    bw.write(f.data(), f.size());
244
9.49k
}
245
246
// BIGINT
247
template <>
248
2.65k
inline std::string CastToString::from_number(const Int64& num) {
249
2.65k
    auto f = fmt::format_int(num);
250
2.65k
    return std::string(f.data(), f.data() + f.size());
251
2.65k
}
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
321
inline std::string CastToString::from_int128(int128_t value) {
267
321
    fmt::memory_buffer buffer;
268
321
    fmt::format_to(buffer, "{}", value);
269
321
    return std::string(buffer.data(), buffer.size());
270
321
}
271
272
2
inline std::string CastToString::from_uint128(uint128_t value) {
273
2
    fmt::memory_buffer buffer;
274
2
    fmt::format_to(buffer, "{}", value);
275
2
    return std::string(buffer.data(), buffer.size());
276
2
}
277
278
1
inline std::string CastToString::from_uint128(UInt128 value) {
279
1
    return value.to_hex_string();
280
1
}
281
282
template <>
283
1
inline std::string CastToString::from_number(const Int128& num) {
284
1
    return from_int128(num);
285
1
}
286
287
template <>
288
0
inline void CastToString::push_number(const Int128& num, ColumnString::Chars& chars) {
289
0
    fmt::memory_buffer buffer;
290
0
    fmt::format_to(buffer, "{}", num);
291
0
    chars.insert(buffer.data(), buffer.data() + buffer.size());
292
0
}
293
294
template <>
295
4.74k
inline void CastToString::push_number(const Int128& num, BufferWritable& bw) {
296
4.74k
    fmt::memory_buffer buffer;
297
4.74k
    fmt::format_to(buffer, "{}", num);
298
4.74k
    bw.write(buffer.data(), buffer.size());
299
4.74k
}
300
301
// FLOAT
302
303
template <>
304
22.0k
inline std::string CastToString::from_number(const Float32& num) {
305
22.0k
    char buf[MAX_FLOAT_STR_LENGTH + 2];
306
22.0k
    int len = _fast_to_buffer(num, buf);
307
22.0k
    return std::string(buf, buf + len);
308
22.0k
}
309
310
template <typename T>
311
    requires(std::is_same_v<T, Float32> || std::is_same_v<T, Float64>)
312
85
inline int CastToString::from_number(const T& from, char* buffer) {
313
85
    return _fast_to_buffer(from, buffer);
314
85
}
_ZN5doris12CastToString11from_numberIfQoosr3stdE9is_same_vIT_fEsr3stdE9is_same_vIS2_dEEEiRKS2_Pc
Line
Count
Source
312
40
inline int CastToString::from_number(const T& from, char* buffer) {
313
40
    return _fast_to_buffer(from, buffer);
314
40
}
_ZN5doris12CastToString11from_numberIdQoosr3stdE9is_same_vIT_fEsr3stdE9is_same_vIS2_dEEEiRKS2_Pc
Line
Count
Source
312
45
inline int CastToString::from_number(const T& from, char* buffer) {
313
45
    return _fast_to_buffer(from, buffer);
314
45
}
315
316
template <>
317
0
inline void CastToString::push_number(const Float32& num, ColumnString::Chars& chars) {
318
0
    char buf[MAX_FLOAT_STR_LENGTH + 2];
319
0
    int len = _fast_to_buffer(num, buf);
320
0
    chars.insert(buf, buf + len);
321
0
}
322
323
template <>
324
5.43k
inline void CastToString::push_number(const Float32& num, BufferWritable& bw) {
325
5.43k
    char buf[MAX_FLOAT_STR_LENGTH + 2];
326
5.43k
    int len = _fast_to_buffer(num, buf);
327
5.43k
    bw.write(buf, len);
328
5.43k
}
329
330
// DOUBLE
331
template <>
332
24.8k
inline std::string CastToString::from_number(const Float64& num) {
333
24.8k
    char buf[MAX_DOUBLE_STR_LENGTH + 2];
334
24.8k
    int len = _fast_to_buffer(num, buf);
335
24.8k
    return std::string(buf, len);
336
24.8k
}
337
338
template <>
339
0
inline void CastToString::push_number(const Float64& num, ColumnString::Chars& chars) {
340
0
    char buf[MAX_DOUBLE_STR_LENGTH + 2];
341
0
    int len = _fast_to_buffer(num, buf);
342
0
    chars.insert(buf, buf + len);
343
0
}
344
345
template <>
346
6.02k
inline void CastToString::push_number(const Float64& num, BufferWritable& bw) {
347
6.02k
    char buf[MAX_DOUBLE_STR_LENGTH + 2];
348
6.02k
    int len = _fast_to_buffer(num, buf);
349
6.02k
    bw.write(buf, len);
350
6.02k
}
351
352
// DECIMAL32
353
template <>
354
2
inline std::string CastToString::from_decimal(const Decimal32& from, UInt32 scale) {
355
2
    return from.to_string(scale);
356
2
}
357
358
template <>
359
4.85k
inline void CastToString::push_decimal(const Decimal32& from, UInt32 scale, BufferWritable& bw) {
360
4.85k
    std::string str = from.to_string(scale);
361
4.85k
    bw.write(str.data(), str.size());
362
4.85k
}
363
364
// DECIMAL64
365
template <>
366
2
inline std::string CastToString::from_decimal(const Decimal64& from, UInt32 scale) {
367
2
    return from.to_string(scale);
368
2
}
369
370
template <>
371
12.6k
inline void CastToString::push_decimal(const Decimal64& from, UInt32 scale, BufferWritable& bw) {
372
12.6k
    std::string str = from.to_string(scale);
373
12.6k
    bw.write(str.data(), str.size());
374
12.6k
}
375
376
// DECIMAL128
377
template <>
378
2
inline std::string CastToString::from_decimal(const Decimal128V3& from, UInt32 scale) {
379
2
    return from.to_string(scale);
380
2
}
381
382
template <>
383
14.0k
inline void CastToString::push_decimal(const Decimal128V3& from, UInt32 scale, BufferWritable& bw) {
384
14.0k
    std::string str = from.to_string(scale);
385
14.0k
    bw.write(str.data(), str.size());
386
14.0k
}
387
388
// DECIMAL256
389
template <>
390
2
inline std::string CastToString::from_decimal(const Decimal256& from, UInt32 scale) {
391
2
    return from.to_string(scale);
392
2
}
393
394
template <>
395
14.7k
inline void CastToString::push_decimal(const Decimal256& from, UInt32 scale, BufferWritable& bw) {
396
14.7k
    std::string str = from.to_string(scale);
397
14.7k
    bw.write(str.data(), str.size());
398
14.7k
}
399
400
// DECIMALV2
401
template <>
402
1
inline std::string CastToString::from_decimal(const Decimal128V2& from, UInt32 scale) {
403
1
    auto value = (DecimalV2Value)from;
404
1
    auto str = value.to_string(scale);
405
1
    return str;
406
1
}
407
408
template <>
409
0
inline void CastToString::push_decimal(const Decimal128V2& from, UInt32 scale, BufferWritable& bw) {
410
0
    auto value = (DecimalV2Value)from;
411
0
    std::string str = value.to_string(scale);
412
0
    bw.write(str.data(), str.size());
413
0
}
414
415
template <>
416
inline void CastToString::push_decimal(const DecimalV2Value& from, UInt32 scale,
417
8.14k
                                       BufferWritable& bw) {
418
8.14k
    std::string str = from.to_string(scale);
419
8.14k
    bw.write(str.data(), str.size());
420
8.14k
}
421
422
// DATEV1 DATETIMEV1
423
2
inline std::string CastToString::from_date_or_datetime(const VecDateTimeValue& from) {
424
2
    char buf[64];
425
2
    char* pos = from.to_string(buf);
426
    // DateTime to_string the end is /0
427
2
    return std::string(buf, pos - 1);
428
2
}
429
430
inline void CastToString::push_date_or_datetime(const VecDateTimeValue& from,
431
0
                                                ColumnString::Chars& chars) {
432
0
    char buf[64];
433
0
    char* pos = from.to_string(buf);
434
0
    // DateTime to_string the end is /0
435
0
    chars.insert(buf, pos - 1);
436
0
}
437
438
6.55k
inline void CastToString::push_date_or_datetime(const VecDateTimeValue& from, BufferWritable& bw) {
439
6.55k
    char buf[64];
440
6.55k
    char* pos = from.to_string(buf);
441
    // DateTime to_string the end is /0
442
6.55k
    bw.write(buf, pos - buf - 1);
443
6.55k
}
444
445
// DATEV2
446
340
inline std::string CastToString::from_datev2(const DateV2Value<DateV2ValueType>& from) {
447
340
    char buf[64];
448
340
    char* pos = from.to_string(buf);
449
    // DateTime to_string the end is /0
450
340
    return std::string(buf, pos - 1);
451
340
}
452
453
inline void CastToString::push_datev2(const DateV2Value<DateV2ValueType>& from,
454
0
                                      ColumnString::Chars& chars) {
455
0
    char buf[64];
456
0
    char* pos = from.to_string(buf);
457
0
    // DateTime to_string the end is /0
458
0
    chars.insert(buf, pos - 1);
459
0
}
460
461
inline void CastToString::push_datev2(const DateV2Value<DateV2ValueType>& from,
462
2.74k
                                      BufferWritable& bw) {
463
2.74k
    char buf[64];
464
2.74k
    char* pos = from.to_string(buf);
465
    // DateTime to_string the end is /0
466
2.74k
    bw.write(buf, pos - buf - 1);
467
2.74k
}
468
469
// DATETIMEV2
470
inline std::string CastToString::from_datetimev2(const DateV2Value<DateTimeV2ValueType>& from,
471
373
                                                 UInt32 scale) {
472
373
    char buf[64];
473
373
    char* pos = from.to_string(buf, scale);
474
    // DateTime to_string the end is /0
475
373
    return std::string(buf, pos - 1);
476
373
}
477
478
4
inline std::string CastToString::from_timestamp_ns(const TimeStampNsValue& from) {
479
4
    return from.to_string();
480
4
}
481
482
inline std::string CastToString::from_timestamptz(const TimestampTzValue& from, UInt32 scale,
483
338
                                                  const cctz::time_zone* timezone) {
484
338
    cctz::time_zone tz;
485
338
    if (timezone == nullptr) {
486
338
        tz = cctz::utc_time_zone();
487
338
    } else {
488
0
        tz = *timezone;
489
0
    }
490
338
    return from.to_string(tz, scale);
491
338
}
492
inline void CastToString::push_datetimev2(const DateV2Value<DateTimeV2ValueType>& from,
493
0
                                          UInt32 scale, ColumnString::Chars& chars) {
494
0
    char buf[64];
495
0
    char* pos = from.to_string(buf, scale);
496
0
    // DateTime to_string the end is /0
497
0
    chars.insert(buf, pos - 1);
498
0
}
499
500
inline void CastToString::push_datetimev2(const DateV2Value<DateTimeV2ValueType>& from,
501
14.4k
                                          UInt32 scale, BufferWritable& bw) {
502
14.4k
    char buf[64];
503
14.4k
    char* pos = from.to_string(buf, scale);
504
    // DateTime to_string the end is /0
505
14.4k
    bw.write(buf, pos - buf - 1);
506
14.4k
}
507
508
60
inline void CastToString::push_timestamp_ns(const TimeStampNsValue& from, BufferWritable& bw) {
509
60
    char buf[40];
510
60
    const int32_t length = from.to_buffer(buf);
511
60
    bw.write(buf, length);
512
60
}
513
514
inline void CastToString::push_timestamptz(const TimestampTzValue& from, UInt32 scale,
515
                                           BufferWritable& bw,
516
7
                                           const DataTypeSerDe::FormatOptions& options) {
517
7
    auto str = from.to_string(*options.timezone, scale);
518
7
    bw.write(str.data(), str.size());
519
7
}
520
521
// IPv4
522
template <>
523
311
inline std::string CastToString::from_ip(const IPv4& from) {
524
311
    auto value = IPv4Value(from);
525
311
    return value.to_string();
526
311
}
527
528
template <>
529
10.1k
inline void CastToString::push_ip(const IPv4& from, BufferWritable& bw) {
530
10.1k
    auto value = IPv4Value(from);
531
10.1k
    std::string str = value.to_string();
532
10.1k
    bw.write(str.data(), str.size());
533
10.1k
}
534
535
//IPv6
536
537
template <>
538
316
inline std::string CastToString::from_ip(const IPv6& from) {
539
316
    auto value = IPv6Value(from);
540
316
    return value.to_string();
541
316
}
542
543
template <>
544
7.17k
inline void CastToString::push_ip(const IPv6& from, BufferWritable& bw) {
545
7.17k
    auto value = IPv6Value(from);
546
7.17k
    std::string str = value.to_string();
547
7.17k
    bw.write(str.data(), str.size());
548
7.17k
}
549
550
// Time
551
1
inline std::string CastToString::from_time(const TimeValue::TimeType& from, UInt32 scale) {
552
1
    return timev2_to_buffer_from_double(from, scale);
553
1
}
554
555
inline void CastToString::push_time(const TimeValue::TimeType& from, UInt32 scale,
556
279
                                    BufferWritable& bw) {
557
279
    std::string str = timev2_to_buffer_from_double(from, scale);
558
279
    bw.write(str.data(), str.size());
559
279
}
560
561
class CastToStringFunction {
562
public:
563
    static Status execute_impl(FunctionContext* context, Block& block,
564
                               const ColumnNumbers& arguments, uint32_t result,
565
                               size_t input_rows_count,
566
27
                               const NullMap::value_type* null_map = nullptr) {
567
27
        const auto& col_with_type_and_name = block.get_by_position(arguments[0]);
568
27
        const IDataType& type = *col_with_type_and_name.type;
569
27
        const IColumn& col_from = *col_with_type_and_name.column;
570
571
27
        auto col_to = ColumnString::create();
572
573
27
        DataTypeSerDe::FormatOptions options;
574
27
        auto time_zone = cctz::utc_time_zone();
575
27
        options.timezone =
576
27
                (context && context->state()) ? &context->state()->timezone_obj() : &time_zone;
577
27
        ColumnPtr limited_col;
578
27
        const IColumn* col_to_serialize = &col_from;
579
27
        if (col_from.size() != input_rows_count) {
580
2
            DORIS_CHECK(col_from.size() >= input_rows_count);
581
2
            limited_col = col_from.cut(0, input_rows_count);
582
2
            col_to_serialize = limited_col.get();
583
2
        }
584
27
        type.get_serde()->to_string_batch(*col_to_serialize, *col_to, options);
585
586
27
        block.replace_by_position(result, std::move(col_to));
587
27
        return Status::OK();
588
27
    }
589
};
590
591
namespace CastWrapper {
592
593
23
inline WrapperType create_string_wrapper(const DataTypePtr& from_type) {
594
23
    return [](FunctionContext* context, Block& block, const ColumnNumbers& arguments,
595
23
              uint32_t result, size_t input_rows_count,
596
23
              const NullMap::value_type* null_map = nullptr) {
597
23
        return CastToStringFunction::execute_impl(context, block, arguments, result,
598
23
                                                  input_rows_count, null_map);
599
23
    };
600
23
}
601
602
}; // namespace CastWrapper
603
} // namespace doris