Skip to content

Commit 326231e

Browse files
authored
fix: parse string of scientific notation to decimal when the scale is 0 (#5740)
* fix: parse string to decimal when scale is 0 * fix fmt
1 parent c08feb4 commit 326231e

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed

arrow-cast/src/parse.rs

+16-12
Original file line numberDiff line numberDiff line change
@@ -869,7 +869,7 @@ pub fn parse_decimal<T: DecimalType>(
869869
"can't parse the string value {s} to decimal"
870870
)));
871871
}
872-
if fractionals == scale {
872+
if fractionals == scale && scale != 0 {
873873
// We have processed all the digits that we need. All that
874874
// is left is to validate that the rest of the string contains
875875
// valid digits.
@@ -2416,6 +2416,8 @@ mod tests {
24162416
("0.12e+6", "120000", 10),
24172417
("000000000001e0", "000000000001", 3),
24182418
("000001.1034567002e0", "000001.1034567002", 3),
2419+
("1.234e16", "12340000000000000", 0),
2420+
("123.4e16", "1234000000000000000", 0),
24192421
];
24202422
for (e, d, scale) in e_notation_tests {
24212423
let result_128_e = parse_decimal::<Decimal128Type>(e, 20, scale);
@@ -2454,17 +2456,19 @@ mod tests {
24542456
);
24552457
}
24562458
let overflow_parse_tests = [
2457-
"12345678",
2458-
"1.2345678e7",
2459-
"12345678.9",
2460-
"1.23456789e+7",
2461-
"99999999.99",
2462-
"9.999999999e7",
2463-
"12345678908765.123456",
2464-
"123456789087651234.56e-4",
2459+
("12345678", 3),
2460+
("1.2345678e7", 3),
2461+
("12345678.9", 3),
2462+
("1.23456789e+7", 3),
2463+
("99999999.99", 3),
2464+
("9.999999999e7", 3),
2465+
("12345678908765.123456", 3),
2466+
("123456789087651234.56e-4", 3),
2467+
("1234560000000", 0),
2468+
("1.23456e12", 0),
24652469
];
2466-
for s in overflow_parse_tests {
2467-
let result_128 = parse_decimal::<Decimal128Type>(s, 10, 3);
2470+
for (s, scale) in overflow_parse_tests {
2471+
let result_128 = parse_decimal::<Decimal128Type>(s, 10, scale);
24682472
let expected_128 = "Parser error: parse decimal overflow";
24692473
let actual_128 = result_128.unwrap_err().to_string();
24702474

@@ -2473,7 +2477,7 @@ mod tests {
24732477
"actual: '{actual_128}', expected: '{expected_128}'"
24742478
);
24752479

2476-
let result_256 = parse_decimal::<Decimal256Type>(s, 10, 3);
2480+
let result_256 = parse_decimal::<Decimal256Type>(s, 10, scale);
24772481
let expected_256 = "Parser error: parse decimal overflow";
24782482
let actual_256 = result_256.unwrap_err().to_string();
24792483

0 commit comments

Comments
 (0)