kazuyukitanimura commented on PR #43705:
URL: https://github.com/apache/spark/pull/43705#issuecomment-1800346639

   Thanks @jcdang 
   > @kazuyukitanimura would you get the result you want if your forced the 
result as an explicit cast to the default DecimalType `decimal(38, 18)`?
   
   Did you mean like `Cast(x * y as Decimal(38, 18)I`? It may not give a 
correct result depending on the data. The underlying issue is that there are 
two places that do rounding. MathContext and TypeCoercion. Let's say
   `x * y = 7890123456789012345.678901234567890123456` (40 digits)
   First `x.multiply(y, new MathContext(38, HALF_UP)) will round it to 
`7890123456789012345.6789012345678901235` (38 dibits). Please note the last 
digits `45` are rounded to `5[0]`.
   Next casting (or type coercion) to `decimal(38, 18)` will round it again to 
`7890123456789012345.678901234567890124` (18 fraction scale). Please note the 
last digits `35` are rounded to `4[0]`. This is not correct because 
`7890123456789012345.678901234567890123` (no round up) should be the right 
answer because the original number was followed by `456` that is less than 
`500`, there shouldn't be any rounding up. 
   
   This PR changes not to round with MathContext. Hopefully this clarifies. 
Some comments are here 
https://github.com/apache/spark/pull/43705/files#diff-87807d437248d04876eac9e116a527577f1a8e53e28337bf26423e5bf94630e1R567-R571


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