Coverage Report

Created: 2026-05-19 04: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
3.80M
        ParseResult* result) {
62
3.80M
    using T = typename PrimitiveTypeTraits<P>::CppType::NativeType;
63
3.80M
    static_assert(std::is_same_v<T, int32_t> || std::is_same_v<T, int64_t> ||
64
3.80M
                          std::is_same_v<T, __int128> || std::is_same_v<T, wide::Int256>,
65
3.80M
                  "Cast string to decimal only support target type int32_t, int64_t, __int128 or "
66
3.80M
                  "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
3.80M
    s = skip_ascii_whitespaces(s, len);
79
80
3.80M
    bool is_negative = false;
81
3.81M
    if (len > 0) {
82
3.81M
        switch (*s) {
83
422k
        case '-':
84
422k
            is_negative = true;
85
422k
            [[fallthrough]];
86
449k
        case '+':
87
449k
            ++s;
88
449k
            --len;
89
3.81M
        }
90
3.81M
    }
91
    // Ignore leading zeros.
92
3.80M
    bool found_value = false;
93
4.14M
    while (len > 0 && UNLIKELY(*s == '0')) {
94
338k
        found_value = true;
95
338k
        ++s;
96
338k
        --len;
97
338k
    }
98
99
3.80M
    int found_dot = 0;
100
3.80M
    if (len > 0 && *s == '.') {
101
84.8k
        found_dot = 1;
102
84.8k
        ++s;
103
84.8k
        --len;
104
84.8k
    }
105
3.80M
    int int_part_count = 0;
106
3.80M
    int i = 0;
107
43.4M
    for (; i != len; ++i) {
108
39.7M
        const char& c = s[i];
109
39.7M
        if (LIKELY('0' <= c && c <= '9')) {
110
36.0M
            found_value = true;
111
36.0M
            if (!found_dot) {
112
8.87M
                ++int_part_count;
113
8.87M
            }
114
36.0M
        } else if (c == '.') {
115
3.54M
            if (found_dot) {
116
2
                *result = StringParser::PARSE_FAILURE;
117
2
                return 0;
118
2
            }
119
3.54M
            found_dot = 1;
120
3.54M
        } else {
121
103k
            break;
122
103k
        }
123
39.7M
    }
124
3.80M
    if (!found_value) {
125
        // '', '.'
126
438
        *result = StringParser::PARSE_FAILURE;
127
438
        return 0;
128
438
    }
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
3.80M
    int64_t exponent = 0;
133
3.80M
    auto end_digit_index = i;
134
3.80M
    if (i != len) {
135
109k
        bool negative_exponent = false;
136
109k
        if (s[i] == 'e' || s[i] == 'E') {
137
109k
            ++i;
138
109k
            if (i != len) {
139
109k
                switch (s[i]) {
140
11.5k
                case '-':
141
11.5k
                    negative_exponent = true;
142
11.5k
                    [[fallthrough]];
143
78.1k
                case '+':
144
78.1k
                    ++i;
145
109k
                }
146
109k
            }
147
109k
            if (i == len) {
148
                // '123e', '123e+', '123e-'
149
6
                *result = StringParser::PARSE_FAILURE;
150
6
                return 0;
151
6
            }
152
319k
            for (; i != len; ++i) {
153
209k
                const char& c = s[i];
154
209k
                if (LIKELY('0' <= c && c <= '9')) {
155
209k
                    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
209k
                    if (exponent > std::numeric_limits<int32_t>::max()) {
161
0
                        *result = StringParser::PARSE_OVERFLOW;
162
0
                        return 0;
163
0
                    }
164
209k
                } else {
165
                    // '123e12abc', '123e1.2'
166
22
                    *result = StringParser::PARSE_FAILURE;
167
22
                    return 0;
168
22
                }
169
209k
            }
170
109k
            if (negative_exponent) {
171
11.5k
                exponent = -exponent;
172
11.5k
            }
173
109k
        } else {
174
120
            *result = StringParser::PARSE_FAILURE;
175
120
            return 0;
176
120
        }
177
109k
    }
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
3.80M
    int64_t tmp_result_int_part_digit_count = int_part_count + exponent;
184
3.80M
    if (tmp_result_int_part_digit_count > std::numeric_limits<int>::max() ||
185
3.80M
        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
3.80M
    int result_int_part_digit_count = tmp_result_int_part_digit_count;
190
3.80M
    T int_part_number = 0;
191
3.80M
    T frac_part_number = 0;
192
3.80M
    int actual_frac_part_count = 0;
193
3.80M
    int digit_index = 0;
194
3.80M
    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
3.80M
        int max_index = std::min(found_dot ? (result_int_part_digit_count +
200
3.63M
                                              ((int_part_count > 0 && exponent > 0) ? 1 : 0))
201
3.80M
                                           : result_int_part_digit_count,
202
3.80M
                                 end_digit_index);
203
3.80M
        max_index = (max_index == std::numeric_limits<int>::min() ? end_digit_index : max_index);
204
        // skip zero number
205
4.66M
        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
3.80M
        if (digit_index != max_index &&
210
3.80M
            (result_int_part_digit_count - digit_index > type_precision - type_scale)) {
211
11.7k
            *result = is_negative ? StringParser::PARSE_UNDERFLOW : StringParser::PARSE_OVERFLOW;
212
11.7k
            return 0;
213
11.7k
        }
214
        // get int part number
215
13.4M
        for (; digit_index != max_index; ++digit_index) {
216
9.65M
            if (UNLIKELY(s[digit_index] == '.')) {
217
71.1k
                continue;
218
71.1k
            }
219
9.58M
            int_part_number = int_part_number * 10 + (s[digit_index] - '0');
220
9.58M
        }
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
3.79M
        auto total_significant_digit_count =
225
3.79M
                end_digit_index - ((found_dot && int_part_count > 0) ? 1 : 0);
226
3.79M
        if (result_int_part_digit_count > total_significant_digit_count) {
227
64.8k
            int_part_number *= get_scale_multiplier<T>(result_int_part_digit_count -
228
64.8k
                                                       total_significant_digit_count);
229
64.8k
        }
230
3.79M
    } else {
231
        // leading zeros of fraction part
232
4.56k
        actual_frac_part_count = -result_int_part_digit_count;
233
4.56k
    }
234
    // get fraction part number
235
32.3M
    for (; digit_index != end_digit_index && actual_frac_part_count < type_scale; ++digit_index) {
236
28.5M
        if (UNLIKELY(s[digit_index] == '.')) {
237
3.46M
            continue;
238
3.46M
        }
239
25.1M
        frac_part_number = frac_part_number * 10 + (s[digit_index] - '0');
240
25.1M
        ++actual_frac_part_count;
241
25.1M
    }
242
3.79M
    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
3.79M
    if (actual_frac_part_count == type_scale && digit_index != end_digit_index) {
248
80.7k
        if (UNLIKELY(s[digit_index] == '.')) {
249
3.46k
            ++digit_index;
250
3.46k
        }
251
80.7k
        if (digit_index != end_digit_index) {
252
            // example: test 1.5 -> decimal(1, 0)
253
79.7k
            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.7k
        }
261
3.71M
    } else {
262
3.71M
        if (actual_frac_part_count < type_scale) {
263
200k
            frac_part_number *= get_scale_multiplier<T>(type_scale - actual_frac_part_count);
264
200k
        }
265
3.71M
    }
266
3.79M
    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
3.79M
    T value = int_part_number * type_scale_multiplier + frac_part_number;
272
3.79M
    *result = StringParser::PARSE_SUCCESS;
273
3.79M
    return is_negative ? T(-value) : T(value);
274
3.79M
}
_ZN5doris12StringParser17string_to_decimalILNS_13PrimitiveTypeE28EEENS_19PrimitiveTypeTraitsIXT_EE7CppType10NativeTypeEPKcmiiPNS0_11ParseResultE
Line
Count
Source
61
1.77M
        ParseResult* result) {
62
1.77M
    using T = typename PrimitiveTypeTraits<P>::CppType::NativeType;
63
1.77M
    static_assert(std::is_same_v<T, int32_t> || std::is_same_v<T, int64_t> ||
64
1.77M
                          std::is_same_v<T, __int128> || std::is_same_v<T, wide::Int256>,
65
1.77M
                  "Cast string to decimal only support target type int32_t, int64_t, __int128 or "
66
1.77M
                  "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
1.77M
    s = skip_ascii_whitespaces(s, len);
79
80
1.77M
    bool is_negative = false;
81
1.77M
    if (len > 0) {
82
1.77M
        switch (*s) {
83
189k
        case '-':
84
189k
            is_negative = true;
85
189k
            [[fallthrough]];
86
196k
        case '+':
87
196k
            ++s;
88
196k
            --len;
89
1.77M
        }
90
1.77M
    }
91
    // Ignore leading zeros.
92
1.77M
    bool found_value = false;
93
1.82M
    while (len > 0 && UNLIKELY(*s == '0')) {
94
53.7k
        found_value = true;
95
53.7k
        ++s;
96
53.7k
        --len;
97
53.7k
    }
98
99
1.77M
    int found_dot = 0;
100
1.77M
    if (len > 0 && *s == '.') {
101
18.0k
        found_dot = 1;
102
18.0k
        ++s;
103
18.0k
        --len;
104
18.0k
    }
105
1.77M
    int int_part_count = 0;
106
1.77M
    int i = 0;
107
10.9M
    for (; i != len; ++i) {
108
9.13M
        const char& c = s[i];
109
9.13M
        if (LIKELY('0' <= c && c <= '9')) {
110
7.51M
            found_value = true;
111
7.51M
            if (!found_dot) {
112
3.23M
                ++int_part_count;
113
3.23M
            }
114
7.51M
        } else if (c == '.') {
115
1.61M
            if (found_dot) {
116
2
                *result = StringParser::PARSE_FAILURE;
117
2
                return 0;
118
2
            }
119
1.61M
            found_dot = 1;
120
1.61M
        } else {
121
6.12k
            break;
122
6.12k
        }
123
9.13M
    }
124
1.77M
    if (!found_value) {
125
        // '', '.'
126
158
        *result = StringParser::PARSE_FAILURE;
127
158
        return 0;
128
158
    }
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
1.77M
    int64_t exponent = 0;
133
1.77M
    auto end_digit_index = i;
134
1.77M
    if (i != len) {
135
9.40k
        bool negative_exponent = false;
136
9.40k
        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
66
            *result = StringParser::PARSE_FAILURE;
175
66
            return 0;
176
66
        }
177
9.40k
    }
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
1.77M
    int64_t tmp_result_int_part_digit_count = int_part_count + exponent;
184
1.77M
    if (tmp_result_int_part_digit_count > std::numeric_limits<int>::max() ||
185
1.77M
        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
1.77M
    int result_int_part_digit_count = tmp_result_int_part_digit_count;
190
1.77M
    T int_part_number = 0;
191
1.77M
    T frac_part_number = 0;
192
1.77M
    int actual_frac_part_count = 0;
193
1.77M
    int digit_index = 0;
194
1.77M
    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
1.77M
        int max_index = std::min(found_dot ? (result_int_part_digit_count +
200
1.63M
                                              ((int_part_count > 0 && exponent > 0) ? 1 : 0))
201
1.77M
                                           : result_int_part_digit_count,
202
1.77M
                                 end_digit_index);
203
1.77M
        max_index = (max_index == std::numeric_limits<int>::min() ? end_digit_index : max_index);
204
        // skip zero number
205
1.98M
        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
1.77M
        if (digit_index != max_index &&
210
1.77M
            (result_int_part_digit_count - digit_index > type_precision - type_scale)) {
211
1.30k
            *result = is_negative ? StringParser::PARSE_UNDERFLOW : StringParser::PARSE_OVERFLOW;
212
1.30k
            return 0;
213
1.30k
        }
214
        // get int part number
215
4.95M
        for (; digit_index != max_index; ++digit_index) {
216
3.17M
            if (UNLIKELY(s[digit_index] == '.')) {
217
1.60k
                continue;
218
1.60k
            }
219
3.17M
            int_part_number = int_part_number * 10 + (s[digit_index] - '0');
220
3.17M
        }
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
1.77M
        auto total_significant_digit_count =
225
1.77M
                end_digit_index - ((found_dot && int_part_count > 0) ? 1 : 0);
226
1.77M
        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
18.4E
    } else {
231
        // leading zeros of fraction part
232
18.4E
        actual_frac_part_count = -result_int_part_digit_count;
233
18.4E
    }
234
    // get fraction part number
235
7.42M
    for (; digit_index != end_digit_index && actual_frac_part_count < type_scale; ++digit_index) {
236
5.65M
        if (UNLIKELY(s[digit_index] == '.')) {
237
1.61M
            continue;
238
1.61M
        }
239
4.04M
        frac_part_number = frac_part_number * 10 + (s[digit_index] - '0');
240
4.04M
        ++actual_frac_part_count;
241
4.04M
    }
242
1.77M
    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
1.77M
    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
1.75M
    } else {
262
1.75M
        if (actual_frac_part_count < type_scale) {
263
27.7k
            frac_part_number *= get_scale_multiplier<T>(type_scale - actual_frac_part_count);
264
27.7k
        }
265
1.75M
    }
266
1.77M
    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
1.77M
    T value = int_part_number * type_scale_multiplier + frac_part_number;
272
1.77M
    *result = StringParser::PARSE_SUCCESS;
273
1.77M
    return is_negative ? T(-value) : T(value);
274
1.77M
}
_ZN5doris12StringParser17string_to_decimalILNS_13PrimitiveTypeE29EEENS_19PrimitiveTypeTraitsIXT_EE7CppType10NativeTypeEPKcmiiPNS0_11ParseResultE
Line
Count
Source
61
1.19M
        ParseResult* result) {
62
1.19M
    using T = typename PrimitiveTypeTraits<P>::CppType::NativeType;
63
1.19M
    static_assert(std::is_same_v<T, int32_t> || std::is_same_v<T, int64_t> ||
64
1.19M
                          std::is_same_v<T, __int128> || std::is_same_v<T, wide::Int256>,
65
1.19M
                  "Cast string to decimal only support target type int32_t, int64_t, __int128 or "
66
1.19M
                  "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
1.19M
    s = skip_ascii_whitespaces(s, len);
79
80
1.19M
    bool is_negative = false;
81
1.19M
    if (len > 0) {
82
1.19M
        switch (*s) {
83
104k
        case '-':
84
104k
            is_negative = true;
85
104k
            [[fallthrough]];
86
111k
        case '+':
87
111k
            ++s;
88
111k
            --len;
89
1.19M
        }
90
1.19M
    }
91
    // Ignore leading zeros.
92
1.19M
    bool found_value = false;
93
1.27M
    while (len > 0 && UNLIKELY(*s == '0')) {
94
76.0k
        found_value = true;
95
76.0k
        ++s;
96
76.0k
        --len;
97
76.0k
    }
98
99
1.19M
    int found_dot = 0;
100
1.19M
    if (len > 0 && *s == '.') {
101
23.8k
        found_dot = 1;
102
23.8k
        ++s;
103
23.8k
        --len;
104
23.8k
    }
105
1.19M
    int int_part_count = 0;
106
1.19M
    int i = 0;
107
13.9M
    for (; i != len; ++i) {
108
12.7M
        const char& c = s[i];
109
12.7M
        if (LIKELY('0' <= c && c <= '9')) {
110
11.6M
            found_value = true;
111
11.6M
            if (!found_dot) {
112
2.26M
                ++int_part_count;
113
2.26M
            }
114
11.6M
        } else if (c == '.') {
115
1.16M
            if (found_dot) {
116
0
                *result = StringParser::PARSE_FAILURE;
117
0
                return 0;
118
0
            }
119
1.16M
            found_dot = 1;
120
1.16M
        } else {
121
10.2k
            break;
122
10.2k
        }
123
12.7M
    }
124
1.19M
    if (!found_value) {
125
        // '', '.'
126
73
        *result = StringParser::PARSE_FAILURE;
127
73
        return 0;
128
73
    }
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
1.19M
    int64_t exponent = 0;
133
1.19M
    auto end_digit_index = i;
134
1.19M
    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.84k
                case '-':
141
3.84k
                    negative_exponent = true;
142
3.84k
                    [[fallthrough]];
143
3.84k
                case '+':
144
3.84k
                    ++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.3k
            for (; i != len; ++i) {
153
20.7k
                const char& c = s[i];
154
20.7k
                if (LIKELY('0' <= c && c <= '9')) {
155
20.7k
                    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.7k
                    if (exponent > std::numeric_limits<int32_t>::max()) {
161
0
                        *result = StringParser::PARSE_OVERFLOW;
162
0
                        return 0;
163
0
                    }
164
20.7k
                } else {
165
                    // '123e12abc', '123e1.2'
166
0
                    *result = StringParser::PARSE_FAILURE;
167
0
                    return 0;
168
0
                }
169
20.7k
            }
170
11.6k
            if (negative_exponent) {
171
3.84k
                exponent = -exponent;
172
3.84k
            }
173
11.6k
        } else {
174
25
            *result = StringParser::PARSE_FAILURE;
175
25
            return 0;
176
25
        }
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
1.19M
    int64_t tmp_result_int_part_digit_count = int_part_count + exponent;
184
1.19M
    if (tmp_result_int_part_digit_count > std::numeric_limits<int>::max() ||
185
1.19M
        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
1.19M
    int result_int_part_digit_count = tmp_result_int_part_digit_count;
190
1.19M
    T int_part_number = 0;
191
1.19M
    T frac_part_number = 0;
192
1.19M
    int actual_frac_part_count = 0;
193
1.19M
    int digit_index = 0;
194
1.19M
    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
1.19M
        int max_index = std::min(found_dot ? (result_int_part_digit_count +
200
1.18M
                                              ((int_part_count > 0 && exponent > 0) ? 1 : 0))
201
1.19M
                                           : result_int_part_digit_count,
202
1.19M
                                 end_digit_index);
203
1.19M
        max_index = (max_index == std::numeric_limits<int>::min() ? end_digit_index : max_index);
204
        // skip zero number
205
1.40M
        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
1.19M
        if (digit_index != max_index &&
210
1.19M
            (result_int_part_digit_count - digit_index > type_precision - type_scale)) {
211
10.1k
            *result = is_negative ? StringParser::PARSE_UNDERFLOW : StringParser::PARSE_OVERFLOW;
212
10.1k
            return 0;
213
10.1k
        }
214
        // get int part number
215
3.26M
        for (; digit_index != max_index; ++digit_index) {
216
2.08M
            if (UNLIKELY(s[digit_index] == '.')) {
217
960
                continue;
218
960
            }
219
2.08M
            int_part_number = int_part_number * 10 + (s[digit_index] - '0');
220
2.08M
        }
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
1.18M
        auto total_significant_digit_count =
225
1.18M
                end_digit_index - ((found_dot && int_part_count > 0) ? 1 : 0);
226
1.18M
        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
1.18M
    } else {
231
        // leading zeros of fraction part
232
2.99k
        actual_frac_part_count = -result_int_part_digit_count;
233
2.99k
    }
234
    // get fraction part number
235
11.4M
    for (; digit_index != end_digit_index && actual_frac_part_count < type_scale; ++digit_index) {
236
10.2M
        if (UNLIKELY(s[digit_index] == '.')) {
237
1.15M
            continue;
238
1.15M
        }
239
9.09M
        frac_part_number = frac_part_number * 10 + (s[digit_index] - '0');
240
9.09M
        ++actual_frac_part_count;
241
9.09M
    }
242
1.18M
    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
1.18M
    if (actual_frac_part_count == type_scale && digit_index != end_digit_index) {
248
20.0k
        if (UNLIKELY(s[digit_index] == '.')) {
249
852
            ++digit_index;
250
852
        }
251
20.0k
        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
1.16M
    } else {
262
1.16M
        if (actual_frac_part_count < type_scale) {
263
31.9k
            frac_part_number *= get_scale_multiplier<T>(type_scale - actual_frac_part_count);
264
31.9k
        }
265
1.16M
    }
266
1.18M
    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
1.18M
    T value = int_part_number * type_scale_multiplier + frac_part_number;
272
1.18M
    *result = StringParser::PARSE_SUCCESS;
273
1.18M
    return is_negative ? T(-value) : T(value);
274
1.18M
}
_ZN5doris12StringParser17string_to_decimalILNS_13PrimitiveTypeE30EEENS_19PrimitiveTypeTraitsIXT_EE7CppType10NativeTypeEPKcmiiPNS0_11ParseResultE
Line
Count
Source
61
707k
        ParseResult* result) {
62
707k
    using T = typename PrimitiveTypeTraits<P>::CppType::NativeType;
63
707k
    static_assert(std::is_same_v<T, int32_t> || std::is_same_v<T, int64_t> ||
64
707k
                          std::is_same_v<T, __int128> || std::is_same_v<T, wide::Int256>,
65
707k
                  "Cast string to decimal only support target type int32_t, int64_t, __int128 or "
66
707k
                  "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
707k
    s = skip_ascii_whitespaces(s, len);
79
80
707k
    bool is_negative = false;
81
707k
    if (len > 0) {
82
707k
        switch (*s) {
83
104k
        case '-':
84
104k
            is_negative = true;
85
104k
            [[fallthrough]];
86
110k
        case '+':
87
110k
            ++s;
88
110k
            --len;
89
707k
        }
90
707k
    }
91
    // Ignore leading zeros.
92
707k
    bool found_value = false;
93
783k
    while (len > 0 && UNLIKELY(*s == '0')) {
94
76.1k
        found_value = true;
95
76.1k
        ++s;
96
76.1k
        --len;
97
76.1k
    }
98
99
707k
    int found_dot = 0;
100
707k
    if (len > 0 && *s == '.') {
101
25.1k
        found_dot = 1;
102
25.1k
        ++s;
103
25.1k
        --len;
104
25.1k
    }
105
707k
    int int_part_count = 0;
106
707k
    int i = 0;
107
14.3M
    for (; i != len; ++i) {
108
13.6M
        const char& c = s[i];
109
13.6M
        if (LIKELY('0' <= c && c <= '9')) {
110
12.9M
            found_value = true;
111
12.9M
            if (!found_dot) {
112
2.24M
                ++int_part_count;
113
2.24M
            }
114
12.9M
        } else if (c == '.') {
115
667k
            if (found_dot) {
116
0
                *result = StringParser::PARSE_FAILURE;
117
0
                return 0;
118
0
            }
119
667k
            found_dot = 1;
120
667k
        } else {
121
10.9k
            break;
122
10.9k
        }
123
13.6M
    }
124
707k
    if (!found_value) {
125
        // '', '.'
126
131
        *result = StringParser::PARSE_FAILURE;
127
131
        return 0;
128
131
    }
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
707k
    int64_t exponent = 0;
133
707k
    auto end_digit_index = i;
134
707k
    if (i != len) {
135
12.3k
        bool negative_exponent = false;
136
12.3k
        if (s[i] == 'e' || s[i] == 'E') {
137
12.3k
            ++i;
138
12.3k
            if (i != len) {
139
12.3k
                switch (s[i]) {
140
4.62k
                case '-':
141
4.62k
                    negative_exponent = true;
142
4.62k
                    [[fallthrough]];
143
4.62k
                case '+':
144
4.62k
                    ++i;
145
12.3k
                }
146
12.3k
            }
147
12.3k
            if (i == len) {
148
                // '123e', '123e+', '123e-'
149
0
                *result = StringParser::PARSE_FAILURE;
150
0
                return 0;
151
0
            }
152
35.7k
            for (; i != len; ++i) {
153
23.3k
                const char& c = s[i];
154
23.3k
                if (LIKELY('0' <= c && c <= '9')) {
155
23.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
23.3k
                    if (exponent > std::numeric_limits<int32_t>::max()) {
161
0
                        *result = StringParser::PARSE_OVERFLOW;
162
0
                        return 0;
163
0
                    }
164
23.3k
                } else {
165
                    // '123e12abc', '123e1.2'
166
0
                    *result = StringParser::PARSE_FAILURE;
167
0
                    return 0;
168
0
                }
169
23.3k
            }
170
12.3k
            if (negative_exponent) {
171
4.62k
                exponent = -exponent;
172
4.62k
            }
173
12.3k
        } else {
174
14
            *result = StringParser::PARSE_FAILURE;
175
14
            return 0;
176
14
        }
177
12.3k
    }
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
707k
    int64_t tmp_result_int_part_digit_count = int_part_count + exponent;
184
707k
    if (tmp_result_int_part_digit_count > std::numeric_limits<int>::max() ||
185
707k
        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
707k
    int result_int_part_digit_count = tmp_result_int_part_digit_count;
190
707k
    T int_part_number = 0;
191
707k
    T frac_part_number = 0;
192
707k
    int actual_frac_part_count = 0;
193
707k
    int digit_index = 0;
194
707k
    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
704k
        int max_index = std::min(found_dot ? (result_int_part_digit_count +
200
691k
                                              ((int_part_count > 0 && exponent > 0) ? 1 : 0))
201
704k
                                           : result_int_part_digit_count,
202
704k
                                 end_digit_index);
203
704k
        max_index = (max_index == std::numeric_limits<int>::min() ? end_digit_index : max_index);
204
        // skip zero number
205
918k
        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
704k
        if (digit_index != max_index &&
210
704k
            (result_int_part_digit_count - digit_index > type_precision - type_scale)) {
211
142
            *result = is_negative ? StringParser::PARSE_UNDERFLOW : StringParser::PARSE_OVERFLOW;
212
142
            return 0;
213
142
        }
214
        // get int part number
215
2.91M
        for (; digit_index != max_index; ++digit_index) {
216
2.20M
            if (UNLIKELY(s[digit_index] == '.')) {
217
962
                continue;
218
962
            }
219
2.20M
            int_part_number = int_part_number * 10 + (s[digit_index] - '0');
220
2.20M
        }
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
704k
        auto total_significant_digit_count =
225
704k
                end_digit_index - ((found_dot && int_part_count > 0) ? 1 : 0);
226
704k
        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
704k
    } else {
231
        // leading zeros of fraction part
232
2.40k
        actual_frac_part_count = -result_int_part_digit_count;
233
2.40k
    }
234
    // get fraction part number
235
11.8M
    for (; digit_index != end_digit_index && actual_frac_part_count < type_scale; ++digit_index) {
236
11.1M
        if (UNLIKELY(s[digit_index] == '.')) {
237
664k
            continue;
238
664k
        }
239
10.4M
        frac_part_number = frac_part_number * 10 + (s[digit_index] - '0');
240
10.4M
        ++actual_frac_part_count;
241
10.4M
    }
242
706k
    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
706k
    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.02k
                ++frac_part_number;
255
8.02k
                if (frac_part_number == type_scale_multiplier) {
256
906
                    frac_part_number = 0;
257
906
                    ++int_part_number;
258
906
                }
259
8.02k
            }
260
19.9k
        }
261
686k
    } else {
262
686k
        if (actual_frac_part_count < type_scale) {
263
51.5k
            frac_part_number *= get_scale_multiplier<T>(type_scale - actual_frac_part_count);
264
51.5k
        }
265
686k
    }
266
706k
    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
706k
    T value = int_part_number * type_scale_multiplier + frac_part_number;
272
706k
    *result = StringParser::PARSE_SUCCESS;
273
706k
    return is_negative ? T(-value) : T(value);
274
706k
}
_ZN5doris12StringParser17string_to_decimalILNS_13PrimitiveTypeE20EEENS_19PrimitiveTypeTraitsIXT_EE7CppType10NativeTypeEPKcmiiPNS0_11ParseResultE
Line
Count
Source
61
13.7k
        ParseResult* result) {
62
13.7k
    using T = typename PrimitiveTypeTraits<P>::CppType::NativeType;
63
13.7k
    static_assert(std::is_same_v<T, int32_t> || std::is_same_v<T, int64_t> ||
64
13.7k
                          std::is_same_v<T, __int128> || std::is_same_v<T, wide::Int256>,
65
13.7k
                  "Cast string to decimal only support target type int32_t, int64_t, __int128 or "
66
13.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
13.7k
    s = skip_ascii_whitespaces(s, len);
79
80
13.7k
    bool is_negative = false;
81
13.7k
    if (len > 0) {
82
13.7k
        switch (*s) {
83
6.72k
        case '-':
84
6.72k
            is_negative = true;
85
6.72k
            [[fallthrough]];
86
6.72k
        case '+':
87
6.72k
            ++s;
88
6.72k
            --len;
89
13.7k
        }
90
13.7k
    }
91
    // Ignore leading zeros.
92
13.7k
    bool found_value = false;
93
52.6k
    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.7k
    int found_dot = 0;
100
13.7k
    if (len > 0 && *s == '.') {
101
2.04k
        found_dot = 1;
102
2.04k
        ++s;
103
2.04k
        --len;
104
2.04k
    }
105
13.7k
    int int_part_count = 0;
106
13.7k
    int i = 0;
107
283k
    for (; i != len; ++i) {
108
269k
        const char& c = s[i];
109
269k
        if (LIKELY('0' <= c && c <= '9')) {
110
257k
            found_value = true;
111
257k
            if (!found_dot) {
112
137k
                ++int_part_count;
113
137k
            }
114
257k
        } else if (c == '.') {
115
11.6k
            if (found_dot) {
116
0
                *result = StringParser::PARSE_FAILURE;
117
0
                return 0;
118
0
            }
119
11.6k
            found_dot = 1;
120
11.6k
        } else {
121
11
            break;
122
11
        }
123
269k
    }
124
13.7k
    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.7k
    int64_t exponent = 0;
133
13.7k
    auto end_digit_index = i;
134
13.7k
    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.7k
    int64_t tmp_result_int_part_digit_count = int_part_count + exponent;
184
13.7k
    if (tmp_result_int_part_digit_count > std::numeric_limits<int>::max() ||
185
13.7k
        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.7k
    int result_int_part_digit_count = tmp_result_int_part_digit_count;
190
13.7k
    T int_part_number = 0;
191
13.7k
    T frac_part_number = 0;
192
13.7k
    int actual_frac_part_count = 0;
193
13.7k
    int digit_index = 0;
194
13.7k
    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.7k
        int max_index = std::min(found_dot ? (result_int_part_digit_count +
200
13.6k
                                              ((int_part_count > 0 && exponent > 0) ? 1 : 0))
201
13.7k
                                           : result_int_part_digit_count,
202
13.7k
                                 end_digit_index);
203
13.7k
        max_index = (max_index == std::numeric_limits<int>::min() ? end_digit_index : max_index);
204
        // skip zero number
205
13.7k
        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.7k
        if (digit_index != max_index &&
210
13.7k
            (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
151k
        for (; digit_index != max_index; ++digit_index) {
216
137k
            if (UNLIKELY(s[digit_index] == '.')) {
217
0
                continue;
218
0
            }
219
137k
            int_part_number = int_part_number * 10 + (s[digit_index] - '0');
220
137k
        }
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.7k
        auto total_significant_digit_count =
225
13.7k
                end_digit_index - ((found_dot && int_part_count > 0) ? 1 : 0);
226
13.7k
        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.7k
    } 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
145k
    for (; digit_index != end_digit_index && actual_frac_part_count < type_scale; ++digit_index) {
236
131k
        if (UNLIKELY(s[digit_index] == '.')) {
237
11.6k
            continue;
238
11.6k
        }
239
120k
        frac_part_number = frac_part_number * 10 + (s[digit_index] - '0');
240
120k
        ++actual_frac_part_count;
241
120k
    }
242
13.7k
    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.7k
    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.7k
    } else {
262
13.7k
        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.7k
    }
266
13.7k
    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.7k
    T value = int_part_number * type_scale_multiplier + frac_part_number;
272
13.7k
    *result = StringParser::PARSE_SUCCESS;
273
13.7k
    return is_negative ? T(-value) : T(value);
274
13.7k
}
_ZN5doris12StringParser17string_to_decimalILNS_13PrimitiveTypeE35EEENS_19PrimitiveTypeTraitsIXT_EE7CppType10NativeTypeEPKcmiiPNS0_11ParseResultE
Line
Count
Source
61
115k
        ParseResult* result) {
62
115k
    using T = typename PrimitiveTypeTraits<P>::CppType::NativeType;
63
115k
    static_assert(std::is_same_v<T, int32_t> || std::is_same_v<T, int64_t> ||
64
115k
                          std::is_same_v<T, __int128> || std::is_same_v<T, wide::Int256>,
65
115k
                  "Cast string to decimal only support target type int32_t, int64_t, __int128 or "
66
115k
                  "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
115k
    s = skip_ascii_whitespaces(s, len);
79
80
115k
    bool is_negative = false;
81
115k
    if (len > 0) {
82
115k
        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
115k
        }
90
115k
    }
91
    // Ignore leading zeros.
92
115k
    bool found_value = false;
93
209k
    while (len > 0 && UNLIKELY(*s == '0')) {
94
93.9k
        found_value = true;
95
93.9k
        ++s;
96
93.9k
        --len;
97
93.9k
    }
98
99
115k
    int found_dot = 0;
100
115k
    if (len > 0 && *s == '.') {
101
15.8k
        found_dot = 1;
102
15.8k
        ++s;
103
15.8k
        --len;
104
15.8k
    }
105
115k
    int int_part_count = 0;
106
115k
    int i = 0;
107
3.88M
    for (; i != len; ++i) {
108
3.84M
        const char& c = s[i];
109
3.84M
        if (LIKELY('0' <= c && c <= '9')) {
110
3.67M
            found_value = true;
111
3.67M
            if (!found_dot) {
112
988k
                ++int_part_count;
113
988k
            }
114
3.67M
        } else if (c == '.') {
115
93.4k
            if (found_dot) {
116
0
                *result = StringParser::PARSE_FAILURE;
117
0
                return 0;
118
0
            }
119
93.4k
            found_dot = 1;
120
93.4k
        } else {
121
76.0k
            break;
122
76.0k
        }
123
3.84M
    }
124
115k
    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
115k
    int64_t exponent = 0;
133
115k
    auto end_digit_index = i;
134
115k
    if (i != len) {
135
75.9k
        bool negative_exponent = false;
136
75.9k
        if (s[i] == 'e' || s[i] == 'E') {
137
75.9k
            ++i;
138
75.9k
            if (i != len) {
139
75.9k
                switch (s[i]) {
140
1.53k
                case '-':
141
1.53k
                    negative_exponent = true;
142
1.53k
                    [[fallthrough]];
143
68.1k
                case '+':
144
68.1k
                    ++i;
145
75.9k
                }
146
75.9k
            }
147
75.9k
            if (i == len) {
148
                // '123e', '123e+', '123e-'
149
0
                *result = StringParser::PARSE_FAILURE;
150
0
                return 0;
151
0
            }
152
226k
            for (; i != len; ++i) {
153
150k
                const char& c = s[i];
154
150k
                if (LIKELY('0' <= c && c <= '9')) {
155
150k
                    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
150k
                    if (exponent > std::numeric_limits<int32_t>::max()) {
161
0
                        *result = StringParser::PARSE_OVERFLOW;
162
0
                        return 0;
163
0
                    }
164
150k
                } else {
165
                    // '123e12abc', '123e1.2'
166
10
                    *result = StringParser::PARSE_FAILURE;
167
10
                    return 0;
168
10
                }
169
150k
            }
170
75.9k
            if (negative_exponent) {
171
1.53k
                exponent = -exponent;
172
1.53k
            }
173
75.9k
        } else {
174
14
            *result = StringParser::PARSE_FAILURE;
175
14
            return 0;
176
14
        }
177
75.9k
    }
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
115k
    int64_t tmp_result_int_part_digit_count = int_part_count + exponent;
184
115k
    if (tmp_result_int_part_digit_count > std::numeric_limits<int>::max() ||
185
115k
        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
115k
    int result_int_part_digit_count = tmp_result_int_part_digit_count;
190
115k
    T int_part_number = 0;
191
115k
    T frac_part_number = 0;
192
115k
    int actual_frac_part_count = 0;
193
115k
    int digit_index = 0;
194
115k
    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
115k
        int max_index = std::min(found_dot ? (result_int_part_digit_count +
200
109k
                                              ((int_part_count > 0 && exponent > 0) ? 1 : 0))
201
115k
                                           : result_int_part_digit_count,
202
115k
                                 end_digit_index);
203
115k
        max_index = (max_index == std::numeric_limits<int>::min() ? end_digit_index : max_index);
204
        // skip zero number
205
331k
        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
115k
        if (digit_index != max_index &&
210
115k
            (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.16M
        for (; digit_index != max_index; ++digit_index) {
216
2.04M
            if (UNLIKELY(s[digit_index] == '.')) {
217
67.5k
                continue;
218
67.5k
            }
219
1.97M
            int_part_number = int_part_number * 10 + (s[digit_index] - '0');
220
1.97M
        }
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
115k
        auto total_significant_digit_count =
225
115k
                end_digit_index - ((found_dot && int_part_count > 0) ? 1 : 0);
226
115k
        if (result_int_part_digit_count > total_significant_digit_count) {
227
64.6k
            int_part_number *= get_scale_multiplier<T>(result_int_part_digit_count -
228
64.6k
                                                       total_significant_digit_count);
229
64.6k
        }
230
115k
    } 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.50M
    for (; digit_index != end_digit_index && actual_frac_part_count < type_scale; ++digit_index) {
236
1.39M
        if (UNLIKELY(s[digit_index] == '.')) {
237
23.9k
            continue;
238
23.9k
        }
239
1.36M
        frac_part_number = frac_part_number * 10 + (s[digit_index] - '0');
240
1.36M
        ++actual_frac_part_count;
241
1.36M
    }
242
115k
    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
115k
    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
96.3k
    } else {
262
96.3k
        if (actual_frac_part_count < type_scale) {
263
87.0k
            frac_part_number *= get_scale_multiplier<T>(type_scale - actual_frac_part_count);
264
87.0k
        }
265
96.3k
    }
266
115k
    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
115k
    T value = int_part_number * type_scale_multiplier + frac_part_number;
272
115k
    *result = StringParser::PARSE_SUCCESS;
273
115k
    return is_negative ? T(-value) : T(value);
274
115k
}
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"