-
Notifications
You must be signed in to change notification settings - Fork 839
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
Get the round result for decimal to a decimal with smaller scale #3224
Merged
tustvold
merged 7 commits into
apache:master
from
liukun4515:decimal_round_#3137_with_false
Dec 3, 2022
Merged
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a4a49e7
support cast decimal for round when the option is false
liukun4515 4536283
Merge remote-tracking branch 'upstream/master' into decimal_round_#31…
liukun4515 01dd8ba
fix conflict after merge
liukun4515 9a0313e
Merge remote-tracking branch 'upstream/master' into decimal_round_#31…
liukun4515 d2fa48b
fix error case
liukun4515 7ee48e2
Merge remote-tracking branch 'upstream/master' into decimal_round_#31…
liukun4515 477d138
change to wrapping api
liukun4515 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2131,6 +2131,7 @@ fn cast_decimal_to_decimal<const BYTE_WIDTH1: usize, const BYTE_WIDTH2: usize>( | |
if BYTE_WIDTH1 == 16 { | ||
let array = array.as_any().downcast_ref::<Decimal128Array>().unwrap(); | ||
if BYTE_WIDTH2 == 16 { | ||
// the div must be greater or equal than 10 | ||
let div = 10_i128 | ||
.pow_checked((input_scale - output_scale) as u32) | ||
.map_err(|_| { | ||
|
@@ -2139,10 +2140,23 @@ fn cast_decimal_to_decimal<const BYTE_WIDTH1: usize, const BYTE_WIDTH2: usize>( | |
*output_scale, | ||
)) | ||
})?; | ||
let half = div / 2; | ||
let neg_half = -half; | ||
|
||
array | ||
.try_unary::<_, Decimal128Type, _>(|v| { | ||
v.checked_div(div).ok_or_else(|| { | ||
// cast to smaller scale, need to round the result | ||
// the div must be gt_eq 10, we don't need to check the overflow for the `div`/`mod` operation | ||
let d = v / div; | ||
let r = v % div; | ||
if v >= 0 && r >= half { | ||
d.checked_add(1) | ||
} else if v < 0 && r <= neg_half { | ||
d.checked_sub(1) | ||
} else { | ||
Some(d) | ||
} | ||
.ok_or_else(|| { | ||
ArrowError::CastError(format!( | ||
"Cannot cast to {:?}({}, {}). Overflowing on {:?}", | ||
Decimal128Type::PREFIX, | ||
|
@@ -2166,9 +2180,23 @@ fn cast_decimal_to_decimal<const BYTE_WIDTH1: usize, const BYTE_WIDTH2: usize>( | |
)) | ||
})?; | ||
|
||
let half = div / i256::from_i128(2_i128); | ||
let neg_half = -half; | ||
|
||
array | ||
.try_unary::<_, Decimal256Type, _>(|v| { | ||
i256::from_i128(v).checked_div(div).ok_or_else(|| { | ||
// the div must be gt_eq 10, we don't need to check the overflow for the `div`/`mod` operation | ||
let v = i256::from_i128(v); | ||
let d = v / div; | ||
let r = v % div; | ||
if v >= i256::ZERO && r >= half { | ||
d.checked_add(i256::ONE) | ||
} else if v < i256::ZERO && r <= neg_half { | ||
d.checked_sub(i256::ONE) | ||
} else { | ||
Some(d) | ||
} | ||
.ok_or_else(|| { | ||
ArrowError::CastError(format!( | ||
"Cannot cast to {:?}({}, {}). Overflowing on {:?}", | ||
Decimal256Type::PREFIX, | ||
|
@@ -2193,10 +2221,21 @@ fn cast_decimal_to_decimal<const BYTE_WIDTH1: usize, const BYTE_WIDTH2: usize>( | |
*output_scale, | ||
)) | ||
})?; | ||
let half = div / i256::from_i128(2_i128); | ||
let neg_half = -half; | ||
if BYTE_WIDTH2 == 16 { | ||
array | ||
.try_unary::<_, Decimal128Type, _>(|v| { | ||
v.checked_div(div).ok_or_else(|| { | ||
// the div must be gt_eq 10, we don't need to check the overflow for the `div`/`mod` operation | ||
let d = v / div; | ||
let r = v % div; | ||
if v >= i256::ZERO && r >= half { | ||
d.checked_add(i256::ONE) | ||
} else if v < i256::ZERO && r <= neg_half { | ||
d.checked_sub(i256::ONE) | ||
} else { | ||
Some(d) | ||
}.ok_or_else(|| { | ||
ArrowError::CastError(format!( | ||
"Cannot cast to {:?}({}, {}). Overflowing on {:?}", | ||
Decimal128Type::PREFIX, | ||
|
@@ -2217,7 +2256,17 @@ fn cast_decimal_to_decimal<const BYTE_WIDTH1: usize, const BYTE_WIDTH2: usize>( | |
} else { | ||
array | ||
.try_unary::<_, Decimal256Type, _>(|v| { | ||
v.checked_div(div).ok_or_else(|| { | ||
// the div must be gt_eq 10, we don't need to check the overflow for the `div`/`mod` operation | ||
let d = v / div; | ||
let r = v % div; | ||
if v >= i256::ZERO && r >= half { | ||
d.checked_add(i256::ONE) | ||
} else if v < i256::ZERO && r <= neg_half { | ||
d.checked_sub(i256::ONE) | ||
} else { | ||
Some(d) | ||
} | ||
.ok_or_else(|| { | ||
ArrowError::CastError(format!( | ||
"Cannot cast to {:?}({}, {}). Overflowing on {:?}", | ||
Decimal256Type::PREFIX, | ||
|
@@ -3588,6 +3637,26 @@ mod tests { | |
} | ||
} | ||
} | ||
|
||
let cast_option = CastOptions { safe: false }; | ||
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. add cast with |
||
let casted_array_with_option = | ||
cast_with_options($INPUT_ARRAY, $OUTPUT_TYPE, &cast_option).unwrap(); | ||
let result_array = casted_array_with_option | ||
.as_any() | ||
.downcast_ref::<$OUTPUT_TYPE_ARRAY>() | ||
.unwrap(); | ||
assert_eq!($OUTPUT_TYPE, result_array.data_type()); | ||
assert_eq!(result_array.len(), $OUTPUT_VALUES.len()); | ||
for (i, x) in $OUTPUT_VALUES.iter().enumerate() { | ||
match x { | ||
Some(x) => { | ||
assert_eq!(result_array.value(i), *x); | ||
} | ||
None => { | ||
assert!(result_array.is_null(i)); | ||
} | ||
} | ||
} | ||
}; | ||
} | ||
|
||
|
@@ -3795,6 +3864,34 @@ mod tests { | |
result.unwrap_err().to_string()); | ||
} | ||
|
||
#[test] | ||
fn test_cast_decimal256_to_decimal128_overflow() { | ||
let input_type = DataType::Decimal256(76, 5); | ||
let output_type = DataType::Decimal128(38, 7); | ||
assert!(can_cast_types(&input_type, &output_type)); | ||
let array = vec![Some(i256::from_i128(i128::MAX))]; | ||
let input_decimal_array = create_decimal256_array(array, 76, 5).unwrap(); | ||
let array = Arc::new(input_decimal_array) as ArrayRef; | ||
let result = | ||
cast_with_options(&array, &output_type, &CastOptions { safe: false }); | ||
assert_eq!("Invalid argument error: 17014118346046923173168730371588410572700 cannot be casted to 128-bit integer for Decimal128", | ||
result.unwrap_err().to_string()); | ||
} | ||
|
||
#[test] | ||
fn test_cast_decimal256_to_decimal256_overflow() { | ||
let input_type = DataType::Decimal256(76, 5); | ||
let output_type = DataType::Decimal256(76, 55); | ||
assert!(can_cast_types(&input_type, &output_type)); | ||
let array = vec![Some(i256::from_i128(i128::MAX))]; | ||
let input_decimal_array = create_decimal256_array(array, 76, 5).unwrap(); | ||
let array = Arc::new(input_decimal_array) as ArrayRef; | ||
let result = | ||
cast_with_options(&array, &output_type, &CastOptions { safe: false }); | ||
assert_eq!("Cast error: Cannot cast to \"Decimal256\"(76, 55). Overflowing on 170141183460469231731687303715884105727", | ||
result.unwrap_err().to_string()); | ||
} | ||
|
||
#[test] | ||
fn test_cast_decimal128_to_decimal256() { | ||
let input_type = DataType::Decimal128(20, 3); | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Do we need to make consistent with the usage and clear compilation result?