-
Notifications
You must be signed in to change notification settings - Fork 881
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: decimal conversion looses value on lower precision #6836
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -112,8 +112,19 @@ where | |
}; | ||
|
||
Ok(match cast_options.safe { | ||
true => array.unary_opt(f), | ||
false => array.try_unary(|x| f(x).ok_or_else(|| error(x)))?, | ||
true => { | ||
array.unary_opt(|x| { | ||
f(x).filter(|v| O::is_valid_decimal_precision(*v, output_precision)) | ||
}) | ||
} | ||
false => { | ||
array.try_unary(|x| { | ||
f(x).ok_or_else(|| error(x)) | ||
.and_then(|v|{ | ||
O:: validate_decimal_precision(v, output_precision).map(|_| v) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not do this when computing the value (the code above), and only when this can fail? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I thought about it, but then there are other examples where is it done this way, so kept it there. We can also do this as part of first error check point - inside this method let error = cast_decimal_to_decimal_error::<I, O>(output_precision, output_scale); |
||
}) | ||
})? | ||
} | ||
}) | ||
} | ||
|
||
|
@@ -135,11 +146,23 @@ where | |
.unwrap() | ||
.pow_checked((output_scale - input_scale) as u32)?; | ||
|
||
let f = |x| O::Native::from_decimal(x).and_then(|x| x.mul_checked(mul).ok()); | ||
let f = |x| | ||
O::Native::from_decimal(x).and_then(|x| x.mul_checked(mul).ok()); | ||
|
||
Ok(match cast_options.safe { | ||
true => array.unary_opt(f), | ||
false => array.try_unary(|x| f(x).ok_or_else(|| error(x)))?, | ||
true => { | ||
array.unary_opt(|x| { | ||
f(x).filter(|v| O::is_valid_decimal_precision(*v, output_precision)) | ||
}) | ||
} | ||
false => { | ||
array.try_unary(|x| { | ||
f(x).ok_or_else(|| error(x)) | ||
.and_then(|v|{ | ||
O:: validate_decimal_precision(v, output_precision).map(|_| v) | ||
}) | ||
})? | ||
} | ||
}) | ||
} | ||
|
||
|
@@ -156,19 +179,9 @@ where | |
T::Native: DecimalCast + ArrowNativeTypeOp, | ||
{ | ||
let array: PrimitiveArray<T> = match input_scale.cmp(&output_scale) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This could be changed to |
||
Ordering::Equal => { | ||
// the scale doesn't change, the native value don't need to be changed | ||
array.clone() | ||
} | ||
Ordering::Greater => convert_to_smaller_scale_decimal::<T, T>( | ||
array, | ||
input_scale, | ||
output_precision, | ||
output_scale, | ||
cast_options, | ||
)?, | ||
Ordering::Less => { | ||
// input_scale < output_scale | ||
Ordering::Equal | Ordering::Less => { | ||
// input_scale <= output_scale | ||
// the scale doesn't change, but precision may change and cause overflow | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why don't we also check precision and skip There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. won't it still has to go through the if (array.validate_decimal_precision().is_err()) but we need to return null for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For example, original code simply returns the original array for same scale. If the new precision is bigger, I think we can still do that, no? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. lets assume, we are converting There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If this is correct, then we can return original array if precision is bigger and scale is equal. select arrow_typeof(cast(cast(1.23 as decimal(10,3)) as decimal(12,3))),
cast(cast(1.23 as decimal(10,3)) as decimal(12,3));
----
Decimal128(12, 3) 1.23 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 12345 in (5, 3) is 12.345. When casting to (6, 3), it is still 12345, why it needs to be 123450? 123450 in (6, 3) is 123.45. If I understand it correctly. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, you are right. fn signature only has output_precision, I can get the input_precision from the calling fn, it's available there and change the signature to include input_precision. would that be fine? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yea There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. @viirya please check. |
||
convert_to_bigger_or_equal_scale_decimal::<T, T>( | ||
array, | ||
input_scale, | ||
|
@@ -177,6 +190,13 @@ where | |
cast_options, | ||
)? | ||
} | ||
Ordering::Greater => convert_to_smaller_scale_decimal::<T, T>( | ||
array, | ||
input_scale, | ||
output_precision, | ||
output_scale, | ||
cast_options, | ||
)? | ||
}; | ||
|
||
Ok(Arc::new(array.with_precision_and_scale( | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9827,4 +9827,72 @@ mod tests { | |
"Cast non-nullable to non-nullable struct field returning null should fail", | ||
); | ||
} | ||
|
||
#[test] | ||
fn test_decimal_to_decimal_throw_error_on_precision_overflow_same_scale(){ | ||
let array = vec![Some(123456789)]; | ||
let array = create_decimal_array(array, 24, 2).unwrap(); | ||
println!("{:?}", array); | ||
let input_type = DataType::Decimal128(24, 2); | ||
let output_type = DataType::Decimal128(6, 2); | ||
assert!(can_cast_types(&input_type, &output_type)); | ||
let mut options = CastOptions::default(); | ||
options.safe = false; | ||
|
||
let result = cast_with_options(&array, &output_type, &options); | ||
|
||
assert_eq!(result.unwrap_err().to_string(), | ||
"InvalidArgumentError(123456789 is too large to store in a Decimal128 of precision 6. Max is 999999)"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The error message format needs updating:
|
||
} | ||
|
||
#[test] | ||
fn test_decimal_to_decimal_throw_error_on_precision_overflow_lower_scale(){ | ||
let array = vec![Some(123456789)]; | ||
let array = create_decimal_array(array, 24, 2).unwrap(); | ||
println!("{:?}", array); | ||
let input_type = DataType::Decimal128(24, 4); | ||
let output_type = DataType::Decimal128(6, 2); | ||
assert!(can_cast_types(&input_type, &output_type)); | ||
let mut options = CastOptions::default(); | ||
options.safe = false; | ||
|
||
let result = cast_with_options(&array, &output_type, &options); | ||
|
||
assert_eq!(result.unwrap_err().to_string(), | ||
"InvalidArgumentError(123456789 is too large to store in a Decimal128 of precision 6. Max is 999999)"); | ||
} | ||
|
||
#[test] | ||
fn test_decimal_to_decimal_throw_error_on_precision_overflow_greater_scale(){ | ||
let array = vec![Some(123456789)]; | ||
let array = create_decimal_array(array, 24, 2).unwrap(); | ||
println!("{:?}", array); | ||
let input_type = DataType::Decimal128(24, 2); | ||
let output_type = DataType::Decimal128(6, 3); | ||
assert!(can_cast_types(&input_type, &output_type)); | ||
let mut options = CastOptions::default(); | ||
options.safe = false; | ||
|
||
let result = cast_with_options(&array, &output_type, &options); | ||
|
||
assert_eq!(result.unwrap_err().to_string(), | ||
"InvalidArgumentError(123456789 is too large to store in a Decimal128 of precision 6. Max is 999999)"); | ||
} | ||
|
||
#[test] | ||
fn test_decimal_to_decimal_throw_error_on_precision_overflow_diff_type(){ | ||
let array = vec![Some(123456789)]; | ||
let array = create_decimal_array(array, 24, 2).unwrap(); | ||
println!("{:?}", array); | ||
let input_type = DataType::Decimal128(24, 2); | ||
let output_type = DataType::Decimal256(6, 2); | ||
assert!(can_cast_types(&input_type, &output_type)); | ||
let mut options = CastOptions::default(); | ||
options.safe = false; | ||
|
||
let result = cast_with_options(&array, &output_type, &options); | ||
|
||
assert_eq!(result.unwrap_err().to_string(), | ||
"InvalidArgumentError(123456789 is too large to store in a Decimal256 of precision 6. Max is 999999)"); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems strange to
match
on a boolean rather than just using anif
statement. I know this is how the existing code was, but perhaps we could improve this while we are here.