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

Round instead of Truncate while casting float to decimal #3000

Merged
merged 5 commits into from
Nov 3, 2022

Conversation

waitingkuo
Copy link
Contributor

@waitingkuo waitingkuo commented Nov 2, 2022

Which issue does this PR close?

Closes #2997

Rationale for this change

casting 0.06999999 to Decimal(18, 2) should be 0.07 instead of 0.06

What changes are included in this PR?

add .round() before casting to int

Are there any user-facing changes?

yes, we round the floating number instead of truncating it

@github-actions github-actions bot added the arrow Changes to the arrow crate label Nov 2, 2022
Comment on lines +3197 to +3198
Some(1.123_456_4), // round down
Some(1.123_456_7), // round up
Copy link
Contributor Author

Choose a reason for hiding this comment

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

i change the test cases to

  1. round down
  2. round up

Comment on lines +3210 to +3211
Some(1123456_i128), // round down
Some(1123457_i128), // round up
Copy link
Contributor Author

Choose a reason for hiding this comment

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

and here's the result

Comment on lines 6004 to 6018
#[test]
#[cfg(not(feature = "force_validate"))]
fn test_cast_f64_to_decimal128() {
// to reproduce https://github.com/apache/arrow-rs/issues/2997

let decimal_type = DataType::Decimal128(18, 2);
let array = Float64Array::from(vec![Some(0.0699999999)]);
let array = Arc::new(array) as ArrayRef;
generate_cast_test_case!(
&array,
Decimal128Array,
&decimal_type,
vec![Some(7_i128),]
);
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

to reproduce the issue #2997

@jimexist
Copy link
Member

jimexist commented Nov 2, 2022

thanks for the contribution, can you:

  1. add 0.5 half way test case
  2. update the rust doc of cast kernel to mark the breaking change?

Comment on lines 300 to 302
/// * Casting from `float32/float64` to `Decimal(precision, scale)` rounds to the `scale` decimals
/// (i.e. casting 6.4999 to Decimal(10, 1) becomes 6.5). This is the breaking change from `26.0.0`.
/// It used to truncate it instead of round (i.e. outputs 6.4 instead)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@jimexist i updated the doc here

Comment on lines +6012 to +6017
let decimal_type = DataType::Decimal128(18, 2);
let array = Float64Array::from(vec![
Some(0.0699999999),
Some(0.0659999999),
Some(0.0650000000),
Some(0.0649999999),
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@jimexist added the 0.065 case which will be rounded up to 0.07

@waitingkuo
Copy link
Contributor Author

thank you @jimexist

@tustvold tustvold merged commit 61cf6f7 into apache:master Nov 3, 2022
@ursabot
Copy link

ursabot commented Nov 3, 2022

Benchmark runs are scheduled for baseline = 24afac4 and contender = 61cf6f7. 61cf6f7 is a master commit associated with this PR. Results will be available as each benchmark for each run completes.
Conbench compare runs links:
[Skipped ⚠️ Benchmarking of arrow-rs-commits is not supported on ec2-t3-xlarge-us-east-2] ec2-t3-xlarge-us-east-2
[Skipped ⚠️ Benchmarking of arrow-rs-commits is not supported on test-mac-arm] test-mac-arm
[Skipped ⚠️ Benchmarking of arrow-rs-commits is not supported on ursa-i9-9960x] ursa-i9-9960x
[Skipped ⚠️ Benchmarking of arrow-rs-commits is not supported on ursa-thinkcentre-m75q] ursa-thinkcentre-m75q
Buildkite builds:
Supported benchmarks:
ec2-t3-xlarge-us-east-2: Supported benchmark langs: Python, R. Runs only benchmarks with cloud = True
test-mac-arm: Supported benchmark langs: C++, Python, R
ursa-i9-9960x: Supported benchmark langs: Python, R, JavaScript
ursa-thinkcentre-m75q: Supported benchmark langs: C++, Java

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
arrow Changes to the arrow crate
Projects
None yet
Development

Successfully merging this pull request may close these issues.

I would like to round rather than truncate when casting f64 to decimal
4 participants