diff --git a/common/src/java/org/apache/hadoop/hive/common/type/HiveDecimal.java b/common/src/java/org/apache/hadoop/hive/common/type/HiveDecimal.java index bdf6b0c..3b0b300 100644 --- a/common/src/java/org/apache/hadoop/hive/common/type/HiveDecimal.java +++ b/common/src/java/org/apache/hadoop/hive/common/type/HiveDecimal.java @@ -146,11 +146,11 @@ public HiveDecimal setScale(int adjustedScale, int rm) { } public HiveDecimal subtract(HiveDecimal dec) { - return new HiveDecimal(bd.subtract(dec.bd)); + return new HiveDecimal(bd.subtract(dec.bd), true); } public HiveDecimal multiply(HiveDecimal dec) { - return new HiveDecimal(bd.multiply(dec.bd)); + return new HiveDecimal(bd.multiply(dec.bd), true); } public BigInteger unscaledValue() { @@ -170,15 +170,15 @@ public HiveDecimal negate() { } public HiveDecimal add(HiveDecimal dec) { - return new HiveDecimal(bd.add(dec.bd)); + return new HiveDecimal(bd.add(dec.bd), true); } public HiveDecimal pow(int n) { - return new HiveDecimal(bd.pow(n)); + return new HiveDecimal(bd.pow(n), true); } public HiveDecimal remainder(HiveDecimal dec) { - return new HiveDecimal(bd.remainder(dec.bd)); + return new HiveDecimal(bd.remainder(dec.bd), true); } public HiveDecimal divide(HiveDecimal dec) { diff --git a/common/src/test/org/apache/hadoop/hive/common/type/TestHiveDecimal.java b/common/src/test/org/apache/hadoop/hive/common/type/TestHiveDecimal.java new file mode 100644 index 0000000..da6d193 --- /dev/null +++ b/common/src/test/org/apache/hadoop/hive/common/type/TestHiveDecimal.java @@ -0,0 +1,48 @@ +package org.apache.hadoop.hive.common.type; + +import org.junit.Test; + +import java.math.BigDecimal; +import java.math.RoundingMode; + +public class TestHiveDecimal { + private HiveDecimal a = new HiveDecimal("4.53"); + private HiveDecimal b = new HiveDecimal("25.86"); + private HiveDecimal c = new HiveDecimal("0.087"); + + @Test + public void testDivideAndMultiply() { + // 4.53 / 25.86 * 0.087 + // It is an ordinal calculation, so it should not throw an exception. + a.divide(b).multiply(c); + } + + @Test + public void testDivideAndPow() { + // (4.53 / 25.86) ** 2 + // It is an ordinal calculation, so it should not throw an exception. + a.divide(b).pow(2); + } + + @Test + public void testDivideAndAdds() { + // (4.53 / 25.86) + (4.53 / 25.86) + ... + (4.53 / 25.86) + // It is an ordinal calculation, so it should not throw an exception. + HiveDecimal ab = a.divide(b); + HiveDecimal ab10 = ab; + for (int i = 0; i < 10; i++) { + ab10 = ab10.add(ab); + } + } + + @Test + public void testDivideAndSubtracts() { + // (4.53 / 25.86) - (4.53 / 25.86) - ... - (4.53 / 25.86) + // It is an ordinal calculation, so it should not throw an exception. + HiveDecimal ab = a.divide(b); + HiveDecimal ab10 = ab; + for (int i = 0; i < 10; i++) { + ab10 = ab10.subtract(ab); + } + } +}