-
Notifications
You must be signed in to change notification settings - Fork 933
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[BUG] HALF_UP and HALF_EVEN rounding can produce the wrong result for decimal128 #14210
Comments
Thanks @revans2 for raising this. Is it fair to say that
Do you think the unit tests you've proposed cover the root cause of all the error cases above? |
@GregoryKimball I am running tests right now to get a better idea. I have not exhaustively tested all combinations of decimal scale. I have seen it happen with scale -37 and scale -36. I will let you know when my tests are done if there are more that fail. |
I ran through a fairly large exhaustive set of tests for various scales at different rounding indexes. For HALF_UP the largest scale factor that I saw got a wrong answer was scale -21 rounding to 0.
For HALF_EVEN it was -22 rounding to 0
But I have a list of 743 values at different scales and roundings that produces the incorrect answers. I suspect it might have something to either do with overflow, or with the fact that a double is being used to calculate powers of 10. Line 226 in 53f0f74
Line 274 in 53f0f74
I had to write some custom code to do 256-bit decimal math, because Spark/Java will go over 128-bits for some operations and I ran into issues with pow being inaccurate, so I wrote a LUT, which was simple enough to cover all possible versions could run into. Not that you have to do the same. Just a point of reference. I'll try to clean up the log file I have with the 743 failures for reference |
out.tsv.gz has a tab separated values file for the failed test cases. |
I looked briefly at the rounding functors themselves but nothing jumped out at me. Based on the variation with precision (it seems that larger scale values work but -21 to -37 fail), my working hypothesis is that this is due to some stage of the process that isn't in "integral" math. There is a reference here to Line 274 in dea0df0
|
Describe the bug
I did some extra testing in Spark for half even rounding (bround) and half up rounding (round) in spark. Specifically for decimal values. Decimal128 values showed a number of differences vs the CPU at various levels.
In this we are rounding 1/id where id is a decimal with a scale of -37. The b columns are for half up and the r columns are the result for half even rounding.
Steps/Code to reproduce bug
I wrote some unit tests
But they don't cover the error cases from the previous test I did in Spark.
Expected behavior
We get the correct answer when rounding decimal128 numbers.
The text was updated successfully, but these errors were encountered: