Skip to content

Commit

Permalink
add more error test case and change the code style (#952) (#976)
Browse files Browse the repository at this point in the history
Co-authored-by: Kun Liu <liukun@apache.org>
  • Loading branch information
alamb and liukun4515 committed Nov 26, 2021
1 parent 6c570cf commit 7e51df0
Showing 1 changed file with 13 additions and 22 deletions.
35 changes: 13 additions & 22 deletions arrow/src/csv/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -779,7 +779,6 @@ fn parse_decimal_with_parameter(s: &str, precision: usize, scale: usize) -> Resu
if PARSE_DECIMAL_RE.is_match(s) {
let mut offset = s.len();
let len = s.len();
// each byte is digit、'-' or '.'
let mut base = 1;

// handle the value after the '.' and meet the scale
Expand All @@ -801,31 +800,23 @@ fn parse_decimal_with_parameter(s: &str, precision: usize, scale: usize) -> Resu
}
};

// each byte is digit、'-' or '.'
let bytes = s.as_bytes();
let mut negative = false;
let mut result: i128 = 0;

while offset > 0 {
match bytes[offset - 1] {
b'-' => {
negative = true;
}
b'.' => {
// do nothing
}
b'0'..=b'9' => {
result += i128::from(bytes[offset - 1] - b'0') * base;
base *= 10;
}
_ => {
return Err(ArrowError::ParseError(format!(
"can't match byte {}",
bytes[offset - 1]
)));
}
bytes[0..offset].iter().rev().for_each(|&byte| match byte {
b'-' => {
negative = true;
}
offset -= 1;
}
b'0'..=b'9' => {
result += i128::from(byte - b'0') * base;
base *= 10;
}
// because of the PARSE_DECIMAL_RE, bytes just contains digit、'-' and '.'.
_ => {}
});

if negative {
result = result.neg();
}
Expand Down Expand Up @@ -1614,7 +1605,7 @@ mod tests {
let result = parse_decimal_with_parameter(s, 20, 3);
assert_eq!(i, result.unwrap())
}
let can_not_parse_tests = ["123,123", "."];
let can_not_parse_tests = ["123,123", ".", "123.123.123"];
for s in can_not_parse_tests {
let result = parse_decimal_with_parameter(s, 20, 3);
assert_eq!(
Expand Down

0 comments on commit 7e51df0

Please sign in to comment.