Coverage Report

Created: 2026-03-13 09:34

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
template <PrimitiveType P>
45
typename PrimitiveTypeTraits<P>::CppType::NativeType StringParser::string_to_decimal(
46
        const char* __restrict s, size_t len, int type_precision, int type_scale,
47
21.2M
        ParseResult* result) {
48
21.2M
    using T = typename PrimitiveTypeTraits<P>::CppType::NativeType;
49
21.2M
    static_assert(std::is_same_v<T, int32_t> || std::is_same_v<T, int64_t> ||
50
21.2M
                          std::is_same_v<T, __int128> || std::is_same_v<T, wide::Int256>,
51
21.2M
                  "Cast string to decimal only support target type int32_t, int64_t, __int128 or "
52
21.2M
                  "wide::Int256.");
53
    // Ignore leading and trailing spaces.
54
21.2M
    s = skip_ascii_whitespaces(s, len);
55
56
21.2M
    bool is_negative = false;
57
21.2M
    if (len > 0) {
58
21.1M
        switch (*s) {
59
515k
        case '-':
60
515k
            is_negative = true;
61
515k
            [[fallthrough]];
62
542k
        case '+':
63
542k
            ++s;
64
542k
            --len;
65
21.1M
        }
66
21.1M
    }
67
    // Ignore leading zeros.
68
21.2M
    bool found_value = false;
69
28.7M
    while (len > 0 && UNLIKELY(*s == '0')) {
70
7.54M
        found_value = true;
71
7.54M
        ++s;
72
7.54M
        --len;
73
7.54M
    }
74
75
21.2M
    int found_dot = 0;
76
21.2M
    if (len > 0 && *s == '.') {
77
7.28M
        found_dot = 1;
78
7.28M
        ++s;
79
7.28M
        --len;
80
7.28M
    }
81
21.2M
    int int_part_count = 0;
82
21.2M
    int i = 0;
83
150M
    for (; i != len; ++i) {
84
129M
        const char& c = s[i];
85
129M
        if (LIKELY('0' <= c && c <= '9')) {
86
119M
            found_value = true;
87
119M
            if (!found_dot) {
88
61.3M
                ++int_part_count;
89
61.3M
            }
90
119M
        } else if (c == '.') {
91
9.74M
            if (found_dot) {
92
2
                *result = StringParser::PARSE_FAILURE;
93
2
                return 0;
94
2
            }
95
9.74M
            found_dot = 1;
96
9.74M
        } else {
97
98.8k
            break;
98
98.8k
        }
99
129M
    }
100
21.2M
    if (!found_value) {
101
        // '', '.'
102
98.0k
        *result = StringParser::PARSE_FAILURE;
103
98.0k
        return 0;
104
98.0k
    }
105
    // parse exponent if any
106
21.1M
    int64_t exponent = 0;
107
21.1M
    auto end_digit_index = i;
108
21.1M
    if (i != len) {
109
112k
        bool negative_exponent = false;
110
112k
        if (s[i] == 'e' || s[i] == 'E') {
111
112k
            ++i;
112
112k
            if (i != len) {
113
112k
                switch (s[i]) {
114
14.5k
                case '-':
115
14.5k
                    negative_exponent = true;
116
14.5k
                    [[fallthrough]];
117
81.1k
                case '+':
118
81.1k
                    ++i;
119
112k
                }
120
112k
            }
121
112k
            if (i == len) {
122
                // '123e', '123e+', '123e-'
123
6
                *result = StringParser::PARSE_FAILURE;
124
6
                return 0;
125
6
            }
126
331k
            for (; i != len; ++i) {
127
219k
                const char& c = s[i];
128
219k
                if (LIKELY('0' <= c && c <= '9')) {
129
219k
                    exponent = exponent * 10 + (c - '0');
130
                    // max string len is config::string_type_length_soft_limit_bytes,
131
                    // whose max value is std::numeric_limits<int32_t>::max() - 4,
132
                    // just check overflow of int32_t to simplify the logic
133
                    // For edge cases like 0.{2147483647 zeros}e+2147483647
134
219k
                    if (exponent > std::numeric_limits<int32_t>::max()) {
135
0
                        *result = StringParser::PARSE_OVERFLOW;
136
0
                        return 0;
137
0
                    }
138
219k
                } else {
139
                    // '123e12abc', '123e1.2'
140
22
                    *result = StringParser::PARSE_FAILURE;
141
22
                    return 0;
142
22
                }
143
219k
            }
144
112k
            if (negative_exponent) {
145
14.5k
                exponent = -exponent;
146
14.5k
            }
147
112k
        } else {
148
203
            *result = StringParser::PARSE_FAILURE;
149
203
            return 0;
150
203
        }
151
112k
    }
152
21.1M
    T int_part_number = 0;
153
21.1M
    T frac_part_number = 0;
154
    // TODO: check limit values of exponent and add UT
155
    // max string len is config::string_type_length_soft_limit_bytes,
156
    // whose max value is std::numeric_limits<int32_t>::max() - 4,
157
    // so int_part_count will be in range of int32_t,
158
    // and int_part_count + exponent will be in range of int64_t
159
21.1M
    int64_t tmp_result_int_part_digit_count = int_part_count + exponent;
160
21.1M
    if (tmp_result_int_part_digit_count > std::numeric_limits<int>::max() ||
161
21.1M
        tmp_result_int_part_digit_count < std::numeric_limits<int>::min()) {
162
0
        *result = is_negative ? StringParser::PARSE_UNDERFLOW : StringParser::PARSE_OVERFLOW;
163
0
        return 0;
164
0
    }
165
21.1M
    int result_int_part_digit_count = tmp_result_int_part_digit_count;
166
21.1M
    int actual_frac_part_count = 0;
167
21.1M
    int digit_index = 0;
168
21.1M
    if (result_int_part_digit_count >= 0) {
169
21.1M
        int max_index = std::min(found_dot ? (result_int_part_digit_count +
170
17.0M
                                              ((int_part_count > 0 && exponent > 0) ? 1 : 0))
171
21.1M
                                           : result_int_part_digit_count,
172
21.1M
                                 end_digit_index);
173
21.1M
        max_index = (max_index == std::numeric_limits<int>::min() ? end_digit_index : max_index);
174
        // skip zero number
175
21.9M
        for (; digit_index != max_index && s[digit_index] == '0'; ++digit_index) {
176
857k
        }
177
        // test 0.00, .00, 0.{00...}e2147483647
178
        // 0.00000e2147483647
179
21.1M
        if (digit_index != max_index &&
180
21.1M
            (result_int_part_digit_count - digit_index > type_precision - type_scale)) {
181
16.9k
            *result = is_negative ? StringParser::PARSE_UNDERFLOW : StringParser::PARSE_OVERFLOW;
182
16.9k
            return 0;
183
16.9k
        }
184
        // get int part number
185
82.9M
        for (; digit_index != max_index; ++digit_index) {
186
61.8M
            if (UNLIKELY(s[digit_index] == '.')) {
187
71.1k
                continue;
188
71.1k
            }
189
61.7M
            int_part_number = int_part_number * 10 + (s[digit_index] - '0');
190
61.7M
        }
191
21.1M
        auto total_significant_digit_count = i - ((found_dot && int_part_count > 0) ? 1 : 0);
192
21.1M
        if (result_int_part_digit_count > total_significant_digit_count) {
193
2.45k
            int_part_number *= get_scale_multiplier<T>(result_int_part_digit_count -
194
2.45k
                                                       total_significant_digit_count);
195
2.45k
        }
196
18.4E
    } else {
197
        // leading zeros of fraction part
198
18.4E
        actual_frac_part_count = -result_int_part_digit_count;
199
18.4E
    }
200
    // get fraction part number
201
86.1M
    for (; digit_index != end_digit_index && actual_frac_part_count < type_scale; ++digit_index) {
202
65.0M
        if (UNLIKELY(s[digit_index] == '.')) {
203
9.44M
            continue;
204
9.44M
        }
205
55.6M
        frac_part_number = frac_part_number * 10 + (s[digit_index] - '0');
206
55.6M
        ++actual_frac_part_count;
207
55.6M
    }
208
21.1M
    auto type_scale_multiplier = get_scale_multiplier<T>(type_scale);
209
    // there are still extra fraction digits left, check rounding
210
21.1M
    if (digit_index != end_digit_index) {
211
291k
        if (UNLIKELY(s[digit_index] == '.')) {
212
208k
            ++digit_index;
213
208k
        }
214
291k
        if (digit_index != end_digit_index) {
215
            // example: test 1.5 -> decimal(1, 0)
216
290k
            if (s[digit_index] >= '5') {
217
205k
                ++frac_part_number;
218
205k
                if (frac_part_number == type_scale_multiplier) {
219
173k
                    frac_part_number = 0;
220
173k
                    ++int_part_number;
221
173k
                }
222
205k
            }
223
290k
        }
224
20.8M
    } else {
225
20.8M
        if (actual_frac_part_count < type_scale) {
226
4.12M
            frac_part_number *= get_scale_multiplier<T>(type_scale - actual_frac_part_count);
227
4.12M
        }
228
20.8M
    }
229
21.1M
    if (int_part_number >= get_scale_multiplier<T>(type_precision - type_scale)) {
230
136
        *result = is_negative ? StringParser::PARSE_UNDERFLOW : StringParser::PARSE_OVERFLOW;
231
136
        return 0;
232
136
    }
233
234
21.1M
    T value = int_part_number * type_scale_multiplier + frac_part_number;
235
21.1M
    *result = StringParser::PARSE_SUCCESS;
236
21.1M
    return is_negative ? T(-value) : T(value);
237
21.1M
}
_ZN5doris12StringParser17string_to_decimalILNS_13PrimitiveTypeE28EEENS_19PrimitiveTypeTraitsIXT_EE7CppType10NativeTypeEPKcmiiPNS0_11ParseResultE
Line
Count
Source
47
2.06M
        ParseResult* result) {
48
2.06M
    using T = typename PrimitiveTypeTraits<P>::CppType::NativeType;
49
2.06M
    static_assert(std::is_same_v<T, int32_t> || std::is_same_v<T, int64_t> ||
50
2.06M
                          std::is_same_v<T, __int128> || std::is_same_v<T, wide::Int256>,
51
2.06M
                  "Cast string to decimal only support target type int32_t, int64_t, __int128 or "
52
2.06M
                  "wide::Int256.");
53
    // Ignore leading and trailing spaces.
54
2.06M
    s = skip_ascii_whitespaces(s, len);
55
56
2.06M
    bool is_negative = false;
57
2.06M
    if (len > 0) {
58
2.06M
        switch (*s) {
59
224k
        case '-':
60
224k
            is_negative = true;
61
224k
            [[fallthrough]];
62
231k
        case '+':
63
231k
            ++s;
64
231k
            --len;
65
2.06M
        }
66
2.06M
    }
67
    // Ignore leading zeros.
68
2.06M
    bool found_value = false;
69
2.12M
    while (len > 0 && UNLIKELY(*s == '0')) {
70
56.6k
        found_value = true;
71
56.6k
        ++s;
72
56.6k
        --len;
73
56.6k
    }
74
75
2.06M
    int found_dot = 0;
76
2.06M
    if (len > 0 && *s == '.') {
77
19.5k
        found_dot = 1;
78
19.5k
        ++s;
79
19.5k
        --len;
80
19.5k
    }
81
2.06M
    int int_part_count = 0;
82
2.06M
    int i = 0;
83
12.6M
    for (; i != len; ++i) {
84
10.5M
        const char& c = s[i];
85
10.5M
        if (LIKELY('0' <= c && c <= '9')) {
86
8.68M
            found_value = true;
87
8.68M
            if (!found_dot) {
88
4.03M
                ++int_part_count;
89
4.03M
            }
90
8.68M
        } else if (c == '.') {
91
1.86M
            if (found_dot) {
92
2
                *result = StringParser::PARSE_FAILURE;
93
2
                return 0;
94
2
            }
95
1.86M
            found_dot = 1;
96
1.86M
        } else {
97
9.54k
            break;
98
9.54k
        }
99
10.5M
    }
100
2.06M
    if (!found_value) {
101
        // '', '.'
102
6.97k
        *result = StringParser::PARSE_FAILURE;
103
6.97k
        return 0;
104
6.97k
    }
105
    // parse exponent if any
106
2.05M
    int64_t exponent = 0;
107
2.05M
    auto end_digit_index = i;
108
2.05M
    if (i != len) {
109
9.42k
        bool negative_exponent = false;
110
9.42k
        if (s[i] == 'e' || s[i] == 'E') {
111
9.33k
            ++i;
112
9.33k
            if (i != len) {
113
9.33k
                switch (s[i]) {
114
1.54k
                case '-':
115
1.54k
                    negative_exponent = true;
116
1.54k
                    [[fallthrough]];
117
1.54k
                case '+':
118
1.54k
                    ++i;
119
9.33k
                }
120
9.33k
            }
121
9.33k
            if (i == len) {
122
                // '123e', '123e+', '123e-'
123
6
                *result = StringParser::PARSE_FAILURE;
124
6
                return 0;
125
6
            }
126
24.6k
            for (; i != len; ++i) {
127
15.3k
                const char& c = s[i];
128
15.3k
                if (LIKELY('0' <= c && c <= '9')) {
129
15.3k
                    exponent = exponent * 10 + (c - '0');
130
                    // max string len is config::string_type_length_soft_limit_bytes,
131
                    // whose max value is std::numeric_limits<int32_t>::max() - 4,
132
                    // just check overflow of int32_t to simplify the logic
133
                    // For edge cases like 0.{2147483647 zeros}e+2147483647
134
15.3k
                    if (exponent > std::numeric_limits<int32_t>::max()) {
135
0
                        *result = StringParser::PARSE_OVERFLOW;
136
0
                        return 0;
137
0
                    }
138
15.3k
                } else {
139
                    // '123e12abc', '123e1.2'
140
12
                    *result = StringParser::PARSE_FAILURE;
141
12
                    return 0;
142
12
                }
143
15.3k
            }
144
9.31k
            if (negative_exponent) {
145
1.53k
                exponent = -exponent;
146
1.53k
            }
147
9.31k
        } else {
148
90
            *result = StringParser::PARSE_FAILURE;
149
90
            return 0;
150
90
        }
151
9.42k
    }
152
2.05M
    T int_part_number = 0;
153
2.05M
    T frac_part_number = 0;
154
    // TODO: check limit values of exponent and add UT
155
    // max string len is config::string_type_length_soft_limit_bytes,
156
    // whose max value is std::numeric_limits<int32_t>::max() - 4,
157
    // so int_part_count will be in range of int32_t,
158
    // and int_part_count + exponent will be in range of int64_t
159
2.05M
    int64_t tmp_result_int_part_digit_count = int_part_count + exponent;
160
2.05M
    if (tmp_result_int_part_digit_count > std::numeric_limits<int>::max() ||
161
2.06M
        tmp_result_int_part_digit_count < std::numeric_limits<int>::min()) {
162
0
        *result = is_negative ? StringParser::PARSE_UNDERFLOW : StringParser::PARSE_OVERFLOW;
163
0
        return 0;
164
0
    }
165
2.05M
    int result_int_part_digit_count = tmp_result_int_part_digit_count;
166
2.05M
    int actual_frac_part_count = 0;
167
2.05M
    int digit_index = 0;
168
2.06M
    if (result_int_part_digit_count >= 0) {
169
2.06M
        int max_index = std::min(found_dot ? (result_int_part_digit_count +
170
1.88M
                                              ((int_part_count > 0 && exponent > 0) ? 1 : 0))
171
2.06M
                                           : result_int_part_digit_count,
172
2.06M
                                 end_digit_index);
173
2.06M
        max_index = (max_index == std::numeric_limits<int>::min() ? end_digit_index : max_index);
174
        // skip zero number
175
2.27M
        for (; digit_index != max_index && s[digit_index] == '0'; ++digit_index) {
176
212k
        }
177
        // test 0.00, .00, 0.{00...}e2147483647
178
        // 0.00000e2147483647
179
2.06M
        if (digit_index != max_index &&
180
2.06M
            (result_int_part_digit_count - digit_index > type_precision - type_scale)) {
181
6.02k
            *result = is_negative ? StringParser::PARSE_UNDERFLOW : StringParser::PARSE_OVERFLOW;
182
6.02k
            return 0;
183
6.02k
        }
184
        // get int part number
185
5.99M
        for (; digit_index != max_index; ++digit_index) {
186
3.93M
            if (UNLIKELY(s[digit_index] == '.')) {
187
1.60k
                continue;
188
1.60k
            }
189
3.93M
            int_part_number = int_part_number * 10 + (s[digit_index] - '0');
190
3.93M
        }
191
2.05M
        auto total_significant_digit_count = i - ((found_dot && int_part_count > 0) ? 1 : 0);
192
2.05M
        if (result_int_part_digit_count > total_significant_digit_count) {
193
100
            int_part_number *= get_scale_multiplier<T>(result_int_part_digit_count -
194
100
                                                       total_significant_digit_count);
195
100
        }
196
18.4E
    } else {
197
        // leading zeros of fraction part
198
18.4E
        actual_frac_part_count = -result_int_part_digit_count;
199
18.4E
    }
200
    // get fraction part number
201
7.90M
    for (; digit_index != end_digit_index && actual_frac_part_count < type_scale; ++digit_index) {
202
5.84M
        if (UNLIKELY(s[digit_index] == '.')) {
203
1.65M
            continue;
204
1.65M
        }
205
4.19M
        frac_part_number = frac_part_number * 10 + (s[digit_index] - '0');
206
4.19M
        ++actual_frac_part_count;
207
4.19M
    }
208
2.05M
    auto type_scale_multiplier = get_scale_multiplier<T>(type_scale);
209
    // there are still extra fraction digits left, check rounding
210
2.05M
    if (digit_index != end_digit_index) {
211
226k
        if (UNLIKELY(s[digit_index] == '.')) {
212
205k
            ++digit_index;
213
205k
        }
214
226k
        if (digit_index != end_digit_index) {
215
            // example: test 1.5 -> decimal(1, 0)
216
225k
            if (s[digit_index] >= '5') {
217
179k
                ++frac_part_number;
218
179k
                if (frac_part_number == type_scale_multiplier) {
219
170k
                    frac_part_number = 0;
220
170k
                    ++int_part_number;
221
170k
                }
222
179k
            }
223
225k
        }
224
1.82M
    } else {
225
1.82M
        if (actual_frac_part_count < type_scale) {
226
30.1k
            frac_part_number *= get_scale_multiplier<T>(type_scale - actual_frac_part_count);
227
30.1k
        }
228
1.82M
    }
229
2.05M
    if (int_part_number >= get_scale_multiplier<T>(type_precision - type_scale)) {
230
24
        *result = is_negative ? StringParser::PARSE_UNDERFLOW : StringParser::PARSE_OVERFLOW;
231
24
        return 0;
232
24
    }
233
234
2.05M
    T value = int_part_number * type_scale_multiplier + frac_part_number;
235
2.05M
    *result = StringParser::PARSE_SUCCESS;
236
2.05M
    return is_negative ? T(-value) : T(value);
237
2.05M
}
_ZN5doris12StringParser17string_to_decimalILNS_13PrimitiveTypeE29EEENS_19PrimitiveTypeTraitsIXT_EE7CppType10NativeTypeEPKcmiiPNS0_11ParseResultE
Line
Count
Source
47
16.2M
        ParseResult* result) {
48
16.2M
    using T = typename PrimitiveTypeTraits<P>::CppType::NativeType;
49
16.2M
    static_assert(std::is_same_v<T, int32_t> || std::is_same_v<T, int64_t> ||
50
16.2M
                          std::is_same_v<T, __int128> || std::is_same_v<T, wide::Int256>,
51
16.2M
                  "Cast string to decimal only support target type int32_t, int64_t, __int128 or "
52
16.2M
                  "wide::Int256.");
53
    // Ignore leading and trailing spaces.
54
16.2M
    s = skip_ascii_whitespaces(s, len);
55
56
16.2M
    bool is_negative = false;
57
16.2M
    if (len > 0) {
58
16.2M
        switch (*s) {
59
112k
        case '-':
60
112k
            is_negative = true;
61
112k
            [[fallthrough]];
62
119k
        case '+':
63
119k
            ++s;
64
119k
            --len;
65
16.2M
        }
66
16.2M
    }
67
    // Ignore leading zeros.
68
16.2M
    bool found_value = false;
69
23.5M
    while (len > 0 && UNLIKELY(*s == '0')) {
70
7.27M
        found_value = true;
71
7.27M
        ++s;
72
7.27M
        --len;
73
7.27M
    }
74
75
16.2M
    int found_dot = 0;
76
16.2M
    if (len > 0 && *s == '.') {
77
7.21M
        found_dot = 1;
78
7.21M
        ++s;
79
7.21M
        --len;
80
7.21M
    }
81
16.2M
    int int_part_count = 0;
82
16.2M
    int i = 0;
83
83.3M
    for (; i != len; ++i) {
84
67.0M
        const char& c = s[i];
85
67.0M
        if (LIKELY('0' <= c && c <= '9')) {
86
61.6M
            found_value = true;
87
61.6M
            if (!found_dot) {
88
29.4M
                ++int_part_count;
89
29.4M
            }
90
61.6M
        } else if (c == '.') {
91
5.39M
            if (found_dot) {
92
0
                *result = StringParser::PARSE_FAILURE;
93
0
                return 0;
94
0
            }
95
5.39M
            found_dot = 1;
96
5.39M
        } else {
97
247
            break;
98
247
        }
99
67.0M
    }
100
16.2M
    if (!found_value) {
101
        // '', '.'
102
378
        *result = StringParser::PARSE_FAILURE;
103
378
        return 0;
104
378
    }
105
    // parse exponent if any
106
16.2M
    int64_t exponent = 0;
107
16.2M
    auto end_digit_index = i;
108
16.2M
    if (i != len) {
109
13.1k
        bool negative_exponent = false;
110
13.1k
        if (s[i] == 'e' || s[i] == 'E') {
111
13.1k
            ++i;
112
13.1k
            if (i != len) {
113
13.1k
                switch (s[i]) {
114
5.35k
                case '-':
115
5.35k
                    negative_exponent = true;
116
5.35k
                    [[fallthrough]];
117
5.35k
                case '+':
118
5.35k
                    ++i;
119
13.1k
                }
120
13.1k
            }
121
13.1k
            if (i == len) {
122
                // '123e', '123e+', '123e-'
123
0
                *result = StringParser::PARSE_FAILURE;
124
0
                return 0;
125
0
            }
126
38.4k
            for (; i != len; ++i) {
127
25.3k
                const char& c = s[i];
128
25.3k
                if (LIKELY('0' <= c && c <= '9')) {
129
25.3k
                    exponent = exponent * 10 + (c - '0');
130
                    // max string len is config::string_type_length_soft_limit_bytes,
131
                    // whose max value is std::numeric_limits<int32_t>::max() - 4,
132
                    // just check overflow of int32_t to simplify the logic
133
                    // For edge cases like 0.{2147483647 zeros}e+2147483647
134
25.3k
                    if (exponent > std::numeric_limits<int32_t>::max()) {
135
0
                        *result = StringParser::PARSE_OVERFLOW;
136
0
                        return 0;
137
0
                    }
138
25.3k
                } else {
139
                    // '123e12abc', '123e1.2'
140
0
                    *result = StringParser::PARSE_FAILURE;
141
0
                    return 0;
142
0
                }
143
25.3k
            }
144
13.1k
            if (negative_exponent) {
145
5.35k
                exponent = -exponent;
146
5.35k
            }
147
13.1k
        } else {
148
78
            *result = StringParser::PARSE_FAILURE;
149
78
            return 0;
150
78
        }
151
13.1k
    }
152
16.2M
    T int_part_number = 0;
153
16.2M
    T frac_part_number = 0;
154
    // TODO: check limit values of exponent and add UT
155
    // max string len is config::string_type_length_soft_limit_bytes,
156
    // whose max value is std::numeric_limits<int32_t>::max() - 4,
157
    // so int_part_count will be in range of int32_t,
158
    // and int_part_count + exponent will be in range of int64_t
159
16.2M
    int64_t tmp_result_int_part_digit_count = int_part_count + exponent;
160
16.2M
    if (tmp_result_int_part_digit_count > std::numeric_limits<int>::max() ||
161
16.2M
        tmp_result_int_part_digit_count < std::numeric_limits<int>::min()) {
162
0
        *result = is_negative ? StringParser::PARSE_UNDERFLOW : StringParser::PARSE_OVERFLOW;
163
0
        return 0;
164
0
    }
165
16.2M
    int result_int_part_digit_count = tmp_result_int_part_digit_count;
166
16.2M
    int actual_frac_part_count = 0;
167
16.2M
    int digit_index = 0;
168
16.2M
    if (result_int_part_digit_count >= 0) {
169
16.2M
        int max_index = std::min(found_dot ? (result_int_part_digit_count +
170
12.6M
                                              ((int_part_count > 0 && exponent > 0) ? 1 : 0))
171
16.2M
                                           : result_int_part_digit_count,
172
16.2M
                                 end_digit_index);
173
16.2M
        max_index = (max_index == std::numeric_limits<int>::min() ? end_digit_index : max_index);
174
        // skip zero number
175
16.4M
        for (; digit_index != max_index && s[digit_index] == '0'; ++digit_index) {
176
213k
        }
177
        // test 0.00, .00, 0.{00...}e2147483647
178
        // 0.00000e2147483647
179
16.2M
        if (digit_index != max_index &&
180
16.2M
            (result_int_part_digit_count - digit_index > type_precision - type_scale)) {
181
10.4k
            *result = is_negative ? StringParser::PARSE_UNDERFLOW : StringParser::PARSE_OVERFLOW;
182
10.4k
            return 0;
183
10.4k
        }
184
        // get int part number
185
45.3M
        for (; digit_index != max_index; ++digit_index) {
186
29.1M
            if (UNLIKELY(s[digit_index] == '.')) {
187
960
                continue;
188
960
            }
189
29.1M
            int_part_number = int_part_number * 10 + (s[digit_index] - '0');
190
29.1M
        }
191
16.2M
        auto total_significant_digit_count = i - ((found_dot && int_part_count > 0) ? 1 : 0);
192
16.2M
        if (result_int_part_digit_count > total_significant_digit_count) {
193
88
            int_part_number *= get_scale_multiplier<T>(result_int_part_digit_count -
194
88
                                                       total_significant_digit_count);
195
88
        }
196
18.4E
    } else {
197
        // leading zeros of fraction part
198
18.4E
        actual_frac_part_count = -result_int_part_digit_count;
199
18.4E
    }
200
    // get fraction part number
201
53.7M
    for (; digit_index != end_digit_index && actual_frac_part_count < type_scale; ++digit_index) {
202
37.4M
        if (UNLIKELY(s[digit_index] == '.')) {
203
5.38M
            continue;
204
5.38M
        }
205
32.0M
        frac_part_number = frac_part_number * 10 + (s[digit_index] - '0');
206
32.0M
        ++actual_frac_part_count;
207
32.0M
    }
208
16.2M
    auto type_scale_multiplier = get_scale_multiplier<T>(type_scale);
209
    // there are still extra fraction digits left, check rounding
210
16.2M
    if (digit_index != end_digit_index) {
211
22.9k
        if (UNLIKELY(s[digit_index] == '.')) {
212
867
            ++digit_index;
213
867
        }
214
22.9k
        if (digit_index != end_digit_index) {
215
            // example: test 1.5 -> decimal(1, 0)
216
22.7k
            if (s[digit_index] >= '5') {
217
9.21k
                ++frac_part_number;
218
9.21k
                if (frac_part_number == type_scale_multiplier) {
219
952
                    frac_part_number = 0;
220
952
                    ++int_part_number;
221
952
                }
222
9.21k
            }
223
22.7k
        }
224
16.2M
    } else {
225
16.2M
        if (actual_frac_part_count < type_scale) {
226
3.63M
            frac_part_number *= get_scale_multiplier<T>(type_scale - actual_frac_part_count);
227
3.63M
        }
228
16.2M
    }
229
16.2M
    if (int_part_number >= get_scale_multiplier<T>(type_precision - type_scale)) {
230
48
        *result = is_negative ? StringParser::PARSE_UNDERFLOW : StringParser::PARSE_OVERFLOW;
231
48
        return 0;
232
48
    }
233
234
16.2M
    T value = int_part_number * type_scale_multiplier + frac_part_number;
235
16.2M
    *result = StringParser::PARSE_SUCCESS;
236
16.2M
    return is_negative ? T(-value) : T(value);
237
16.2M
}
_ZN5doris12StringParser17string_to_decimalILNS_13PrimitiveTypeE30EEENS_19PrimitiveTypeTraitsIXT_EE7CppType10NativeTypeEPKcmiiPNS0_11ParseResultE
Line
Count
Source
47
2.75M
        ParseResult* result) {
48
2.75M
    using T = typename PrimitiveTypeTraits<P>::CppType::NativeType;
49
2.75M
    static_assert(std::is_same_v<T, int32_t> || std::is_same_v<T, int64_t> ||
50
2.75M
                          std::is_same_v<T, __int128> || std::is_same_v<T, wide::Int256>,
51
2.75M
                  "Cast string to decimal only support target type int32_t, int64_t, __int128 or "
52
2.75M
                  "wide::Int256.");
53
    // Ignore leading and trailing spaces.
54
2.75M
    s = skip_ascii_whitespaces(s, len);
55
56
2.75M
    bool is_negative = false;
57
2.75M
    if (len > 0) {
58
2.65M
        switch (*s) {
59
151k
        case '-':
60
151k
            is_negative = true;
61
151k
            [[fallthrough]];
62
158k
        case '+':
63
158k
            ++s;
64
158k
            --len;
65
2.65M
        }
66
2.65M
    }
67
    // Ignore leading zeros.
68
2.75M
    bool found_value = false;
69
2.83M
    while (len > 0 && UNLIKELY(*s == '0')) {
70
80.7k
        found_value = true;
71
80.7k
        ++s;
72
80.7k
        --len;
73
80.7k
    }
74
75
2.75M
    int found_dot = 0;
76
2.75M
    if (len > 0 && *s == '.') {
77
28.9k
        found_dot = 1;
78
28.9k
        ++s;
79
28.9k
        --len;
80
28.9k
    }
81
2.75M
    int int_part_count = 0;
82
2.75M
    int i = 0;
83
48.9M
    for (; i != len; ++i) {
84
46.2M
        const char& c = s[i];
85
46.2M
        if (LIKELY('0' <= c && c <= '9')) {
86
43.8M
            found_value = true;
87
43.8M
            if (!found_dot) {
88
26.0M
                ++int_part_count;
89
26.0M
            }
90
43.8M
        } else if (c == '.') {
91
2.36M
            if (found_dot) {
92
0
                *result = StringParser::PARSE_FAILURE;
93
0
                return 0;
94
0
            }
95
2.36M
            found_dot = 1;
96
2.36M
        } else {
97
11.6k
            break;
98
11.6k
        }
99
46.2M
    }
100
2.75M
    if (!found_value) {
101
        // '', '.'
102
90.6k
        *result = StringParser::PARSE_FAILURE;
103
90.6k
        return 0;
104
90.6k
    }
105
    // parse exponent if any
106
2.66M
    int64_t exponent = 0;
107
2.66M
    auto end_digit_index = i;
108
2.66M
    if (i != len) {
109
12.3k
        bool negative_exponent = false;
110
12.3k
        if (s[i] == 'e' || s[i] == 'E') {
111
12.3k
            ++i;
112
12.3k
            if (i != len) {
113
12.3k
                switch (s[i]) {
114
4.61k
                case '-':
115
4.61k
                    negative_exponent = true;
116
4.61k
                    [[fallthrough]];
117
4.61k
                case '+':
118
4.61k
                    ++i;
119
12.3k
                }
120
12.3k
            }
121
12.3k
            if (i == len) {
122
                // '123e', '123e+', '123e-'
123
0
                *result = StringParser::PARSE_FAILURE;
124
0
                return 0;
125
0
            }
126
35.6k
            for (; i != len; ++i) {
127
23.3k
                const char& c = s[i];
128
23.3k
                if (LIKELY('0' <= c && c <= '9')) {
129
23.3k
                    exponent = exponent * 10 + (c - '0');
130
                    // max string len is config::string_type_length_soft_limit_bytes,
131
                    // whose max value is std::numeric_limits<int32_t>::max() - 4,
132
                    // just check overflow of int32_t to simplify the logic
133
                    // For edge cases like 0.{2147483647 zeros}e+2147483647
134
23.3k
                    if (exponent > std::numeric_limits<int32_t>::max()) {
135
0
                        *result = StringParser::PARSE_OVERFLOW;
136
0
                        return 0;
137
0
                    }
138
23.3k
                } else {
139
                    // '123e12abc', '123e1.2'
140
0
                    *result = StringParser::PARSE_FAILURE;
141
0
                    return 0;
142
0
                }
143
23.3k
            }
144
12.3k
            if (negative_exponent) {
145
4.61k
                exponent = -exponent;
146
4.61k
            }
147
12.3k
        } else {
148
20
            *result = StringParser::PARSE_FAILURE;
149
20
            return 0;
150
20
        }
151
12.3k
    }
152
2.66M
    T int_part_number = 0;
153
2.66M
    T frac_part_number = 0;
154
    // TODO: check limit values of exponent and add UT
155
    // max string len is config::string_type_length_soft_limit_bytes,
156
    // whose max value is std::numeric_limits<int32_t>::max() - 4,
157
    // so int_part_count will be in range of int32_t,
158
    // and int_part_count + exponent will be in range of int64_t
159
2.66M
    int64_t tmp_result_int_part_digit_count = int_part_count + exponent;
160
2.66M
    if (tmp_result_int_part_digit_count > std::numeric_limits<int>::max() ||
161
2.66M
        tmp_result_int_part_digit_count < std::numeric_limits<int>::min()) {
162
0
        *result = is_negative ? StringParser::PARSE_UNDERFLOW : StringParser::PARSE_OVERFLOW;
163
0
        return 0;
164
0
    }
165
2.66M
    int result_int_part_digit_count = tmp_result_int_part_digit_count;
166
2.66M
    int actual_frac_part_count = 0;
167
2.66M
    int digit_index = 0;
168
2.66M
    if (result_int_part_digit_count >= 0) {
169
2.65M
        int max_index = std::min(found_dot ? (result_int_part_digit_count +
170
2.39M
                                              ((int_part_count > 0 && exponent > 0) ? 1 : 0))
171
2.65M
                                           : result_int_part_digit_count,
172
2.65M
                                 end_digit_index);
173
2.65M
        max_index = (max_index == std::numeric_limits<int>::min() ? end_digit_index : max_index);
174
        // skip zero number
175
2.87M
        for (; digit_index != max_index && s[digit_index] == '0'; ++digit_index) {
176
213k
        }
177
        // test 0.00, .00, 0.{00...}e2147483647
178
        // 0.00000e2147483647
179
2.65M
        if (digit_index != max_index &&
180
2.65M
            (result_int_part_digit_count - digit_index > type_precision - type_scale)) {
181
143
            *result = is_negative ? StringParser::PARSE_UNDERFLOW : StringParser::PARSE_OVERFLOW;
182
143
            return 0;
183
143
        }
184
        // get int part number
185
28.6M
        for (; digit_index != max_index; ++digit_index) {
186
26.0M
            if (UNLIKELY(s[digit_index] == '.')) {
187
960
                continue;
188
960
            }
189
26.0M
            int_part_number = int_part_number * 10 + (s[digit_index] - '0');
190
26.0M
        }
191
2.65M
        auto total_significant_digit_count = i - ((found_dot && int_part_count > 0) ? 1 : 0);
192
2.65M
        if (result_int_part_digit_count > total_significant_digit_count) {
193
76
            int_part_number *= get_scale_multiplier<T>(result_int_part_digit_count -
194
76
                                                       total_significant_digit_count);
195
76
        }
196
2.65M
    } else {
197
        // leading zeros of fraction part
198
3.06k
        actual_frac_part_count = -result_int_part_digit_count;
199
3.06k
    }
200
    // get fraction part number
201
22.5M
    for (; digit_index != end_digit_index && actual_frac_part_count < type_scale; ++digit_index) {
202
19.8M
        if (UNLIKELY(s[digit_index] == '.')) {
203
2.35M
            continue;
204
2.35M
        }
205
17.5M
        frac_part_number = frac_part_number * 10 + (s[digit_index] - '0');
206
17.5M
        ++actual_frac_part_count;
207
17.5M
    }
208
2.66M
    auto type_scale_multiplier = get_scale_multiplier<T>(type_scale);
209
    // there are still extra fraction digits left, check rounding
210
2.66M
    if (digit_index != end_digit_index) {
211
21.7k
        if (UNLIKELY(s[digit_index] == '.')) {
212
852
            ++digit_index;
213
852
        }
214
21.7k
        if (digit_index != end_digit_index) {
215
            // example: test 1.5 -> decimal(1, 0)
216
21.4k
            if (s[digit_index] >= '5') {
217
8.02k
                ++frac_part_number;
218
8.02k
                if (frac_part_number == type_scale_multiplier) {
219
906
                    frac_part_number = 0;
220
906
                    ++int_part_number;
221
906
                }
222
8.02k
            }
223
21.4k
        }
224
2.63M
    } else {
225
2.63M
        if (actual_frac_part_count < type_scale) {
226
359k
            frac_part_number *= get_scale_multiplier<T>(type_scale - actual_frac_part_count);
227
359k
        }
228
2.63M
    }
229
2.66M
    if (int_part_number >= get_scale_multiplier<T>(type_precision - type_scale)) {
230
16
        *result = is_negative ? StringParser::PARSE_UNDERFLOW : StringParser::PARSE_OVERFLOW;
231
16
        return 0;
232
16
    }
233
234
2.66M
    T value = int_part_number * type_scale_multiplier + frac_part_number;
235
2.66M
    *result = StringParser::PARSE_SUCCESS;
236
2.66M
    return is_negative ? T(-value) : T(value);
237
2.66M
}
_ZN5doris12StringParser17string_to_decimalILNS_13PrimitiveTypeE20EEENS_19PrimitiveTypeTraitsIXT_EE7CppType10NativeTypeEPKcmiiPNS0_11ParseResultE
Line
Count
Source
47
13.8k
        ParseResult* result) {
48
13.8k
    using T = typename PrimitiveTypeTraits<P>::CppType::NativeType;
49
13.8k
    static_assert(std::is_same_v<T, int32_t> || std::is_same_v<T, int64_t> ||
50
13.8k
                          std::is_same_v<T, __int128> || std::is_same_v<T, wide::Int256>,
51
13.8k
                  "Cast string to decimal only support target type int32_t, int64_t, __int128 or "
52
13.8k
                  "wide::Int256.");
53
    // Ignore leading and trailing spaces.
54
13.8k
    s = skip_ascii_whitespaces(s, len);
55
56
13.8k
    bool is_negative = false;
57
13.8k
    if (len > 0) {
58
13.8k
        switch (*s) {
59
6.72k
        case '-':
60
6.72k
            is_negative = true;
61
6.72k
            [[fallthrough]];
62
6.72k
        case '+':
63
6.72k
            ++s;
64
6.72k
            --len;
65
13.8k
        }
66
13.8k
    }
67
    // Ignore leading zeros.
68
13.8k
    bool found_value = false;
69
52.7k
    while (len > 0 && UNLIKELY(*s == '0')) {
70
38.8k
        found_value = true;
71
38.8k
        ++s;
72
38.8k
        --len;
73
38.8k
    }
74
75
13.8k
    int found_dot = 0;
76
13.8k
    if (len > 0 && *s == '.') {
77
2.04k
        found_dot = 1;
78
2.04k
        ++s;
79
2.04k
        --len;
80
2.04k
    }
81
13.8k
    int int_part_count = 0;
82
13.8k
    int i = 0;
83
284k
    for (; i != len; ++i) {
84
270k
        const char& c = s[i];
85
270k
        if (LIKELY('0' <= c && c <= '9')) {
86
258k
            found_value = true;
87
258k
            if (!found_dot) {
88
138k
                ++int_part_count;
89
138k
            }
90
258k
        } else if (c == '.') {
91
11.6k
            if (found_dot) {
92
0
                *result = StringParser::PARSE_FAILURE;
93
0
                return 0;
94
0
            }
95
11.6k
            found_dot = 1;
96
11.6k
        } else {
97
12
            break;
98
12
        }
99
270k
    }
100
13.8k
    if (!found_value) {
101
        // '', '.'
102
11
        *result = StringParser::PARSE_FAILURE;
103
11
        return 0;
104
11
    }
105
    // parse exponent if any
106
13.8k
    int64_t exponent = 0;
107
13.8k
    auto end_digit_index = i;
108
13.8k
    if (i != len) {
109
1
        bool negative_exponent = false;
110
1
        if (s[i] == 'e' || s[i] == 'E') {
111
0
            ++i;
112
0
            if (i != len) {
113
0
                switch (s[i]) {
114
0
                case '-':
115
0
                    negative_exponent = true;
116
0
                    [[fallthrough]];
117
0
                case '+':
118
0
                    ++i;
119
0
                }
120
0
            }
121
0
            if (i == len) {
122
                // '123e', '123e+', '123e-'
123
0
                *result = StringParser::PARSE_FAILURE;
124
0
                return 0;
125
0
            }
126
0
            for (; i != len; ++i) {
127
0
                const char& c = s[i];
128
0
                if (LIKELY('0' <= c && c <= '9')) {
129
0
                    exponent = exponent * 10 + (c - '0');
130
                    // max string len is config::string_type_length_soft_limit_bytes,
131
                    // whose max value is std::numeric_limits<int32_t>::max() - 4,
132
                    // just check overflow of int32_t to simplify the logic
133
                    // For edge cases like 0.{2147483647 zeros}e+2147483647
134
0
                    if (exponent > std::numeric_limits<int32_t>::max()) {
135
0
                        *result = StringParser::PARSE_OVERFLOW;
136
0
                        return 0;
137
0
                    }
138
0
                } else {
139
                    // '123e12abc', '123e1.2'
140
0
                    *result = StringParser::PARSE_FAILURE;
141
0
                    return 0;
142
0
                }
143
0
            }
144
0
            if (negative_exponent) {
145
0
                exponent = -exponent;
146
0
            }
147
1
        } else {
148
1
            *result = StringParser::PARSE_FAILURE;
149
1
            return 0;
150
1
        }
151
1
    }
152
13.8k
    T int_part_number = 0;
153
13.8k
    T frac_part_number = 0;
154
    // TODO: check limit values of exponent and add UT
155
    // max string len is config::string_type_length_soft_limit_bytes,
156
    // whose max value is std::numeric_limits<int32_t>::max() - 4,
157
    // so int_part_count will be in range of int32_t,
158
    // and int_part_count + exponent will be in range of int64_t
159
13.8k
    int64_t tmp_result_int_part_digit_count = int_part_count + exponent;
160
13.8k
    if (tmp_result_int_part_digit_count > std::numeric_limits<int>::max() ||
161
13.8k
        tmp_result_int_part_digit_count < std::numeric_limits<int>::min()) {
162
0
        *result = is_negative ? StringParser::PARSE_UNDERFLOW : StringParser::PARSE_OVERFLOW;
163
0
        return 0;
164
0
    }
165
13.8k
    int result_int_part_digit_count = tmp_result_int_part_digit_count;
166
13.8k
    int actual_frac_part_count = 0;
167
13.8k
    int digit_index = 0;
168
13.8k
    if (result_int_part_digit_count >= 0) {
169
13.8k
        int max_index = std::min(found_dot ? (result_int_part_digit_count +
170
13.7k
                                              ((int_part_count > 0 && exponent > 0) ? 1 : 0))
171
13.8k
                                           : result_int_part_digit_count,
172
13.8k
                                 end_digit_index);
173
13.8k
        max_index = (max_index == std::numeric_limits<int>::min() ? end_digit_index : max_index);
174
        // skip zero number
175
13.8k
        for (; digit_index != max_index && s[digit_index] == '0'; ++digit_index) {
176
0
        }
177
        // test 0.00, .00, 0.{00...}e2147483647
178
        // 0.00000e2147483647
179
13.8k
        if (digit_index != max_index &&
180
13.8k
            (result_int_part_digit_count - digit_index > type_precision - type_scale)) {
181
8
            *result = is_negative ? StringParser::PARSE_UNDERFLOW : StringParser::PARSE_OVERFLOW;
182
8
            return 0;
183
8
        }
184
        // get int part number
185
152k
        for (; digit_index != max_index; ++digit_index) {
186
138k
            if (UNLIKELY(s[digit_index] == '.')) {
187
0
                continue;
188
0
            }
189
138k
            int_part_number = int_part_number * 10 + (s[digit_index] - '0');
190
138k
        }
191
13.8k
        auto total_significant_digit_count = i - ((found_dot && int_part_count > 0) ? 1 : 0);
192
13.8k
        if (result_int_part_digit_count > total_significant_digit_count) {
193
0
            int_part_number *= get_scale_multiplier<T>(result_int_part_digit_count -
194
0
                                                       total_significant_digit_count);
195
0
        }
196
13.8k
    } else {
197
        // leading zeros of fraction part
198
0
        actual_frac_part_count = -result_int_part_digit_count;
199
0
    }
200
    // get fraction part number
201
145k
    for (; digit_index != end_digit_index && actual_frac_part_count < type_scale; ++digit_index) {
202
131k
        if (UNLIKELY(s[digit_index] == '.')) {
203
11.6k
            continue;
204
11.6k
        }
205
119k
        frac_part_number = frac_part_number * 10 + (s[digit_index] - '0');
206
119k
        ++actual_frac_part_count;
207
119k
    }
208
13.8k
    auto type_scale_multiplier = get_scale_multiplier<T>(type_scale);
209
    // there are still extra fraction digits left, check rounding
210
13.8k
    if (digit_index != end_digit_index) {
211
17
        if (UNLIKELY(s[digit_index] == '.')) {
212
0
            ++digit_index;
213
0
        }
214
17
        if (digit_index != end_digit_index) {
215
            // example: test 1.5 -> decimal(1, 0)
216
17
            if (s[digit_index] >= '5') {
217
17
                ++frac_part_number;
218
17
                if (frac_part_number == type_scale_multiplier) {
219
0
                    frac_part_number = 0;
220
0
                    ++int_part_number;
221
0
                }
222
17
            }
223
17
        }
224
13.8k
    } else {
225
13.8k
        if (actual_frac_part_count < type_scale) {
226
2.17k
            frac_part_number *= get_scale_multiplier<T>(type_scale - actual_frac_part_count);
227
2.17k
        }
228
13.8k
    }
229
13.8k
    if (int_part_number >= get_scale_multiplier<T>(type_precision - type_scale)) {
230
0
        *result = is_negative ? StringParser::PARSE_UNDERFLOW : StringParser::PARSE_OVERFLOW;
231
0
        return 0;
232
0
    }
233
234
13.8k
    T value = int_part_number * type_scale_multiplier + frac_part_number;
235
13.8k
    *result = StringParser::PARSE_SUCCESS;
236
13.8k
    return is_negative ? T(-value) : T(value);
237
13.8k
}
_ZN5doris12StringParser17string_to_decimalILNS_13PrimitiveTypeE35EEENS_19PrimitiveTypeTraitsIXT_EE7CppType10NativeTypeEPKcmiiPNS0_11ParseResultE
Line
Count
Source
47
134k
        ParseResult* result) {
48
134k
    using T = typename PrimitiveTypeTraits<P>::CppType::NativeType;
49
134k
    static_assert(std::is_same_v<T, int32_t> || std::is_same_v<T, int64_t> ||
50
134k
                          std::is_same_v<T, __int128> || std::is_same_v<T, wide::Int256>,
51
134k
                  "Cast string to decimal only support target type int32_t, int64_t, __int128 or "
52
134k
                  "wide::Int256.");
53
    // Ignore leading and trailing spaces.
54
134k
    s = skip_ascii_whitespaces(s, len);
55
56
134k
    bool is_negative = false;
57
134k
    if (len > 0) {
58
134k
        switch (*s) {
59
19.8k
        case '-':
60
19.8k
            is_negative = true;
61
19.8k
            [[fallthrough]];
62
26.5k
        case '+':
63
26.5k
            ++s;
64
26.5k
            --len;
65
134k
        }
66
134k
    }
67
    // Ignore leading zeros.
68
134k
    bool found_value = false;
69
232k
    while (len > 0 && UNLIKELY(*s == '0')) {
70
97.7k
        found_value = true;
71
97.7k
        ++s;
72
97.7k
        --len;
73
97.7k
    }
74
75
134k
    int found_dot = 0;
76
134k
    if (len > 0 && *s == '.') {
77
16.6k
        found_dot = 1;
78
16.6k
        ++s;
79
16.6k
        --len;
80
16.6k
    }
81
134k
    int int_part_count = 0;
82
134k
    int i = 0;
83
4.94M
    for (; i != len; ++i) {
84
4.88M
        const char& c = s[i];
85
4.88M
        if (LIKELY('0' <= c && c <= '9')) {
86
4.70M
            found_value = true;
87
4.70M
            if (!found_dot) {
88
1.73M
                ++int_part_count;
89
1.73M
            }
90
4.70M
        } else if (c == '.') {
91
105k
            if (found_dot) {
92
0
                *result = StringParser::PARSE_FAILURE;
93
0
                return 0;
94
0
            }
95
105k
            found_dot = 1;
96
105k
        } else {
97
77.4k
            break;
98
77.4k
        }
99
4.88M
    }
100
134k
    if (!found_value) {
101
        // '', '.'
102
78
        *result = StringParser::PARSE_FAILURE;
103
78
        return 0;
104
78
    }
105
    // parse exponent if any
106
134k
    int64_t exponent = 0;
107
134k
    auto end_digit_index = i;
108
134k
    if (i != len) {
109
77.4k
        bool negative_exponent = false;
110
77.4k
        if (s[i] == 'e' || s[i] == 'E') {
111
77.4k
            ++i;
112
77.4k
            if (i != len) {
113
77.4k
                switch (s[i]) {
114
3.04k
                case '-':
115
3.04k
                    negative_exponent = true;
116
3.04k
                    [[fallthrough]];
117
69.6k
                case '+':
118
69.6k
                    ++i;
119
77.4k
                }
120
77.4k
            }
121
77.4k
            if (i == len) {
122
                // '123e', '123e+', '123e-'
123
0
                *result = StringParser::PARSE_FAILURE;
124
0
                return 0;
125
0
            }
126
232k
            for (; i != len; ++i) {
127
155k
                const char& c = s[i];
128
155k
                if (LIKELY('0' <= c && c <= '9')) {
129
155k
                    exponent = exponent * 10 + (c - '0');
130
                    // max string len is config::string_type_length_soft_limit_bytes,
131
                    // whose max value is std::numeric_limits<int32_t>::max() - 4,
132
                    // just check overflow of int32_t to simplify the logic
133
                    // For edge cases like 0.{2147483647 zeros}e+2147483647
134
155k
                    if (exponent > std::numeric_limits<int32_t>::max()) {
135
0
                        *result = StringParser::PARSE_OVERFLOW;
136
0
                        return 0;
137
0
                    }
138
155k
                } else {
139
                    // '123e12abc', '123e1.2'
140
10
                    *result = StringParser::PARSE_FAILURE;
141
10
                    return 0;
142
10
                }
143
155k
            }
144
77.4k
            if (negative_exponent) {
145
3.04k
                exponent = -exponent;
146
3.04k
            }
147
77.4k
        } else {
148
14
            *result = StringParser::PARSE_FAILURE;
149
14
            return 0;
150
14
        }
151
77.4k
    }
152
134k
    T int_part_number = 0;
153
134k
    T frac_part_number = 0;
154
    // TODO: check limit values of exponent and add UT
155
    // max string len is config::string_type_length_soft_limit_bytes,
156
    // whose max value is std::numeric_limits<int32_t>::max() - 4,
157
    // so int_part_count will be in range of int32_t,
158
    // and int_part_count + exponent will be in range of int64_t
159
134k
    int64_t tmp_result_int_part_digit_count = int_part_count + exponent;
160
134k
    if (tmp_result_int_part_digit_count > std::numeric_limits<int>::max() ||
161
134k
        tmp_result_int_part_digit_count < std::numeric_limits<int>::min()) {
162
0
        *result = is_negative ? StringParser::PARSE_UNDERFLOW : StringParser::PARSE_OVERFLOW;
163
0
        return 0;
164
0
    }
165
134k
    int result_int_part_digit_count = tmp_result_int_part_digit_count;
166
134k
    int actual_frac_part_count = 0;
167
134k
    int digit_index = 0;
168
134k
    if (result_int_part_digit_count >= 0) {
169
134k
        int max_index = std::min(found_dot ? (result_int_part_digit_count +
170
121k
                                              ((int_part_count > 0 && exponent > 0) ? 1 : 0))
171
134k
                                           : result_int_part_digit_count,
172
134k
                                 end_digit_index);
173
134k
        max_index = (max_index == std::numeric_limits<int>::min() ? end_digit_index : max_index);
174
        // skip zero number
175
351k
        for (; digit_index != max_index && s[digit_index] == '0'; ++digit_index) {
176
217k
        }
177
        // test 0.00, .00, 0.{00...}e2147483647
178
        // 0.00000e2147483647
179
134k
        if (digit_index != max_index &&
180
134k
            (result_int_part_digit_count - digit_index > type_precision - type_scale)) {
181
336
            *result = is_negative ? StringParser::PARSE_UNDERFLOW : StringParser::PARSE_OVERFLOW;
182
336
            return 0;
183
336
        }
184
        // get int part number
185
2.76M
        for (; digit_index != max_index; ++digit_index) {
186
2.63M
            if (UNLIKELY(s[digit_index] == '.')) {
187
67.5k
                continue;
188
67.5k
            }
189
2.56M
            int_part_number = int_part_number * 10 + (s[digit_index] - '0');
190
2.56M
        }
191
134k
        auto total_significant_digit_count = i - ((found_dot && int_part_count > 0) ? 1 : 0);
192
134k
        if (result_int_part_digit_count > total_significant_digit_count) {
193
2.18k
            int_part_number *= get_scale_multiplier<T>(result_int_part_digit_count -
194
2.18k
                                                       total_significant_digit_count);
195
2.18k
        }
196
134k
    } else {
197
        // leading zeros of fraction part
198
146
        actual_frac_part_count = -result_int_part_digit_count;
199
146
    }
200
    // get fraction part number
201
1.86M
    for (; digit_index != end_digit_index && actual_frac_part_count < type_scale; ++digit_index) {
202
1.73M
        if (UNLIKELY(s[digit_index] == '.')) {
203
34.1k
            continue;
204
34.1k
        }
205
1.69M
        frac_part_number = frac_part_number * 10 + (s[digit_index] - '0');
206
1.69M
        ++actual_frac_part_count;
207
1.69M
    }
208
134k
    auto type_scale_multiplier = get_scale_multiplier<T>(type_scale);
209
    // there are still extra fraction digits left, check rounding
210
134k
    if (digit_index != end_digit_index) {
211
20.8k
        if (UNLIKELY(s[digit_index] == '.')) {
212
860
            ++digit_index;
213
860
        }
214
20.8k
        if (digit_index != end_digit_index) {
215
            // example: test 1.5 -> decimal(1, 0)
216
20.5k
            if (s[digit_index] >= '5') {
217
8.73k
                ++frac_part_number;
218
8.73k
                if (frac_part_number == type_scale_multiplier) {
219
952
                    frac_part_number = 0;
220
952
                    ++int_part_number;
221
952
                }
222
8.73k
            }
223
20.5k
        }
224
113k
    } else {
225
113k
        if (actual_frac_part_count < type_scale) {
226
91.5k
            frac_part_number *= get_scale_multiplier<T>(type_scale - actual_frac_part_count);
227
91.5k
        }
228
113k
    }
229
134k
    if (int_part_number >= get_scale_multiplier<T>(type_precision - type_scale)) {
230
48
        *result = is_negative ? StringParser::PARSE_UNDERFLOW : StringParser::PARSE_OVERFLOW;
231
48
        return 0;
232
48
    }
233
234
134k
    T value = int_part_number * type_scale_multiplier + frac_part_number;
235
134k
    *result = StringParser::PARSE_SUCCESS;
236
134k
    return is_negative ? T(-value) : T(value);
237
134k
}
238
239
template Int32 StringParser::string_to_decimal<PrimitiveType::TYPE_DECIMAL32>(
240
        const char* __restrict s, size_t len, int type_precision, int type_scale,
241
        ParseResult* result);
242
template Int64 StringParser::string_to_decimal<PrimitiveType::TYPE_DECIMAL64>(
243
        const char* __restrict s, size_t len, int type_precision, int type_scale,
244
        ParseResult* result);
245
template Int128 StringParser::string_to_decimal<PrimitiveType::TYPE_DECIMAL128I>(
246
        const char* __restrict s, size_t len, int type_precision, int type_scale,
247
        ParseResult* result);
248
template Int128 StringParser::string_to_decimal<PrimitiveType::TYPE_DECIMALV2>(
249
        const char* __restrict s, size_t len, int type_precision, int type_scale,
250
        ParseResult* result);
251
template wide::Int256 StringParser::string_to_decimal<PrimitiveType::TYPE_DECIMAL256>(
252
        const char* __restrict s, size_t len, int type_precision, int type_scale,
253
        ParseResult* result);
254
} // end namespace doris
255
#include "common/compile_check_avoid_end.h"