/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 | | #include "vec/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 | 351k | ParseResult* result) { |
48 | 351k | using T = typename PrimitiveTypeTraits<P>::CppType::NativeType; |
49 | 351k | static_assert(std::is_same_v<T, int32_t> || std::is_same_v<T, int64_t> || |
50 | 351k | std::is_same_v<T, __int128> || std::is_same_v<T, wide::Int256>, |
51 | 351k | "Cast string to decimal only support target type int32_t, int64_t, __int128 or " |
52 | 351k | "wide::Int256."); |
53 | | // Ignore leading and trailing spaces. |
54 | 351k | s = skip_ascii_whitespaces(s, len); |
55 | | |
56 | 351k | bool is_negative = false; |
57 | 351k | if (len > 0) { |
58 | 351k | switch (*s) { |
59 | 92.3k | case '-': |
60 | 92.3k | is_negative = true; |
61 | 92.3k | [[fallthrough]]; |
62 | 119k | case '+': |
63 | 119k | ++s; |
64 | 119k | --len; |
65 | 351k | } |
66 | 351k | } |
67 | | // Ignore leading zeros. |
68 | 351k | bool found_value = false; |
69 | 688k | while (len > 0 && UNLIKELY(*s == '0')) { |
70 | 337k | found_value = true; |
71 | 337k | ++s; |
72 | 337k | --len; |
73 | 337k | } |
74 | | |
75 | 351k | int found_dot = 0; |
76 | 351k | if (len > 0 && *s == '.') { |
77 | 84.5k | found_dot = 1; |
78 | 84.5k | ++s; |
79 | 84.5k | --len; |
80 | 84.5k | } |
81 | 351k | int int_part_count = 0; |
82 | 351k | int i = 0; |
83 | 8.19M | for (; i != len; ++i) { |
84 | 7.95M | const char& c = s[i]; |
85 | 7.95M | if (LIKELY('0' <= c && c <= '9')) { |
86 | 7.62M | found_value = true; |
87 | 7.62M | if (!found_dot) { |
88 | 2.35M | ++int_part_count; |
89 | 2.35M | } |
90 | 7.62M | } else if (c == '.') { |
91 | 222k | if (found_dot) { |
92 | 2 | *result = StringParser::PARSE_FAILURE; |
93 | 2 | return 0; |
94 | 2 | } |
95 | 222k | found_dot = 1; |
96 | 222k | } else { |
97 | 109k | break; |
98 | 109k | } |
99 | 7.95M | } |
100 | 351k | if (!found_value) { |
101 | | // '', '.' |
102 | 346 | *result = StringParser::PARSE_FAILURE; |
103 | 346 | return 0; |
104 | 346 | } |
105 | | // parse exponent if any |
106 | 351k | int64_t exponent = 0; |
107 | 351k | auto end_digit_index = i; |
108 | 351k | if (i != len) { |
109 | 109k | bool negative_exponent = false; |
110 | 109k | if (s[i] == 'e' || s[i] == 'E') { |
111 | 109k | ++i; |
112 | 109k | if (i != len) { |
113 | 109k | switch (s[i]) { |
114 | 11.5k | case '-': |
115 | 11.5k | negative_exponent = true; |
116 | 11.5k | [[fallthrough]]; |
117 | 78.1k | case '+': |
118 | 78.1k | ++i; |
119 | 109k | } |
120 | 109k | } |
121 | 109k | if (i == len) { |
122 | | // '123e', '123e+', '123e-' |
123 | 6 | *result = StringParser::PARSE_FAILURE; |
124 | 6 | return 0; |
125 | 6 | } |
126 | 319k | for (; i != len; ++i) { |
127 | 209k | const char& c = s[i]; |
128 | 209k | if (LIKELY('0' <= c && c <= '9')) { |
129 | 209k | 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 | 209k | if (exponent > std::numeric_limits<int32_t>::max()) { |
135 | 0 | *result = StringParser::PARSE_OVERFLOW; |
136 | 0 | return 0; |
137 | 0 | } |
138 | 209k | } else { |
139 | | // '123e12abc', '123e1.2' |
140 | 22 | *result = StringParser::PARSE_FAILURE; |
141 | 22 | return 0; |
142 | 22 | } |
143 | 209k | } |
144 | 109k | if (negative_exponent) { |
145 | 11.5k | exponent = -exponent; |
146 | 11.5k | } |
147 | 109k | } else { |
148 | 110 | *result = StringParser::PARSE_FAILURE; |
149 | 110 | return 0; |
150 | 110 | } |
151 | 109k | } |
152 | 350k | T int_part_number = 0; |
153 | 350k | 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 | 350k | int64_t tmp_result_int_part_digit_count = int_part_count + exponent; |
160 | 350k | if (tmp_result_int_part_digit_count > std::numeric_limits<int>::max() || |
161 | 350k | 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 | 350k | int result_int_part_digit_count = tmp_result_int_part_digit_count; |
166 | 350k | int actual_frac_part_count = 0; |
167 | 350k | int digit_index = 0; |
168 | 350k | if (result_int_part_digit_count >= 0) { |
169 | 345k | int max_index = std::min(found_dot ? (result_int_part_digit_count + |
170 | 304k | ((int_part_count > 0 && exponent > 0) ? 1 : 0)) |
171 | 345k | : result_int_part_digit_count, |
172 | 345k | end_digit_index); |
173 | 345k | max_index = (max_index == std::numeric_limits<int>::min() ? end_digit_index : max_index); |
174 | | // skip zero number |
175 | 1.20M | for (; digit_index != max_index && s[digit_index] == '0'; ++digit_index) { |
176 | 855k | } |
177 | | // test 0.00, .00, 0.{00...}e2147483647 |
178 | | // 0.00000e2147483647 |
179 | 345k | if (digit_index != max_index && |
180 | 345k | (result_int_part_digit_count - digit_index > type_precision - type_scale)) { |
181 | 11.7k | *result = is_negative ? StringParser::PARSE_UNDERFLOW : StringParser::PARSE_OVERFLOW; |
182 | 11.7k | return 0; |
183 | 11.7k | } |
184 | | // get int part number |
185 | 3.46M | for (; digit_index != max_index; ++digit_index) { |
186 | 3.13M | if (UNLIKELY(s[digit_index] == '.')) { |
187 | 71.1k | continue; |
188 | 71.1k | } |
189 | 3.06M | int_part_number = int_part_number * 10 + (s[digit_index] - '0'); |
190 | 3.06M | } |
191 | 333k | auto total_significant_digit_count = i - ((found_dot && int_part_count > 0) ? 1 : 0); |
192 | 333k | if (result_int_part_digit_count > total_significant_digit_count) { |
193 | 2.42k | int_part_number *= get_scale_multiplier<T>(result_int_part_digit_count - |
194 | 2.42k | total_significant_digit_count); |
195 | 2.42k | } |
196 | 333k | } else { |
197 | | // leading zeros of fraction part |
198 | 5.58k | actual_frac_part_count = -result_int_part_digit_count; |
199 | 5.58k | } |
200 | | // get fraction part number |
201 | 3.62M | for (; digit_index != end_digit_index && actual_frac_part_count < type_scale; ++digit_index) { |
202 | 3.28M | if (UNLIKELY(s[digit_index] == '.')) { |
203 | 131k | continue; |
204 | 131k | } |
205 | 3.15M | frac_part_number = frac_part_number * 10 + (s[digit_index] - '0'); |
206 | 3.15M | ++actual_frac_part_count; |
207 | 3.15M | } |
208 | 339k | auto type_scale_multiplier = get_scale_multiplier<T>(type_scale); |
209 | | // there are still extra fraction digits left, check rounding |
210 | 339k | if (digit_index != end_digit_index) { |
211 | 82.3k | if (UNLIKELY(s[digit_index] == '.')) { |
212 | 3.46k | ++digit_index; |
213 | 3.46k | } |
214 | 82.3k | if (digit_index != end_digit_index) { |
215 | | // example: test 1.5 -> decimal(1, 0) |
216 | 81.3k | if (s[digit_index] >= '5') { |
217 | 32.9k | ++frac_part_number; |
218 | 32.9k | if (frac_part_number == type_scale_multiplier) { |
219 | 3.43k | frac_part_number = 0; |
220 | 3.43k | ++int_part_number; |
221 | 3.43k | } |
222 | 32.9k | } |
223 | 81.3k | } |
224 | 256k | } else { |
225 | 256k | if (actual_frac_part_count < type_scale) { |
226 | 192k | frac_part_number *= get_scale_multiplier<T>(type_scale - actual_frac_part_count); |
227 | 192k | } |
228 | 256k | } |
229 | 339k | if (int_part_number >= get_scale_multiplier<T>(type_precision - type_scale)) { |
230 | 72 | *result = is_negative ? StringParser::PARSE_UNDERFLOW : StringParser::PARSE_OVERFLOW; |
231 | 72 | return 0; |
232 | 72 | } |
233 | | |
234 | 339k | T value = int_part_number * type_scale_multiplier + frac_part_number; |
235 | 339k | *result = StringParser::PARSE_SUCCESS; |
236 | 339k | return is_negative ? T(-value) : T(value); |
237 | 339k | } _ZN5doris12StringParser17string_to_decimalILNS_13PrimitiveTypeE28EEENS_19PrimitiveTypeTraitsIXT_EE7CppType10NativeTypeEPKcmiiPNS0_11ParseResultE Line | Count | Source | 47 | 54.1k | ParseResult* result) { | 48 | 54.1k | using T = typename PrimitiveTypeTraits<P>::CppType::NativeType; | 49 | 54.1k | static_assert(std::is_same_v<T, int32_t> || std::is_same_v<T, int64_t> || | 50 | 54.1k | std::is_same_v<T, __int128> || std::is_same_v<T, wide::Int256>, | 51 | 54.1k | "Cast string to decimal only support target type int32_t, int64_t, __int128 or " | 52 | 54.1k | "wide::Int256."); | 53 | | // Ignore leading and trailing spaces. | 54 | 54.1k | s = skip_ascii_whitespaces(s, len); | 55 | | | 56 | 54.1k | bool is_negative = false; | 57 | 54.1k | if (len > 0) { | 58 | 54.1k | switch (*s) { | 59 | 24.9k | case '-': | 60 | 24.9k | is_negative = true; | 61 | 24.9k | [[fallthrough]]; | 62 | 32.2k | case '+': | 63 | 32.2k | ++s; | 64 | 32.2k | --len; | 65 | 54.1k | } | 66 | 54.1k | } | 67 | | // Ignore leading zeros. | 68 | 54.1k | bool found_value = false; | 69 | 107k | while (len > 0 && UNLIKELY(*s == '0')) { | 70 | 53.6k | found_value = true; | 71 | 53.6k | ++s; | 72 | 53.6k | --len; | 73 | 53.6k | } | 74 | | | 75 | 54.1k | int found_dot = 0; | 76 | 54.1k | if (len > 0 && *s == '.') { | 77 | 17.8k | found_dot = 1; | 78 | 17.8k | ++s; | 79 | 17.8k | --len; | 80 | 17.8k | } | 81 | 54.1k | int int_part_count = 0; | 82 | 54.1k | int i = 0; | 83 | 577k | for (; i != len; ++i) { | 84 | 532k | const char& c = s[i]; | 85 | 532k | if (LIKELY('0' <= c && c <= '9')) { | 86 | 508k | found_value = true; | 87 | 508k | if (!found_dot) { | 88 | 158k | ++int_part_count; | 89 | 158k | } | 90 | 508k | } else if (c == '.') { | 91 | 14.5k | if (found_dot) { | 92 | 2 | *result = StringParser::PARSE_FAILURE; | 93 | 2 | return 0; | 94 | 2 | } | 95 | 14.5k | found_dot = 1; | 96 | 14.5k | } else { | 97 | 9.52k | break; | 98 | 9.52k | } | 99 | 532k | } | 100 | 54.1k | if (!found_value) { | 101 | | // '', '.' | 102 | 146 | *result = StringParser::PARSE_FAILURE; | 103 | 146 | return 0; | 104 | 146 | } | 105 | | // parse exponent if any | 106 | 54.0k | int64_t exponent = 0; | 107 | 54.0k | auto end_digit_index = i; | 108 | 54.0k | if (i != len) { | 109 | 9.39k | bool negative_exponent = false; | 110 | 9.39k | 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 | 60 | *result = StringParser::PARSE_FAILURE; | 149 | 60 | return 0; | 150 | 60 | } | 151 | 9.39k | } | 152 | 53.9k | T int_part_number = 0; | 153 | 53.9k | 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 | 53.9k | int64_t tmp_result_int_part_digit_count = int_part_count + exponent; | 160 | 53.9k | if (tmp_result_int_part_digit_count > std::numeric_limits<int>::max() || | 161 | 53.9k | 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 | 53.9k | int result_int_part_digit_count = tmp_result_int_part_digit_count; | 166 | 53.9k | int actual_frac_part_count = 0; | 167 | 53.9k | int digit_index = 0; | 168 | 53.9k | if (result_int_part_digit_count >= 0) { | 169 | 53.8k | int max_index = std::min(found_dot ? (result_int_part_digit_count + | 170 | 32.3k | ((int_part_count > 0 && exponent > 0) ? 1 : 0)) | 171 | 53.8k | : result_int_part_digit_count, | 172 | 53.8k | end_digit_index); | 173 | 53.8k | max_index = (max_index == std::numeric_limits<int>::min() ? end_digit_index : max_index); | 174 | | // skip zero number | 175 | 266k | 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 | 53.8k | if (digit_index != max_index && | 180 | 53.8k | (result_int_part_digit_count - digit_index > type_precision - type_scale)) { | 181 | 1.30k | *result = is_negative ? StringParser::PARSE_UNDERFLOW : StringParser::PARSE_OVERFLOW; | 182 | 1.30k | return 0; | 183 | 1.30k | } | 184 | | // get int part number | 185 | 154k | for (; digit_index != max_index; ++digit_index) { | 186 | 101k | if (UNLIKELY(s[digit_index] == '.')) { | 187 | 1.60k | continue; | 188 | 1.60k | } | 189 | 100k | int_part_number = int_part_number * 10 + (s[digit_index] - '0'); | 190 | 100k | } | 191 | 52.5k | auto total_significant_digit_count = i - ((found_dot && int_part_count > 0) ? 1 : 0); | 192 | 52.5k | 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 | 52.5k | } else { | 197 | | // leading zeros of fraction part | 198 | 48 | actual_frac_part_count = -result_int_part_digit_count; | 199 | 48 | } | 200 | | // get fraction part number | 201 | 168k | for (; digit_index != end_digit_index && actual_frac_part_count < type_scale; ++digit_index) { | 202 | 115k | if (UNLIKELY(s[digit_index] == '.')) { | 203 | 9.93k | continue; | 204 | 9.93k | } | 205 | 106k | frac_part_number = frac_part_number * 10 + (s[digit_index] - '0'); | 206 | 106k | ++actual_frac_part_count; | 207 | 106k | } | 208 | 52.6k | auto type_scale_multiplier = get_scale_multiplier<T>(type_scale); | 209 | | // there are still extra fraction digits left, check rounding | 210 | 52.6k | if (digit_index != end_digit_index) { | 211 | 21.4k | if (UNLIKELY(s[digit_index] == '.')) { | 212 | 904 | ++digit_index; | 213 | 904 | } | 214 | 21.4k | if (digit_index != end_digit_index) { | 215 | | // example: test 1.5 -> decimal(1, 0) | 216 | 21.1k | if (s[digit_index] >= '5') { | 217 | 8.96k | ++frac_part_number; | 218 | 8.96k | if (frac_part_number == type_scale_multiplier) { | 219 | 856 | frac_part_number = 0; | 220 | 856 | ++int_part_number; | 221 | 856 | } | 222 | 8.96k | } | 223 | 21.1k | } | 224 | 31.1k | } else { | 225 | 31.1k | if (actual_frac_part_count < type_scale) { | 226 | 27.7k | frac_part_number *= get_scale_multiplier<T>(type_scale - actual_frac_part_count); | 227 | 27.7k | } | 228 | 31.1k | } | 229 | 52.6k | 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 | 52.6k | T value = int_part_number * type_scale_multiplier + frac_part_number; | 235 | 52.6k | *result = StringParser::PARSE_SUCCESS; | 236 | 52.6k | return is_negative ? T(-value) : T(value); | 237 | 52.6k | } |
_ZN5doris12StringParser17string_to_decimalILNS_13PrimitiveTypeE29EEENS_19PrimitiveTypeTraitsIXT_EE7CppType10NativeTypeEPKcmiiPNS0_11ParseResultE Line | Count | Source | 47 | 85.7k | ParseResult* result) { | 48 | 85.7k | using T = typename PrimitiveTypeTraits<P>::CppType::NativeType; | 49 | 85.7k | static_assert(std::is_same_v<T, int32_t> || std::is_same_v<T, int64_t> || | 50 | 85.7k | std::is_same_v<T, __int128> || std::is_same_v<T, wide::Int256>, | 51 | 85.7k | "Cast string to decimal only support target type int32_t, int64_t, __int128 or " | 52 | 85.7k | "wide::Int256."); | 53 | | // Ignore leading and trailing spaces. | 54 | 85.7k | s = skip_ascii_whitespaces(s, len); | 55 | | | 56 | 85.7k | bool is_negative = false; | 57 | 85.7k | if (len > 0) { | 58 | 85.7k | switch (*s) { | 59 | 21.7k | case '-': | 60 | 21.7k | is_negative = true; | 61 | 21.7k | [[fallthrough]]; | 62 | 28.3k | case '+': | 63 | 28.3k | ++s; | 64 | 28.3k | --len; | 65 | 85.7k | } | 66 | 85.7k | } | 67 | | // Ignore leading zeros. | 68 | 85.7k | bool found_value = false; | 69 | 161k | while (len > 0 && UNLIKELY(*s == '0')) { | 70 | 76.0k | found_value = true; | 71 | 76.0k | ++s; | 72 | 76.0k | --len; | 73 | 76.0k | } | 74 | | | 75 | 85.7k | int found_dot = 0; | 76 | 85.7k | if (len > 0 && *s == '.') { | 77 | 23.8k | found_dot = 1; | 78 | 23.8k | ++s; | 79 | 23.8k | --len; | 80 | 23.8k | } | 81 | 85.7k | int int_part_count = 0; | 82 | 85.7k | int i = 0; | 83 | 1.40M | for (; i != len; ++i) { | 84 | 1.32M | const char& c = s[i]; | 85 | 1.32M | if (LIKELY('0' <= c && c <= '9')) { | 86 | 1.26M | found_value = true; | 87 | 1.26M | if (!found_dot) { | 88 | 517k | ++int_part_count; | 89 | 517k | } | 90 | 1.26M | } else if (c == '.') { | 91 | 53.2k | if (found_dot) { | 92 | 0 | *result = StringParser::PARSE_FAILURE; | 93 | 0 | return 0; | 94 | 0 | } | 95 | 53.2k | found_dot = 1; | 96 | 53.2k | } else { | 97 | 11.6k | break; | 98 | 11.6k | } | 99 | 1.32M | } | 100 | 85.7k | if (!found_value) { | 101 | | // '', '.' | 102 | 69 | *result = StringParser::PARSE_FAILURE; | 103 | 69 | return 0; | 104 | 69 | } | 105 | | // parse exponent if any | 106 | 85.6k | int64_t exponent = 0; | 107 | 85.6k | auto end_digit_index = i; | 108 | 85.6k | if (i != len) { | 109 | 11.6k | bool negative_exponent = false; | 110 | 11.6k | if (s[i] == 'e' || s[i] == 'E') { | 111 | 11.6k | ++i; | 112 | 11.6k | if (i != len) { | 113 | 11.6k | switch (s[i]) { | 114 | 3.84k | case '-': | 115 | 3.84k | negative_exponent = true; | 116 | 3.84k | [[fallthrough]]; | 117 | 3.84k | case '+': | 118 | 3.84k | ++i; | 119 | 11.6k | } | 120 | 11.6k | } | 121 | 11.6k | if (i == len) { | 122 | | // '123e', '123e+', '123e-' | 123 | 0 | *result = StringParser::PARSE_FAILURE; | 124 | 0 | return 0; | 125 | 0 | } | 126 | 32.3k | for (; i != len; ++i) { | 127 | 20.7k | const char& c = s[i]; | 128 | 20.7k | if (LIKELY('0' <= c && c <= '9')) { | 129 | 20.7k | 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 | 20.7k | if (exponent > std::numeric_limits<int32_t>::max()) { | 135 | 0 | *result = StringParser::PARSE_OVERFLOW; | 136 | 0 | return 0; | 137 | 0 | } | 138 | 20.7k | } else { | 139 | | // '123e12abc', '123e1.2' | 140 | 0 | *result = StringParser::PARSE_FAILURE; | 141 | 0 | return 0; | 142 | 0 | } | 143 | 20.7k | } | 144 | 11.6k | if (negative_exponent) { | 145 | 3.84k | exponent = -exponent; | 146 | 3.84k | } | 147 | 11.6k | } else { | 148 | 23 | *result = StringParser::PARSE_FAILURE; | 149 | 23 | return 0; | 150 | 23 | } | 151 | 11.6k | } | 152 | 85.6k | T int_part_number = 0; | 153 | 85.6k | 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 | 85.6k | int64_t tmp_result_int_part_digit_count = int_part_count + exponent; | 160 | 85.6k | if (tmp_result_int_part_digit_count > std::numeric_limits<int>::max() || | 161 | 85.6k | 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 | 85.6k | int result_int_part_digit_count = tmp_result_int_part_digit_count; | 166 | 85.6k | int actual_frac_part_count = 0; | 167 | 85.6k | int digit_index = 0; | 168 | 85.6k | if (result_int_part_digit_count >= 0) { | 169 | 83.2k | int max_index = std::min(found_dot ? (result_int_part_digit_count + | 170 | 76.2k | ((int_part_count > 0 && exponent > 0) ? 1 : 0)) | 171 | 83.2k | : result_int_part_digit_count, | 172 | 83.2k | end_digit_index); | 173 | 83.2k | max_index = (max_index == std::numeric_limits<int>::min() ? end_digit_index : max_index); | 174 | | // skip zero number | 175 | 295k | 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 | 83.2k | if (digit_index != max_index && | 180 | 83.2k | (result_int_part_digit_count - digit_index > type_precision - type_scale)) { | 181 | 10.1k | *result = is_negative ? StringParser::PARSE_UNDERFLOW : StringParser::PARSE_OVERFLOW; | 182 | 10.1k | return 0; | 183 | 10.1k | } | 184 | | // get int part number | 185 | 408k | for (; digit_index != max_index; ++digit_index) { | 186 | 335k | if (UNLIKELY(s[digit_index] == '.')) { | 187 | 960 | continue; | 188 | 960 | } | 189 | 334k | int_part_number = int_part_number * 10 + (s[digit_index] - '0'); | 190 | 334k | } | 191 | 73.1k | auto total_significant_digit_count = i - ((found_dot && int_part_count > 0) ? 1 : 0); | 192 | 73.1k | 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 | 73.1k | } else { | 197 | | // leading zeros of fraction part | 198 | 2.35k | actual_frac_part_count = -result_int_part_digit_count; | 199 | 2.35k | } | 200 | | // get fraction part number | 201 | 589k | for (; digit_index != end_digit_index && actual_frac_part_count < type_scale; ++digit_index) { | 202 | 514k | if (UNLIKELY(s[digit_index] == '.')) { | 203 | 40.3k | continue; | 204 | 40.3k | } | 205 | 473k | frac_part_number = frac_part_number * 10 + (s[digit_index] - '0'); | 206 | 473k | ++actual_frac_part_count; | 207 | 473k | } | 208 | 75.4k | auto type_scale_multiplier = get_scale_multiplier<T>(type_scale); | 209 | | // there are still extra fraction digits left, check rounding | 210 | 75.4k | if (digit_index != end_digit_index) { | 211 | 20.0k | if (UNLIKELY(s[digit_index] == '.')) { | 212 | 852 | ++digit_index; | 213 | 852 | } | 214 | 20.0k | if (digit_index != end_digit_index) { | 215 | | // example: test 1.5 -> decimal(1, 0) | 216 | 19.8k | if (s[digit_index] >= '5') { | 217 | 7.95k | ++frac_part_number; | 218 | 7.95k | if (frac_part_number == type_scale_multiplier) { | 219 | 836 | frac_part_number = 0; | 220 | 836 | ++int_part_number; | 221 | 836 | } | 222 | 7.95k | } | 223 | 19.8k | } | 224 | 55.4k | } else { | 225 | 55.4k | if (actual_frac_part_count < type_scale) { | 226 | 31.4k | frac_part_number *= get_scale_multiplier<T>(type_scale - actual_frac_part_count); | 227 | 31.4k | } | 228 | 55.4k | } | 229 | 75.4k | 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 | 75.4k | T value = int_part_number * type_scale_multiplier + frac_part_number; | 235 | 75.4k | *result = StringParser::PARSE_SUCCESS; | 236 | 75.4k | return is_negative ? T(-value) : T(value); | 237 | 75.4k | } |
_ZN5doris12StringParser17string_to_decimalILNS_13PrimitiveTypeE30EEENS_19PrimitiveTypeTraitsIXT_EE7CppType10NativeTypeEPKcmiiPNS0_11ParseResultE Line | Count | Source | 47 | 82.3k | ParseResult* result) { | 48 | 82.3k | using T = typename PrimitiveTypeTraits<P>::CppType::NativeType; | 49 | 82.3k | static_assert(std::is_same_v<T, int32_t> || std::is_same_v<T, int64_t> || | 50 | 82.3k | std::is_same_v<T, __int128> || std::is_same_v<T, wide::Int256>, | 51 | 82.3k | "Cast string to decimal only support target type int32_t, int64_t, __int128 or " | 52 | 82.3k | "wide::Int256."); | 53 | | // Ignore leading and trailing spaces. | 54 | 82.3k | s = skip_ascii_whitespaces(s, len); | 55 | | | 56 | 82.3k | bool is_negative = false; | 57 | 82.3k | if (len > 0) { | 58 | 82.3k | switch (*s) { | 59 | 21.7k | case '-': | 60 | 21.7k | is_negative = true; | 61 | 21.7k | [[fallthrough]]; | 62 | 28.3k | case '+': | 63 | 28.3k | ++s; | 64 | 28.3k | --len; | 65 | 82.3k | } | 66 | 82.3k | } | 67 | | // Ignore leading zeros. | 68 | 82.3k | bool found_value = false; | 69 | 157k | while (len > 0 && UNLIKELY(*s == '0')) { | 70 | 74.6k | found_value = true; | 71 | 74.6k | ++s; | 72 | 74.6k | --len; | 73 | 74.6k | } | 74 | | | 75 | 82.3k | int found_dot = 0; | 76 | 82.3k | if (len > 0 && *s == '.') { | 77 | 24.9k | found_dot = 1; | 78 | 24.9k | ++s; | 79 | 24.9k | --len; | 80 | 24.9k | } | 81 | 82.3k | int int_part_count = 0; | 82 | 82.3k | int i = 0; | 83 | 2.05M | for (; i != len; ++i) { | 84 | 1.98M | const char& c = s[i]; | 85 | 1.98M | if (LIKELY('0' <= c && c <= '9')) { | 86 | 1.92M | found_value = true; | 87 | 1.92M | if (!found_dot) { | 88 | 558k | ++int_part_count; | 89 | 558k | } | 90 | 1.92M | } else if (c == '.') { | 91 | 49.5k | if (found_dot) { | 92 | 0 | *result = StringParser::PARSE_FAILURE; | 93 | 0 | return 0; | 94 | 0 | } | 95 | 49.5k | found_dot = 1; | 96 | 49.5k | } else { | 97 | 12.4k | break; | 98 | 12.4k | } | 99 | 1.98M | } | 100 | 82.3k | if (!found_value) { | 101 | | // '', '.' | 102 | 55 | *result = StringParser::PARSE_FAILURE; | 103 | 55 | return 0; | 104 | 55 | } | 105 | | // parse exponent if any | 106 | 82.2k | int64_t exponent = 0; | 107 | 82.2k | auto end_digit_index = i; | 108 | 82.2k | 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 | 12 | *result = StringParser::PARSE_FAILURE; | 149 | 12 | return 0; | 150 | 12 | } | 151 | 12.3k | } | 152 | 82.2k | T int_part_number = 0; | 153 | 82.2k | 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 | 82.2k | int64_t tmp_result_int_part_digit_count = int_part_count + exponent; | 160 | 82.2k | if (tmp_result_int_part_digit_count > std::numeric_limits<int>::max() || | 161 | 82.2k | 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 | 82.2k | int result_int_part_digit_count = tmp_result_int_part_digit_count; | 166 | 82.2k | int actual_frac_part_count = 0; | 167 | 82.2k | int digit_index = 0; | 168 | 82.2k | if (result_int_part_digit_count >= 0) { | 169 | 79.1k | int max_index = std::min(found_dot ? (result_int_part_digit_count + | 170 | 72.8k | ((int_part_count > 0 && exponent > 0) ? 1 : 0)) | 171 | 79.1k | : result_int_part_digit_count, | 172 | 79.1k | end_digit_index); | 173 | 79.1k | max_index = (max_index == std::numeric_limits<int>::min() ? end_digit_index : max_index); | 174 | | // skip zero number | 175 | 293k | 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 | 79.1k | if (digit_index != max_index && | 180 | 79.1k | (result_int_part_digit_count - digit_index > type_precision - type_scale)) { | 181 | 140 | *result = is_negative ? StringParser::PARSE_UNDERFLOW : StringParser::PARSE_OVERFLOW; | 182 | 140 | return 0; | 183 | 140 | } | 184 | | // get int part number | 185 | 596k | for (; digit_index != max_index; ++digit_index) { | 186 | 518k | if (UNLIKELY(s[digit_index] == '.')) { | 187 | 960 | continue; | 188 | 960 | } | 189 | 517k | int_part_number = int_part_number * 10 + (s[digit_index] - '0'); | 190 | 517k | } | 191 | 78.9k | auto total_significant_digit_count = i - ((found_dot && int_part_count > 0) ? 1 : 0); | 192 | 78.9k | 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 | 78.9k | } else { | 197 | | // leading zeros of fraction part | 198 | 3.12k | actual_frac_part_count = -result_int_part_digit_count; | 199 | 3.12k | } | 200 | | // get fraction part number | 201 | 1.21M | for (; digit_index != end_digit_index && actual_frac_part_count < type_scale; ++digit_index) { | 202 | 1.13M | if (UNLIKELY(s[digit_index] == '.')) { | 203 | 45.9k | continue; | 204 | 45.9k | } | 205 | 1.08M | frac_part_number = frac_part_number * 10 + (s[digit_index] - '0'); | 206 | 1.08M | ++actual_frac_part_count; | 207 | 1.08M | } | 208 | 82.1k | auto type_scale_multiplier = get_scale_multiplier<T>(type_scale); | 209 | | // there are still extra fraction digits left, check rounding | 210 | 82.1k | 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.03k | ++frac_part_number; | 218 | 8.03k | if (frac_part_number == type_scale_multiplier) { | 219 | 906 | frac_part_number = 0; | 220 | 906 | ++int_part_number; | 221 | 906 | } | 222 | 8.03k | } | 223 | 21.4k | } | 224 | 60.4k | } else { | 225 | 60.4k | if (actual_frac_part_count < type_scale) { | 226 | 44.5k | frac_part_number *= get_scale_multiplier<T>(type_scale - actual_frac_part_count); | 227 | 44.5k | } | 228 | 60.4k | } | 229 | 82.1k | 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 | 82.1k | T value = int_part_number * type_scale_multiplier + frac_part_number; | 235 | 82.1k | *result = StringParser::PARSE_SUCCESS; | 236 | 82.1k | return is_negative ? T(-value) : T(value); | 237 | 82.1k | } |
_ZN5doris12StringParser17string_to_decimalILNS_13PrimitiveTypeE20EEENS_19PrimitiveTypeTraitsIXT_EE7CppType10NativeTypeEPKcmiiPNS0_11ParseResultE Line | Count | Source | 47 | 13.5k | ParseResult* result) { | 48 | 13.5k | using T = typename PrimitiveTypeTraits<P>::CppType::NativeType; | 49 | 13.5k | static_assert(std::is_same_v<T, int32_t> || std::is_same_v<T, int64_t> || | 50 | 13.5k | std::is_same_v<T, __int128> || std::is_same_v<T, wide::Int256>, | 51 | 13.5k | "Cast string to decimal only support target type int32_t, int64_t, __int128 or " | 52 | 13.5k | "wide::Int256."); | 53 | | // Ignore leading and trailing spaces. | 54 | 13.5k | s = skip_ascii_whitespaces(s, len); | 55 | | | 56 | 13.5k | bool is_negative = false; | 57 | 13.5k | if (len > 0) { | 58 | 13.5k | switch (*s) { | 59 | 6.68k | case '-': | 60 | 6.68k | is_negative = true; | 61 | 6.68k | [[fallthrough]]; | 62 | 6.68k | case '+': | 63 | 6.68k | ++s; | 64 | 6.68k | --len; | 65 | 13.5k | } | 66 | 13.5k | } | 67 | | // Ignore leading zeros. | 68 | 13.5k | bool found_value = false; | 69 | 52.3k | while (len > 0 && UNLIKELY(*s == '0')) { | 70 | 38.8k | found_value = true; | 71 | 38.8k | ++s; | 72 | 38.8k | --len; | 73 | 38.8k | } | 74 | | | 75 | 13.5k | int found_dot = 0; | 76 | 13.5k | if (len > 0 && *s == '.') { | 77 | 2.00k | found_dot = 1; | 78 | 2.00k | ++s; | 79 | 2.00k | --len; | 80 | 2.00k | } | 81 | 13.5k | int int_part_count = 0; | 82 | 13.5k | int i = 0; | 83 | 279k | for (; i != len; ++i) { | 84 | 266k | const char& c = s[i]; | 85 | 266k | if (LIKELY('0' <= c && c <= '9')) { | 86 | 254k | found_value = true; | 87 | 254k | if (!found_dot) { | 88 | 136k | ++int_part_count; | 89 | 136k | } | 90 | 254k | } else if (c == '.') { | 91 | 11.4k | if (found_dot) { | 92 | 0 | *result = StringParser::PARSE_FAILURE; | 93 | 0 | return 0; | 94 | 0 | } | 95 | 11.4k | found_dot = 1; | 96 | 11.4k | } else { | 97 | 11 | break; | 98 | 11 | } | 99 | 266k | } | 100 | 13.5k | if (!found_value) { | 101 | | // '', '.' | 102 | 10 | *result = StringParser::PARSE_FAILURE; | 103 | 10 | return 0; | 104 | 10 | } | 105 | | // parse exponent if any | 106 | 13.5k | int64_t exponent = 0; | 107 | 13.5k | auto end_digit_index = i; | 108 | 13.5k | 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.5k | T int_part_number = 0; | 153 | 13.5k | 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.5k | int64_t tmp_result_int_part_digit_count = int_part_count + exponent; | 160 | 13.5k | if (tmp_result_int_part_digit_count > std::numeric_limits<int>::max() || | 161 | 13.5k | 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.5k | int result_int_part_digit_count = tmp_result_int_part_digit_count; | 166 | 13.5k | int actual_frac_part_count = 0; | 167 | 13.5k | int digit_index = 0; | 168 | 13.5k | if (result_int_part_digit_count >= 0) { | 169 | 13.5k | int max_index = std::min(found_dot ? (result_int_part_digit_count + | 170 | 13.4k | ((int_part_count > 0 && exponent > 0) ? 1 : 0)) | 171 | 13.5k | : result_int_part_digit_count, | 172 | 13.5k | end_digit_index); | 173 | 13.5k | max_index = (max_index == std::numeric_limits<int>::min() ? end_digit_index : max_index); | 174 | | // skip zero number | 175 | 13.5k | 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.5k | if (digit_index != max_index && | 180 | 13.5k | (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 | 149k | for (; digit_index != max_index; ++digit_index) { | 186 | 136k | if (UNLIKELY(s[digit_index] == '.')) { | 187 | 0 | continue; | 188 | 0 | } | 189 | 136k | int_part_number = int_part_number * 10 + (s[digit_index] - '0'); | 190 | 136k | } | 191 | 13.5k | auto total_significant_digit_count = i - ((found_dot && int_part_count > 0) ? 1 : 0); | 192 | 13.5k | 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.5k | } 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 | 143k | for (; digit_index != end_digit_index && actual_frac_part_count < type_scale; ++digit_index) { | 202 | 129k | if (UNLIKELY(s[digit_index] == '.')) { | 203 | 11.4k | continue; | 204 | 11.4k | } | 205 | 118k | frac_part_number = frac_part_number * 10 + (s[digit_index] - '0'); | 206 | 118k | ++actual_frac_part_count; | 207 | 118k | } | 208 | 13.5k | auto type_scale_multiplier = get_scale_multiplier<T>(type_scale); | 209 | | // there are still extra fraction digits left, check rounding | 210 | 13.5k | 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.4k | } else { | 225 | 13.4k | if (actual_frac_part_count < type_scale) { | 226 | 1.94k | frac_part_number *= get_scale_multiplier<T>(type_scale - actual_frac_part_count); | 227 | 1.94k | } | 228 | 13.4k | } | 229 | 13.5k | 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.5k | T value = int_part_number * type_scale_multiplier + frac_part_number; | 235 | 13.5k | *result = StringParser::PARSE_SUCCESS; | 236 | 13.5k | return is_negative ? T(-value) : T(value); | 237 | 13.5k | } |
_ZN5doris12StringParser17string_to_decimalILNS_13PrimitiveTypeE35EEENS_19PrimitiveTypeTraitsIXT_EE7CppType10NativeTypeEPKcmiiPNS0_11ParseResultE Line | Count | Source | 47 | 115k | ParseResult* result) { | 48 | 115k | using T = typename PrimitiveTypeTraits<P>::CppType::NativeType; | 49 | 115k | static_assert(std::is_same_v<T, int32_t> || std::is_same_v<T, int64_t> || | 50 | 115k | std::is_same_v<T, __int128> || std::is_same_v<T, wide::Int256>, | 51 | 115k | "Cast string to decimal only support target type int32_t, int64_t, __int128 or " | 52 | 115k | "wide::Int256."); | 53 | | // Ignore leading and trailing spaces. | 54 | 115k | s = skip_ascii_whitespaces(s, len); | 55 | | | 56 | 115k | bool is_negative = false; | 57 | 115k | if (len > 0) { | 58 | 115k | switch (*s) { | 59 | 17.3k | case '-': | 60 | 17.3k | is_negative = true; | 61 | 17.3k | [[fallthrough]]; | 62 | 23.9k | case '+': | 63 | 23.9k | ++s; | 64 | 23.9k | --len; | 65 | 115k | } | 66 | 115k | } | 67 | | // Ignore leading zeros. | 68 | 115k | bool found_value = false; | 69 | 209k | while (len > 0 && UNLIKELY(*s == '0')) { | 70 | 93.9k | found_value = true; | 71 | 93.9k | ++s; | 72 | 93.9k | --len; | 73 | 93.9k | } | 74 | | | 75 | 115k | int found_dot = 0; | 76 | 115k | if (len > 0 && *s == '.') { | 77 | 15.8k | found_dot = 1; | 78 | 15.8k | ++s; | 79 | 15.8k | --len; | 80 | 15.8k | } | 81 | 115k | int int_part_count = 0; | 82 | 115k | int i = 0; | 83 | 3.88M | for (; i != len; ++i) { | 84 | 3.84M | const char& c = s[i]; | 85 | 3.84M | if (LIKELY('0' <= c && c <= '9')) { | 86 | 3.67M | found_value = true; | 87 | 3.67M | if (!found_dot) { | 88 | 982k | ++int_part_count; | 89 | 982k | } | 90 | 3.67M | } else if (c == '.') { | 91 | 93.4k | if (found_dot) { | 92 | 0 | *result = StringParser::PARSE_FAILURE; | 93 | 0 | return 0; | 94 | 0 | } | 95 | 93.4k | found_dot = 1; | 96 | 93.4k | } else { | 97 | 76.0k | break; | 98 | 76.0k | } | 99 | 3.84M | } | 100 | 115k | if (!found_value) { | 101 | | // '', '.' | 102 | 66 | *result = StringParser::PARSE_FAILURE; | 103 | 66 | return 0; | 104 | 66 | } | 105 | | // parse exponent if any | 106 | 115k | int64_t exponent = 0; | 107 | 115k | auto end_digit_index = i; | 108 | 115k | if (i != len) { | 109 | 75.9k | bool negative_exponent = false; | 110 | 75.9k | if (s[i] == 'e' || s[i] == 'E') { | 111 | 75.9k | ++i; | 112 | 75.9k | if (i != len) { | 113 | 75.9k | switch (s[i]) { | 114 | 1.53k | case '-': | 115 | 1.53k | negative_exponent = true; | 116 | 1.53k | [[fallthrough]]; | 117 | 68.1k | case '+': | 118 | 68.1k | ++i; | 119 | 75.9k | } | 120 | 75.9k | } | 121 | 75.9k | if (i == len) { | 122 | | // '123e', '123e+', '123e-' | 123 | 0 | *result = StringParser::PARSE_FAILURE; | 124 | 0 | return 0; | 125 | 0 | } | 126 | 226k | for (; i != len; ++i) { | 127 | 150k | const char& c = s[i]; | 128 | 150k | if (LIKELY('0' <= c && c <= '9')) { | 129 | 150k | 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 | 150k | if (exponent > std::numeric_limits<int32_t>::max()) { | 135 | 0 | *result = StringParser::PARSE_OVERFLOW; | 136 | 0 | return 0; | 137 | 0 | } | 138 | 150k | } else { | 139 | | // '123e12abc', '123e1.2' | 140 | 10 | *result = StringParser::PARSE_FAILURE; | 141 | 10 | return 0; | 142 | 10 | } | 143 | 150k | } | 144 | 75.9k | if (negative_exponent) { | 145 | 1.53k | exponent = -exponent; | 146 | 1.53k | } | 147 | 75.9k | } else { | 148 | 14 | *result = StringParser::PARSE_FAILURE; | 149 | 14 | return 0; | 150 | 14 | } | 151 | 75.9k | } | 152 | 115k | T int_part_number = 0; | 153 | 115k | 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 | 115k | int64_t tmp_result_int_part_digit_count = int_part_count + exponent; | 160 | 115k | if (tmp_result_int_part_digit_count > std::numeric_limits<int>::max() || | 161 | 115k | 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 | 115k | int result_int_part_digit_count = tmp_result_int_part_digit_count; | 166 | 115k | int actual_frac_part_count = 0; | 167 | 115k | int digit_index = 0; | 168 | 115k | if (result_int_part_digit_count >= 0) { | 169 | 115k | int max_index = std::min(found_dot ? (result_int_part_digit_count + | 170 | 109k | ((int_part_count > 0 && exponent > 0) ? 1 : 0)) | 171 | 115k | : result_int_part_digit_count, | 172 | 115k | end_digit_index); | 173 | 115k | max_index = (max_index == std::numeric_limits<int>::min() ? end_digit_index : max_index); | 174 | | // skip zero number | 175 | 331k | for (; digit_index != max_index && s[digit_index] == '0'; ++digit_index) { | 176 | 216k | } | 177 | | // test 0.00, .00, 0.{00...}e2147483647 | 178 | | // 0.00000e2147483647 | 179 | 115k | if (digit_index != max_index && | 180 | 115k | (result_int_part_digit_count - digit_index > type_precision - type_scale)) { | 181 | 112 | *result = is_negative ? StringParser::PARSE_UNDERFLOW : StringParser::PARSE_OVERFLOW; | 182 | 112 | return 0; | 183 | 112 | } | 184 | | // get int part number | 185 | 2.15M | for (; digit_index != max_index; ++digit_index) { | 186 | 2.03M | if (UNLIKELY(s[digit_index] == '.')) { | 187 | 67.5k | continue; | 188 | 67.5k | } | 189 | 1.97M | int_part_number = int_part_number * 10 + (s[digit_index] - '0'); | 190 | 1.97M | } | 191 | 115k | auto total_significant_digit_count = i - ((found_dot && int_part_count > 0) ? 1 : 0); | 192 | 115k | if (result_int_part_digit_count > total_significant_digit_count) { | 193 | 2.17k | int_part_number *= get_scale_multiplier<T>(result_int_part_digit_count - | 194 | 2.17k | total_significant_digit_count); | 195 | 2.17k | } | 196 | 115k | } else { | 197 | | // leading zeros of fraction part | 198 | 48 | actual_frac_part_count = -result_int_part_digit_count; | 199 | 48 | } | 200 | | // get fraction part number | 201 | 1.50M | for (; digit_index != end_digit_index && actual_frac_part_count < type_scale; ++digit_index) { | 202 | 1.39M | if (UNLIKELY(s[digit_index] == '.')) { | 203 | 23.9k | continue; | 204 | 23.9k | } | 205 | 1.36M | frac_part_number = frac_part_number * 10 + (s[digit_index] - '0'); | 206 | 1.36M | ++actual_frac_part_count; | 207 | 1.36M | } | 208 | 115k | auto type_scale_multiplier = get_scale_multiplier<T>(type_scale); | 209 | | // there are still extra fraction digits left, check rounding | 210 | 115k | if (digit_index != end_digit_index) { | 211 | 19.0k | if (UNLIKELY(s[digit_index] == '.')) { | 212 | 852 | ++digit_index; | 213 | 852 | } | 214 | 19.0k | if (digit_index != end_digit_index) { | 215 | | // example: test 1.5 -> decimal(1, 0) | 216 | 18.8k | if (s[digit_index] >= '5') { | 217 | 7.94k | ++frac_part_number; | 218 | 7.94k | if (frac_part_number == type_scale_multiplier) { | 219 | 836 | frac_part_number = 0; | 220 | 836 | ++int_part_number; | 221 | 836 | } | 222 | 7.94k | } | 223 | 18.8k | } | 224 | 96.3k | } else { | 225 | 96.3k | if (actual_frac_part_count < type_scale) { | 226 | 87.0k | frac_part_number *= get_scale_multiplier<T>(type_scale - actual_frac_part_count); | 227 | 87.0k | } | 228 | 96.3k | } | 229 | 115k | 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 | 115k | T value = int_part_number * type_scale_multiplier + frac_part_number; | 235 | 115k | *result = StringParser::PARSE_SUCCESS; | 236 | 115k | return is_negative ? T(-value) : T(value); | 237 | 115k | } |
|
238 | | |
239 | | template vectorized::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 vectorized::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 vectorized::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 vectorized::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" |