DRILL-1216: Fix decimal multiplication when precision can exceed 38
Project: http://git-wip-us.apache.org/repos/asf/incubator-drill/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-drill/commit/66c055ee Tree: http://git-wip-us.apache.org/repos/asf/incubator-drill/tree/66c055ee Diff: http://git-wip-us.apache.org/repos/asf/incubator-drill/diff/66c055ee Branch: refs/heads/master Commit: 66c055ee5ed2d000b7bd6997875cad18616bccb6 Parents: 5873442 Author: Mehant Baid <[email protected]> Authored: Wed Jul 30 16:43:14 2014 -0700 Committer: Jacques Nadeau <[email protected]> Committed: Wed Aug 6 16:44:21 2014 -0700 ---------------------------------------------------------------------- .../templates/Decimal/DecimalFunctions.java | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/66c055ee/exec/java-exec/src/main/codegen/templates/Decimal/DecimalFunctions.java ---------------------------------------------------------------------- diff --git a/exec/java-exec/src/main/codegen/templates/Decimal/DecimalFunctions.java b/exec/java-exec/src/main/codegen/templates/Decimal/DecimalFunctions.java index 671d15b..5278d9c 100644 --- a/exec/java-exec/src/main/codegen/templates/Decimal/DecimalFunctions.java +++ b/exec/java-exec/src/main/codegen/templates/Decimal/DecimalFunctions.java @@ -446,10 +446,6 @@ public class ${type.name}Functions { */ int lastScaleIndex = currentIndex + resultIntegerSize + resultScaleSize - 1; - // Compute the power of 10 necessary to chop of the fractional part - int scaleFactor = (int) (org.apache.drill.common.util.DecimalUtility.getPowerOfTen( - org.apache.drill.common.util.DecimalUtility.MAX_DIGITS - (result.scale % org.apache.drill.common.util.DecimalUtility.MAX_DIGITS))); - // compute the power of 10 necessary to find if we need to round up int roundFactor = (int) (org.apache.drill.common.util.DecimalUtility.getPowerOfTen( org.apache.drill.common.util.DecimalUtility.MAX_DIGITS - ((result.scale + 1) % org.apache.drill.common.util.DecimalUtility.MAX_DIGITS))); @@ -460,12 +456,18 @@ public class ${type.name}Functions { // Check the first chopped digit to see if we need to round up int carry = ((tempResult[roundIndex] / roundFactor) % 10) > 4 ? 1 : 0; - // Adjust the carry so that it gets added to the correct digit - carry *= scaleFactor; + if (result.scale > 0) { + + // Compute the power of 10 necessary to chop of the fractional part + int scaleFactor = (int) (org.apache.drill.common.util.DecimalUtility.getPowerOfTen( + org.apache.drill.common.util.DecimalUtility.MAX_DIGITS - (result.scale % org.apache.drill.common.util.DecimalUtility.MAX_DIGITS))); + // Chop the unwanted fractional part + tempResult[lastScaleIndex] /= scaleFactor; + tempResult[lastScaleIndex] *= scaleFactor; - // Chop the unwanted fractional part - tempResult[lastScaleIndex] /= scaleFactor; - tempResult[lastScaleIndex] *= scaleFactor; + // Adjust the carry so that it gets added to the correct digit + carry *= scaleFactor; + } // propogate the carry while (carry > 0 && lastScaleIndex >= 0) {
