Skip to content

Commit

Permalink
fix(rust): parse sign for decimal properly (#11302)
Browse files Browse the repository at this point in the history
  • Loading branch information
orlp authored Sep 25, 2023
1 parent 14823ba commit b92caa0
Showing 1 changed file with 23 additions and 6 deletions.
29 changes: 23 additions & 6 deletions crates/polars-arrow/src/compute/decimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,20 @@ pub fn infer_scale(bytes: &[u8]) -> Option<u8> {
/// Deserializes bytes to a single i128 representing a decimal
/// The decimal precision and scale are not checked.
#[inline]
pub(super) fn deserialize_decimal(bytes: &[u8], precision: Option<u8>, scale: u8) -> Option<i128> {
pub(super) fn deserialize_decimal(
mut bytes: &[u8],
precision: Option<u8>,
scale: u8,
) -> Option<i128> {
let negative = bytes.first() == Some(&b'-');
if negative {
bytes = &bytes[1..];
};
let (lhs, rhs) = split_decimal_bytes(bytes);
let precision = precision.unwrap_or(u8::MAX);

let lhs_b = lhs?;
parse_integer_checked(lhs_b).and_then(|x| {
let abs = parse_integer_checked(lhs_b).and_then(|x| {
match rhs {
Some(rhs) => {
parse_integer_checked(rhs)
Expand Down Expand Up @@ -77,9 +85,7 @@ pub(super) fn deserialize_decimal(bytes: &[u8], precision: Option<u8>, scale: u8
Some((lhs, rhs))
}
})
.map(|(lhs, rhs)| {
lhs * 10i128.pow(scale as u32) + (if lhs < 0 { -rhs } else { rhs })
})
.map(|(lhs, rhs)| lhs * 10i128.pow(scale as u32) + rhs)
},
None => {
if lhs_b.len() > precision as usize || scale != 0 {
Expand All @@ -88,7 +94,12 @@ pub(super) fn deserialize_decimal(bytes: &[u8], precision: Option<u8>, scale: u8
parse_integer_checked(lhs_b)
},
}
})
});
if negative {
Some(-abs?)
} else {
abs
}
}

#[cfg(test)]
Expand Down Expand Up @@ -117,6 +128,12 @@ mod test {
Some(14390)
);

let val = "-0.5";
assert_eq!(
deserialize_decimal(val.as_bytes(), precision, scale),
Some(-50)
);

let val = "-1.5";
assert_eq!(
deserialize_decimal(val.as_bytes(), precision, scale),
Expand Down

0 comments on commit b92caa0

Please sign in to comment.