You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by "viirya (via GitHub)" <gi...@apache.org> on 2023/04/12 20:00:58 UTC

[GitHub] [arrow-datafusion] viirya opened a new pull request, #5980: Decimal multiply kernel should not cause precision loss

viirya opened a new pull request, #5980:
URL: https://github.com/apache/arrow-datafusion/pull/5980

   # 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 #5674.
   Closes #3387.
   
   # 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.  
   -->
   
   # 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.
   -->
   
   # Are these changes tested?
   
   <!--
   We typically require tests for all PRs in order to:
   1. Prevent the code from being accidentally broken by subsequent changes
   2. Serve as another way to document the expected behavior of the code
   
   If tests are not included in your PR, please explain why (for example, are they covered by existing tests)?
   -->
   
   # Are there any user-facing changes?
   
   <!--
   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 `api 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-datafusion] Dandandan commented on pull request #5980: Decimal multiply kernel should not cause precision loss

Posted by "Dandandan (via GitHub)" <gi...@apache.org>.
Dandandan commented on PR #5980:
URL: https://github.com/apache/arrow-datafusion/pull/5980#issuecomment-1514305301

   FYI @mingmwang @andygrove this PR also has some effect on performance, as casting is changed (mostly reduced).


-- 
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-datafusion] viirya commented on pull request #5980: Decimal multiply kernel should not cause precision loss

Posted by "viirya (via GitHub)" <gi...@apache.org>.
viirya commented on PR #5980:
URL: https://github.com/apache/arrow-datafusion/pull/5980#issuecomment-1510496247

   There is a compilation error. Going to fix it at #6029.


-- 
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-datafusion] alamb commented on pull request #5980: Decimal multiply kernel should not cause precision loss

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on PR #5980:
URL: https://github.com/apache/arrow-datafusion/pull/5980#issuecomment-1520543719

   🎉  


-- 
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-datafusion] Dandandan commented on pull request #5980: Decimal multiply kernel should not cause precision loss

Posted by "Dandandan (via GitHub)" <gi...@apache.org>.
Dandandan commented on PR #5980:
URL: https://github.com/apache/arrow-datafusion/pull/5980#issuecomment-1514338925

   Ran the benchmarks for TPCH(SF=1) in memory.
   
   Performance is mostly the same, except a ~30% improvement for q1 compared to main 🚀 
   
   
   
   
   


-- 
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-datafusion] viirya commented on a diff in pull request #5980: Decimal multiply kernel should not cause precision loss

Posted by "viirya (via GitHub)" <gi...@apache.org>.
viirya commented on code in PR #5980:
URL: https://github.com/apache/arrow-datafusion/pull/5980#discussion_r1169177389


##########
datafusion/physical-expr/src/expressions/binary.rs:
##########
@@ -4801,18 +4878,18 @@ mod tests {
 
         // subtract: decimal array subtract int32 array
         let schema = Arc::new(Schema::new(vec![
-            Field::new("b", DataType::Int32, true),
             Field::new("a", DataType::Decimal128(10, 2), true),

Review Comment:
   Previously the field order is incorrect. But as we did coerce type on both side of the op anyway, so it still worked before. Now we don't coerce the decimal field (which is wrongly bound to Int32Array) before into binary expression, so wrong field causes an error.



-- 
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-datafusion] Dandandan commented on pull request #5980: Decimal multiply kernel should not cause precision loss

Posted by "Dandandan (via GitHub)" <gi...@apache.org>.
Dandandan commented on PR #5980:
URL: https://github.com/apache/arrow-datafusion/pull/5980#issuecomment-1512599257

   I wonder if this already fixes https://github.com/apache/arrow-datafusion/issues/4024


-- 
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-datafusion] viirya commented on pull request #5980: Decimal multiply kernel should not cause precision loss

Posted by "viirya (via GitHub)" <gi...@apache.org>.
viirya commented on PR #5980:
URL: https://github.com/apache/arrow-datafusion/pull/5980#issuecomment-1512117023

   cc @alamb @liukun4515 


-- 
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-datafusion] viirya commented on a diff in pull request #5980: Decimal multiply kernel should not cause precision loss

Posted by "viirya (via GitHub)" <gi...@apache.org>.
viirya commented on code in PR #5980:
URL: https://github.com/apache/arrow-datafusion/pull/5980#discussion_r1169167115


##########
datafusion/physical-expr/src/expressions/binary.rs:
##########
@@ -3258,7 +3308,17 @@ mod tests {
             Arc::new(schema),
             vec![Arc::new(a), Arc::new(b)],
             Operator::Divide,
-            create_decimal_array(&[Some(0), None, None, Some(1), Some(1)], 10, 0),
+            create_decimal_array(
+                &[
+                    Some(99193548387), // 0.99193548387
+                    None,
+                    None,
+                    Some(100813008130), // 1.0081300813
+                    Some(100000000000), // 1.0
+                ],
+                21,
+                11,

Review Comment:
   Previously, this division losses precision. Now we get it back.



-- 
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-datafusion] viirya commented on a diff in pull request #5980: Decimal multiply kernel should not cause precision loss

Posted by "viirya (via GitHub)" <gi...@apache.org>.
viirya commented on code in PR #5980:
URL: https://github.com/apache/arrow-datafusion/pull/5980#discussion_r1169216648


##########
datafusion/core/tests/sqllogictests/src/engines/conversion.rs:
##########
@@ -74,9 +75,9 @@ pub fn f64_to_str(value: f64) -> String {
     }
 }
 
-pub fn i128_to_str(value: i128, scale: u32) -> String {
+pub fn i128_to_str(value: i128, precision: &u8, scale: &i8) -> String {
     big_decimal_to_str(
-        BigDecimal::from_str(&Decimal::from_i128_with_scale(value, scale).to_string())
+        BigDecimal::from_str(&Decimal128Type::format_decimal(value, *precision, *scale))

Review Comment:
   See https://github.com/apache/arrow-datafusion/pull/5675/files#r1148798935



-- 
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-datafusion] Dandandan commented on pull request #5980: Decimal multiply kernel should not cause precision loss

Posted by "Dandandan (via GitHub)" <gi...@apache.org>.
Dandandan commented on PR #5980:
URL: https://github.com/apache/arrow-datafusion/pull/5980#issuecomment-1514299232

   Let's wait ~24hrs so other reviewers can have a chance.


-- 
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-datafusion] viirya commented on pull request #5980: Decimal multiply kernel should not cause precision loss

Posted by "viirya (via GitHub)" <gi...@apache.org>.
viirya commented on PR #5980:
URL: https://github.com/apache/arrow-datafusion/pull/5980#issuecomment-1512622096

   > I wonder if this already fixes #4024
   
   Yea, just verified locally that this can pass `verify_q6`.


-- 
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-datafusion] viirya commented on a diff in pull request #5980: Decimal multiply kernel should not cause precision loss

Posted by "viirya (via GitHub)" <gi...@apache.org>.
viirya commented on code in PR #5980:
URL: https://github.com/apache/arrow-datafusion/pull/5980#discussion_r1169177870


##########
datafusion/core/tests/sqllogictests/test_files/tpch.slt:
##########
@@ -118,14 +118,18 @@ CREATE EXTERNAL TABLE IF NOT EXISTS supplier (
 
 
 # q1
+# The decimal calculation `sum(l_extendedprice * (1 - l_discount) * (1 + l_tax)) as sum_charge` actually causes
+# overflow on decimal multiplication. Because the kernel DataFusion uses doesn't allow precision loss, we need
+# a cast to decimal(12,2) to avoid the overflow. We can get rid of it once we can use new multiply kernel from
+# arrow-rs which allows precision-loss.
 query TTRRRRRRRI
 select
     l_returnflag,
     l_linestatus,
     sum(l_quantity) as sum_qty,
     sum(l_extendedprice) as sum_base_price,
     sum(l_extendedprice * (1 - l_discount)) as sum_disc_price,
-    sum(l_extendedprice * (1 - l_discount) * (1 + l_tax)) as sum_charge,
+    sum(cast(l_extendedprice as decimal(12,2)) * (1 - l_discount) * (1 + l_tax)) as sum_charge,

Review Comment:
   See https://github.com/apache/arrow-datafusion/pull/5675/files#r1148798281



-- 
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-datafusion] Dandandan merged pull request #5980: Decimal multiply kernel should not cause precision loss

Posted by "Dandandan (via GitHub)" <gi...@apache.org>.
Dandandan merged PR #5980:
URL: https://github.com/apache/arrow-datafusion/pull/5980


-- 
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-datafusion] viirya commented on a diff in pull request #5980: Decimal multiply kernel should not cause precision loss

Posted by "viirya (via GitHub)" <gi...@apache.org>.
viirya commented on code in PR #5980:
URL: https://github.com/apache/arrow-datafusion/pull/5980#discussion_r1169216170


##########
benchmarks/queries/q8.sql:
##########
@@ -1,9 +1,9 @@
 select
     o_year,
-    sum(case
-            when nation = 'BRAZIL' then volume
-            else 0
-        end) / sum(volume) as mkt_share
+    cast(cast(sum(case
+                      when nation = 'BRAZIL' then volume
+                      else 0
+        end) as decimal(12,2)) / cast(sum(volume) as decimal(12,2)) as decimal(15,2)) as mkt_share

Review Comment:
   See https://github.com/apache/arrow-datafusion/pull/5675/files#r1152896889



-- 
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-datafusion] viirya commented on pull request #5980: Decimal multiply kernel should not cause precision loss

Posted by "viirya (via GitHub)" <gi...@apache.org>.
viirya commented on PR #5980:
URL: https://github.com/apache/arrow-datafusion/pull/5980#issuecomment-1513592308

   Thanks @Dandandan 


-- 
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-datafusion] viirya commented on pull request #5980: Decimal multiply kernel should not cause precision loss

Posted by "viirya (via GitHub)" <gi...@apache.org>.
viirya commented on PR #5980:
URL: https://github.com/apache/arrow-datafusion/pull/5980#issuecomment-1505857277

   Different to #5675, this doesn't add new expression node `PromotePrecision` and defers decimal type coercion to the phase of math expression evaluation.


-- 
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