Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add more error test case and change the code style #952

Merged
merged 1 commit into from
Nov 17, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 looking much nicer!

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"];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

for s in can_not_parse_tests {
let result = parse_decimal_with_parameter(s, 20, 3);
assert_eq!(
Expand Down