Skip to content

Commit

Permalink
fix: parse string of scientific notation to decimal when the scale is…
Browse files Browse the repository at this point in the history
… 0 (#5740)

* fix: parse string to decimal when scale is 0

* fix fmt
  • Loading branch information
yjshen committed May 13, 2024
1 parent c08feb4 commit 326231e
Showing 1 changed file with 16 additions and 12 deletions.
28 changes: 16 additions & 12 deletions arrow-cast/src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -869,7 +869,7 @@ pub fn parse_decimal<T: DecimalType>(
"can't parse the string value {s} to decimal"
)));
}
if fractionals == scale {
if fractionals == scale && scale != 0 {
// We have processed all the digits that we need. All that
// is left is to validate that the rest of the string contains
// valid digits.
Expand Down Expand Up @@ -2416,6 +2416,8 @@ mod tests {
("0.12e+6", "120000", 10),
("000000000001e0", "000000000001", 3),
("000001.1034567002e0", "000001.1034567002", 3),
("1.234e16", "12340000000000000", 0),
("123.4e16", "1234000000000000000", 0),
];
for (e, d, scale) in e_notation_tests {
let result_128_e = parse_decimal::<Decimal128Type>(e, 20, scale);
Expand Down Expand Up @@ -2454,17 +2456,19 @@ mod tests {
);
}
let overflow_parse_tests = [
"12345678",
"1.2345678e7",
"12345678.9",
"1.23456789e+7",
"99999999.99",
"9.999999999e7",
"12345678908765.123456",
"123456789087651234.56e-4",
("12345678", 3),
("1.2345678e7", 3),
("12345678.9", 3),
("1.23456789e+7", 3),
("99999999.99", 3),
("9.999999999e7", 3),
("12345678908765.123456", 3),
("123456789087651234.56e-4", 3),
("1234560000000", 0),
("1.23456e12", 0),
];
for s in overflow_parse_tests {
let result_128 = parse_decimal::<Decimal128Type>(s, 10, 3);
for (s, scale) in overflow_parse_tests {
let result_128 = parse_decimal::<Decimal128Type>(s, 10, scale);
let expected_128 = "Parser error: parse decimal overflow";
let actual_128 = result_128.unwrap_err().to_string();

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

let result_256 = parse_decimal::<Decimal256Type>(s, 10, 3);
let result_256 = parse_decimal::<Decimal256Type>(s, 10, scale);
let expected_256 = "Parser error: parse decimal overflow";
let actual_256 = result_256.unwrap_err().to_string();

Expand Down

0 comments on commit 326231e

Please sign in to comment.