You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by GitBox <gi...@apache.org> on 2022/11/02 09:55:56 UTC

[GitHub] [arrow-rs] waitingkuo opened a new pull request, #3000: Round instead of Truncate while casting float to decimal

waitingkuo opened a new pull request, #3000:
URL: https://github.com/apache/arrow-rs/pull/3000

   # Which issue does this PR close?
   
   <!--
   We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes #123` indicates that this PR will close issue #123.
   -->
   
   Closes #2997
   
   # Rationale for this change
    
   <!--
   Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed.
   Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes.
   -->
   
   casting 0.06999999 to `Decimal(18, 2)` should be  0.07 instead of 0.06
   
   # What changes are included in this PR?
   
   <!--
   There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR.
   -->
   
   add `.round()` before casting to int
   
   # Are there any user-facing changes?
   
   yes, we round the floating number instead of truncate it
   
   <!--
   If there are user-facing changes then we may require documentation to be updated before approving the PR.
   -->
   
   <!---
   If there are any breaking changes to public APIs, please add the `breaking change` label.
   -->
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-rs] waitingkuo commented on a diff in pull request #3000: Round instead of Truncate while casting float to decimal

Posted by GitBox <gi...@apache.org>.
waitingkuo commented on code in PR #3000:
URL: https://github.com/apache/arrow-rs/pull/3000#discussion_r1011952240


##########
arrow/src/compute/kernels/cast.rs:
##########
@@ -297,6 +297,9 @@ pub fn can_cast_types(from_type: &DataType, to_type: &DataType) -> bool {
 /// * Time32 and Time64: precision lost when going to higher interval
 /// * Timestamp and Date{32|64}: precision lost when going to higher interval
 /// * Temporal to/from backing primitive: zero-copy with data type change
+/// * 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)

Review Comment:
   @Jimexist i updated the doc here



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-rs] ursabot commented on pull request #3000: Round instead of Truncate while casting float to decimal

Posted by GitBox <gi...@apache.org>.
ursabot commented on PR #3000:
URL: https://github.com/apache/arrow-rs/pull/3000#issuecomment-1302741048

   Benchmark runs are scheduled for baseline = 24afac49063d4ee92c0a2be84fd7c305d6088e14 and contender = 61cf6f75c4e03ca950f750cb2fdba4adee534372. 61cf6f75c4e03ca950f750cb2fdba4adee534372 is a master commit associated with this PR. Results will be available as each benchmark for each run completes.
   Conbench compare runs links:
   [Skipped :warning: Benchmarking of arrow-rs-commits is not supported on ec2-t3-xlarge-us-east-2] [ec2-t3-xlarge-us-east-2](https://conbench.ursa.dev/compare/runs/dd71a850fca84c07aae7d3827d3dec02...dcadace1db5e4312b1154a2d999963a5/)
   [Skipped :warning: Benchmarking of arrow-rs-commits is not supported on test-mac-arm] [test-mac-arm](https://conbench.ursa.dev/compare/runs/4769bd6f399f4b8d9c3b290de59601f4...b00e5d6efd36473ebedd2fae37a54825/)
   [Skipped :warning: Benchmarking of arrow-rs-commits is not supported on ursa-i9-9960x] [ursa-i9-9960x](https://conbench.ursa.dev/compare/runs/d849acddbe544ce8bf58a51c4247f920...78795ff04b88494884405b71544aa5e3/)
   [Skipped :warning: Benchmarking of arrow-rs-commits is not supported on ursa-thinkcentre-m75q] [ursa-thinkcentre-m75q](https://conbench.ursa.dev/compare/runs/83ce5af0e4d842e0b2a4f3d54313d21a...5b7007ff29c34266a19e119126e1c3eb/)
   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
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-rs] waitingkuo commented on pull request #3000: Round instead of Truncate while casting float to decimal

Posted by GitBox <gi...@apache.org>.
waitingkuo commented on PR #3000:
URL: https://github.com/apache/arrow-rs/pull/3000#issuecomment-1300698486

   thank you @Jimexist 


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-rs] waitingkuo commented on a diff in pull request #3000: Round instead of Truncate while casting float to decimal

Posted by GitBox <gi...@apache.org>.
waitingkuo commented on code in PR #3000:
URL: https://github.com/apache/arrow-rs/pull/3000#discussion_r1011494924


##########
arrow/src/compute/kernels/cast.rs:
##########
@@ -3192,8 +3194,8 @@ mod tests {
             Some(2.2),
             Some(4.4),
             None,
-            Some(1.123_456_7),
-            Some(1.123_456_7),
+            Some(1.123_456_4), // round down
+            Some(1.123_456_7), // round up

Review Comment:
   i change the test cases to 
   1. round down
   2. round up



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-rs] tustvold merged pull request #3000: Round instead of Truncate while casting float to decimal

Posted by GitBox <gi...@apache.org>.
tustvold merged PR #3000:
URL: https://github.com/apache/arrow-rs/pull/3000


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-rs] waitingkuo commented on a diff in pull request #3000: Round instead of Truncate while casting float to decimal

Posted by GitBox <gi...@apache.org>.
waitingkuo commented on code in PR #3000:
URL: https://github.com/apache/arrow-rs/pull/3000#discussion_r1011495242


##########
arrow/src/compute/kernels/cast.rs:
##########
@@ -3205,8 +3207,8 @@ mod tests {
                 Some(2200000_i128),
                 Some(4400000_i128),
                 None,
-                Some(1123456_i128),
-                Some(1123456_i128),
+                Some(1123456_i128), // round down
+                Some(1123457_i128), // round up

Review Comment:
   and here's the result



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-rs] waitingkuo commented on a diff in pull request #3000: Round instead of Truncate while casting float to decimal

Posted by GitBox <gi...@apache.org>.
waitingkuo commented on code in PR #3000:
URL: https://github.com/apache/arrow-rs/pull/3000#discussion_r1011495960


##########
arrow/src/compute/kernels/cast.rs:
##########
@@ -5994,4 +6000,20 @@ mod tests {
             .collect::<Vec<_>>();
         assert_eq!(&out, &vec!["[0, 1, 2]", "[3, 4, 5]", "[6, 7]"]);
     }
+
+    #[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),]
+        );
+    }

Review Comment:
   to reproduce the issue #2997 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-rs] Jimexist commented on pull request #3000: Round instead of Truncate while casting float to decimal

Posted by GitBox <gi...@apache.org>.
Jimexist commented on PR #3000:
URL: https://github.com/apache/arrow-rs/pull/3000#issuecomment-1300468192

   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?


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-rs] waitingkuo commented on a diff in pull request #3000: Round instead of Truncate while casting float to decimal

Posted by GitBox <gi...@apache.org>.
waitingkuo commented on code in PR #3000:
URL: https://github.com/apache/arrow-rs/pull/3000#discussion_r1011953551


##########
arrow/src/compute/kernels/cast.rs:
##########
@@ -5994,4 +6003,50 @@ mod tests {
             .collect::<Vec<_>>();
         assert_eq!(&out, &vec!["[0, 1, 2]", "[3, 4, 5]", "[6, 7]"]);
     }
+
+    #[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),
+            Some(0.0659999999),
+            Some(0.0650000000),
+            Some(0.0649999999),

Review Comment:
   @Jimexist added the 0.065 case which will be rounded up to 0.07



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org