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

[GitHub] [arrow-datafusion] mingmwang opened a new pull request, #6331: refine decimal multiply, avoid cast to wider type

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

   # 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 #6278, bring the performance back.
   
   # Rationale for this change
   
   For decimal multiply,  avoid casting to the wider type, decimals can multiply decimals directly.
   
   
   <!--
    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] mingmwang commented on pull request #6331: refine decimal multiply, avoid cast to wider type

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

   
   ```spark  
   override def resultDecimalType(p1: Int, s1: Int, p2: Int, s2: Int): DecimalType = {
       val resultScale = s1 + s2
       val resultPrecision = p1 + p2 + 1
       if (allowPrecisionLoss) {
         DecimalType.adjustPrecisionScale(resultPrecision, resultScale)
       } else {
         DecimalType.bounded(resultPrecision, resultScale)
       }
     }
   
     private lazy val numeric = TypeUtils.getNumeric(dataType, failOnError)
   
     protected override def nullSafeEval(input1: Any, input2: Any): Any = dataType match {
       case DecimalType.Fixed(precision, scale) =>
         checkDecimalOverflow(numeric.times(input1, input2).asInstanceOf[Decimal], precision, scale)
       case _: IntegerType if failOnError =>
         MathUtils.multiplyExact(
           input1.asInstanceOf[Int],
           input2.asInstanceOf[Int],
           getContextOrNull())
       case _: LongType if failOnError =>
         MathUtils.multiplyExact(
           input1.asInstanceOf[Long],
           input2.asInstanceOf[Long],
           getContextOrNull())
       case _ => numeric.times(input1, input2)
     }
   ```


-- 
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 #6331: refine decimal multiply, avoid cast to wider type

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


##########
datafusion/physical-expr/src/expressions/binary/kernels_arrow.rs:
##########
@@ -703,6 +685,8 @@ pub(crate) fn modulus_decimal_dyn_scalar(
 #[cfg(test)]
 mod tests {
     use super::*;
+    use datafusion_expr::type_coercion::binary::decimal_op_mathematics_type;
+    use datafusion_expr::Operator;

Review Comment:
   Oh, I see. It is not used at the top.



-- 
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 #6331: refine decimal multiply, avoid cast to wider type

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


##########
datafusion/expr/src/type_coercion/binary.rs:
##########
@@ -514,7 +514,7 @@ pub fn coercion_decimal_mathematics_type(
                 left_decimal_type,
                 right_decimal_type,
             ),
-            Operator::Multiply | Operator::Divide | Operator::Modulo => {
+            Operator::Divide | Operator::Modulo => {
                 get_wider_decimal_type(left_decimal_type, right_decimal_type)
             }

Review Comment:
   Hm, I remember this follows Spark decimal coercion rule to cast lhs/rhs to wider decimal type. I don't see Spark does this now for multiply.
   
   Yea, you should be correct. We don't need to cast lhs/rhs to wider decimal type for multiply.
   
   It's too late. Let me think it again tomorrow morning. 😄 



-- 
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] mingmwang commented on pull request #6331: refine decimal multiply, avoid cast to wider type

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

   And existing UTs show the result is correct.
   
   
   ```
   query T
   select arrow_typeof(c1*c5) from decimal_simple limit 1;
   ----
   Decimal128(23, 13)
   
   
   query R rowsort
   select c1*c5 from decimal_simple;
   ----
   0.00000000014
   0.00000000033
   0.00000000038
   0.0000000005
   0.00000000096
   0.00000000105
   0.0000000016
   0.0000000016
   0.00000000165
   0.00000000176
   0.00000000176
   0.0000000026
   0.0000000034
   0.0000000039
   0.000000005
   
   ```


-- 
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 #6331: refine decimal multiply, avoid cast to wider type

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


##########
datafusion/physical-expr/src/expressions/binary/kernels_arrow.rs:
##########
@@ -703,6 +685,8 @@ pub(crate) fn modulus_decimal_dyn_scalar(
 #[cfg(test)]
 mod tests {
     use super::*;
+    use datafusion_expr::type_coercion::binary::decimal_op_mathematics_type;
+    use datafusion_expr::Operator;

Review Comment:
   Do you change anything in this `tests` module? Why add these?



-- 
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] mingmwang commented on a diff in pull request #6331: refine decimal multiply, avoid cast to wider type

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


##########
datafusion/expr/src/type_coercion/binary.rs:
##########
@@ -514,7 +514,7 @@ pub fn coercion_decimal_mathematics_type(
                 left_decimal_type,
                 right_decimal_type,
             ),
-            Operator::Multiply | Operator::Divide | Operator::Modulo => {
+            Operator::Divide | Operator::Modulo => {
                 get_wider_decimal_type(left_decimal_type, right_decimal_type)
             }

Review Comment:
   My understanding is it just need to multiply the internal i128 integers, and cast the result back to the required result 
   precision and scale.
   
   



##########
datafusion/expr/src/type_coercion/binary.rs:
##########
@@ -514,7 +514,7 @@ pub fn coercion_decimal_mathematics_type(
                 left_decimal_type,
                 right_decimal_type,
             ),
-            Operator::Multiply | Operator::Divide | Operator::Modulo => {
+            Operator::Divide | Operator::Modulo => {
                 get_wider_decimal_type(left_decimal_type, right_decimal_type)
             }

Review Comment:
   My understanding is it just need to multiply the internal i128 integers, and cast the result back to the required result precision and scale.
   
   



-- 
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] mingmwang commented on pull request #6331: refine decimal multiply, avoid cast to wider type

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

   ```scala  
   override def checkInputDataTypes(): TypeCheckResult = (left.dataType, right.dataType) match {
       case (l: DecimalType, r: DecimalType) if inputType.acceptsType(l) && inputType.acceptsType(r) =>
         // We allow decimal type inputs with different precision and scale, and use special formulas
         // to calculate the result precision and scale.
         TypeCheckResult.TypeCheckSuccess
       case _ => super.checkInputDataTypes()
     }
   ```
   


-- 
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 #6331: refine decimal multiply, avoid cast to wider type

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


##########
datafusion/expr/src/type_coercion/binary.rs:
##########
@@ -514,7 +514,7 @@ pub fn coercion_decimal_mathematics_type(
                 left_decimal_type,
                 right_decimal_type,
             ),
-            Operator::Multiply | Operator::Divide | Operator::Modulo => {
+            Operator::Divide | Operator::Modulo => {
                 get_wider_decimal_type(left_decimal_type, right_decimal_type)
             }

Review Comment:
   Huh? This is not correct...The rule to coerce lhs and rhs of decimal mathematic operations is well designed.



-- 
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 #6331: refine decimal multiply, avoid cast to wider type

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


##########
datafusion/expr/src/type_coercion/binary.rs:
##########
@@ -514,7 +514,7 @@ pub fn coercion_decimal_mathematics_type(
                 left_decimal_type,
                 right_decimal_type,
             ),
-            Operator::Multiply | Operator::Divide | Operator::Modulo => {
+            Operator::Divide | Operator::Modulo => {
                 get_wider_decimal_type(left_decimal_type, right_decimal_type)
             }

Review Comment:
   How do you multiply two decimals with different precision and scale? Don't forget that this is not a decimal object like JVM. The decimals are represented as i128 integers.



-- 
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 #6331: refine decimal multiply, avoid cast to wider type

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


##########
datafusion/expr/src/type_coercion/binary.rs:
##########
@@ -514,7 +514,7 @@ pub fn coercion_decimal_mathematics_type(
                 left_decimal_type,
                 right_decimal_type,
             ),
-            Operator::Multiply | Operator::Divide | Operator::Modulo => {
+            Operator::Divide | Operator::Modulo => {
                 get_wider_decimal_type(left_decimal_type, right_decimal_type)
             }

Review Comment:
   Rethink about it. The issue is not to follow Spark or not in this. But for multiply, we don't need to align two integers.



-- 
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] mingmwang commented on a diff in pull request #6331: refine decimal multiply, avoid cast to wider type

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


##########
datafusion/expr/src/type_coercion/binary.rs:
##########
@@ -514,7 +514,7 @@ pub fn coercion_decimal_mathematics_type(
                 left_decimal_type,
                 right_decimal_type,
             ),
-            Operator::Multiply | Operator::Divide | Operator::Modulo => {
+            Operator::Divide | Operator::Modulo => {
                 get_wider_decimal_type(left_decimal_type, right_decimal_type)
             }

Review Comment:
   Why decimal multiply decimal need to cast to a common wider type ? I do not change the get result type logic.
   I think in the latest SparkSQL,  for decimal multiply, they do not cast to common wider type either.



-- 
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 #6331: refine decimal multiply, avoid cast to wider type

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

   FYI, I'm going to improve the i256 operations at the upstream: https://github.com/apache/arrow-rs/pull/4303.


-- 
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] mingmwang commented on pull request #6331: refine decimal multiply, avoid cast to wider type

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

   > Good catch. This fixed the tpch-q1 performance. But for the root cause, we still need to improve the i256 operations. Otherwise for queries containing decimal array to array multiplying, it still suffers performance issue.
   
   Yes, you are right. We still need to address the i256 operations performance, otherwise when it runs into the related logic, the performance is still not good.


-- 
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] andygrove merged pull request #6331: refine decimal multiply, avoid cast to wider type

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


-- 
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 #6331: refine decimal multiply, avoid cast to wider type

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


##########
datafusion/expr/src/type_coercion/binary.rs:
##########
@@ -514,7 +514,7 @@ pub fn coercion_decimal_mathematics_type(
                 left_decimal_type,
                 right_decimal_type,
             ),
-            Operator::Multiply | Operator::Divide | Operator::Modulo => {
+            Operator::Divide | Operator::Modulo => {
                 get_wider_decimal_type(left_decimal_type, right_decimal_type)
             }

Review Comment:
   Huh? This is not correct...



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