Coverage Report

Created: 2025-11-28 10:44

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/root/doris/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 "vec/core/extended_types.h"
23
namespace doris {
24
#include "common/compile_check_avoid_begin.h"
25
// Supported decimal number format:
26
// <decimal> ::= <whitespace>* <value> <whitespace>*
27
//
28
// <whitespace> ::= " " | "\t" | "\n" | "\r" | "\f" | "\v"
29
//
30
// <value> ::= <sign>? <significand> <exponent>?
31
//
32
// <sign> ::= "+" | "-"
33
//
34
// <significand> ::= <digits> "." <digits> | <digits> | <digits> "." | "." <digits>
35
//
36
// <digits> ::= <digit>+
37
//
38
// <digit> ::= "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"
39
//
40
// <exponent> ::= <e_marker> <sign>? <digits>
41
//
42
// <e_marker> ::= "e" | "E"
43
template <PrimitiveType P>
44
typename PrimitiveTypeTraits<P>::CppType::NativeType StringParser::string_to_decimal(
45
        const char* __restrict s, size_t len, int type_precision, int type_scale,
46
351k
        ParseResult* result) {
47
351k
    using T = typename PrimitiveTypeTraits<P>::CppType::NativeType;
48
351k
    static_assert(std::is_same_v<T, int32_t> || std::is_same_v<T, int64_t> ||
49
351k
                          std::is_same_v<T, __int128> || std::is_same_v<T, wide::Int256>,
50
351k
                  "Cast string to decimal only support target type int32_t, int64_t, __int128 or "
51
351k
                  "wide::Int256.");
52
    // Ignore leading and trailing spaces.
53
351k
    s = skip_ascii_whitespaces(s, len);
54
55
351k
    bool is_negative = false;
56
351k
    if (len > 0) {
57
351k
        switch (*s) {
58
92.3k
        case '-':
59
92.3k
            is_negative = true;
60
92.3k
            [[fallthrough]];
61
119k
        case '+':
62
119k
            ++s;
63
119k
            --len;
64
351k
        }
65
351k
    }
66
    // Ignore leading zeros.
67
351k
    bool found_value = false;
68
688k
    while (len > 0 && UNLIKELY(*s == '0')) {
69
337k
        found_value = true;
70
337k
        ++s;
71
337k
        --len;
72
337k
    }
73
74
351k
    int found_dot = 0;
75
351k
    if (len > 0 && *s == '.') {
76
84.5k
        found_dot = 1;
77
84.5k
        ++s;
78
84.5k
        --len;
79
84.5k
    }
80
351k
    int int_part_count = 0;
81
351k
    std::vector<unsigned char> digits;
82
351k
    if (len > 0) {
83
343k
        digits.resize(len);
84
343k
    }
85
351k
    int total_digit_count = 0;
86
351k
    int i = 0;
87
8.19M
    for (; i != len; ++i) {
88
7.95M
        const char& c = s[i];
89
7.95M
        if (LIKELY('0' <= c && c <= '9')) {
90
7.62M
            found_value = true;
91
7.62M
            digits[total_digit_count++] = c - '0';
92
7.62M
            if (!found_dot) {
93
2.35M
                ++int_part_count;
94
2.35M
            }
95
7.62M
        } else if (c == '.') {
96
222k
            if (found_dot) {
97
2
                *result = StringParser::PARSE_FAILURE;
98
2
                return 0;
99
2
            }
100
222k
            found_dot = 1;
101
222k
        } else {
102
109k
            break;
103
109k
        }
104
7.95M
    }
105
351k
    if (!found_value) {
106
        // '', '.'
107
346
        *result = StringParser::PARSE_FAILURE;
108
346
        return 0;
109
346
    }
110
    // parse exponent if any
111
351k
    int64_t exponent = 0;
112
351k
    if (i != len) {
113
109k
        bool negative_exponent = false;
114
109k
        if (s[i] == 'e' || s[i] == 'E') {
115
109k
            ++i;
116
109k
            if (i != len) {
117
109k
                switch (s[i]) {
118
11.5k
                case '-':
119
11.5k
                    negative_exponent = true;
120
11.5k
                    [[fallthrough]];
121
78.1k
                case '+':
122
78.1k
                    ++i;
123
109k
                }
124
109k
            }
125
109k
            if (i == len) {
126
                // '123e', '123e+', '123e-'
127
6
                *result = StringParser::PARSE_FAILURE;
128
6
                return 0;
129
6
            }
130
319k
            for (; i != len; ++i) {
131
209k
                const char& c = s[i];
132
209k
                if (LIKELY('0' <= c && c <= '9')) {
133
209k
                    exponent = exponent * 10 + (c - '0');
134
                    // max string len is config::string_type_length_soft_limit_bytes,
135
                    // whose max value is std::numeric_limits<int32_t>::max() - 4,
136
                    // just check overflow of int32_t to simplify the logic
137
                    // For edge cases like 0.{2147483647 zeros}e+2147483647
138
209k
                    if (exponent > std::numeric_limits<int32_t>::max()) {
139
0
                        *result = StringParser::PARSE_OVERFLOW;
140
0
                        return 0;
141
0
                    }
142
209k
                } else {
143
                    // '123e12abc', '123e1.2'
144
22
                    *result = StringParser::PARSE_FAILURE;
145
22
                    return 0;
146
22
                }
147
209k
            }
148
109k
            if (negative_exponent) {
149
11.5k
                exponent = -exponent;
150
11.5k
            }
151
109k
        } else {
152
110
            *result = StringParser::PARSE_FAILURE;
153
110
            return 0;
154
110
        }
155
109k
    }
156
350k
    T int_part_number = 0;
157
350k
    T frac_part_number = 0;
158
    // TODO: check limit values of exponent and add UT
159
    // max string len is config::string_type_length_soft_limit_bytes,
160
    // whose max value is std::numeric_limits<int32_t>::max() - 4,
161
    // so int_part_count will be in range of int32_t,
162
    // and int_part_count + exponent will be in range of int64_t
163
350k
    int64_t tmp_actual_int_part_count = int_part_count + exponent;
164
350k
    if (tmp_actual_int_part_count > std::numeric_limits<int>::max() ||
165
350k
        tmp_actual_int_part_count < std::numeric_limits<int>::min()) {
166
0
        *result = StringParser::PARSE_OVERFLOW;
167
0
        return 0;
168
0
    }
169
350k
    int actual_int_part_count = tmp_actual_int_part_count;
170
350k
    int actual_frac_part_count = 0;
171
350k
    int digit_index = 0;
172
350k
    if (actual_int_part_count >= 0) {
173
345k
        int max_index = std::min(actual_int_part_count, total_digit_count);
174
        // skip zero number
175
1.20M
        for (; digit_index != max_index && digits[digit_index] == 0; ++digit_index) {
176
855k
        }
177
        // test 0.00, .00, 0.{00...}e2147483647
178
        // 0.00000e2147483647
179
345k
        if (max_index - digit_index > type_precision - type_scale) {
180
11.7k
            *result = is_negative ? StringParser::PARSE_UNDERFLOW : StringParser::PARSE_OVERFLOW;
181
11.7k
            return 0;
182
11.7k
        }
183
        // get int part number
184
3.39M
        for (; digit_index != max_index; ++digit_index) {
185
3.06M
            int_part_number = int_part_number * 10 + digits[digit_index];
186
3.06M
        }
187
333k
        if (digit_index != actual_int_part_count) {
188
64.8k
            int_part_number *= get_scale_multiplier<T>(actual_int_part_count - digit_index);
189
64.8k
        }
190
333k
    } else {
191
        // leading zeros of fraction part
192
5.58k
        actual_frac_part_count = -actual_int_part_count;
193
5.58k
    }
194
    // get fraction part number
195
3.49M
    for (; digit_index != total_digit_count && actual_frac_part_count < type_scale;
196
3.15M
         ++digit_index, ++actual_frac_part_count) {
197
3.15M
        frac_part_number = frac_part_number * 10 + digits[digit_index];
198
3.15M
    }
199
339k
    auto type_scale_multiplier = get_scale_multiplier<T>(type_scale);
200
    // there are still extra fraction digits left, check rounding
201
339k
    if (digit_index != total_digit_count) {
202
        // example: test 1.5 -> decimal(1, 0)
203
81.3k
        if (digits[digit_index] >= 5) {
204
32.9k
            ++frac_part_number;
205
32.9k
            if (frac_part_number == type_scale_multiplier) {
206
3.43k
                frac_part_number = 0;
207
3.43k
                ++int_part_number;
208
3.43k
            }
209
32.9k
        }
210
257k
    } else {
211
257k
        if (actual_frac_part_count < type_scale) {
212
192k
            frac_part_number *= get_scale_multiplier<T>(type_scale - actual_frac_part_count);
213
192k
        }
214
257k
    }
215
339k
    if (int_part_number >= get_scale_multiplier<T>(type_precision - type_scale)) {
216
73
        *result = is_negative ? StringParser::PARSE_UNDERFLOW : StringParser::PARSE_OVERFLOW;
217
73
        return 0;
218
73
    }
219
220
339k
    T value = int_part_number * type_scale_multiplier + frac_part_number;
221
339k
    *result = StringParser::PARSE_SUCCESS;
222
339k
    return is_negative ? T(-value) : T(value);
223
339k
}
_ZN5doris12StringParser17string_to_decimalILNS_13PrimitiveTypeE28EEENS_19PrimitiveTypeTraitsIXT_EE7CppType10NativeTypeEPKcmiiPNS0_11ParseResultE
Line
Count
Source
46
54.1k
        ParseResult* result) {
47
54.1k
    using T = typename PrimitiveTypeTraits<P>::CppType::NativeType;
48
54.1k
    static_assert(std::is_same_v<T, int32_t> || std::is_same_v<T, int64_t> ||
49
54.1k
                          std::is_same_v<T, __int128> || std::is_same_v<T, wide::Int256>,
50
54.1k
                  "Cast string to decimal only support target type int32_t, int64_t, __int128 or "
51
54.1k
                  "wide::Int256.");
52
    // Ignore leading and trailing spaces.
53
54.1k
    s = skip_ascii_whitespaces(s, len);
54
55
54.1k
    bool is_negative = false;
56
54.1k
    if (len > 0) {
57
54.1k
        switch (*s) {
58
24.9k
        case '-':
59
24.9k
            is_negative = true;
60
24.9k
            [[fallthrough]];
61
32.2k
        case '+':
62
32.2k
            ++s;
63
32.2k
            --len;
64
54.1k
        }
65
54.1k
    }
66
    // Ignore leading zeros.
67
54.1k
    bool found_value = false;
68
107k
    while (len > 0 && UNLIKELY(*s == '0')) {
69
53.6k
        found_value = true;
70
53.6k
        ++s;
71
53.6k
        --len;
72
53.6k
    }
73
74
54.1k
    int found_dot = 0;
75
54.1k
    if (len > 0 && *s == '.') {
76
17.8k
        found_dot = 1;
77
17.8k
        ++s;
78
17.8k
        --len;
79
17.8k
    }
80
54.1k
    int int_part_count = 0;
81
54.1k
    std::vector<unsigned char> digits;
82
54.1k
    if (len > 0) {
83
52.0k
        digits.resize(len);
84
52.0k
    }
85
54.1k
    int total_digit_count = 0;
86
54.1k
    int i = 0;
87
577k
    for (; i != len; ++i) {
88
532k
        const char& c = s[i];
89
532k
        if (LIKELY('0' <= c && c <= '9')) {
90
508k
            found_value = true;
91
508k
            digits[total_digit_count++] = c - '0';
92
508k
            if (!found_dot) {
93
158k
                ++int_part_count;
94
158k
            }
95
508k
        } else if (c == '.') {
96
14.5k
            if (found_dot) {
97
2
                *result = StringParser::PARSE_FAILURE;
98
2
                return 0;
99
2
            }
100
14.5k
            found_dot = 1;
101
14.5k
        } else {
102
9.52k
            break;
103
9.52k
        }
104
532k
    }
105
54.1k
    if (!found_value) {
106
        // '', '.'
107
146
        *result = StringParser::PARSE_FAILURE;
108
146
        return 0;
109
146
    }
110
    // parse exponent if any
111
54.0k
    int64_t exponent = 0;
112
54.0k
    if (i != len) {
113
9.39k
        bool negative_exponent = false;
114
9.39k
        if (s[i] == 'e' || s[i] == 'E') {
115
9.33k
            ++i;
116
9.33k
            if (i != len) {
117
9.33k
                switch (s[i]) {
118
1.54k
                case '-':
119
1.54k
                    negative_exponent = true;
120
1.54k
                    [[fallthrough]];
121
1.54k
                case '+':
122
1.54k
                    ++i;
123
9.33k
                }
124
9.33k
            }
125
9.33k
            if (i == len) {
126
                // '123e', '123e+', '123e-'
127
6
                *result = StringParser::PARSE_FAILURE;
128
6
                return 0;
129
6
            }
130
24.6k
            for (; i != len; ++i) {
131
15.3k
                const char& c = s[i];
132
15.3k
                if (LIKELY('0' <= c && c <= '9')) {
133
15.3k
                    exponent = exponent * 10 + (c - '0');
134
                    // max string len is config::string_type_length_soft_limit_bytes,
135
                    // whose max value is std::numeric_limits<int32_t>::max() - 4,
136
                    // just check overflow of int32_t to simplify the logic
137
                    // For edge cases like 0.{2147483647 zeros}e+2147483647
138
15.3k
                    if (exponent > std::numeric_limits<int32_t>::max()) {
139
0
                        *result = StringParser::PARSE_OVERFLOW;
140
0
                        return 0;
141
0
                    }
142
15.3k
                } else {
143
                    // '123e12abc', '123e1.2'
144
12
                    *result = StringParser::PARSE_FAILURE;
145
12
                    return 0;
146
12
                }
147
15.3k
            }
148
9.31k
            if (negative_exponent) {
149
1.53k
                exponent = -exponent;
150
1.53k
            }
151
9.31k
        } else {
152
60
            *result = StringParser::PARSE_FAILURE;
153
60
            return 0;
154
60
        }
155
9.39k
    }
156
53.9k
    T int_part_number = 0;
157
53.9k
    T frac_part_number = 0;
158
    // TODO: check limit values of exponent and add UT
159
    // max string len is config::string_type_length_soft_limit_bytes,
160
    // whose max value is std::numeric_limits<int32_t>::max() - 4,
161
    // so int_part_count will be in range of int32_t,
162
    // and int_part_count + exponent will be in range of int64_t
163
53.9k
    int64_t tmp_actual_int_part_count = int_part_count + exponent;
164
53.9k
    if (tmp_actual_int_part_count > std::numeric_limits<int>::max() ||
165
53.9k
        tmp_actual_int_part_count < std::numeric_limits<int>::min()) {
166
0
        *result = StringParser::PARSE_OVERFLOW;
167
0
        return 0;
168
0
    }
169
53.9k
    int actual_int_part_count = tmp_actual_int_part_count;
170
53.9k
    int actual_frac_part_count = 0;
171
53.9k
    int digit_index = 0;
172
53.9k
    if (actual_int_part_count >= 0) {
173
53.8k
        int max_index = std::min(actual_int_part_count, total_digit_count);
174
        // skip zero number
175
266k
        for (; digit_index != max_index && digits[digit_index] == 0; ++digit_index) {
176
212k
        }
177
        // test 0.00, .00, 0.{00...}e2147483647
178
        // 0.00000e2147483647
179
53.8k
        if (max_index - digit_index > type_precision - type_scale) {
180
1.30k
            *result = is_negative ? StringParser::PARSE_UNDERFLOW : StringParser::PARSE_OVERFLOW;
181
1.30k
            return 0;
182
1.30k
        }
183
        // get int part number
184
152k
        for (; digit_index != max_index; ++digit_index) {
185
100k
            int_part_number = int_part_number * 10 + digits[digit_index];
186
100k
        }
187
52.5k
        if (digit_index != actual_int_part_count) {
188
100
            int_part_number *= get_scale_multiplier<T>(actual_int_part_count - digit_index);
189
100
        }
190
52.5k
    } else {
191
        // leading zeros of fraction part
192
48
        actual_frac_part_count = -actual_int_part_count;
193
48
    }
194
    // get fraction part number
195
158k
    for (; digit_index != total_digit_count && actual_frac_part_count < type_scale;
196
106k
         ++digit_index, ++actual_frac_part_count) {
197
106k
        frac_part_number = frac_part_number * 10 + digits[digit_index];
198
106k
    }
199
52.6k
    auto type_scale_multiplier = get_scale_multiplier<T>(type_scale);
200
    // there are still extra fraction digits left, check rounding
201
52.6k
    if (digit_index != total_digit_count) {
202
        // example: test 1.5 -> decimal(1, 0)
203
21.1k
        if (digits[digit_index] >= 5) {
204
8.96k
            ++frac_part_number;
205
8.96k
            if (frac_part_number == type_scale_multiplier) {
206
856
                frac_part_number = 0;
207
856
                ++int_part_number;
208
856
            }
209
8.96k
        }
210
31.5k
    } else {
211
31.5k
        if (actual_frac_part_count < type_scale) {
212
27.7k
            frac_part_number *= get_scale_multiplier<T>(type_scale - actual_frac_part_count);
213
27.7k
        }
214
31.5k
    }
215
52.6k
    if (int_part_number >= get_scale_multiplier<T>(type_precision - type_scale)) {
216
24
        *result = is_negative ? StringParser::PARSE_UNDERFLOW : StringParser::PARSE_OVERFLOW;
217
24
        return 0;
218
24
    }
219
220
52.6k
    T value = int_part_number * type_scale_multiplier + frac_part_number;
221
52.6k
    *result = StringParser::PARSE_SUCCESS;
222
52.6k
    return is_negative ? T(-value) : T(value);
223
52.6k
}
_ZN5doris12StringParser17string_to_decimalILNS_13PrimitiveTypeE29EEENS_19PrimitiveTypeTraitsIXT_EE7CppType10NativeTypeEPKcmiiPNS0_11ParseResultE
Line
Count
Source
46
85.7k
        ParseResult* result) {
47
85.7k
    using T = typename PrimitiveTypeTraits<P>::CppType::NativeType;
48
85.7k
    static_assert(std::is_same_v<T, int32_t> || std::is_same_v<T, int64_t> ||
49
85.7k
                          std::is_same_v<T, __int128> || std::is_same_v<T, wide::Int256>,
50
85.7k
                  "Cast string to decimal only support target type int32_t, int64_t, __int128 or "
51
85.7k
                  "wide::Int256.");
52
    // Ignore leading and trailing spaces.
53
85.7k
    s = skip_ascii_whitespaces(s, len);
54
55
85.7k
    bool is_negative = false;
56
85.7k
    if (len > 0) {
57
85.7k
        switch (*s) {
58
21.7k
        case '-':
59
21.7k
            is_negative = true;
60
21.7k
            [[fallthrough]];
61
28.3k
        case '+':
62
28.3k
            ++s;
63
28.3k
            --len;
64
85.7k
        }
65
85.7k
    }
66
    // Ignore leading zeros.
67
85.7k
    bool found_value = false;
68
161k
    while (len > 0 && UNLIKELY(*s == '0')) {
69
76.0k
        found_value = true;
70
76.0k
        ++s;
71
76.0k
        --len;
72
76.0k
    }
73
74
85.7k
    int found_dot = 0;
75
85.7k
    if (len > 0 && *s == '.') {
76
23.8k
        found_dot = 1;
77
23.8k
        ++s;
78
23.8k
        --len;
79
23.8k
    }
80
85.7k
    int int_part_count = 0;
81
85.7k
    std::vector<unsigned char> digits;
82
85.7k
    if (len > 0) {
83
83.6k
        digits.resize(len);
84
83.6k
    }
85
85.7k
    int total_digit_count = 0;
86
85.7k
    int i = 0;
87
1.40M
    for (; i != len; ++i) {
88
1.32M
        const char& c = s[i];
89
1.32M
        if (LIKELY('0' <= c && c <= '9')) {
90
1.26M
            found_value = true;
91
1.26M
            digits[total_digit_count++] = c - '0';
92
1.26M
            if (!found_dot) {
93
517k
                ++int_part_count;
94
517k
            }
95
1.26M
        } else if (c == '.') {
96
53.2k
            if (found_dot) {
97
0
                *result = StringParser::PARSE_FAILURE;
98
0
                return 0;
99
0
            }
100
53.2k
            found_dot = 1;
101
53.2k
        } else {
102
11.6k
            break;
103
11.6k
        }
104
1.32M
    }
105
85.7k
    if (!found_value) {
106
        // '', '.'
107
69
        *result = StringParser::PARSE_FAILURE;
108
69
        return 0;
109
69
    }
110
    // parse exponent if any
111
85.6k
    int64_t exponent = 0;
112
85.6k
    if (i != len) {
113
11.6k
        bool negative_exponent = false;
114
11.6k
        if (s[i] == 'e' || s[i] == 'E') {
115
11.6k
            ++i;
116
11.6k
            if (i != len) {
117
11.6k
                switch (s[i]) {
118
3.84k
                case '-':
119
3.84k
                    negative_exponent = true;
120
3.84k
                    [[fallthrough]];
121
3.84k
                case '+':
122
3.84k
                    ++i;
123
11.6k
                }
124
11.6k
            }
125
11.6k
            if (i == len) {
126
                // '123e', '123e+', '123e-'
127
0
                *result = StringParser::PARSE_FAILURE;
128
0
                return 0;
129
0
            }
130
32.3k
            for (; i != len; ++i) {
131
20.7k
                const char& c = s[i];
132
20.7k
                if (LIKELY('0' <= c && c <= '9')) {
133
20.7k
                    exponent = exponent * 10 + (c - '0');
134
                    // max string len is config::string_type_length_soft_limit_bytes,
135
                    // whose max value is std::numeric_limits<int32_t>::max() - 4,
136
                    // just check overflow of int32_t to simplify the logic
137
                    // For edge cases like 0.{2147483647 zeros}e+2147483647
138
20.7k
                    if (exponent > std::numeric_limits<int32_t>::max()) {
139
0
                        *result = StringParser::PARSE_OVERFLOW;
140
0
                        return 0;
141
0
                    }
142
20.7k
                } else {
143
                    // '123e12abc', '123e1.2'
144
0
                    *result = StringParser::PARSE_FAILURE;
145
0
                    return 0;
146
0
                }
147
20.7k
            }
148
11.6k
            if (negative_exponent) {
149
3.84k
                exponent = -exponent;
150
3.84k
            }
151
11.6k
        } else {
152
23
            *result = StringParser::PARSE_FAILURE;
153
23
            return 0;
154
23
        }
155
11.6k
    }
156
85.6k
    T int_part_number = 0;
157
85.6k
    T frac_part_number = 0;
158
    // TODO: check limit values of exponent and add UT
159
    // max string len is config::string_type_length_soft_limit_bytes,
160
    // whose max value is std::numeric_limits<int32_t>::max() - 4,
161
    // so int_part_count will be in range of int32_t,
162
    // and int_part_count + exponent will be in range of int64_t
163
85.6k
    int64_t tmp_actual_int_part_count = int_part_count + exponent;
164
85.6k
    if (tmp_actual_int_part_count > std::numeric_limits<int>::max() ||
165
85.6k
        tmp_actual_int_part_count < std::numeric_limits<int>::min()) {
166
0
        *result = StringParser::PARSE_OVERFLOW;
167
0
        return 0;
168
0
    }
169
85.6k
    int actual_int_part_count = tmp_actual_int_part_count;
170
85.6k
    int actual_frac_part_count = 0;
171
85.6k
    int digit_index = 0;
172
85.6k
    if (actual_int_part_count >= 0) {
173
83.2k
        int max_index = std::min(actual_int_part_count, total_digit_count);
174
        // skip zero number
175
295k
        for (; digit_index != max_index && digits[digit_index] == 0; ++digit_index) {
176
212k
        }
177
        // test 0.00, .00, 0.{00...}e2147483647
178
        // 0.00000e2147483647
179
83.2k
        if (max_index - digit_index > type_precision - type_scale) {
180
10.1k
            *result = is_negative ? StringParser::PARSE_UNDERFLOW : StringParser::PARSE_OVERFLOW;
181
10.1k
            return 0;
182
10.1k
        }
183
        // get int part number
184
407k
        for (; digit_index != max_index; ++digit_index) {
185
334k
            int_part_number = int_part_number * 10 + digits[digit_index];
186
334k
        }
187
73.1k
        if (digit_index != actual_int_part_count) {
188
77
            int_part_number *= get_scale_multiplier<T>(actual_int_part_count - digit_index);
189
77
        }
190
73.1k
    } else {
191
        // leading zeros of fraction part
192
2.35k
        actual_frac_part_count = -actual_int_part_count;
193
2.35k
    }
194
    // get fraction part number
195
549k
    for (; digit_index != total_digit_count && actual_frac_part_count < type_scale;
196
473k
         ++digit_index, ++actual_frac_part_count) {
197
473k
        frac_part_number = frac_part_number * 10 + digits[digit_index];
198
473k
    }
199
75.4k
    auto type_scale_multiplier = get_scale_multiplier<T>(type_scale);
200
    // there are still extra fraction digits left, check rounding
201
75.4k
    if (digit_index != total_digit_count) {
202
        // example: test 1.5 -> decimal(1, 0)
203
19.8k
        if (digits[digit_index] >= 5) {
204
7.95k
            ++frac_part_number;
205
7.95k
            if (frac_part_number == type_scale_multiplier) {
206
836
                frac_part_number = 0;
207
836
                ++int_part_number;
208
836
            }
209
7.95k
        }
210
55.6k
    } else {
211
55.6k
        if (actual_frac_part_count < type_scale) {
212
31.4k
            frac_part_number *= get_scale_multiplier<T>(type_scale - actual_frac_part_count);
213
31.4k
        }
214
55.6k
    }
215
75.4k
    if (int_part_number >= get_scale_multiplier<T>(type_precision - type_scale)) {
216
17
        *result = is_negative ? StringParser::PARSE_UNDERFLOW : StringParser::PARSE_OVERFLOW;
217
17
        return 0;
218
17
    }
219
220
75.4k
    T value = int_part_number * type_scale_multiplier + frac_part_number;
221
75.4k
    *result = StringParser::PARSE_SUCCESS;
222
75.4k
    return is_negative ? T(-value) : T(value);
223
75.4k
}
_ZN5doris12StringParser17string_to_decimalILNS_13PrimitiveTypeE30EEENS_19PrimitiveTypeTraitsIXT_EE7CppType10NativeTypeEPKcmiiPNS0_11ParseResultE
Line
Count
Source
46
82.3k
        ParseResult* result) {
47
82.3k
    using T = typename PrimitiveTypeTraits<P>::CppType::NativeType;
48
82.3k
    static_assert(std::is_same_v<T, int32_t> || std::is_same_v<T, int64_t> ||
49
82.3k
                          std::is_same_v<T, __int128> || std::is_same_v<T, wide::Int256>,
50
82.3k
                  "Cast string to decimal only support target type int32_t, int64_t, __int128 or "
51
82.3k
                  "wide::Int256.");
52
    // Ignore leading and trailing spaces.
53
82.3k
    s = skip_ascii_whitespaces(s, len);
54
55
82.3k
    bool is_negative = false;
56
82.3k
    if (len > 0) {
57
82.3k
        switch (*s) {
58
21.7k
        case '-':
59
21.7k
            is_negative = true;
60
21.7k
            [[fallthrough]];
61
28.3k
        case '+':
62
28.3k
            ++s;
63
28.3k
            --len;
64
82.3k
        }
65
82.3k
    }
66
    // Ignore leading zeros.
67
82.3k
    bool found_value = false;
68
157k
    while (len > 0 && UNLIKELY(*s == '0')) {
69
74.6k
        found_value = true;
70
74.6k
        ++s;
71
74.6k
        --len;
72
74.6k
    }
73
74
82.3k
    int found_dot = 0;
75
82.3k
    if (len > 0 && *s == '.') {
76
24.9k
        found_dot = 1;
77
24.9k
        ++s;
78
24.9k
        --len;
79
24.9k
    }
80
82.3k
    int int_part_count = 0;
81
82.3k
    std::vector<unsigned char> digits;
82
82.3k
    if (len > 0) {
83
80.2k
        digits.resize(len);
84
80.2k
    }
85
82.3k
    int total_digit_count = 0;
86
82.3k
    int i = 0;
87
2.05M
    for (; i != len; ++i) {
88
1.98M
        const char& c = s[i];
89
1.98M
        if (LIKELY('0' <= c && c <= '9')) {
90
1.92M
            found_value = true;
91
1.92M
            digits[total_digit_count++] = c - '0';
92
1.92M
            if (!found_dot) {
93
558k
                ++int_part_count;
94
558k
            }
95
1.92M
        } else if (c == '.') {
96
49.5k
            if (found_dot) {
97
0
                *result = StringParser::PARSE_FAILURE;
98
0
                return 0;
99
0
            }
100
49.5k
            found_dot = 1;
101
49.5k
        } else {
102
12.4k
            break;
103
12.4k
        }
104
1.98M
    }
105
82.3k
    if (!found_value) {
106
        // '', '.'
107
55
        *result = StringParser::PARSE_FAILURE;
108
55
        return 0;
109
55
    }
110
    // parse exponent if any
111
82.2k
    int64_t exponent = 0;
112
82.2k
    if (i != len) {
113
12.3k
        bool negative_exponent = false;
114
12.3k
        if (s[i] == 'e' || s[i] == 'E') {
115
12.3k
            ++i;
116
12.3k
            if (i != len) {
117
12.3k
                switch (s[i]) {
118
4.61k
                case '-':
119
4.61k
                    negative_exponent = true;
120
4.61k
                    [[fallthrough]];
121
4.61k
                case '+':
122
4.61k
                    ++i;
123
12.3k
                }
124
12.3k
            }
125
12.3k
            if (i == len) {
126
                // '123e', '123e+', '123e-'
127
0
                *result = StringParser::PARSE_FAILURE;
128
0
                return 0;
129
0
            }
130
35.6k
            for (; i != len; ++i) {
131
23.3k
                const char& c = s[i];
132
23.3k
                if (LIKELY('0' <= c && c <= '9')) {
133
23.3k
                    exponent = exponent * 10 + (c - '0');
134
                    // max string len is config::string_type_length_soft_limit_bytes,
135
                    // whose max value is std::numeric_limits<int32_t>::max() - 4,
136
                    // just check overflow of int32_t to simplify the logic
137
                    // For edge cases like 0.{2147483647 zeros}e+2147483647
138
23.3k
                    if (exponent > std::numeric_limits<int32_t>::max()) {
139
0
                        *result = StringParser::PARSE_OVERFLOW;
140
0
                        return 0;
141
0
                    }
142
23.3k
                } else {
143
                    // '123e12abc', '123e1.2'
144
0
                    *result = StringParser::PARSE_FAILURE;
145
0
                    return 0;
146
0
                }
147
23.3k
            }
148
12.3k
            if (negative_exponent) {
149
4.61k
                exponent = -exponent;
150
4.61k
            }
151
12.3k
        } else {
152
12
            *result = StringParser::PARSE_FAILURE;
153
12
            return 0;
154
12
        }
155
12.3k
    }
156
82.2k
    T int_part_number = 0;
157
82.2k
    T frac_part_number = 0;
158
    // TODO: check limit values of exponent and add UT
159
    // max string len is config::string_type_length_soft_limit_bytes,
160
    // whose max value is std::numeric_limits<int32_t>::max() - 4,
161
    // so int_part_count will be in range of int32_t,
162
    // and int_part_count + exponent will be in range of int64_t
163
82.2k
    int64_t tmp_actual_int_part_count = int_part_count + exponent;
164
82.2k
    if (tmp_actual_int_part_count > std::numeric_limits<int>::max() ||
165
82.2k
        tmp_actual_int_part_count < std::numeric_limits<int>::min()) {
166
0
        *result = StringParser::PARSE_OVERFLOW;
167
0
        return 0;
168
0
    }
169
82.2k
    int actual_int_part_count = tmp_actual_int_part_count;
170
82.2k
    int actual_frac_part_count = 0;
171
82.2k
    int digit_index = 0;
172
82.2k
    if (actual_int_part_count >= 0) {
173
79.1k
        int max_index = std::min(actual_int_part_count, total_digit_count);
174
        // skip zero number
175
293k
        for (; digit_index != max_index && digits[digit_index] == 0; ++digit_index) {
176
213k
        }
177
        // test 0.00, .00, 0.{00...}e2147483647
178
        // 0.00000e2147483647
179
79.1k
        if (max_index - digit_index > type_precision - type_scale) {
180
140
            *result = is_negative ? StringParser::PARSE_UNDERFLOW : StringParser::PARSE_OVERFLOW;
181
140
            return 0;
182
140
        }
183
        // get int part number
184
596k
        for (; digit_index != max_index; ++digit_index) {
185
517k
            int_part_number = int_part_number * 10 + digits[digit_index];
186
517k
        }
187
78.9k
        if (digit_index != actual_int_part_count) {
188
76
            int_part_number *= get_scale_multiplier<T>(actual_int_part_count - digit_index);
189
76
        }
190
78.9k
    } else {
191
        // leading zeros of fraction part
192
3.12k
        actual_frac_part_count = -actual_int_part_count;
193
3.12k
    }
194
    // get fraction part number
195
1.16M
    for (; digit_index != total_digit_count && actual_frac_part_count < type_scale;
196
1.08M
         ++digit_index, ++actual_frac_part_count) {
197
1.08M
        frac_part_number = frac_part_number * 10 + digits[digit_index];
198
1.08M
    }
199
82.1k
    auto type_scale_multiplier = get_scale_multiplier<T>(type_scale);
200
    // there are still extra fraction digits left, check rounding
201
82.1k
    if (digit_index != total_digit_count) {
202
        // example: test 1.5 -> decimal(1, 0)
203
21.4k
        if (digits[digit_index] >= 5) {
204
8.03k
            ++frac_part_number;
205
8.03k
            if (frac_part_number == type_scale_multiplier) {
206
906
                frac_part_number = 0;
207
906
                ++int_part_number;
208
906
            }
209
8.03k
        }
210
60.6k
    } else {
211
60.6k
        if (actual_frac_part_count < type_scale) {
212
44.5k
            frac_part_number *= get_scale_multiplier<T>(type_scale - actual_frac_part_count);
213
44.5k
        }
214
60.6k
    }
215
82.1k
    if (int_part_number >= get_scale_multiplier<T>(type_precision - type_scale)) {
216
16
        *result = is_negative ? StringParser::PARSE_UNDERFLOW : StringParser::PARSE_OVERFLOW;
217
16
        return 0;
218
16
    }
219
220
82.1k
    T value = int_part_number * type_scale_multiplier + frac_part_number;
221
82.1k
    *result = StringParser::PARSE_SUCCESS;
222
82.1k
    return is_negative ? T(-value) : T(value);
223
82.1k
}
_ZN5doris12StringParser17string_to_decimalILNS_13PrimitiveTypeE20EEENS_19PrimitiveTypeTraitsIXT_EE7CppType10NativeTypeEPKcmiiPNS0_11ParseResultE
Line
Count
Source
46
13.5k
        ParseResult* result) {
47
13.5k
    using T = typename PrimitiveTypeTraits<P>::CppType::NativeType;
48
13.5k
    static_assert(std::is_same_v<T, int32_t> || std::is_same_v<T, int64_t> ||
49
13.5k
                          std::is_same_v<T, __int128> || std::is_same_v<T, wide::Int256>,
50
13.5k
                  "Cast string to decimal only support target type int32_t, int64_t, __int128 or "
51
13.5k
                  "wide::Int256.");
52
    // Ignore leading and trailing spaces.
53
13.5k
    s = skip_ascii_whitespaces(s, len);
54
55
13.5k
    bool is_negative = false;
56
13.5k
    if (len > 0) {
57
13.5k
        switch (*s) {
58
6.68k
        case '-':
59
6.68k
            is_negative = true;
60
6.68k
            [[fallthrough]];
61
6.68k
        case '+':
62
6.68k
            ++s;
63
6.68k
            --len;
64
13.5k
        }
65
13.5k
    }
66
    // Ignore leading zeros.
67
13.5k
    bool found_value = false;
68
52.3k
    while (len > 0 && UNLIKELY(*s == '0')) {
69
38.8k
        found_value = true;
70
38.8k
        ++s;
71
38.8k
        --len;
72
38.8k
    }
73
74
13.5k
    int found_dot = 0;
75
13.5k
    if (len > 0 && *s == '.') {
76
2.00k
        found_dot = 1;
77
2.00k
        ++s;
78
2.00k
        --len;
79
2.00k
    }
80
13.5k
    int int_part_count = 0;
81
13.5k
    std::vector<unsigned char> digits;
82
13.5k
    if (len > 0) {
83
13.5k
        digits.resize(len);
84
13.5k
    }
85
13.5k
    int total_digit_count = 0;
86
13.5k
    int i = 0;
87
279k
    for (; i != len; ++i) {
88
266k
        const char& c = s[i];
89
266k
        if (LIKELY('0' <= c && c <= '9')) {
90
254k
            found_value = true;
91
254k
            digits[total_digit_count++] = c - '0';
92
254k
            if (!found_dot) {
93
136k
                ++int_part_count;
94
136k
            }
95
254k
        } else if (c == '.') {
96
11.4k
            if (found_dot) {
97
0
                *result = StringParser::PARSE_FAILURE;
98
0
                return 0;
99
0
            }
100
11.4k
            found_dot = 1;
101
11.4k
        } else {
102
11
            break;
103
11
        }
104
266k
    }
105
13.5k
    if (!found_value) {
106
        // '', '.'
107
10
        *result = StringParser::PARSE_FAILURE;
108
10
        return 0;
109
10
    }
110
    // parse exponent if any
111
13.5k
    int64_t exponent = 0;
112
13.5k
    if (i != len) {
113
1
        bool negative_exponent = false;
114
1
        if (s[i] == 'e' || s[i] == 'E') {
115
0
            ++i;
116
0
            if (i != len) {
117
0
                switch (s[i]) {
118
0
                case '-':
119
0
                    negative_exponent = true;
120
0
                    [[fallthrough]];
121
0
                case '+':
122
0
                    ++i;
123
0
                }
124
0
            }
125
0
            if (i == len) {
126
                // '123e', '123e+', '123e-'
127
0
                *result = StringParser::PARSE_FAILURE;
128
0
                return 0;
129
0
            }
130
0
            for (; i != len; ++i) {
131
0
                const char& c = s[i];
132
0
                if (LIKELY('0' <= c && c <= '9')) {
133
0
                    exponent = exponent * 10 + (c - '0');
134
                    // max string len is config::string_type_length_soft_limit_bytes,
135
                    // whose max value is std::numeric_limits<int32_t>::max() - 4,
136
                    // just check overflow of int32_t to simplify the logic
137
                    // For edge cases like 0.{2147483647 zeros}e+2147483647
138
0
                    if (exponent > std::numeric_limits<int32_t>::max()) {
139
0
                        *result = StringParser::PARSE_OVERFLOW;
140
0
                        return 0;
141
0
                    }
142
0
                } else {
143
                    // '123e12abc', '123e1.2'
144
0
                    *result = StringParser::PARSE_FAILURE;
145
0
                    return 0;
146
0
                }
147
0
            }
148
0
            if (negative_exponent) {
149
0
                exponent = -exponent;
150
0
            }
151
1
        } else {
152
1
            *result = StringParser::PARSE_FAILURE;
153
1
            return 0;
154
1
        }
155
1
    }
156
13.5k
    T int_part_number = 0;
157
13.5k
    T frac_part_number = 0;
158
    // TODO: check limit values of exponent and add UT
159
    // max string len is config::string_type_length_soft_limit_bytes,
160
    // whose max value is std::numeric_limits<int32_t>::max() - 4,
161
    // so int_part_count will be in range of int32_t,
162
    // and int_part_count + exponent will be in range of int64_t
163
13.5k
    int64_t tmp_actual_int_part_count = int_part_count + exponent;
164
13.5k
    if (tmp_actual_int_part_count > std::numeric_limits<int>::max() ||
165
13.5k
        tmp_actual_int_part_count < std::numeric_limits<int>::min()) {
166
0
        *result = StringParser::PARSE_OVERFLOW;
167
0
        return 0;
168
0
    }
169
13.5k
    int actual_int_part_count = tmp_actual_int_part_count;
170
13.5k
    int actual_frac_part_count = 0;
171
13.5k
    int digit_index = 0;
172
13.5k
    if (actual_int_part_count >= 0) {
173
13.5k
        int max_index = std::min(actual_int_part_count, total_digit_count);
174
        // skip zero number
175
13.5k
        for (; digit_index != max_index && digits[digit_index] == 0; ++digit_index) {
176
0
        }
177
        // test 0.00, .00, 0.{00...}e2147483647
178
        // 0.00000e2147483647
179
13.5k
        if (max_index - digit_index > type_precision - type_scale) {
180
8
            *result = is_negative ? StringParser::PARSE_UNDERFLOW : StringParser::PARSE_OVERFLOW;
181
8
            return 0;
182
8
        }
183
        // get int part number
184
149k
        for (; digit_index != max_index; ++digit_index) {
185
136k
            int_part_number = int_part_number * 10 + digits[digit_index];
186
136k
        }
187
13.5k
        if (digit_index != actual_int_part_count) {
188
0
            int_part_number *= get_scale_multiplier<T>(actual_int_part_count - digit_index);
189
0
        }
190
13.5k
    } else {
191
        // leading zeros of fraction part
192
0
        actual_frac_part_count = -actual_int_part_count;
193
0
    }
194
    // get fraction part number
195
131k
    for (; digit_index != total_digit_count && actual_frac_part_count < type_scale;
196
118k
         ++digit_index, ++actual_frac_part_count) {
197
118k
        frac_part_number = frac_part_number * 10 + digits[digit_index];
198
118k
    }
199
13.5k
    auto type_scale_multiplier = get_scale_multiplier<T>(type_scale);
200
    // there are still extra fraction digits left, check rounding
201
13.5k
    if (digit_index != total_digit_count) {
202
        // example: test 1.5 -> decimal(1, 0)
203
17
        if (digits[digit_index] >= 5) {
204
17
            ++frac_part_number;
205
17
            if (frac_part_number == type_scale_multiplier) {
206
0
                frac_part_number = 0;
207
0
                ++int_part_number;
208
0
            }
209
17
        }
210
13.4k
    } else {
211
13.4k
        if (actual_frac_part_count < type_scale) {
212
1.94k
            frac_part_number *= get_scale_multiplier<T>(type_scale - actual_frac_part_count);
213
1.94k
        }
214
13.4k
    }
215
13.5k
    if (int_part_number >= get_scale_multiplier<T>(type_precision - type_scale)) {
216
0
        *result = is_negative ? StringParser::PARSE_UNDERFLOW : StringParser::PARSE_OVERFLOW;
217
0
        return 0;
218
0
    }
219
220
13.5k
    T value = int_part_number * type_scale_multiplier + frac_part_number;
221
13.5k
    *result = StringParser::PARSE_SUCCESS;
222
13.5k
    return is_negative ? T(-value) : T(value);
223
13.5k
}
_ZN5doris12StringParser17string_to_decimalILNS_13PrimitiveTypeE35EEENS_19PrimitiveTypeTraitsIXT_EE7CppType10NativeTypeEPKcmiiPNS0_11ParseResultE
Line
Count
Source
46
115k
        ParseResult* result) {
47
115k
    using T = typename PrimitiveTypeTraits<P>::CppType::NativeType;
48
115k
    static_assert(std::is_same_v<T, int32_t> || std::is_same_v<T, int64_t> ||
49
115k
                          std::is_same_v<T, __int128> || std::is_same_v<T, wide::Int256>,
50
115k
                  "Cast string to decimal only support target type int32_t, int64_t, __int128 or "
51
115k
                  "wide::Int256.");
52
    // Ignore leading and trailing spaces.
53
115k
    s = skip_ascii_whitespaces(s, len);
54
55
115k
    bool is_negative = false;
56
115k
    if (len > 0) {
57
115k
        switch (*s) {
58
17.3k
        case '-':
59
17.3k
            is_negative = true;
60
17.3k
            [[fallthrough]];
61
23.9k
        case '+':
62
23.9k
            ++s;
63
23.9k
            --len;
64
115k
        }
65
115k
    }
66
    // Ignore leading zeros.
67
115k
    bool found_value = false;
68
209k
    while (len > 0 && UNLIKELY(*s == '0')) {
69
93.9k
        found_value = true;
70
93.9k
        ++s;
71
93.9k
        --len;
72
93.9k
    }
73
74
115k
    int found_dot = 0;
75
115k
    if (len > 0 && *s == '.') {
76
15.8k
        found_dot = 1;
77
15.8k
        ++s;
78
15.8k
        --len;
79
15.8k
    }
80
115k
    int int_part_count = 0;
81
115k
    std::vector<unsigned char> digits;
82
115k
    if (len > 0) {
83
113k
        digits.resize(len);
84
113k
    }
85
115k
    int total_digit_count = 0;
86
115k
    int i = 0;
87
3.88M
    for (; i != len; ++i) {
88
3.84M
        const char& c = s[i];
89
3.84M
        if (LIKELY('0' <= c && c <= '9')) {
90
3.67M
            found_value = true;
91
3.67M
            digits[total_digit_count++] = c - '0';
92
3.67M
            if (!found_dot) {
93
988k
                ++int_part_count;
94
988k
            }
95
3.67M
        } else if (c == '.') {
96
93.4k
            if (found_dot) {
97
0
                *result = StringParser::PARSE_FAILURE;
98
0
                return 0;
99
0
            }
100
93.4k
            found_dot = 1;
101
93.4k
        } else {
102
76.0k
            break;
103
76.0k
        }
104
3.84M
    }
105
115k
    if (!found_value) {
106
        // '', '.'
107
66
        *result = StringParser::PARSE_FAILURE;
108
66
        return 0;
109
66
    }
110
    // parse exponent if any
111
115k
    int64_t exponent = 0;
112
115k
    if (i != len) {
113
75.9k
        bool negative_exponent = false;
114
75.9k
        if (s[i] == 'e' || s[i] == 'E') {
115
75.9k
            ++i;
116
75.9k
            if (i != len) {
117
75.9k
                switch (s[i]) {
118
1.53k
                case '-':
119
1.53k
                    negative_exponent = true;
120
1.53k
                    [[fallthrough]];
121
68.1k
                case '+':
122
68.1k
                    ++i;
123
75.9k
                }
124
75.9k
            }
125
75.9k
            if (i == len) {
126
                // '123e', '123e+', '123e-'
127
0
                *result = StringParser::PARSE_FAILURE;
128
0
                return 0;
129
0
            }
130
226k
            for (; i != len; ++i) {
131
150k
                const char& c = s[i];
132
150k
                if (LIKELY('0' <= c && c <= '9')) {
133
150k
                    exponent = exponent * 10 + (c - '0');
134
                    // max string len is config::string_type_length_soft_limit_bytes,
135
                    // whose max value is std::numeric_limits<int32_t>::max() - 4,
136
                    // just check overflow of int32_t to simplify the logic
137
                    // For edge cases like 0.{2147483647 zeros}e+2147483647
138
150k
                    if (exponent > std::numeric_limits<int32_t>::max()) {
139
0
                        *result = StringParser::PARSE_OVERFLOW;
140
0
                        return 0;
141
0
                    }
142
150k
                } else {
143
                    // '123e12abc', '123e1.2'
144
10
                    *result = StringParser::PARSE_FAILURE;
145
10
                    return 0;
146
10
                }
147
150k
            }
148
75.9k
            if (negative_exponent) {
149
1.53k
                exponent = -exponent;
150
1.53k
            }
151
75.9k
        } else {
152
14
            *result = StringParser::PARSE_FAILURE;
153
14
            return 0;
154
14
        }
155
75.9k
    }
156
115k
    T int_part_number = 0;
157
115k
    T frac_part_number = 0;
158
    // TODO: check limit values of exponent and add UT
159
    // max string len is config::string_type_length_soft_limit_bytes,
160
    // whose max value is std::numeric_limits<int32_t>::max() - 4,
161
    // so int_part_count will be in range of int32_t,
162
    // and int_part_count + exponent will be in range of int64_t
163
115k
    int64_t tmp_actual_int_part_count = int_part_count + exponent;
164
115k
    if (tmp_actual_int_part_count > std::numeric_limits<int>::max() ||
165
115k
        tmp_actual_int_part_count < std::numeric_limits<int>::min()) {
166
0
        *result = StringParser::PARSE_OVERFLOW;
167
0
        return 0;
168
0
    }
169
115k
    int actual_int_part_count = tmp_actual_int_part_count;
170
115k
    int actual_frac_part_count = 0;
171
115k
    int digit_index = 0;
172
115k
    if (actual_int_part_count >= 0) {
173
115k
        int max_index = std::min(actual_int_part_count, total_digit_count);
174
        // skip zero number
175
331k
        for (; digit_index != max_index && digits[digit_index] == 0; ++digit_index) {
176
216k
        }
177
        // test 0.00, .00, 0.{00...}e2147483647
178
        // 0.00000e2147483647
179
115k
        if (max_index - digit_index > type_precision - type_scale) {
180
112
            *result = is_negative ? StringParser::PARSE_UNDERFLOW : StringParser::PARSE_OVERFLOW;
181
112
            return 0;
182
112
        }
183
        // get int part number
184
2.09M
        for (; digit_index != max_index; ++digit_index) {
185
1.97M
            int_part_number = int_part_number * 10 + digits[digit_index];
186
1.97M
        }
187
115k
        if (digit_index != actual_int_part_count) {
188
64.6k
            int_part_number *= get_scale_multiplier<T>(actual_int_part_count - digit_index);
189
64.6k
        }
190
115k
    } else {
191
        // leading zeros of fraction part
192
48
        actual_frac_part_count = -actual_int_part_count;
193
48
    }
194
    // get fraction part number
195
1.48M
    for (; digit_index != total_digit_count && actual_frac_part_count < type_scale;
196
1.36M
         ++digit_index, ++actual_frac_part_count) {
197
1.36M
        frac_part_number = frac_part_number * 10 + digits[digit_index];
198
1.36M
    }
199
115k
    auto type_scale_multiplier = get_scale_multiplier<T>(type_scale);
200
    // there are still extra fraction digits left, check rounding
201
115k
    if (digit_index != total_digit_count) {
202
        // example: test 1.5 -> decimal(1, 0)
203
18.8k
        if (digits[digit_index] >= 5) {
204
7.94k
            ++frac_part_number;
205
7.94k
            if (frac_part_number == type_scale_multiplier) {
206
836
                frac_part_number = 0;
207
836
                ++int_part_number;
208
836
            }
209
7.94k
        }
210
96.5k
    } else {
211
96.5k
        if (actual_frac_part_count < type_scale) {
212
87.0k
            frac_part_number *= get_scale_multiplier<T>(type_scale - actual_frac_part_count);
213
87.0k
        }
214
96.5k
    }
215
115k
    if (int_part_number >= get_scale_multiplier<T>(type_precision - type_scale)) {
216
16
        *result = is_negative ? StringParser::PARSE_UNDERFLOW : StringParser::PARSE_OVERFLOW;
217
16
        return 0;
218
16
    }
219
220
115k
    T value = int_part_number * type_scale_multiplier + frac_part_number;
221
115k
    *result = StringParser::PARSE_SUCCESS;
222
115k
    return is_negative ? T(-value) : T(value);
223
115k
}
224
template vectorized::Int32 StringParser::string_to_decimal<PrimitiveType::TYPE_DECIMAL32>(
225
        const char* __restrict s, size_t len, int type_precision, int type_scale,
226
        ParseResult* result);
227
template vectorized::Int64 StringParser::string_to_decimal<PrimitiveType::TYPE_DECIMAL64>(
228
        const char* __restrict s, size_t len, int type_precision, int type_scale,
229
        ParseResult* result);
230
template vectorized::Int128 StringParser::string_to_decimal<PrimitiveType::TYPE_DECIMAL128I>(
231
        const char* __restrict s, size_t len, int type_precision, int type_scale,
232
        ParseResult* result);
233
template vectorized::Int128 StringParser::string_to_decimal<PrimitiveType::TYPE_DECIMALV2>(
234
        const char* __restrict s, size_t len, int type_precision, int type_scale,
235
        ParseResult* result);
236
template wide::Int256 StringParser::string_to_decimal<PrimitiveType::TYPE_DECIMAL256>(
237
        const char* __restrict s, size_t len, int type_precision, int type_scale,
238
        ParseResult* result);
239
} // end namespace doris
240
#include "common/compile_check_avoid_end.h"