Coverage Report

Created: 2026-08-20 09:01

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