kazuyukitanimura opened a new pull request, #43678:
URL: https://github.com/apache/spark/pull/43678

   ### What changes were proposed in this pull request?
   This PR fixes inaccurate Decimal multiplication and division results.
   
   
   ### Why are the changes needed?
   Decimal multiplication and division results may be inaccurate due to 
rounding issues.
   #### Multiplication:
   ```
   scala> sql("select  -14120025096157587712113961295153.858047 * 
-0.4652").show(truncate=false)
   +----------------------------------------------------+                       
   
   |(-14120025096157587712113961295153.858047 * -0.4652)|
   +----------------------------------------------------+
   |6568635674732509803675414794505.574764              |
   +----------------------------------------------------+
   ```
   The correct answer is `6568635674732509803675414794505.574763`
   
   Please note that the last digit is `3` instead of `4` as
   
   ```
   scala> 
java.math.BigDecimal("-14120025096157587712113961295153.858047").multiply(java.math.BigDecimal("-0.4652"))
   val res21: java.math.BigDecimal = 6568635674732509803675414794505.5747634644
   ```
   Since the factional part `.574763` is followed by `4644`, it should not be 
rounded up.
   
   #### Division:
   ```
   scala> sql("select -0.172787979 / 
533704665545018957788294905796.5").show(truncate=false)
   +-------------------------------------------------+
   |(-0.172787979 / 533704665545018957788294905796.5)|
   +-------------------------------------------------+
   |-3.237521E-31                                    |
   +-------------------------------------------------+
   ```
   The correct answer is `-3.237520E-31`
   
   Please note that the last digit is `0` instead of `1` as
   
   ```
   scala> 
java.math.BigDecimal("-0.172787979").divide(java.math.BigDecimal("533704665545018957788294905796.5"),
 100, java.math.RoundingMode.DOWN)
   val res22: java.math.BigDecimal = 
-3.237520489418037889998826491401059986665344697406144511563561222578738E-31
   ```
   Since the factional part `.237520` is followed by `4894...`, it should not 
be rounded up.
   
   
   ### Does this PR introduce _any_ user-facing change?
   Yes, users will see correct Decimal multiplication and division results.
   Directly multiplying and dividing with 
`org.apache.spark.sql.types.Decimal()` (not via SQL) will return 39 digit at 
maximum instead of 38 at maximum and round down instead of round half-up
   
   
   ### How was this patch tested?
   Test added
   
   
   ### Was this patch authored or co-authored using generative AI tooling?
   No
   


-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to