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

fix: bad cast to Decimal128 for negative precision #6413

Closed
wants to merge 1 commit into from
Closed
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
62 changes: 25 additions & 37 deletions arrow-cast/src/cast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,41 +315,29 @@ where
<T as ArrowPrimitiveType>::Native: AsPrimitive<M>,
M: ArrowNativeTypeOp,
{
let scale_factor = base.pow_checked(scale.unsigned_abs() as u32).map_err(|_| {
ArrowError::CastError(format!(
"Cannot cast to {:?}({}, {}). The scale causes overflow.",
D::PREFIX,
precision,
scale,
))
})?;
let scale_factor = base
.pow_checked(if scale < 0 { 0 } else { scale } as u32)
.map_err(|_| {
ArrowError::CastError(format!(
"Cannot cast to {:?}({}, {}). The scale causes overflow.",
D::PREFIX,
precision,
scale,
))
})?;

let array = if scale < 0 {
match cast_options.safe {
true => array.unary_opt::<_, D>(|v| {
v.as_().div_checked(scale_factor).ok().and_then(|v| {
(D::validate_decimal_precision(v, precision).is_ok()).then_some(v)
})
}),
false => array.try_unary::<_, D, _>(|v| {
v.as_()
.div_checked(scale_factor)
.and_then(|v| D::validate_decimal_precision(v, precision).map(|_| v))
})?,
}
} else {
match cast_options.safe {
true => array.unary_opt::<_, D>(|v| {
v.as_().mul_checked(scale_factor).ok().and_then(|v| {
(D::validate_decimal_precision(v, precision).is_ok()).then_some(v)
})
}),
false => array.try_unary::<_, D, _>(|v| {
v.as_()
.mul_checked(scale_factor)
.and_then(|v| D::validate_decimal_precision(v, precision).map(|_| v))
})?,
}
let array = match cast_options.safe {
true => array.unary_opt::<_, D>(|v| {
v.as_()
.mul_checked(scale_factor)
.ok()
.and_then(|v| (D::validate_decimal_precision(v, precision).is_ok()).then_some(v))
}),
false => array.try_unary::<_, D, _>(|v| {
v.as_()
.mul_checked(scale_factor)
.and_then(|v| D::validate_decimal_precision(v, precision).map(|_| v))
})?,
};

Ok(Arc::new(array.with_precision_and_scale(precision, scale)?))
Expand Down Expand Up @@ -8121,9 +8109,9 @@ mod tests {
let casted_array = cast(&array, &decimal_type).unwrap();
let decimal_arr = casted_array.as_primitive::<Decimal128Type>();

assert_eq!("1123450", decimal_arr.value_as_string(0));
assert_eq!("2123450", decimal_arr.value_as_string(1));
assert_eq!("3123450", decimal_arr.value_as_string(2));
assert_eq!("11234560", decimal_arr.value_as_string(0));
assert_eq!("21234560", decimal_arr.value_as_string(1));
assert_eq!("31234560", decimal_arr.value_as_string(2));
Comment on lines -8124 to +8114
Copy link
Member

@wjones127 wjones127 Sep 19, 2024

Choose a reason for hiding this comment

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

I'm not sure this behavior change is actually what we want 🤔

See my comment in this issue: #5793 (comment)

If we do commit this, we should mark this as breaking-change.


let array = Arc::new(Float32Array::from(vec![
Some(1123.456),
Expand Down
Loading