Coverage Report

Created: 2026-09-22 19:54

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