Coverage Report

Created: 2026-03-19 12:20

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
17.6M
        ParseResult* result) {
48
17.6M
    using T = typename PrimitiveTypeTraits<P>::CppType::NativeType;
49
17.6M
    static_assert(std::is_same_v<T, int32_t> || std::is_same_v<T, int64_t> ||
50
17.6M
                          std::is_same_v<T, __int128> || std::is_same_v<T, wide::Int256>,
51
17.6M
                  "Cast string to decimal only support target type int32_t, int64_t, __int128 or "
52
17.6M
                  "wide::Int256.");
53
    // Ignore leading and trailing spaces.
54
17.6M
    s = skip_ascii_whitespaces(s, len);
55
56
17.6M
    bool is_negative = false;
57
17.6M
    if (len > 0) {
58
17.5M
        switch (*s) {
59
185k
        case '-':
60
185k
            is_negative = true;
61
185k
            [[fallthrough]];
62
213k
        case '+':
63
213k
            ++s;
64
213k
            --len;
65
17.5M
        }
66
17.5M
    }
67
    // Ignore leading zeros.
68
17.6M
    bool found_value = false;
69
25.2M
    while (len > 0 && UNLIKELY(*s == '0')) {
70
7.55M
        found_value = true;
71
7.55M
        ++s;
72
7.55M
        --len;
73
7.55M
    }
74
75
17.6M
    int found_dot = 0;
76
17.6M
    if (len > 0 && *s == '.') {
77
7.29M
        found_dot = 1;
78
7.29M
        ++s;
79
7.29M
        --len;
80
7.29M
    }
81
17.6M
    int int_part_count = 0;
82
17.6M
    int i = 0;
83
114M
    for (; i != len; ++i) {
84
96.8M
        const char& c = s[i];
85
96.8M
        if (LIKELY('0' <= c && c <= '9')) {
86
90.3M
            found_value = true;
87
90.3M
            if (!found_dot) {
88
54.3M
                ++int_part_count;
89
54.3M
            }
90
90.3M
        } else if (c == '.') {
91
6.41M
            if (found_dot) {
92
2
                *result = StringParser::PARSE_FAILURE;
93
2
                return 0;
94
2
            }
95
6.41M
            found_dot = 1;
96
6.41M
        } else {
97
107k
            break;
98
107k
        }
99
96.8M
    }
100
17.6M
    if (!found_value) {
101
        // '', '.'
102
97.9k
        *result = StringParser::PARSE_FAILURE;
103
97.9k
        return 0;
104
97.9k
    }
105
    // parse exponent if any
106
17.5M
    int64_t exponent = 0;
107
17.5M
    auto end_digit_index = i;
108
17.5M
    if (i != len) {
109
113k
        bool negative_exponent = false;
110
113k
        if (s[i] == 'e' || s[i] == 'E') {
111
113k
            ++i;
112
113k
            if (i != len) {
113
113k
                switch (s[i]) {
114
15.6k
                case '-':
115
15.6k
                    negative_exponent = true;
116
15.6k
                    [[fallthrough]];
117
82.2k
                case '+':
118
82.2k
                    ++i;
119
113k
                }
120
113k
            }
121
113k
            if (i == len) {
122
                // '123e', '123e+', '123e-'
123
6
                *result = StringParser::PARSE_FAILURE;
124
6
                return 0;
125
6
            }
126
335k
            for (; i != len; ++i) {
127
222k
                const char& c = s[i];
128
222k
                if (LIKELY('0' <= c && c <= '9')) {
129
222k
                    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
222k
                    if (exponent > std::numeric_limits<int32_t>::max()) {
135
0
                        *result = StringParser::PARSE_OVERFLOW;
136
0
                        return 0;
137
0
                    }
138
222k
                } else {
139
                    // '123e12abc', '123e1.2'
140
22
                    *result = StringParser::PARSE_FAILURE;
141
22
                    return 0;
142
22
                }
143
222k
            }
144
113k
            if (negative_exponent) {
145
15.6k
                exponent = -exponent;
146
15.6k
            }
147
113k
        } else {
148
193
            *result = StringParser::PARSE_FAILURE;
149
193
            return 0;
150
193
        }
151
113k
    }
152
17.5M
    T int_part_number = 0;
153
17.5M
    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
17.5M
    int64_t tmp_result_int_part_digit_count = int_part_count + exponent;
160
17.5M
    if (tmp_result_int_part_digit_count > std::numeric_limits<int>::max() ||
161
17.6M
        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
17.5M
    int result_int_part_digit_count = tmp_result_int_part_digit_count;
166
17.5M
    int actual_frac_part_count = 0;
167
17.5M
    int digit_index = 0;
168
17.5M
    if (result_int_part_digit_count >= 0) {
169
17.5M
        int max_index = std::min(found_dot ? (result_int_part_digit_count +
170
13.6M
                                              ((int_part_count > 0 && exponent > 0) ? 1 : 0))
171
17.5M
                                           : result_int_part_digit_count,
172
17.5M
                                 end_digit_index);
173
17.5M
        max_index = (max_index == std::numeric_limits<int>::min() ? end_digit_index : max_index);
174
        // skip zero number
175
18.4M
        for (; digit_index != max_index && s[digit_index] == '0'; ++digit_index) {
176
858k
        }
177
        // test 0.00, .00, 0.{00...}e2147483647
178
        // 0.00000e2147483647
179
17.5M
        if (digit_index != max_index &&
180
17.5M
            (result_int_part_digit_count - digit_index > type_precision - type_scale)) {
181
17.0k
            *result = is_negative ? StringParser::PARSE_UNDERFLOW : StringParser::PARSE_OVERFLOW;
182
17.0k
            return 0;
183
17.0k
        }
184
        // get int part number
185
72.2M
        for (; digit_index != max_index; ++digit_index) {
186
54.6M
            if (UNLIKELY(s[digit_index] == '.')) {
187
71.1k
                continue;
188
71.1k
            }
189
54.6M
            int_part_number = int_part_number * 10 + (s[digit_index] - '0');
190
54.6M
        }
191
17.5M
        auto total_significant_digit_count = i - ((found_dot && int_part_count > 0) ? 1 : 0);
192
17.5M
        if (result_int_part_digit_count > total_significant_digit_count) {
193
2.46k
            int_part_number *= get_scale_multiplier<T>(result_int_part_digit_count -
194
2.46k
                                                       total_significant_digit_count);
195
2.46k
        }
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
57.4M
    for (; digit_index != end_digit_index && actual_frac_part_count < type_scale; ++digit_index) {
202
39.8M
        if (UNLIKELY(s[digit_index] == '.')) {
203
6.11M
            continue;
204
6.11M
        }
205
33.7M
        frac_part_number = frac_part_number * 10 + (s[digit_index] - '0');
206
33.7M
        ++actual_frac_part_count;
207
33.7M
    }
208
17.5M
    auto type_scale_multiplier = get_scale_multiplier<T>(type_scale);
209
    // there are still extra fraction digits left, check rounding
210
17.5M
    if (digit_index != end_digit_index) {
211
289k
        if (UNLIKELY(s[digit_index] == '.')) {
212
205k
            ++digit_index;
213
205k
        }
214
289k
        if (digit_index != end_digit_index) {
215
            // example: test 1.5 -> decimal(1, 0)
216
288k
            if (s[digit_index] >= '5') {
217
202k
                ++frac_part_number;
218
202k
                if (frac_part_number == type_scale_multiplier) {
219
171k
                    frac_part_number = 0;
220
171k
                    ++int_part_number;
221
171k
                }
222
202k
            }
223
288k
        }
224
17.2M
    } else {
225
17.2M
        if (actual_frac_part_count < type_scale) {
226
4.10M
            frac_part_number *= get_scale_multiplier<T>(type_scale - actual_frac_part_count);
227
4.10M
        }
228
17.2M
    }
229
17.5M
    if (int_part_number >= get_scale_multiplier<T>(type_precision - type_scale)) {
230
152
        *result = is_negative ? StringParser::PARSE_UNDERFLOW : StringParser::PARSE_OVERFLOW;
231
152
        return 0;
232
152
    }
233
234
17.5M
    T value = int_part_number * type_scale_multiplier + frac_part_number;
235
17.5M
    *result = StringParser::PARSE_SUCCESS;
236
17.5M
    return is_negative ? T(-value) : T(value);
237
17.5M
}
_ZN5doris12StringParser17string_to_decimalILNS_13PrimitiveTypeE28EEENS_19PrimitiveTypeTraitsIXT_EE7CppType10NativeTypeEPKcmiiPNS0_11ParseResultE
Line
Count
Source
47
331k
        ParseResult* result) {
48
331k
    using T = typename PrimitiveTypeTraits<P>::CppType::NativeType;
49
331k
    static_assert(std::is_same_v<T, int32_t> || std::is_same_v<T, int64_t> ||
50
331k
                          std::is_same_v<T, __int128> || std::is_same_v<T, wide::Int256>,
51
331k
                  "Cast string to decimal only support target type int32_t, int64_t, __int128 or "
52
331k
                  "wide::Int256.");
53
    // Ignore leading and trailing spaces.
54
331k
    s = skip_ascii_whitespaces(s, len);
55
56
331k
    bool is_negative = false;
57
331k
    if (len > 0) {
58
325k
        switch (*s) {
59
58.3k
        case '-':
60
58.3k
            is_negative = true;
61
58.3k
            [[fallthrough]];
62
65.6k
        case '+':
63
65.6k
            ++s;
64
65.6k
            --len;
65
325k
        }
66
325k
    }
67
    // Ignore leading zeros.
68
331k
    bool found_value = false;
69
387k
    while (len > 0 && UNLIKELY(*s == '0')) {
70
56.1k
        found_value = true;
71
56.1k
        ++s;
72
56.1k
        --len;
73
56.1k
    }
74
75
331k
    int found_dot = 0;
76
331k
    if (len > 0 && *s == '.') {
77
19.6k
        found_dot = 1;
78
19.6k
        ++s;
79
19.6k
        --len;
80
19.6k
    }
81
331k
    int int_part_count = 0;
82
331k
    int i = 0;
83
2.20M
    for (; i != len; ++i) {
84
1.88M
        const char& c = s[i];
85
1.88M
        if (LIKELY('0' <= c && c <= '9')) {
86
1.60M
            found_value = true;
87
1.60M
            if (!found_dot) {
88
899k
                ++int_part_count;
89
899k
            }
90
1.60M
        } else if (c == '.') {
91
265k
            if (found_dot) {
92
2
                *result = StringParser::PARSE_FAILURE;
93
2
                return 0;
94
2
            }
95
265k
            found_dot = 1;
96
265k
        } else {
97
10.9k
            break;
98
10.9k
        }
99
1.88M
    }
100
331k
    if (!found_value) {
101
        // '', '.'
102
6.96k
        *result = StringParser::PARSE_FAILURE;
103
6.96k
        return 0;
104
6.96k
    }
105
    // parse exponent if any
106
324k
    int64_t exponent = 0;
107
324k
    auto end_digit_index = i;
108
324k
    if (i != len) {
109
9.41k
        bool negative_exponent = false;
110
9.41k
        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
84
            *result = StringParser::PARSE_FAILURE;
149
84
            return 0;
150
84
        }
151
9.41k
    }
152
324k
    T int_part_number = 0;
153
324k
    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
324k
    int64_t tmp_result_int_part_digit_count = int_part_count + exponent;
160
324k
    if (tmp_result_int_part_digit_count > std::numeric_limits<int>::max() ||
161
324k
        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
324k
    int result_int_part_digit_count = tmp_result_int_part_digit_count;
166
324k
    int actual_frac_part_count = 0;
167
324k
    int digit_index = 0;
168
324k
    if (result_int_part_digit_count >= 0) {
169
324k
        int max_index = std::min(found_dot ? (result_int_part_digit_count +
170
284k
                                              ((int_part_count > 0 && exponent > 0) ? 1 : 0))
171
324k
                                           : result_int_part_digit_count,
172
324k
                                 end_digit_index);
173
324k
        max_index = (max_index == std::numeric_limits<int>::min() ? end_digit_index : max_index);
174
        // skip zero number
175
536k
        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
324k
        if (digit_index != max_index &&
180
324k
            (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
1.11M
        for (; digit_index != max_index; ++digit_index) {
186
800k
            if (UNLIKELY(s[digit_index] == '.')) {
187
1.60k
                continue;
188
1.60k
            }
189
798k
            int_part_number = int_part_number * 10 + (s[digit_index] - '0');
190
798k
        }
191
317k
        auto total_significant_digit_count = i - ((found_dot && int_part_count > 0) ? 1 : 0);
192
317k
        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
317k
    } else {
197
        // leading zeros of fraction part
198
54
        actual_frac_part_count = -result_int_part_digit_count;
199
54
    }
200
    // get fraction part number
201
627k
    for (; digit_index != end_digit_index && actual_frac_part_count < type_scale; ++digit_index) {
202
309k
        if (UNLIKELY(s[digit_index] == '.')) {
203
53.5k
            continue;
204
53.5k
        }
205
256k
        frac_part_number = frac_part_number * 10 + (s[digit_index] - '0');
206
256k
        ++actual_frac_part_count;
207
256k
    }
208
318k
    auto type_scale_multiplier = get_scale_multiplier<T>(type_scale);
209
    // there are still extra fraction digits left, check rounding
210
318k
    if (digit_index != end_digit_index) {
211
223k
        if (UNLIKELY(s[digit_index] == '.')) {
212
203k
            ++digit_index;
213
203k
        }
214
223k
        if (digit_index != end_digit_index) {
215
            // example: test 1.5 -> decimal(1, 0)
216
223k
            if (s[digit_index] >= '5') {
217
176k
                ++frac_part_number;
218
176k
                if (frac_part_number == type_scale_multiplier) {
219
168k
                    frac_part_number = 0;
220
168k
                    ++int_part_number;
221
168k
                }
222
176k
            }
223
223k
        }
224
223k
    } else {
225
94.2k
        if (actual_frac_part_count < type_scale) {
226
30.0k
            frac_part_number *= get_scale_multiplier<T>(type_scale - actual_frac_part_count);
227
30.0k
        }
228
94.2k
    }
229
318k
    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
318k
    T value = int_part_number * type_scale_multiplier + frac_part_number;
235
318k
    *result = StringParser::PARSE_SUCCESS;
236
318k
    return is_negative ? T(-value) : T(value);
237
318k
}
_ZN5doris12StringParser17string_to_decimalILNS_13PrimitiveTypeE29EEENS_19PrimitiveTypeTraitsIXT_EE7CppType10NativeTypeEPKcmiiPNS0_11ParseResultE
Line
Count
Source
47
15.1M
        ParseResult* result) {
48
15.1M
    using T = typename PrimitiveTypeTraits<P>::CppType::NativeType;
49
15.1M
    static_assert(std::is_same_v<T, int32_t> || std::is_same_v<T, int64_t> ||
50
15.1M
                          std::is_same_v<T, __int128> || std::is_same_v<T, wide::Int256>,
51
15.1M
                  "Cast string to decimal only support target type int32_t, int64_t, __int128 or "
52
15.1M
                  "wide::Int256.");
53
    // Ignore leading and trailing spaces.
54
15.1M
    s = skip_ascii_whitespaces(s, len);
55
56
15.1M
    bool is_negative = false;
57
15.1M
    if (len > 0) {
58
15.1M
        switch (*s) {
59
30.2k
        case '-':
60
30.2k
            is_negative = true;
61
30.2k
            [[fallthrough]];
62
36.9k
        case '+':
63
36.9k
            ++s;
64
36.9k
            --len;
65
15.1M
        }
66
15.1M
    }
67
    // Ignore leading zeros.
68
15.1M
    bool found_value = false;
69
22.3M
    while (len > 0 && UNLIKELY(*s == '0')) {
70
7.28M
        found_value = true;
71
7.28M
        ++s;
72
7.28M
        --len;
73
7.28M
    }
74
75
15.1M
    int found_dot = 0;
76
15.1M
    if (len > 0 && *s == '.') {
77
7.22M
        found_dot = 1;
78
7.22M
        ++s;
79
7.22M
        --len;
80
7.22M
    }
81
15.1M
    int int_part_count = 0;
82
15.1M
    int i = 0;
83
70.7M
    for (; i != len; ++i) {
84
55.6M
        const char& c = s[i];
85
55.6M
        if (LIKELY('0' <= c && c <= '9')) {
86
51.3M
            found_value = true;
87
51.3M
            if (!found_dot) {
88
27.6M
                ++int_part_count;
89
27.6M
            }
90
51.3M
        } else if (c == '.') {
91
4.28M
            if (found_dot) {
92
0
                *result = StringParser::PARSE_FAILURE;
93
0
                return 0;
94
0
            }
95
4.28M
            found_dot = 1;
96
4.28M
        } else {
97
7.95k
            break;
98
7.95k
        }
99
55.6M
    }
100
15.1M
    if (!found_value) {
101
        // '', '.'
102
374
        *result = StringParser::PARSE_FAILURE;
103
374
        return 0;
104
374
    }
105
    // parse exponent if any
106
15.1M
    int64_t exponent = 0;
107
15.1M
    auto end_digit_index = i;
108
15.1M
    if (i != len) {
109
13.7k
        bool negative_exponent = false;
110
13.7k
        if (s[i] == 'e' || s[i] == 'E') {
111
13.6k
            ++i;
112
13.6k
            if (i != len) {
113
13.6k
                switch (s[i]) {
114
5.89k
                case '-':
115
5.89k
                    negative_exponent = true;
116
5.89k
                    [[fallthrough]];
117
5.89k
                case '+':
118
5.89k
                    ++i;
119
13.6k
                }
120
13.6k
            }
121
13.6k
            if (i == len) {
122
                // '123e', '123e+', '123e-'
123
0
                *result = StringParser::PARSE_FAILURE;
124
0
                return 0;
125
0
            }
126
40.7k
            for (; i != len; ++i) {
127
27.0k
                const char& c = s[i];
128
27.0k
                if (LIKELY('0' <= c && c <= '9')) {
129
27.0k
                    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
27.0k
                    if (exponent > std::numeric_limits<int32_t>::max()) {
135
0
                        *result = StringParser::PARSE_OVERFLOW;
136
0
                        return 0;
137
0
                    }
138
27.0k
                } else {
139
                    // '123e12abc', '123e1.2'
140
0
                    *result = StringParser::PARSE_FAILURE;
141
0
                    return 0;
142
0
                }
143
27.0k
            }
144
13.6k
            if (negative_exponent) {
145
5.89k
                exponent = -exponent;
146
5.89k
            }
147
13.6k
        } else {
148
76
            *result = StringParser::PARSE_FAILURE;
149
76
            return 0;
150
76
        }
151
13.7k
    }
152
15.1M
    T int_part_number = 0;
153
15.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
15.1M
    int64_t tmp_result_int_part_digit_count = int_part_count + exponent;
160
15.1M
    if (tmp_result_int_part_digit_count > std::numeric_limits<int>::max() ||
161
15.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
15.1M
    int result_int_part_digit_count = tmp_result_int_part_digit_count;
166
15.1M
    int actual_frac_part_count = 0;
167
15.1M
    int digit_index = 0;
168
15.1M
    if (result_int_part_digit_count >= 0) {
169
15.1M
        int max_index = std::min(found_dot ? (result_int_part_digit_count +
170
11.5M
                                              ((int_part_count > 0 && exponent > 0) ? 1 : 0))
171
15.1M
                                           : result_int_part_digit_count,
172
15.1M
                                 end_digit_index);
173
15.1M
        max_index = (max_index == std::numeric_limits<int>::min() ? end_digit_index : max_index);
174
        // skip zero number
175
15.3M
        for (; digit_index != max_index && s[digit_index] == '0'; ++digit_index) {
176
214k
        }
177
        // test 0.00, .00, 0.{00...}e2147483647
178
        // 0.00000e2147483647
179
15.1M
        if (digit_index != max_index &&
180
15.1M
            (result_int_part_digit_count - digit_index > type_precision - type_scale)) {
181
10.5k
            *result = is_negative ? StringParser::PARSE_UNDERFLOW : StringParser::PARSE_OVERFLOW;
182
10.5k
            return 0;
183
10.5k
        }
184
        // get int part number
185
42.3M
        for (; digit_index != max_index; ++digit_index) {
186
27.2M
            if (UNLIKELY(s[digit_index] == '.')) {
187
960
                continue;
188
960
            }
189
27.2M
            int_part_number = int_part_number * 10 + (s[digit_index] - '0');
190
27.2M
        }
191
15.1M
        auto total_significant_digit_count = i - ((found_dot && int_part_count > 0) ? 1 : 0);
192
15.1M
        if (result_int_part_digit_count > total_significant_digit_count) {
193
92
            int_part_number *= get_scale_multiplier<T>(result_int_part_digit_count -
194
92
                                                       total_significant_digit_count);
195
92
        }
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
42.8M
    for (; digit_index != end_digit_index && actual_frac_part_count < type_scale; ++digit_index) {
202
27.7M
        if (UNLIKELY(s[digit_index] == '.')) {
203
4.26M
            continue;
204
4.26M
        }
205
23.4M
        frac_part_number = frac_part_number * 10 + (s[digit_index] - '0');
206
23.4M
        ++actual_frac_part_count;
207
23.4M
    }
208
15.1M
    auto type_scale_multiplier = get_scale_multiplier<T>(type_scale);
209
    // there are still extra fraction digits left, check rounding
210
15.1M
    if (digit_index != end_digit_index) {
211
22.4k
        if (UNLIKELY(s[digit_index] == '.')) {
212
869
            ++digit_index;
213
869
        }
214
22.4k
        if (digit_index != end_digit_index) {
215
            // example: test 1.5 -> decimal(1, 0)
216
22.2k
            if (s[digit_index] >= '5') {
217
8.99k
                ++frac_part_number;
218
8.99k
                if (frac_part_number == type_scale_multiplier) {
219
988
                    frac_part_number = 0;
220
988
                    ++int_part_number;
221
988
                }
222
8.99k
            }
223
22.2k
        }
224
15.0M
    } else {
225
15.0M
        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
15.0M
    }
229
15.1M
    if (int_part_number >= get_scale_multiplier<T>(type_precision - type_scale)) {
230
56
        *result = is_negative ? StringParser::PARSE_UNDERFLOW : StringParser::PARSE_OVERFLOW;
231
56
        return 0;
232
56
    }
233
234
15.1M
    T value = int_part_number * type_scale_multiplier + frac_part_number;
235
15.1M
    *result = StringParser::PARSE_SUCCESS;
236
15.1M
    return is_negative ? T(-value) : T(value);
237
15.1M
}
_ZN5doris12StringParser17string_to_decimalILNS_13PrimitiveTypeE30EEENS_19PrimitiveTypeTraitsIXT_EE7CppType10NativeTypeEPKcmiiPNS0_11ParseResultE
Line
Count
Source
47
2.09M
        ParseResult* result) {
48
2.09M
    using T = typename PrimitiveTypeTraits<P>::CppType::NativeType;
49
2.09M
    static_assert(std::is_same_v<T, int32_t> || std::is_same_v<T, int64_t> ||
50
2.09M
                          std::is_same_v<T, __int128> || std::is_same_v<T, wide::Int256>,
51
2.09M
                  "Cast string to decimal only support target type int32_t, int64_t, __int128 or "
52
2.09M
                  "wide::Int256.");
53
    // Ignore leading and trailing spaces.
54
2.09M
    s = skip_ascii_whitespaces(s, len);
55
56
2.09M
    bool is_negative = false;
57
2.09M
    if (len > 0) {
58
2.00M
        switch (*s) {
59
69.7k
        case '-':
60
69.7k
            is_negative = true;
61
69.7k
            [[fallthrough]];
62
76.4k
        case '+':
63
76.4k
            ++s;
64
76.4k
            --len;
65
2.00M
        }
66
2.00M
    }
67
    // Ignore leading zeros.
68
2.09M
    bool found_value = false;
69
2.17M
    while (len > 0 && UNLIKELY(*s == '0')) {
70
79.4k
        found_value = true;
71
79.4k
        ++s;
72
79.4k
        --len;
73
79.4k
    }
74
75
2.09M
    int found_dot = 0;
76
2.09M
    if (len > 0 && *s == '.') {
77
29.1k
        found_dot = 1;
78
29.1k
        ++s;
79
29.1k
        --len;
80
29.1k
    }
81
2.09M
    int int_part_count = 0;
82
2.09M
    int i = 0;
83
36.1M
    for (; i != len; ++i) {
84
34.0M
        const char& c = s[i];
85
34.0M
        if (LIKELY('0' <= c && c <= '9')) {
86
32.2M
            found_value = true;
87
32.2M
            if (!found_dot) {
88
23.8M
                ++int_part_count;
89
23.8M
            }
90
32.2M
        } else if (c == '.') {
91
1.74M
            if (found_dot) {
92
0
                *result = StringParser::PARSE_FAILURE;
93
0
                return 0;
94
0
            }
95
1.74M
            found_dot = 1;
96
1.74M
        } else {
97
10.7k
            break;
98
10.7k
        }
99
34.0M
    }
100
2.09M
    if (!found_value) {
101
        // '', '.'
102
90.5k
        *result = StringParser::PARSE_FAILURE;
103
90.5k
        return 0;
104
90.5k
    }
105
    // parse exponent if any
106
2.00M
    int64_t exponent = 0;
107
2.00M
    auto end_digit_index = i;
108
2.00M
    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
18
            *result = StringParser::PARSE_FAILURE;
149
18
            return 0;
150
18
        }
151
12.3k
    }
152
2.00M
    T int_part_number = 0;
153
2.00M
    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.00M
    int64_t tmp_result_int_part_digit_count = int_part_count + exponent;
160
2.00M
    if (tmp_result_int_part_digit_count > std::numeric_limits<int>::max() ||
161
2.00M
        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.00M
    int result_int_part_digit_count = tmp_result_int_part_digit_count;
166
2.00M
    int actual_frac_part_count = 0;
167
2.00M
    int digit_index = 0;
168
2.00M
    if (result_int_part_digit_count >= 0) {
169
2.00M
        int max_index = std::min(found_dot ? (result_int_part_digit_count +
170
1.77M
                                              ((int_part_count > 0 && exponent > 0) ? 1 : 0))
171
2.00M
                                           : result_int_part_digit_count,
172
2.00M
                                 end_digit_index);
173
2.00M
        max_index = (max_index == std::numeric_limits<int>::min() ? end_digit_index : max_index);
174
        // skip zero number
175
2.21M
        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.00M
        if (digit_index != max_index &&
180
2.00M
            (result_int_part_digit_count - digit_index > type_precision - type_scale)) {
181
141
            *result = is_negative ? StringParser::PARSE_UNDERFLOW : StringParser::PARSE_OVERFLOW;
182
141
            return 0;
183
141
        }
184
        // get int part number
185
25.8M
        for (; digit_index != max_index; ++digit_index) {
186
23.8M
            if (UNLIKELY(s[digit_index] == '.')) {
187
960
                continue;
188
960
            }
189
23.8M
            int_part_number = int_part_number * 10 + (s[digit_index] - '0');
190
23.8M
        }
191
2.00M
        auto total_significant_digit_count = i - ((found_dot && int_part_count > 0) ? 1 : 0);
192
2.00M
        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.00M
    } else {
197
        // leading zeros of fraction part
198
862
        actual_frac_part_count = -result_int_part_digit_count;
199
862
    }
200
    // get fraction part number
201
11.9M
    for (; digit_index != end_digit_index && actual_frac_part_count < type_scale; ++digit_index) {
202
9.90M
        if (UNLIKELY(s[digit_index] == '.')) {
203
1.74M
            continue;
204
1.74M
        }
205
8.16M
        frac_part_number = frac_part_number * 10 + (s[digit_index] - '0');
206
8.16M
        ++actual_frac_part_count;
207
8.16M
    }
208
2.00M
    auto type_scale_multiplier = get_scale_multiplier<T>(type_scale);
209
    // there are still extra fraction digits left, check rounding
210
2.00M
    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
1.98M
    } else {
225
1.98M
        if (actual_frac_part_count < type_scale) {
226
350k
            frac_part_number *= get_scale_multiplier<T>(type_scale - actual_frac_part_count);
227
350k
        }
228
1.98M
    }
229
2.00M
    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.00M
    T value = int_part_number * type_scale_multiplier + frac_part_number;
235
2.00M
    *result = StringParser::PARSE_SUCCESS;
236
2.00M
    return is_negative ? T(-value) : T(value);
237
2.00M
}
_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.73k
        case '-':
60
6.73k
            is_negative = true;
61
6.73k
            [[fallthrough]];
62
6.73k
        case '+':
63
6.73k
            ++s;
64
6.73k
            --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.9k
        found_value = true;
71
38.9k
        ++s;
72
38.9k
        --len;
73
38.9k
    }
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
283k
    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.6k
                                              ((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.16k
            frac_part_number *= get_scale_multiplier<T>(type_scale - actual_frac_part_count);
227
2.16k
        }
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
136k
        ParseResult* result) {
48
136k
    using T = typename PrimitiveTypeTraits<P>::CppType::NativeType;
49
136k
    static_assert(std::is_same_v<T, int32_t> || std::is_same_v<T, int64_t> ||
50
136k
                          std::is_same_v<T, __int128> || std::is_same_v<T, wide::Int256>,
51
136k
                  "Cast string to decimal only support target type int32_t, int64_t, __int128 or "
52
136k
                  "wide::Int256.");
53
    // Ignore leading and trailing spaces.
54
136k
    s = skip_ascii_whitespaces(s, len);
55
56
136k
    bool is_negative = false;
57
136k
    if (len > 0) {
58
136k
        switch (*s) {
59
20.6k
        case '-':
60
20.6k
            is_negative = true;
61
20.6k
            [[fallthrough]];
62
27.3k
        case '+':
63
27.3k
            ++s;
64
27.3k
            --len;
65
136k
        }
66
136k
    }
67
    // Ignore leading zeros.
68
136k
    bool found_value = false;
69
234k
    while (len > 0 && UNLIKELY(*s == '0')) {
70
98.9k
        found_value = true;
71
98.9k
        ++s;
72
98.9k
        --len;
73
98.9k
    }
74
75
136k
    int found_dot = 0;
76
136k
    if (len > 0 && *s == '.') {
77
17.0k
        found_dot = 1;
78
17.0k
        ++s;
79
17.0k
        --len;
80
17.0k
    }
81
136k
    int int_part_count = 0;
82
136k
    int i = 0;
83
5.06M
    for (; i != len; ++i) {
84
5.00M
        const char& c = s[i];
85
5.00M
        if (LIKELY('0' <= c && c <= '9')) {
86
4.81M
            found_value = true;
87
4.81M
            if (!found_dot) {
88
1.80M
                ++int_part_count;
89
1.80M
            }
90
4.81M
        } else if (c == '.') {
91
106k
            if (found_dot) {
92
0
                *result = StringParser::PARSE_FAILURE;
93
0
                return 0;
94
0
            }
95
106k
            found_dot = 1;
96
106k
        } else {
97
78.0k
            break;
98
78.0k
        }
99
5.00M
    }
100
136k
    if (!found_value) {
101
        // '', '.'
102
78
        *result = StringParser::PARSE_FAILURE;
103
78
        return 0;
104
78
    }
105
    // parse exponent if any
106
135k
    int64_t exponent = 0;
107
135k
    auto end_digit_index = i;
108
135k
    if (i != len) {
109
78.0k
        bool negative_exponent = false;
110
78.0k
        if (s[i] == 'e' || s[i] == 'E') {
111
77.9k
            ++i;
112
77.9k
            if (i != len) {
113
77.9k
                switch (s[i]) {
114
3.58k
                case '-':
115
3.58k
                    negative_exponent = true;
116
3.58k
                    [[fallthrough]];
117
70.2k
                case '+':
118
70.2k
                    ++i;
119
77.9k
                }
120
77.9k
            }
121
77.9k
            if (i == len) {
122
                // '123e', '123e+', '123e-'
123
0
                *result = StringParser::PARSE_FAILURE;
124
0
                return 0;
125
0
            }
126
234k
            for (; i != len; ++i) {
127
156k
                const char& c = s[i];
128
156k
                if (LIKELY('0' <= c && c <= '9')) {
129
156k
                    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
156k
                    if (exponent > std::numeric_limits<int32_t>::max()) {
135
0
                        *result = StringParser::PARSE_OVERFLOW;
136
0
                        return 0;
137
0
                    }
138
156k
                } else {
139
                    // '123e12abc', '123e1.2'
140
10
                    *result = StringParser::PARSE_FAILURE;
141
10
                    return 0;
142
10
                }
143
156k
            }
144
77.9k
            if (negative_exponent) {
145
3.58k
                exponent = -exponent;
146
3.58k
            }
147
77.9k
        } else {
148
14
            *result = StringParser::PARSE_FAILURE;
149
14
            return 0;
150
14
        }
151
78.0k
    }
152
135k
    T int_part_number = 0;
153
135k
    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
135k
    int64_t tmp_result_int_part_digit_count = int_part_count + exponent;
160
135k
    if (tmp_result_int_part_digit_count > std::numeric_limits<int>::max() ||
161
135k
        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
135k
    int result_int_part_digit_count = tmp_result_int_part_digit_count;
166
135k
    int actual_frac_part_count = 0;
167
135k
    int digit_index = 0;
168
135k
    if (result_int_part_digit_count >= 0) {
169
135k
        int max_index = std::min(found_dot ? (result_int_part_digit_count +
170
123k
                                              ((int_part_count > 0 && exponent > 0) ? 1 : 0))
171
135k
                                           : result_int_part_digit_count,
172
135k
                                 end_digit_index);
173
135k
        max_index = (max_index == std::numeric_limits<int>::min() ? end_digit_index : max_index);
174
        // skip zero number
175
353k
        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
135k
        if (digit_index != max_index &&
180
135k
            (result_int_part_digit_count - digit_index > type_precision - type_scale)) {
181
392
            *result = is_negative ? StringParser::PARSE_UNDERFLOW : StringParser::PARSE_OVERFLOW;
182
392
            return 0;
183
392
        }
184
        // get int part number
185
2.78M
        for (; digit_index != max_index; ++digit_index) {
186
2.64M
            if (UNLIKELY(s[digit_index] == '.')) {
187
67.5k
                continue;
188
67.5k
            }
189
2.58M
            int_part_number = int_part_number * 10 + (s[digit_index] - '0');
190
2.58M
        }
191
135k
        auto total_significant_digit_count = i - ((found_dot && int_part_count > 0) ? 1 : 0);
192
135k
        if (result_int_part_digit_count > total_significant_digit_count) {
193
2.19k
            int_part_number *= get_scale_multiplier<T>(result_int_part_digit_count -
194
2.19k
                                                       total_significant_digit_count);
195
2.19k
        }
196
135k
    } else {
197
        // leading zeros of fraction part
198
175
        actual_frac_part_count = -result_int_part_digit_count;
199
175
    }
200
    // get fraction part number
201
1.93M
    for (; digit_index != end_digit_index && actual_frac_part_count < type_scale; ++digit_index) {
202
1.79M
        if (UNLIKELY(s[digit_index] == '.')) {
203
35.2k
            continue;
204
35.2k
        }
205
1.76M
        frac_part_number = frac_part_number * 10 + (s[digit_index] - '0');
206
1.76M
        ++actual_frac_part_count;
207
1.76M
    }
208
135k
    auto type_scale_multiplier = get_scale_multiplier<T>(type_scale);
209
    // there are still extra fraction digits left, check rounding
210
135k
    if (digit_index != end_digit_index) {
211
21.4k
        if (UNLIKELY(s[digit_index] == '.')) {
212
862
            ++digit_index;
213
862
        }
214
21.4k
        if (digit_index != end_digit_index) {
215
            // example: test 1.5 -> decimal(1, 0)
216
21.2k
            if (s[digit_index] >= '5') {
217
8.99k
                ++frac_part_number;
218
8.99k
                if (frac_part_number == type_scale_multiplier) {
219
988
                    frac_part_number = 0;
220
988
                    ++int_part_number;
221
988
                }
222
8.99k
            }
223
21.2k
        }
224
114k
    } else {
225
114k
        if (actual_frac_part_count < type_scale) {
226
91.6k
            frac_part_number *= get_scale_multiplier<T>(type_scale - actual_frac_part_count);
227
91.6k
        }
228
114k
    }
229
135k
    if (int_part_number >= get_scale_multiplier<T>(type_precision - type_scale)) {
230
56
        *result = is_negative ? StringParser::PARSE_UNDERFLOW : StringParser::PARSE_OVERFLOW;
231
56
        return 0;
232
56
    }
233
234
135k
    T value = int_part_number * type_scale_multiplier + frac_part_number;
235
135k
    *result = StringParser::PARSE_SUCCESS;
236
135k
    return is_negative ? T(-value) : T(value);
237
135k
}
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"