Coverage Report

Created: 2026-05-25 12:05

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/util/string_parser.cpp
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
#include "util/string_parser.hpp"
19
20
#include <limits>
21
22
#include "core/extended_types.h"
23
#include "core/types.h"
24
namespace doris {
25
#include "common/compile_check_avoid_begin.h"
26
// Supported decimal number format:
27
// <decimal> ::= <whitespace>* <value> <whitespace>*
28
//
29
// <whitespace> ::= " " | "\t" | "\n" | "\r" | "\f" | "\v"
30
//
31
// <value> ::= <sign>? <significand> <exponent>?
32
//
33
// <sign> ::= "+" | "-"
34
//
35
// <significand> ::= <digits> "." <digits> | <digits> | <digits> "." | "." <digits>
36
//
37
// <digits> ::= <digit>+
38
//
39
// <digit> ::= "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"
40
//
41
// <exponent> ::= <e_marker> <sign>? <digits>
42
//
43
// <e_marker> ::= "e" | "E"
44
//
45
// Parsing algorithm:
46
// 1. Trim spaces and the sign, then normalize the significand by skipping leading zeros and an
47
//    optional leading dot. During this scan, count digits that belong to the original integral
48
//    part (`int_part_count`) and remember where the significand ends (`end_digit_index`).
49
// 2. Parse the optional exponent. Scientific notation is handled by moving the decimal point:
50
//    `result_int_part_digit_count = int_part_count + exponent`. For example, "12.34e-1" has
51
//    int_part_count=2 and exponent=-1, so the result has one integral digit: "1.234".
52
// 3. Build the result in scaled-integer form: first collect the integral digits up to the shifted
53
//    decimal point, then collect up to `type_scale` fractional digits, padding with zeros when the
54
//    input has fewer fractional digits than the target scale.
55
// 4. If there are extra fractional digits, round half up using the first discarded digit. Finally,
56
//    check the integral digit count against `type_precision - type_scale` and return the signed
57
//    scaled integer value.
58
template <PrimitiveType P>
59
typename PrimitiveTypeTraits<P>::CppType::NativeType StringParser::string_to_decimal(
60
        const char* __restrict s, size_t len, int type_precision, int type_scale,
61
356k
        ParseResult* result) {
62
356k
    using T = typename PrimitiveTypeTraits<P>::CppType::NativeType;
63
356k
    static_assert(std::is_same_v<T, int32_t> || std::is_same_v<T, int64_t> ||
64
356k
                          std::is_same_v<T, __int128> || std::is_same_v<T, wide::Int256>,
65
356k
                  "Cast string to decimal only support target type int32_t, int64_t, __int128 or "
66
356k
                  "wide::Int256.");
67
68
    // Parse in two logical coordinate systems:
69
    // 1. `s[0, end_digit_index)` is the normalized significand after trimming spaces, sign and
70
    //    leading zeros. If the original value starts with '.', the dot is also skipped so
71
    //    ".14E+3" is parsed as significand "14" with exponent 3.
72
    // 2. `result_int_part_digit_count = int_part_count + exponent` is the decimal point position
73
    //    after applying scientific notation. For example, "1.4E+2" has int_part_count=1,
74
    //    exponent=2, result_int_part_digit_count=3, so "14" becomes integer 140.
75
    // `digit_index` always indexes the normalized significand string, which may still contain a
76
    // dot for inputs like "1.4E+2"; loops that build numbers skip that dot explicitly.
77
    // Ignore leading and trailing spaces.
78
356k
    s = skip_ascii_whitespaces(s, len);
79
80
356k
    bool is_negative = false;
81
356k
    if (len > 0) {
82
356k
        switch (*s) {
83
92.6k
        case '-':
84
92.6k
            is_negative = true;
85
92.6k
            [[fallthrough]];
86
119k
        case '+':
87
119k
            ++s;
88
119k
            --len;
89
356k
        }
90
356k
    }
91
    // Ignore leading zeros.
92
356k
    bool found_value = false;
93
694k
    while (len > 0 && UNLIKELY(*s == '0')) {
94
337k
        found_value = true;
95
337k
        ++s;
96
337k
        --len;
97
337k
    }
98
99
356k
    int found_dot = 0;
100
356k
    if (len > 0 && *s == '.') {
101
84.9k
        found_dot = 1;
102
84.9k
        ++s;
103
84.9k
        --len;
104
84.9k
    }
105
356k
    int int_part_count = 0;
106
356k
    int i = 0;
107
8.27M
    for (; i != len; ++i) {
108
8.03M
        const char& c = s[i];
109
8.03M
        if (LIKELY('0' <= c && c <= '9')) {
110
7.69M
            found_value = true;
111
7.69M
            if (!found_dot) {
112
2.37M
                ++int_part_count;
113
2.37M
            }
114
7.69M
        } else if (c == '.') {
115
226k
            if (found_dot) {
116
2
                *result = StringParser::PARSE_FAILURE;
117
2
                return 0;
118
2
            }
119
226k
            found_dot = 1;
120
226k
        } else {
121
111k
            break;
122
111k
        }
123
8.03M
    }
124
356k
    if (!found_value) {
125
        // '', '.'
126
346
        *result = StringParser::PARSE_FAILURE;
127
346
        return 0;
128
346
    }
129
    // Parse exponent if any. Keep `end_digit_index` before consuming 'e/E' so later digit counts
130
    // ignore exponent syntax. For "1.4E+2", end_digit_index points just after "1.4", not after
131
    // "E+2".
132
356k
    int64_t exponent = 0;
133
356k
    auto end_digit_index = i;
134
356k
    if (i != len) {
135
111k
        bool negative_exponent = false;
136
111k
        if (s[i] == 'e' || s[i] == 'E') {
137
111k
            ++i;
138
111k
            if (i != len) {
139
111k
                switch (s[i]) {
140
11.6k
                case '-':
141
11.6k
                    negative_exponent = true;
142
11.6k
                    [[fallthrough]];
143
80.2k
                case '+':
144
80.2k
                    ++i;
145
111k
                }
146
111k
            }
147
111k
            if (i == len) {
148
                // '123e', '123e+', '123e-'
149
6
                *result = StringParser::PARSE_FAILURE;
150
6
                return 0;
151
6
            }
152
325k
            for (; i != len; ++i) {
153
213k
                const char& c = s[i];
154
213k
                if (LIKELY('0' <= c && c <= '9')) {
155
213k
                    exponent = exponent * 10 + (c - '0');
156
                    // max string len is config::string_type_length_soft_limit_bytes,
157
                    // whose max value is std::numeric_limits<int32_t>::max() - 4,
158
                    // just check overflow of int32_t to simplify the logic
159
                    // For edge cases like 0.{2147483647 zeros}e+2147483647
160
213k
                    if (exponent > std::numeric_limits<int32_t>::max()) {
161
0
                        *result = StringParser::PARSE_OVERFLOW;
162
0
                        return 0;
163
0
                    }
164
213k
                } else {
165
                    // '123e12abc', '123e1.2'
166
22
                    *result = StringParser::PARSE_FAILURE;
167
22
                    return 0;
168
22
                }
169
213k
            }
170
111k
            if (negative_exponent) {
171
11.6k
                exponent = -exponent;
172
11.6k
            }
173
111k
        } else {
174
110
            *result = StringParser::PARSE_FAILURE;
175
110
            return 0;
176
110
        }
177
111k
    }
178
    // TODO: check limit values of exponent and add UT
179
    // max string len is config::string_type_length_soft_limit_bytes,
180
    // whose max value is std::numeric_limits<int32_t>::max() - 4,
181
    // so int_part_count will be in range of int32_t,
182
    // and int_part_count + exponent will be in range of int64_t
183
356k
    int64_t tmp_result_int_part_digit_count = int_part_count + exponent;
184
356k
    if (tmp_result_int_part_digit_count > std::numeric_limits<int>::max() ||
185
356k
        tmp_result_int_part_digit_count < std::numeric_limits<int>::min()) {
186
0
        *result = is_negative ? StringParser::PARSE_UNDERFLOW : StringParser::PARSE_OVERFLOW;
187
0
        return 0;
188
0
    }
189
356k
    int result_int_part_digit_count = tmp_result_int_part_digit_count;
190
356k
    T int_part_number = 0;
191
356k
    T frac_part_number = 0;
192
356k
    int actual_frac_part_count = 0;
193
356k
    int digit_index = 0;
194
356k
    if (result_int_part_digit_count >= 0) {
195
        // `max_index` is the raw significand index where integer-part digits stop. Add one extra
196
        // raw character only when crossing an in-buffer dot, e.g. "1.4E+2" must scan "1.4" to
197
        // collect three integer digits after the exponent shift. It is capped by end_digit_index
198
        // because missing digits are appended later by multiplying with powers of 10.
199
350k
        int max_index = std::min(found_dot ? (result_int_part_digit_count +
200
308k
                                              ((int_part_count > 0 && exponent > 0) ? 1 : 0))
201
350k
                                           : result_int_part_digit_count,
202
350k
                                 end_digit_index);
203
350k
        max_index = (max_index == std::numeric_limits<int>::min() ? end_digit_index : max_index);
204
        // skip zero number
205
1.20M
        for (; digit_index != max_index && s[digit_index] == '0'; ++digit_index) {
206
855k
        }
207
        // test 0.00, .00, 0.{00...}e2147483647
208
        // 0.00000e2147483647
209
350k
        if (digit_index != max_index &&
210
350k
            (result_int_part_digit_count - digit_index > type_precision - type_scale)) {
211
12.0k
            *result = is_negative ? StringParser::PARSE_UNDERFLOW : StringParser::PARSE_OVERFLOW;
212
12.0k
            return 0;
213
12.0k
        }
214
        // get int part number
215
3.52M
        for (; digit_index != max_index; ++digit_index) {
216
3.18M
            if (UNLIKELY(s[digit_index] == '.')) {
217
73.0k
                continue;
218
73.0k
            }
219
3.10M
            int_part_number = int_part_number * 10 + (s[digit_index] - '0');
220
3.10M
        }
221
        // Count only significand digits, not exponent syntax. If the exponent moves the decimal
222
        // point past all available significant digits, append zeros by scaling the integer part:
223
        // "1.4E+2" scans integer 14, total_significant_digit_count=2, then multiplies by 10.
224
338k
        auto total_significant_digit_count =
225
338k
                end_digit_index - ((found_dot && int_part_count > 0) ? 1 : 0);
226
338k
        if (result_int_part_digit_count > total_significant_digit_count) {
227
66.7k
            int_part_number *= get_scale_multiplier<T>(result_int_part_digit_count -
228
66.7k
                                                       total_significant_digit_count);
229
66.7k
        }
230
338k
    } else {
231
        // leading zeros of fraction part
232
5.74k
        actual_frac_part_count = -result_int_part_digit_count;
233
5.74k
    }
234
    // get fraction part number
235
3.64M
    for (; digit_index != end_digit_index && actual_frac_part_count < type_scale; ++digit_index) {
236
3.30M
        if (UNLIKELY(s[digit_index] == '.')) {
237
133k
            continue;
238
133k
        }
239
3.17M
        frac_part_number = frac_part_number * 10 + (s[digit_index] - '0');
240
3.17M
        ++actual_frac_part_count;
241
3.17M
    }
242
344k
    auto type_scale_multiplier = get_scale_multiplier<T>(type_scale);
243
    // Round only when the next parsed significand digit is exactly the first discarded fractional
244
    // digit. If `actual_frac_part_count` is already greater than type_scale, the missing positions
245
    // are implicit zeros from a negative exponent, so "5e-17" to scale 15 must stay 0 instead of
246
    // rounding up.
247
344k
    if (actual_frac_part_count == type_scale && digit_index != end_digit_index) {
248
80.8k
        if (UNLIKELY(s[digit_index] == '.')) {
249
3.46k
            ++digit_index;
250
3.46k
        }
251
80.8k
        if (digit_index != end_digit_index) {
252
            // example: test 1.5 -> decimal(1, 0)
253
79.8k
            if (s[digit_index] >= '5') {
254
32.9k
                ++frac_part_number;
255
32.9k
                if (frac_part_number == type_scale_multiplier) {
256
3.43k
                    frac_part_number = 0;
257
3.43k
                    ++int_part_number;
258
3.43k
                }
259
32.9k
            }
260
79.8k
        }
261
263k
    } else {
262
263k
        if (actual_frac_part_count < type_scale) {
263
197k
            frac_part_number *= get_scale_multiplier<T>(type_scale - actual_frac_part_count);
264
197k
        }
265
263k
    }
266
344k
    if (int_part_number >= get_scale_multiplier<T>(type_precision - type_scale)) {
267
72
        *result = is_negative ? StringParser::PARSE_UNDERFLOW : StringParser::PARSE_OVERFLOW;
268
72
        return 0;
269
72
    }
270
271
343k
    T value = int_part_number * type_scale_multiplier + frac_part_number;
272
343k
    *result = StringParser::PARSE_SUCCESS;
273
343k
    return is_negative ? T(-value) : T(value);
274
344k
}
_ZN5doris12StringParser17string_to_decimalILNS_13PrimitiveTypeE28EEENS_19PrimitiveTypeTraitsIXT_EE7CppType10NativeTypeEPKcmiiPNS0_11ParseResultE
Line
Count
Source
61
54.7k
        ParseResult* result) {
62
54.7k
    using T = typename PrimitiveTypeTraits<P>::CppType::NativeType;
63
54.7k
    static_assert(std::is_same_v<T, int32_t> || std::is_same_v<T, int64_t> ||
64
54.7k
                          std::is_same_v<T, __int128> || std::is_same_v<T, wide::Int256>,
65
54.7k
                  "Cast string to decimal only support target type int32_t, int64_t, __int128 or "
66
54.7k
                  "wide::Int256.");
67
68
    // Parse in two logical coordinate systems:
69
    // 1. `s[0, end_digit_index)` is the normalized significand after trimming spaces, sign and
70
    //    leading zeros. If the original value starts with '.', the dot is also skipped so
71
    //    ".14E+3" is parsed as significand "14" with exponent 3.
72
    // 2. `result_int_part_digit_count = int_part_count + exponent` is the decimal point position
73
    //    after applying scientific notation. For example, "1.4E+2" has int_part_count=1,
74
    //    exponent=2, result_int_part_digit_count=3, so "14" becomes integer 140.
75
    // `digit_index` always indexes the normalized significand string, which may still contain a
76
    // dot for inputs like "1.4E+2"; loops that build numbers skip that dot explicitly.
77
    // Ignore leading and trailing spaces.
78
54.7k
    s = skip_ascii_whitespaces(s, len);
79
80
54.7k
    bool is_negative = false;
81
54.7k
    if (len > 0) {
82
54.7k
        switch (*s) {
83
25.1k
        case '-':
84
25.1k
            is_negative = true;
85
25.1k
            [[fallthrough]];
86
32.4k
        case '+':
87
32.4k
            ++s;
88
32.4k
            --len;
89
54.7k
        }
90
54.7k
    }
91
    // Ignore leading zeros.
92
54.7k
    bool found_value = false;
93
108k
    while (len > 0 && UNLIKELY(*s == '0')) {
94
53.6k
        found_value = true;
95
53.6k
        ++s;
96
53.6k
        --len;
97
53.6k
    }
98
99
54.7k
    int found_dot = 0;
100
54.7k
    if (len > 0 && *s == '.') {
101
17.9k
        found_dot = 1;
102
17.9k
        ++s;
103
17.9k
        --len;
104
17.9k
    }
105
54.7k
    int int_part_count = 0;
106
54.7k
    int i = 0;
107
579k
    for (; i != len; ++i) {
108
534k
        const char& c = s[i];
109
534k
        if (LIKELY('0' <= c && c <= '9')) {
110
509k
            found_value = true;
111
509k
            if (!found_dot) {
112
159k
                ++int_part_count;
113
159k
            }
114
509k
        } else if (c == '.') {
115
14.7k
            if (found_dot) {
116
2
                *result = StringParser::PARSE_FAILURE;
117
2
                return 0;
118
2
            }
119
14.7k
            found_dot = 1;
120
14.7k
        } else {
121
9.52k
            break;
122
9.52k
        }
123
534k
    }
124
54.7k
    if (!found_value) {
125
        // '', '.'
126
146
        *result = StringParser::PARSE_FAILURE;
127
146
        return 0;
128
146
    }
129
    // Parse exponent if any. Keep `end_digit_index` before consuming 'e/E' so later digit counts
130
    // ignore exponent syntax. For "1.4E+2", end_digit_index points just after "1.4", not after
131
    // "E+2".
132
54.6k
    int64_t exponent = 0;
133
54.6k
    auto end_digit_index = i;
134
54.6k
    if (i != len) {
135
9.39k
        bool negative_exponent = false;
136
9.39k
        if (s[i] == 'e' || s[i] == 'E') {
137
9.33k
            ++i;
138
9.33k
            if (i != len) {
139
9.33k
                switch (s[i]) {
140
1.54k
                case '-':
141
1.54k
                    negative_exponent = true;
142
1.54k
                    [[fallthrough]];
143
1.54k
                case '+':
144
1.54k
                    ++i;
145
9.33k
                }
146
9.33k
            }
147
9.33k
            if (i == len) {
148
                // '123e', '123e+', '123e-'
149
6
                *result = StringParser::PARSE_FAILURE;
150
6
                return 0;
151
6
            }
152
24.6k
            for (; i != len; ++i) {
153
15.3k
                const char& c = s[i];
154
15.3k
                if (LIKELY('0' <= c && c <= '9')) {
155
15.3k
                    exponent = exponent * 10 + (c - '0');
156
                    // max string len is config::string_type_length_soft_limit_bytes,
157
                    // whose max value is std::numeric_limits<int32_t>::max() - 4,
158
                    // just check overflow of int32_t to simplify the logic
159
                    // For edge cases like 0.{2147483647 zeros}e+2147483647
160
15.3k
                    if (exponent > std::numeric_limits<int32_t>::max()) {
161
0
                        *result = StringParser::PARSE_OVERFLOW;
162
0
                        return 0;
163
0
                    }
164
15.3k
                } else {
165
                    // '123e12abc', '123e1.2'
166
12
                    *result = StringParser::PARSE_FAILURE;
167
12
                    return 0;
168
12
                }
169
15.3k
            }
170
9.31k
            if (negative_exponent) {
171
1.53k
                exponent = -exponent;
172
1.53k
            }
173
9.31k
        } else {
174
60
            *result = StringParser::PARSE_FAILURE;
175
60
            return 0;
176
60
        }
177
9.39k
    }
178
    // TODO: check limit values of exponent and add UT
179
    // max string len is config::string_type_length_soft_limit_bytes,
180
    // whose max value is std::numeric_limits<int32_t>::max() - 4,
181
    // so int_part_count will be in range of int32_t,
182
    // and int_part_count + exponent will be in range of int64_t
183
54.5k
    int64_t tmp_result_int_part_digit_count = int_part_count + exponent;
184
54.5k
    if (tmp_result_int_part_digit_count > std::numeric_limits<int>::max() ||
185
54.5k
        tmp_result_int_part_digit_count < std::numeric_limits<int>::min()) {
186
0
        *result = is_negative ? StringParser::PARSE_UNDERFLOW : StringParser::PARSE_OVERFLOW;
187
0
        return 0;
188
0
    }
189
54.5k
    int result_int_part_digit_count = tmp_result_int_part_digit_count;
190
54.5k
    T int_part_number = 0;
191
54.5k
    T frac_part_number = 0;
192
54.5k
    int actual_frac_part_count = 0;
193
54.5k
    int digit_index = 0;
194
54.5k
    if (result_int_part_digit_count >= 0) {
195
        // `max_index` is the raw significand index where integer-part digits stop. Add one extra
196
        // raw character only when crossing an in-buffer dot, e.g. "1.4E+2" must scan "1.4" to
197
        // collect three integer digits after the exponent shift. It is capped by end_digit_index
198
        // because missing digits are appended later by multiplying with powers of 10.
199
54.5k
        int max_index = std::min(found_dot ? (result_int_part_digit_count +
200
32.5k
                                              ((int_part_count > 0 && exponent > 0) ? 1 : 0))
201
54.5k
                                           : result_int_part_digit_count,
202
54.5k
                                 end_digit_index);
203
54.5k
        max_index = (max_index == std::numeric_limits<int>::min() ? end_digit_index : max_index);
204
        // skip zero number
205
267k
        for (; digit_index != max_index && s[digit_index] == '0'; ++digit_index) {
206
212k
        }
207
        // test 0.00, .00, 0.{00...}e2147483647
208
        // 0.00000e2147483647
209
54.5k
        if (digit_index != max_index &&
210
54.5k
            (result_int_part_digit_count - digit_index > type_precision - type_scale)) {
211
1.33k
            *result = is_negative ? StringParser::PARSE_UNDERFLOW : StringParser::PARSE_OVERFLOW;
212
1.33k
            return 0;
213
1.33k
        }
214
        // get int part number
215
155k
        for (; digit_index != max_index; ++digit_index) {
216
102k
            if (UNLIKELY(s[digit_index] == '.')) {
217
1.60k
                continue;
218
1.60k
            }
219
101k
            int_part_number = int_part_number * 10 + (s[digit_index] - '0');
220
101k
        }
221
        // Count only significand digits, not exponent syntax. If the exponent moves the decimal
222
        // point past all available significant digits, append zeros by scaling the integer part:
223
        // "1.4E+2" scans integer 14, total_significant_digit_count=2, then multiplies by 10.
224
53.1k
        auto total_significant_digit_count =
225
53.1k
                end_digit_index - ((found_dot && int_part_count > 0) ? 1 : 0);
226
53.1k
        if (result_int_part_digit_count > total_significant_digit_count) {
227
100
            int_part_number *= get_scale_multiplier<T>(result_int_part_digit_count -
228
100
                                                       total_significant_digit_count);
229
100
        }
230
53.1k
    } else {
231
        // leading zeros of fraction part
232
48
        actual_frac_part_count = -result_int_part_digit_count;
233
48
    }
234
    // get fraction part number
235
169k
    for (; digit_index != end_digit_index && actual_frac_part_count < type_scale; ++digit_index) {
236
116k
        if (UNLIKELY(s[digit_index] == '.')) {
237
10.0k
            continue;
238
10.0k
        }
239
106k
        frac_part_number = frac_part_number * 10 + (s[digit_index] - '0');
240
106k
        ++actual_frac_part_count;
241
106k
    }
242
53.2k
    auto type_scale_multiplier = get_scale_multiplier<T>(type_scale);
243
    // Round only when the next parsed significand digit is exactly the first discarded fractional
244
    // digit. If `actual_frac_part_count` is already greater than type_scale, the missing positions
245
    // are implicit zeros from a negative exponent, so "5e-17" to scale 15 must stay 0 instead of
246
    // rounding up.
247
53.2k
    if (actual_frac_part_count == type_scale && digit_index != end_digit_index) {
248
21.4k
        if (UNLIKELY(s[digit_index] == '.')) {
249
904
            ++digit_index;
250
904
        }
251
21.4k
        if (digit_index != end_digit_index) {
252
            // example: test 1.5 -> decimal(1, 0)
253
21.1k
            if (s[digit_index] >= '5') {
254
8.96k
                ++frac_part_number;
255
8.96k
                if (frac_part_number == type_scale_multiplier) {
256
856
                    frac_part_number = 0;
257
856
                    ++int_part_number;
258
856
                }
259
8.96k
            }
260
21.1k
        }
261
31.7k
    } else {
262
31.7k
        if (actual_frac_part_count < type_scale) {
263
28.3k
            frac_part_number *= get_scale_multiplier<T>(type_scale - actual_frac_part_count);
264
28.3k
        }
265
31.7k
    }
266
53.2k
    if (int_part_number >= get_scale_multiplier<T>(type_precision - type_scale)) {
267
24
        *result = is_negative ? StringParser::PARSE_UNDERFLOW : StringParser::PARSE_OVERFLOW;
268
24
        return 0;
269
24
    }
270
271
53.2k
    T value = int_part_number * type_scale_multiplier + frac_part_number;
272
53.2k
    *result = StringParser::PARSE_SUCCESS;
273
53.2k
    return is_negative ? T(-value) : T(value);
274
53.2k
}
_ZN5doris12StringParser17string_to_decimalILNS_13PrimitiveTypeE29EEENS_19PrimitiveTypeTraitsIXT_EE7CppType10NativeTypeEPKcmiiPNS0_11ParseResultE
Line
Count
Source
61
86.9k
        ParseResult* result) {
62
86.9k
    using T = typename PrimitiveTypeTraits<P>::CppType::NativeType;
63
86.9k
    static_assert(std::is_same_v<T, int32_t> || std::is_same_v<T, int64_t> ||
64
86.9k
                          std::is_same_v<T, __int128> || std::is_same_v<T, wide::Int256>,
65
86.9k
                  "Cast string to decimal only support target type int32_t, int64_t, __int128 or "
66
86.9k
                  "wide::Int256.");
67
68
    // Parse in two logical coordinate systems:
69
    // 1. `s[0, end_digit_index)` is the normalized significand after trimming spaces, sign and
70
    //    leading zeros. If the original value starts with '.', the dot is also skipped so
71
    //    ".14E+3" is parsed as significand "14" with exponent 3.
72
    // 2. `result_int_part_digit_count = int_part_count + exponent` is the decimal point position
73
    //    after applying scientific notation. For example, "1.4E+2" has int_part_count=1,
74
    //    exponent=2, result_int_part_digit_count=3, so "14" becomes integer 140.
75
    // `digit_index` always indexes the normalized significand string, which may still contain a
76
    // dot for inputs like "1.4E+2"; loops that build numbers skip that dot explicitly.
77
    // Ignore leading and trailing spaces.
78
86.9k
    s = skip_ascii_whitespaces(s, len);
79
80
86.9k
    bool is_negative = false;
81
86.9k
    if (len > 0) {
82
86.9k
        switch (*s) {
83
21.7k
        case '-':
84
21.7k
            is_negative = true;
85
21.7k
            [[fallthrough]];
86
28.3k
        case '+':
87
28.3k
            ++s;
88
28.3k
            --len;
89
86.9k
        }
90
86.9k
    }
91
    // Ignore leading zeros.
92
86.9k
    bool found_value = false;
93
163k
    while (len > 0 && UNLIKELY(*s == '0')) {
94
76.2k
        found_value = true;
95
76.2k
        ++s;
96
76.2k
        --len;
97
76.2k
    }
98
99
86.9k
    int found_dot = 0;
100
86.9k
    if (len > 0 && *s == '.') {
101
24.0k
        found_dot = 1;
102
24.0k
        ++s;
103
24.0k
        --len;
104
24.0k
    }
105
86.9k
    int int_part_count = 0;
106
86.9k
    int i = 0;
107
1.41M
    for (; i != len; ++i) {
108
1.34M
        const char& c = s[i];
109
1.34M
        if (LIKELY('0' <= c && c <= '9')) {
110
1.27M
            found_value = true;
111
1.27M
            if (!found_dot) {
112
524k
                ++int_part_count;
113
524k
            }
114
1.27M
        } else if (c == '.') {
115
54.1k
            if (found_dot) {
116
0
                *result = StringParser::PARSE_FAILURE;
117
0
                return 0;
118
0
            }
119
54.1k
            found_dot = 1;
120
54.1k
        } else {
121
11.7k
            break;
122
11.7k
        }
123
1.34M
    }
124
86.9k
    if (!found_value) {
125
        // '', '.'
126
69
        *result = StringParser::PARSE_FAILURE;
127
69
        return 0;
128
69
    }
129
    // Parse exponent if any. Keep `end_digit_index` before consuming 'e/E' so later digit counts
130
    // ignore exponent syntax. For "1.4E+2", end_digit_index points just after "1.4", not after
131
    // "E+2".
132
86.8k
    int64_t exponent = 0;
133
86.8k
    auto end_digit_index = i;
134
86.8k
    if (i != len) {
135
11.6k
        bool negative_exponent = false;
136
11.6k
        if (s[i] == 'e' || s[i] == 'E') {
137
11.6k
            ++i;
138
11.6k
            if (i != len) {
139
11.6k
                switch (s[i]) {
140
3.91k
                case '-':
141
3.91k
                    negative_exponent = true;
142
3.91k
                    [[fallthrough]];
143
3.91k
                case '+':
144
3.91k
                    ++i;
145
11.6k
                }
146
11.6k
            }
147
11.6k
            if (i == len) {
148
                // '123e', '123e+', '123e-'
149
0
                *result = StringParser::PARSE_FAILURE;
150
0
                return 0;
151
0
            }
152
32.5k
            for (; i != len; ++i) {
153
20.8k
                const char& c = s[i];
154
20.8k
                if (LIKELY('0' <= c && c <= '9')) {
155
20.8k
                    exponent = exponent * 10 + (c - '0');
156
                    // max string len is config::string_type_length_soft_limit_bytes,
157
                    // whose max value is std::numeric_limits<int32_t>::max() - 4,
158
                    // just check overflow of int32_t to simplify the logic
159
                    // For edge cases like 0.{2147483647 zeros}e+2147483647
160
20.8k
                    if (exponent > std::numeric_limits<int32_t>::max()) {
161
0
                        *result = StringParser::PARSE_OVERFLOW;
162
0
                        return 0;
163
0
                    }
164
20.8k
                } else {
165
                    // '123e12abc', '123e1.2'
166
0
                    *result = StringParser::PARSE_FAILURE;
167
0
                    return 0;
168
0
                }
169
20.8k
            }
170
11.6k
            if (negative_exponent) {
171
3.91k
                exponent = -exponent;
172
3.91k
            }
173
11.6k
        } else {
174
23
            *result = StringParser::PARSE_FAILURE;
175
23
            return 0;
176
23
        }
177
11.6k
    }
178
    // TODO: check limit values of exponent and add UT
179
    // max string len is config::string_type_length_soft_limit_bytes,
180
    // whose max value is std::numeric_limits<int32_t>::max() - 4,
181
    // so int_part_count will be in range of int32_t,
182
    // and int_part_count + exponent will be in range of int64_t
183
86.8k
    int64_t tmp_result_int_part_digit_count = int_part_count + exponent;
184
86.8k
    if (tmp_result_int_part_digit_count > std::numeric_limits<int>::max() ||
185
86.8k
        tmp_result_int_part_digit_count < std::numeric_limits<int>::min()) {
186
0
        *result = is_negative ? StringParser::PARSE_UNDERFLOW : StringParser::PARSE_OVERFLOW;
187
0
        return 0;
188
0
    }
189
86.8k
    int result_int_part_digit_count = tmp_result_int_part_digit_count;
190
86.8k
    T int_part_number = 0;
191
86.8k
    T frac_part_number = 0;
192
86.8k
    int actual_frac_part_count = 0;
193
86.8k
    int digit_index = 0;
194
86.8k
    if (result_int_part_digit_count >= 0) {
195
        // `max_index` is the raw significand index where integer-part digits stop. Add one extra
196
        // raw character only when crossing an in-buffer dot, e.g. "1.4E+2" must scan "1.4" to
197
        // collect three integer digits after the exponent shift. It is capped by end_digit_index
198
        // because missing digits are appended later by multiplying with powers of 10.
199
84.4k
        int max_index = std::min(found_dot ? (result_int_part_digit_count +
200
77.3k
                                              ((int_part_count > 0 && exponent > 0) ? 1 : 0))
201
84.4k
                                           : result_int_part_digit_count,
202
84.4k
                                 end_digit_index);
203
84.4k
        max_index = (max_index == std::numeric_limits<int>::min() ? end_digit_index : max_index);
204
        // skip zero number
205
296k
        for (; digit_index != max_index && s[digit_index] == '0'; ++digit_index) {
206
212k
        }
207
        // test 0.00, .00, 0.{00...}e2147483647
208
        // 0.00000e2147483647
209
84.4k
        if (digit_index != max_index &&
210
84.4k
            (result_int_part_digit_count - digit_index > type_precision - type_scale)) {
211
10.4k
            *result = is_negative ? StringParser::PARSE_UNDERFLOW : StringParser::PARSE_OVERFLOW;
212
10.4k
            return 0;
213
10.4k
        }
214
        // get int part number
215
412k
        for (; digit_index != max_index; ++digit_index) {
216
338k
            if (UNLIKELY(s[digit_index] == '.')) {
217
960
                continue;
218
960
            }
219
337k
            int_part_number = int_part_number * 10 + (s[digit_index] - '0');
220
337k
        }
221
        // Count only significand digits, not exponent syntax. If the exponent moves the decimal
222
        // point past all available significant digits, append zeros by scaling the integer part:
223
        // "1.4E+2" scans integer 14, total_significant_digit_count=2, then multiplies by 10.
224
73.9k
        auto total_significant_digit_count =
225
73.9k
                end_digit_index - ((found_dot && int_part_count > 0) ? 1 : 0);
226
73.9k
        if (result_int_part_digit_count > total_significant_digit_count) {
227
76
            int_part_number *= get_scale_multiplier<T>(result_int_part_digit_count -
228
76
                                                       total_significant_digit_count);
229
76
        }
230
73.9k
    } else {
231
        // leading zeros of fraction part
232
2.42k
        actual_frac_part_count = -result_int_part_digit_count;
233
2.42k
    }
234
    // get fraction part number
235
595k
    for (; digit_index != end_digit_index && actual_frac_part_count < type_scale; ++digit_index) {
236
519k
        if (UNLIKELY(s[digit_index] == '.')) {
237
41.0k
            continue;
238
41.0k
        }
239
478k
        frac_part_number = frac_part_number * 10 + (s[digit_index] - '0');
240
478k
        ++actual_frac_part_count;
241
478k
    }
242
76.3k
    auto type_scale_multiplier = get_scale_multiplier<T>(type_scale);
243
    // Round only when the next parsed significand digit is exactly the first discarded fractional
244
    // digit. If `actual_frac_part_count` is already greater than type_scale, the missing positions
245
    // are implicit zeros from a negative exponent, so "5e-17" to scale 15 must stay 0 instead of
246
    // rounding up.
247
76.3k
    if (actual_frac_part_count == type_scale && digit_index != end_digit_index) {
248
20.1k
        if (UNLIKELY(s[digit_index] == '.')) {
249
852
            ++digit_index;
250
852
        }
251
20.1k
        if (digit_index != end_digit_index) {
252
            // example: test 1.5 -> decimal(1, 0)
253
19.8k
            if (s[digit_index] >= '5') {
254
7.95k
                ++frac_part_number;
255
7.95k
                if (frac_part_number == type_scale_multiplier) {
256
836
                    frac_part_number = 0;
257
836
                    ++int_part_number;
258
836
                }
259
7.95k
            }
260
19.8k
        }
261
56.2k
    } else {
262
56.2k
        if (actual_frac_part_count < type_scale) {
263
32.0k
            frac_part_number *= get_scale_multiplier<T>(type_scale - actual_frac_part_count);
264
32.0k
        }
265
56.2k
    }
266
76.3k
    if (int_part_number >= get_scale_multiplier<T>(type_precision - type_scale)) {
267
16
        *result = is_negative ? StringParser::PARSE_UNDERFLOW : StringParser::PARSE_OVERFLOW;
268
16
        return 0;
269
16
    }
270
271
76.3k
    T value = int_part_number * type_scale_multiplier + frac_part_number;
272
76.3k
    *result = StringParser::PARSE_SUCCESS;
273
76.3k
    return is_negative ? T(-value) : T(value);
274
76.3k
}
_ZN5doris12StringParser17string_to_decimalILNS_13PrimitiveTypeE30EEENS_19PrimitiveTypeTraitsIXT_EE7CppType10NativeTypeEPKcmiiPNS0_11ParseResultE
Line
Count
Source
61
83.4k
        ParseResult* result) {
62
83.4k
    using T = typename PrimitiveTypeTraits<P>::CppType::NativeType;
63
83.4k
    static_assert(std::is_same_v<T, int32_t> || std::is_same_v<T, int64_t> ||
64
83.4k
                          std::is_same_v<T, __int128> || std::is_same_v<T, wide::Int256>,
65
83.4k
                  "Cast string to decimal only support target type int32_t, int64_t, __int128 or "
66
83.4k
                  "wide::Int256.");
67
68
    // Parse in two logical coordinate systems:
69
    // 1. `s[0, end_digit_index)` is the normalized significand after trimming spaces, sign and
70
    //    leading zeros. If the original value starts with '.', the dot is also skipped so
71
    //    ".14E+3" is parsed as significand "14" with exponent 3.
72
    // 2. `result_int_part_digit_count = int_part_count + exponent` is the decimal point position
73
    //    after applying scientific notation. For example, "1.4E+2" has int_part_count=1,
74
    //    exponent=2, result_int_part_digit_count=3, so "14" becomes integer 140.
75
    // `digit_index` always indexes the normalized significand string, which may still contain a
76
    // dot for inputs like "1.4E+2"; loops that build numbers skip that dot explicitly.
77
    // Ignore leading and trailing spaces.
78
83.4k
    s = skip_ascii_whitespaces(s, len);
79
80
83.4k
    bool is_negative = false;
81
83.4k
    if (len > 0) {
82
83.4k
        switch (*s) {
83
21.7k
        case '-':
84
21.7k
            is_negative = true;
85
21.7k
            [[fallthrough]];
86
28.3k
        case '+':
87
28.3k
            ++s;
88
28.3k
            --len;
89
83.4k
        }
90
83.4k
    }
91
    // Ignore leading zeros.
92
83.4k
    bool found_value = false;
93
158k
    while (len > 0 && UNLIKELY(*s == '0')) {
94
74.9k
        found_value = true;
95
74.9k
        ++s;
96
74.9k
        --len;
97
74.9k
    }
98
99
83.4k
    int found_dot = 0;
100
83.4k
    if (len > 0 && *s == '.') {
101
25.1k
        found_dot = 1;
102
25.1k
        ++s;
103
25.1k
        --len;
104
25.1k
    }
105
83.4k
    int int_part_count = 0;
106
83.4k
    int i = 0;
107
2.06M
    for (; i != len; ++i) {
108
1.99M
        const char& c = s[i];
109
1.99M
        if (LIKELY('0' <= c && c <= '9')) {
110
1.93M
            found_value = true;
111
1.93M
            if (!found_dot) {
112
562k
                ++int_part_count;
113
562k
            }
114
1.93M
        } else if (c == '.') {
115
50.3k
            if (found_dot) {
116
0
                *result = StringParser::PARSE_FAILURE;
117
0
                return 0;
118
0
            }
119
50.3k
            found_dot = 1;
120
50.3k
        } else {
121
12.5k
            break;
122
12.5k
        }
123
1.99M
    }
124
83.4k
    if (!found_value) {
125
        // '', '.'
126
55
        *result = StringParser::PARSE_FAILURE;
127
55
        return 0;
128
55
    }
129
    // Parse exponent if any. Keep `end_digit_index` before consuming 'e/E' so later digit counts
130
    // ignore exponent syntax. For "1.4E+2", end_digit_index points just after "1.4", not after
131
    // "E+2".
132
83.3k
    int64_t exponent = 0;
133
83.3k
    auto end_digit_index = i;
134
83.3k
    if (i != len) {
135
12.4k
        bool negative_exponent = false;
136
12.4k
        if (s[i] == 'e' || s[i] == 'E') {
137
12.4k
            ++i;
138
12.4k
            if (i != len) {
139
12.4k
                switch (s[i]) {
140
4.70k
                case '-':
141
4.70k
                    negative_exponent = true;
142
4.70k
                    [[fallthrough]];
143
4.71k
                case '+':
144
4.71k
                    ++i;
145
12.4k
                }
146
12.4k
            }
147
12.4k
            if (i == len) {
148
                // '123e', '123e+', '123e-'
149
0
                *result = StringParser::PARSE_FAILURE;
150
0
                return 0;
151
0
            }
152
35.9k
            for (; i != len; ++i) {
153
23.4k
                const char& c = s[i];
154
23.4k
                if (LIKELY('0' <= c && c <= '9')) {
155
23.4k
                    exponent = exponent * 10 + (c - '0');
156
                    // max string len is config::string_type_length_soft_limit_bytes,
157
                    // whose max value is std::numeric_limits<int32_t>::max() - 4,
158
                    // just check overflow of int32_t to simplify the logic
159
                    // For edge cases like 0.{2147483647 zeros}e+2147483647
160
23.4k
                    if (exponent > std::numeric_limits<int32_t>::max()) {
161
0
                        *result = StringParser::PARSE_OVERFLOW;
162
0
                        return 0;
163
0
                    }
164
23.4k
                } else {
165
                    // '123e12abc', '123e1.2'
166
0
                    *result = StringParser::PARSE_FAILURE;
167
0
                    return 0;
168
0
                }
169
23.4k
            }
170
12.4k
            if (negative_exponent) {
171
4.70k
                exponent = -exponent;
172
4.70k
            }
173
12.4k
        } else {
174
12
            *result = StringParser::PARSE_FAILURE;
175
12
            return 0;
176
12
        }
177
12.4k
    }
178
    // TODO: check limit values of exponent and add UT
179
    // max string len is config::string_type_length_soft_limit_bytes,
180
    // whose max value is std::numeric_limits<int32_t>::max() - 4,
181
    // so int_part_count will be in range of int32_t,
182
    // and int_part_count + exponent will be in range of int64_t
183
83.3k
    int64_t tmp_result_int_part_digit_count = int_part_count + exponent;
184
83.3k
    if (tmp_result_int_part_digit_count > std::numeric_limits<int>::max() ||
185
83.3k
        tmp_result_int_part_digit_count < std::numeric_limits<int>::min()) {
186
0
        *result = is_negative ? StringParser::PARSE_UNDERFLOW : StringParser::PARSE_OVERFLOW;
187
0
        return 0;
188
0
    }
189
83.3k
    int result_int_part_digit_count = tmp_result_int_part_digit_count;
190
83.3k
    T int_part_number = 0;
191
83.3k
    T frac_part_number = 0;
192
83.3k
    int actual_frac_part_count = 0;
193
83.3k
    int digit_index = 0;
194
83.3k
    if (result_int_part_digit_count >= 0) {
195
        // `max_index` is the raw significand index where integer-part digits stop. Add one extra
196
        // raw character only when crossing an in-buffer dot, e.g. "1.4E+2" must scan "1.4" to
197
        // collect three integer digits after the exponent shift. It is capped by end_digit_index
198
        // because missing digits are appended later by multiplying with powers of 10.
199
80.1k
        int max_index = std::min(found_dot ? (result_int_part_digit_count +
200
73.8k
                                              ((int_part_count > 0 && exponent > 0) ? 1 : 0))
201
80.1k
                                           : result_int_part_digit_count,
202
80.1k
                                 end_digit_index);
203
80.1k
        max_index = (max_index == std::numeric_limits<int>::min() ? end_digit_index : max_index);
204
        // skip zero number
205
293k
        for (; digit_index != max_index && s[digit_index] == '0'; ++digit_index) {
206
213k
        }
207
        // test 0.00, .00, 0.{00...}e2147483647
208
        // 0.00000e2147483647
209
80.1k
        if (digit_index != max_index &&
210
80.1k
            (result_int_part_digit_count - digit_index > type_precision - type_scale)) {
211
140
            *result = is_negative ? StringParser::PARSE_UNDERFLOW : StringParser::PARSE_OVERFLOW;
212
140
            return 0;
213
140
        }
214
        // get int part number
215
602k
        for (; digit_index != max_index; ++digit_index) {
216
522k
            if (UNLIKELY(s[digit_index] == '.')) {
217
962
                continue;
218
962
            }
219
521k
            int_part_number = int_part_number * 10 + (s[digit_index] - '0');
220
521k
        }
221
        // Count only significand digits, not exponent syntax. If the exponent moves the decimal
222
        // point past all available significant digits, append zeros by scaling the integer part:
223
        // "1.4E+2" scans integer 14, total_significant_digit_count=2, then multiplies by 10.
224
79.9k
        auto total_significant_digit_count =
225
79.9k
                end_digit_index - ((found_dot && int_part_count > 0) ? 1 : 0);
226
79.9k
        if (result_int_part_digit_count > total_significant_digit_count) {
227
80
            int_part_number *= get_scale_multiplier<T>(result_int_part_digit_count -
228
80
                                                       total_significant_digit_count);
229
80
        }
230
79.9k
    } else {
231
        // leading zeros of fraction part
232
3.22k
        actual_frac_part_count = -result_int_part_digit_count;
233
3.22k
    }
234
    // get fraction part number
235
1.22M
    for (; digit_index != end_digit_index && actual_frac_part_count < type_scale; ++digit_index) {
236
1.13M
        if (UNLIKELY(s[digit_index] == '.')) {
237
46.6k
            continue;
238
46.6k
        }
239
1.09M
        frac_part_number = frac_part_number * 10 + (s[digit_index] - '0');
240
1.09M
        ++actual_frac_part_count;
241
1.09M
    }
242
83.1k
    auto type_scale_multiplier = get_scale_multiplier<T>(type_scale);
243
    // Round only when the next parsed significand digit is exactly the first discarded fractional
244
    // digit. If `actual_frac_part_count` is already greater than type_scale, the missing positions
245
    // are implicit zeros from a negative exponent, so "5e-17" to scale 15 must stay 0 instead of
246
    // rounding up.
247
83.1k
    if (actual_frac_part_count == type_scale && digit_index != end_digit_index) {
248
20.1k
        if (UNLIKELY(s[digit_index] == '.')) {
249
852
            ++digit_index;
250
852
        }
251
20.1k
        if (digit_index != end_digit_index) {
252
            // example: test 1.5 -> decimal(1, 0)
253
19.9k
            if (s[digit_index] >= '5') {
254
8.03k
                ++frac_part_number;
255
8.03k
                if (frac_part_number == type_scale_multiplier) {
256
908
                    frac_part_number = 0;
257
908
                    ++int_part_number;
258
908
                }
259
8.03k
            }
260
19.9k
        }
261
63.0k
    } else {
262
63.0k
        if (actual_frac_part_count < type_scale) {
263
45.5k
            frac_part_number *= get_scale_multiplier<T>(type_scale - actual_frac_part_count);
264
45.5k
        }
265
63.0k
    }
266
83.1k
    if (int_part_number >= get_scale_multiplier<T>(type_precision - type_scale)) {
267
16
        *result = is_negative ? StringParser::PARSE_UNDERFLOW : StringParser::PARSE_OVERFLOW;
268
16
        return 0;
269
16
    }
270
271
83.1k
    T value = int_part_number * type_scale_multiplier + frac_part_number;
272
83.1k
    *result = StringParser::PARSE_SUCCESS;
273
83.1k
    return is_negative ? T(-value) : T(value);
274
83.1k
}
_ZN5doris12StringParser17string_to_decimalILNS_13PrimitiveTypeE20EEENS_19PrimitiveTypeTraitsIXT_EE7CppType10NativeTypeEPKcmiiPNS0_11ParseResultE
Line
Count
Source
61
13.5k
        ParseResult* result) {
62
13.5k
    using T = typename PrimitiveTypeTraits<P>::CppType::NativeType;
63
13.5k
    static_assert(std::is_same_v<T, int32_t> || std::is_same_v<T, int64_t> ||
64
13.5k
                          std::is_same_v<T, __int128> || std::is_same_v<T, wide::Int256>,
65
13.5k
                  "Cast string to decimal only support target type int32_t, int64_t, __int128 or "
66
13.5k
                  "wide::Int256.");
67
68
    // Parse in two logical coordinate systems:
69
    // 1. `s[0, end_digit_index)` is the normalized significand after trimming spaces, sign and
70
    //    leading zeros. If the original value starts with '.', the dot is also skipped so
71
    //    ".14E+3" is parsed as significand "14" with exponent 3.
72
    // 2. `result_int_part_digit_count = int_part_count + exponent` is the decimal point position
73
    //    after applying scientific notation. For example, "1.4E+2" has int_part_count=1,
74
    //    exponent=2, result_int_part_digit_count=3, so "14" becomes integer 140.
75
    // `digit_index` always indexes the normalized significand string, which may still contain a
76
    // dot for inputs like "1.4E+2"; loops that build numbers skip that dot explicitly.
77
    // Ignore leading and trailing spaces.
78
13.5k
    s = skip_ascii_whitespaces(s, len);
79
80
13.5k
    bool is_negative = false;
81
13.5k
    if (len > 0) {
82
13.5k
        switch (*s) {
83
6.68k
        case '-':
84
6.68k
            is_negative = true;
85
6.68k
            [[fallthrough]];
86
6.68k
        case '+':
87
6.68k
            ++s;
88
6.68k
            --len;
89
13.5k
        }
90
13.5k
    }
91
    // Ignore leading zeros.
92
13.5k
    bool found_value = false;
93
52.3k
    while (len > 0 && UNLIKELY(*s == '0')) {
94
38.8k
        found_value = true;
95
38.8k
        ++s;
96
38.8k
        --len;
97
38.8k
    }
98
99
13.5k
    int found_dot = 0;
100
13.5k
    if (len > 0 && *s == '.') {
101
2.00k
        found_dot = 1;
102
2.00k
        ++s;
103
2.00k
        --len;
104
2.00k
    }
105
13.5k
    int int_part_count = 0;
106
13.5k
    int i = 0;
107
279k
    for (; i != len; ++i) {
108
266k
        const char& c = s[i];
109
266k
        if (LIKELY('0' <= c && c <= '9')) {
110
254k
            found_value = true;
111
254k
            if (!found_dot) {
112
136k
                ++int_part_count;
113
136k
            }
114
254k
        } else if (c == '.') {
115
11.4k
            if (found_dot) {
116
0
                *result = StringParser::PARSE_FAILURE;
117
0
                return 0;
118
0
            }
119
11.4k
            found_dot = 1;
120
11.4k
        } else {
121
11
            break;
122
11
        }
123
266k
    }
124
13.5k
    if (!found_value) {
125
        // '', '.'
126
10
        *result = StringParser::PARSE_FAILURE;
127
10
        return 0;
128
10
    }
129
    // Parse exponent if any. Keep `end_digit_index` before consuming 'e/E' so later digit counts
130
    // ignore exponent syntax. For "1.4E+2", end_digit_index points just after "1.4", not after
131
    // "E+2".
132
13.5k
    int64_t exponent = 0;
133
13.5k
    auto end_digit_index = i;
134
13.5k
    if (i != len) {
135
1
        bool negative_exponent = false;
136
1
        if (s[i] == 'e' || s[i] == 'E') {
137
0
            ++i;
138
0
            if (i != len) {
139
0
                switch (s[i]) {
140
0
                case '-':
141
0
                    negative_exponent = true;
142
0
                    [[fallthrough]];
143
0
                case '+':
144
0
                    ++i;
145
0
                }
146
0
            }
147
0
            if (i == len) {
148
                // '123e', '123e+', '123e-'
149
0
                *result = StringParser::PARSE_FAILURE;
150
0
                return 0;
151
0
            }
152
0
            for (; i != len; ++i) {
153
0
                const char& c = s[i];
154
0
                if (LIKELY('0' <= c && c <= '9')) {
155
0
                    exponent = exponent * 10 + (c - '0');
156
                    // max string len is config::string_type_length_soft_limit_bytes,
157
                    // whose max value is std::numeric_limits<int32_t>::max() - 4,
158
                    // just check overflow of int32_t to simplify the logic
159
                    // For edge cases like 0.{2147483647 zeros}e+2147483647
160
0
                    if (exponent > std::numeric_limits<int32_t>::max()) {
161
0
                        *result = StringParser::PARSE_OVERFLOW;
162
0
                        return 0;
163
0
                    }
164
0
                } else {
165
                    // '123e12abc', '123e1.2'
166
0
                    *result = StringParser::PARSE_FAILURE;
167
0
                    return 0;
168
0
                }
169
0
            }
170
0
            if (negative_exponent) {
171
0
                exponent = -exponent;
172
0
            }
173
1
        } else {
174
1
            *result = StringParser::PARSE_FAILURE;
175
1
            return 0;
176
1
        }
177
1
    }
178
    // TODO: check limit values of exponent and add UT
179
    // max string len is config::string_type_length_soft_limit_bytes,
180
    // whose max value is std::numeric_limits<int32_t>::max() - 4,
181
    // so int_part_count will be in range of int32_t,
182
    // and int_part_count + exponent will be in range of int64_t
183
13.5k
    int64_t tmp_result_int_part_digit_count = int_part_count + exponent;
184
13.5k
    if (tmp_result_int_part_digit_count > std::numeric_limits<int>::max() ||
185
13.5k
        tmp_result_int_part_digit_count < std::numeric_limits<int>::min()) {
186
0
        *result = is_negative ? StringParser::PARSE_UNDERFLOW : StringParser::PARSE_OVERFLOW;
187
0
        return 0;
188
0
    }
189
13.5k
    int result_int_part_digit_count = tmp_result_int_part_digit_count;
190
13.5k
    T int_part_number = 0;
191
13.5k
    T frac_part_number = 0;
192
13.5k
    int actual_frac_part_count = 0;
193
13.5k
    int digit_index = 0;
194
13.5k
    if (result_int_part_digit_count >= 0) {
195
        // `max_index` is the raw significand index where integer-part digits stop. Add one extra
196
        // raw character only when crossing an in-buffer dot, e.g. "1.4E+2" must scan "1.4" to
197
        // collect three integer digits after the exponent shift. It is capped by end_digit_index
198
        // because missing digits are appended later by multiplying with powers of 10.
199
13.5k
        int max_index = std::min(found_dot ? (result_int_part_digit_count +
200
13.4k
                                              ((int_part_count > 0 && exponent > 0) ? 1 : 0))
201
13.5k
                                           : result_int_part_digit_count,
202
13.5k
                                 end_digit_index);
203
13.5k
        max_index = (max_index == std::numeric_limits<int>::min() ? end_digit_index : max_index);
204
        // skip zero number
205
13.5k
        for (; digit_index != max_index && s[digit_index] == '0'; ++digit_index) {
206
0
        }
207
        // test 0.00, .00, 0.{00...}e2147483647
208
        // 0.00000e2147483647
209
13.5k
        if (digit_index != max_index &&
210
13.5k
            (result_int_part_digit_count - digit_index > type_precision - type_scale)) {
211
8
            *result = is_negative ? StringParser::PARSE_UNDERFLOW : StringParser::PARSE_OVERFLOW;
212
8
            return 0;
213
8
        }
214
        // get int part number
215
150k
        for (; digit_index != max_index; ++digit_index) {
216
136k
            if (UNLIKELY(s[digit_index] == '.')) {
217
0
                continue;
218
0
            }
219
136k
            int_part_number = int_part_number * 10 + (s[digit_index] - '0');
220
136k
        }
221
        // Count only significand digits, not exponent syntax. If the exponent moves the decimal
222
        // point past all available significant digits, append zeros by scaling the integer part:
223
        // "1.4E+2" scans integer 14, total_significant_digit_count=2, then multiplies by 10.
224
13.5k
        auto total_significant_digit_count =
225
13.5k
                end_digit_index - ((found_dot && int_part_count > 0) ? 1 : 0);
226
13.5k
        if (result_int_part_digit_count > total_significant_digit_count) {
227
0
            int_part_number *= get_scale_multiplier<T>(result_int_part_digit_count -
228
0
                                                       total_significant_digit_count);
229
0
        }
230
13.5k
    } else {
231
        // leading zeros of fraction part
232
0
        actual_frac_part_count = -result_int_part_digit_count;
233
0
    }
234
    // get fraction part number
235
143k
    for (; digit_index != end_digit_index && actual_frac_part_count < type_scale; ++digit_index) {
236
129k
        if (UNLIKELY(s[digit_index] == '.')) {
237
11.4k
            continue;
238
11.4k
        }
239
118k
        frac_part_number = frac_part_number * 10 + (s[digit_index] - '0');
240
118k
        ++actual_frac_part_count;
241
118k
    }
242
13.5k
    auto type_scale_multiplier = get_scale_multiplier<T>(type_scale);
243
    // Round only when the next parsed significand digit is exactly the first discarded fractional
244
    // digit. If `actual_frac_part_count` is already greater than type_scale, the missing positions
245
    // are implicit zeros from a negative exponent, so "5e-17" to scale 15 must stay 0 instead of
246
    // rounding up.
247
13.5k
    if (actual_frac_part_count == type_scale && digit_index != end_digit_index) {
248
17
        if (UNLIKELY(s[digit_index] == '.')) {
249
0
            ++digit_index;
250
0
        }
251
17
        if (digit_index != end_digit_index) {
252
            // example: test 1.5 -> decimal(1, 0)
253
17
            if (s[digit_index] >= '5') {
254
17
                ++frac_part_number;
255
17
                if (frac_part_number == type_scale_multiplier) {
256
0
                    frac_part_number = 0;
257
0
                    ++int_part_number;
258
0
                }
259
17
            }
260
17
        }
261
13.5k
    } else {
262
13.5k
        if (actual_frac_part_count < type_scale) {
263
1.94k
            frac_part_number *= get_scale_multiplier<T>(type_scale - actual_frac_part_count);
264
1.94k
        }
265
13.5k
    }
266
13.5k
    if (int_part_number >= get_scale_multiplier<T>(type_precision - type_scale)) {
267
0
        *result = is_negative ? StringParser::PARSE_UNDERFLOW : StringParser::PARSE_OVERFLOW;
268
0
        return 0;
269
0
    }
270
271
13.5k
    T value = int_part_number * type_scale_multiplier + frac_part_number;
272
13.5k
    *result = StringParser::PARSE_SUCCESS;
273
13.5k
    return is_negative ? T(-value) : T(value);
274
13.5k
}
_ZN5doris12StringParser17string_to_decimalILNS_13PrimitiveTypeE35EEENS_19PrimitiveTypeTraitsIXT_EE7CppType10NativeTypeEPKcmiiPNS0_11ParseResultE
Line
Count
Source
61
117k
        ParseResult* result) {
62
117k
    using T = typename PrimitiveTypeTraits<P>::CppType::NativeType;
63
117k
    static_assert(std::is_same_v<T, int32_t> || std::is_same_v<T, int64_t> ||
64
117k
                          std::is_same_v<T, __int128> || std::is_same_v<T, wide::Int256>,
65
117k
                  "Cast string to decimal only support target type int32_t, int64_t, __int128 or "
66
117k
                  "wide::Int256.");
67
68
    // Parse in two logical coordinate systems:
69
    // 1. `s[0, end_digit_index)` is the normalized significand after trimming spaces, sign and
70
    //    leading zeros. If the original value starts with '.', the dot is also skipped so
71
    //    ".14E+3" is parsed as significand "14" with exponent 3.
72
    // 2. `result_int_part_digit_count = int_part_count + exponent` is the decimal point position
73
    //    after applying scientific notation. For example, "1.4E+2" has int_part_count=1,
74
    //    exponent=2, result_int_part_digit_count=3, so "14" becomes integer 140.
75
    // `digit_index` always indexes the normalized significand string, which may still contain a
76
    // dot for inputs like "1.4E+2"; loops that build numbers skip that dot explicitly.
77
    // Ignore leading and trailing spaces.
78
117k
    s = skip_ascii_whitespaces(s, len);
79
80
117k
    bool is_negative = false;
81
117k
    if (len > 0) {
82
117k
        switch (*s) {
83
17.3k
        case '-':
84
17.3k
            is_negative = true;
85
17.3k
            [[fallthrough]];
86
23.9k
        case '+':
87
23.9k
            ++s;
88
23.9k
            --len;
89
117k
        }
90
117k
    }
91
    // Ignore leading zeros.
92
117k
    bool found_value = false;
93
211k
    while (len > 0 && UNLIKELY(*s == '0')) {
94
94.0k
        found_value = true;
95
94.0k
        ++s;
96
94.0k
        --len;
97
94.0k
    }
98
99
117k
    int found_dot = 0;
100
117k
    if (len > 0 && *s == '.') {
101
15.8k
        found_dot = 1;
102
15.8k
        ++s;
103
15.8k
        --len;
104
15.8k
    }
105
117k
    int int_part_count = 0;
106
117k
    int i = 0;
107
3.93M
    for (; i != len; ++i) {
108
3.89M
        const char& c = s[i];
109
3.89M
        if (LIKELY('0' <= c && c <= '9')) {
110
3.72M
            found_value = true;
111
3.72M
            if (!found_dot) {
112
994k
                ++int_part_count;
113
994k
            }
114
3.72M
        } else if (c == '.') {
115
95.6k
            if (found_dot) {
116
0
                *result = StringParser::PARSE_FAILURE;
117
0
                return 0;
118
0
            }
119
95.6k
            found_dot = 1;
120
95.6k
        } else {
121
77.9k
            break;
122
77.9k
        }
123
3.89M
    }
124
117k
    if (!found_value) {
125
        // '', '.'
126
66
        *result = StringParser::PARSE_FAILURE;
127
66
        return 0;
128
66
    }
129
    // Parse exponent if any. Keep `end_digit_index` before consuming 'e/E' so later digit counts
130
    // ignore exponent syntax. For "1.4E+2", end_digit_index points just after "1.4", not after
131
    // "E+2".
132
117k
    int64_t exponent = 0;
133
117k
    auto end_digit_index = i;
134
117k
    if (i != len) {
135
77.8k
        bool negative_exponent = false;
136
77.8k
        if (s[i] == 'e' || s[i] == 'E') {
137
77.8k
            ++i;
138
77.8k
            if (i != len) {
139
77.8k
                switch (s[i]) {
140
1.53k
                case '-':
141
1.53k
                    negative_exponent = true;
142
1.53k
                    [[fallthrough]];
143
70.0k
                case '+':
144
70.0k
                    ++i;
145
77.8k
                }
146
77.8k
            }
147
77.8k
            if (i == len) {
148
                // '123e', '123e+', '123e-'
149
0
                *result = StringParser::PARSE_FAILURE;
150
0
                return 0;
151
0
            }
152
232k
            for (; i != len; ++i) {
153
154k
                const char& c = s[i];
154
154k
                if (LIKELY('0' <= c && c <= '9')) {
155
154k
                    exponent = exponent * 10 + (c - '0');
156
                    // max string len is config::string_type_length_soft_limit_bytes,
157
                    // whose max value is std::numeric_limits<int32_t>::max() - 4,
158
                    // just check overflow of int32_t to simplify the logic
159
                    // For edge cases like 0.{2147483647 zeros}e+2147483647
160
154k
                    if (exponent > std::numeric_limits<int32_t>::max()) {
161
0
                        *result = StringParser::PARSE_OVERFLOW;
162
0
                        return 0;
163
0
                    }
164
154k
                } else {
165
                    // '123e12abc', '123e1.2'
166
10
                    *result = StringParser::PARSE_FAILURE;
167
10
                    return 0;
168
10
                }
169
154k
            }
170
77.8k
            if (negative_exponent) {
171
1.53k
                exponent = -exponent;
172
1.53k
            }
173
77.8k
        } else {
174
14
            *result = StringParser::PARSE_FAILURE;
175
14
            return 0;
176
14
        }
177
77.8k
    }
178
    // TODO: check limit values of exponent and add UT
179
    // max string len is config::string_type_length_soft_limit_bytes,
180
    // whose max value is std::numeric_limits<int32_t>::max() - 4,
181
    // so int_part_count will be in range of int32_t,
182
    // and int_part_count + exponent will be in range of int64_t
183
117k
    int64_t tmp_result_int_part_digit_count = int_part_count + exponent;
184
117k
    if (tmp_result_int_part_digit_count > std::numeric_limits<int>::max() ||
185
117k
        tmp_result_int_part_digit_count < std::numeric_limits<int>::min()) {
186
0
        *result = is_negative ? StringParser::PARSE_UNDERFLOW : StringParser::PARSE_OVERFLOW;
187
0
        return 0;
188
0
    }
189
117k
    int result_int_part_digit_count = tmp_result_int_part_digit_count;
190
117k
    T int_part_number = 0;
191
117k
    T frac_part_number = 0;
192
117k
    int actual_frac_part_count = 0;
193
117k
    int digit_index = 0;
194
117k
    if (result_int_part_digit_count >= 0) {
195
        // `max_index` is the raw significand index where integer-part digits stop. Add one extra
196
        // raw character only when crossing an in-buffer dot, e.g. "1.4E+2" must scan "1.4" to
197
        // collect three integer digits after the exponent shift. It is capped by end_digit_index
198
        // because missing digits are appended later by multiplying with powers of 10.
199
117k
        int max_index = std::min(found_dot ? (result_int_part_digit_count +
200
111k
                                              ((int_part_count > 0 && exponent > 0) ? 1 : 0))
201
117k
                                           : result_int_part_digit_count,
202
117k
                                 end_digit_index);
203
117k
        max_index = (max_index == std::numeric_limits<int>::min() ? end_digit_index : max_index);
204
        // skip zero number
205
334k
        for (; digit_index != max_index && s[digit_index] == '0'; ++digit_index) {
206
216k
        }
207
        // test 0.00, .00, 0.{00...}e2147483647
208
        // 0.00000e2147483647
209
117k
        if (digit_index != max_index &&
210
117k
            (result_int_part_digit_count - digit_index > type_precision - type_scale)) {
211
112
            *result = is_negative ? StringParser::PARSE_UNDERFLOW : StringParser::PARSE_OVERFLOW;
212
112
            return 0;
213
112
        }
214
        // get int part number
215
2.20M
        for (; digit_index != max_index; ++digit_index) {
216
2.08M
            if (UNLIKELY(s[digit_index] == '.')) {
217
69.4k
                continue;
218
69.4k
            }
219
2.01M
            int_part_number = int_part_number * 10 + (s[digit_index] - '0');
220
2.01M
        }
221
        // Count only significand digits, not exponent syntax. If the exponent moves the decimal
222
        // point past all available significant digits, append zeros by scaling the integer part:
223
        // "1.4E+2" scans integer 14, total_significant_digit_count=2, then multiplies by 10.
224
117k
        auto total_significant_digit_count =
225
117k
                end_digit_index - ((found_dot && int_part_count > 0) ? 1 : 0);
226
117k
        if (result_int_part_digit_count > total_significant_digit_count) {
227
66.4k
            int_part_number *= get_scale_multiplier<T>(result_int_part_digit_count -
228
66.4k
                                                       total_significant_digit_count);
229
66.4k
        }
230
117k
    } else {
231
        // leading zeros of fraction part
232
48
        actual_frac_part_count = -result_int_part_digit_count;
233
48
    }
234
    // get fraction part number
235
1.51M
    for (; digit_index != end_digit_index && actual_frac_part_count < type_scale; ++digit_index) {
236
1.40M
        if (UNLIKELY(s[digit_index] == '.')) {
237
24.2k
            continue;
238
24.2k
        }
239
1.37M
        frac_part_number = frac_part_number * 10 + (s[digit_index] - '0');
240
1.37M
        ++actual_frac_part_count;
241
1.37M
    }
242
117k
    auto type_scale_multiplier = get_scale_multiplier<T>(type_scale);
243
    // Round only when the next parsed significand digit is exactly the first discarded fractional
244
    // digit. If `actual_frac_part_count` is already greater than type_scale, the missing positions
245
    // are implicit zeros from a negative exponent, so "5e-17" to scale 15 must stay 0 instead of
246
    // rounding up.
247
117k
    if (actual_frac_part_count == type_scale && digit_index != end_digit_index) {
248
19.0k
        if (UNLIKELY(s[digit_index] == '.')) {
249
852
            ++digit_index;
250
852
        }
251
19.0k
        if (digit_index != end_digit_index) {
252
            // example: test 1.5 -> decimal(1, 0)
253
18.8k
            if (s[digit_index] >= '5') {
254
7.94k
                ++frac_part_number;
255
7.94k
                if (frac_part_number == type_scale_multiplier) {
256
836
                    frac_part_number = 0;
257
836
                    ++int_part_number;
258
836
                }
259
7.94k
            }
260
18.8k
        }
261
98.5k
    } else {
262
98.5k
        if (actual_frac_part_count < type_scale) {
263
89.3k
            frac_part_number *= get_scale_multiplier<T>(type_scale - actual_frac_part_count);
264
89.3k
        }
265
98.5k
    }
266
117k
    if (int_part_number >= get_scale_multiplier<T>(type_precision - type_scale)) {
267
16
        *result = is_negative ? StringParser::PARSE_UNDERFLOW : StringParser::PARSE_OVERFLOW;
268
16
        return 0;
269
16
    }
270
271
117k
    T value = int_part_number * type_scale_multiplier + frac_part_number;
272
117k
    *result = StringParser::PARSE_SUCCESS;
273
117k
    return is_negative ? T(-value) : T(value);
274
117k
}
275
276
template Int32 StringParser::string_to_decimal<PrimitiveType::TYPE_DECIMAL32>(
277
        const char* __restrict s, size_t len, int type_precision, int type_scale,
278
        ParseResult* result);
279
template Int64 StringParser::string_to_decimal<PrimitiveType::TYPE_DECIMAL64>(
280
        const char* __restrict s, size_t len, int type_precision, int type_scale,
281
        ParseResult* result);
282
template Int128 StringParser::string_to_decimal<PrimitiveType::TYPE_DECIMAL128I>(
283
        const char* __restrict s, size_t len, int type_precision, int type_scale,
284
        ParseResult* result);
285
template Int128 StringParser::string_to_decimal<PrimitiveType::TYPE_DECIMALV2>(
286
        const char* __restrict s, size_t len, int type_precision, int type_scale,
287
        ParseResult* result);
288
template wide::Int256 StringParser::string_to_decimal<PrimitiveType::TYPE_DECIMAL256>(
289
        const char* __restrict s, size_t len, int type_precision, int type_scale,
290
        ParseResult* result);
291
} // end namespace doris
292
#include "common/compile_check_avoid_end.h"