Skip to content

Commit

Permalink
support decimal arithmetic precision loss mode
Browse files Browse the repository at this point in the history
  • Loading branch information
jinchengchenghh committed Sep 11, 2024
1 parent 7e4d626 commit 0dc20f6
Show file tree
Hide file tree
Showing 5 changed files with 251 additions and 42 deletions.
30 changes: 29 additions & 1 deletion velox/docs/functions/spark/decimal.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ https://cwiki.apache.org/confluence/download/attachments/27362075/Hive_Decimal_P

https://msdn.microsoft.com/en-us/library/ms190476.aspx

For decimal addition, subtraction, multiplication, the precision and scale computation logic is same.

Addition and Subtraction
------------------------

Expand All @@ -27,14 +29,26 @@ Multiplication

Division
--------
When allowing precision loss:

::

p = p1 - s1 + s2 + max(6, s1 + p2 + 1)
s = max(6, s1 + p2 + 1)

When not allowing precision loss:
::

wholeDigits = min(38, p1 - s1 + s2);
fractionalDigits = min(38, max(6, s1 + p2 + 1));
p = wholeDigits + fractionalDigits
s = fractionalDigits

Decimal Precision and Scale Adjustment
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

For above arithmetic operators, when the precision of result exceeds 38,
caps p at 38 and reduces the scale, in order to prevent the truncation of
caps p at 38 and reduces the scale when allowing precision loss, in order to prevent the truncation of
the integer part of the decimals. Below formula illustrates how the result
precision and scale are adjusted.

Expand All @@ -43,6 +57,20 @@ precision and scale are adjusted.
precision = 38
scale = max(38 - (p - s), min(s, 6))

Caps p at 38 and the scale should not be reduced when not allowing precision loss.
For decimal addition, subtraction, multiplication, the adjustment logic is same.

::
precision = 38
scale = min(38, s)

But for decimal division, it is different as following:

::

precision = 38
scale = fractionalDigits - (wholeDigits + fractionalDigits - 38) / 2 - 1

Users experience runtime errors when the actual result cannot be represented
with the calculated decimal type.

Expand Down
Loading

0 comments on commit 0dc20f6

Please sign in to comment.