diff --git common/src/java/org/apache/hadoop/hive/common/type/HiveDecimal.java common/src/java/org/apache/hadoop/hive/common/type/HiveDecimal.java index bdf6b0c..3b0b300 100644 --- common/src/java/org/apache/hadoop/hive/common/type/HiveDecimal.java +++ 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 common/src/test/org/apache/hadoop/hive/common/type/TestHiveDecimal.java common/src/test/org/apache/hadoop/hive/common/type/TestHiveDecimal.java new file mode 100644 index 0000000..da6d193 --- /dev/null +++ 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); + } + } +} diff --git ql/src/test/org/apache/hadoop/hive/ql/io/orc/TestOrcFile.java ql/src/test/org/apache/hadoop/hive/ql/io/orc/TestOrcFile.java index 5af6bf7..07b2c91 100644 --- ql/src/test/org/apache/hadoop/hive/ql/io/orc/TestOrcFile.java +++ ql/src/test/org/apache/hadoop/hive/ql/io/orc/TestOrcFile.java @@ -659,7 +659,7 @@ public void testUnionAndTimestamp() throws Exception { assertEquals(303, stats.getNumberOfValues()); assertEquals(new HiveDecimal("-5643.234"), stats.getMinimum()); assertEquals(maxValue, stats.getMaximum()); - assertEquals(null, stats.getSum()); + assertEquals(new HiveDecimal("1125034550468522965760747751134275941.2"), stats.getSum()); int stripeCount = 0; int rowCount = 0; long currentOffset = -1; diff --git ql/src/test/results/clientpositive/decimal_precision.q.out ql/src/test/results/clientpositive/decimal_precision.q.out index cf392ec..c2ead36 100644 --- ql/src/test/results/clientpositive/decimal_precision.q.out +++ ql/src/test/results/clientpositive/decimal_precision.q.out @@ -453,45 +453,45 @@ NULL NULL -99999999999999999999999999999999999999 NULL -999999999999999999999999999999999999 NULL -99999999999999999999999999999999999 NULL --0.0000000000000000000000000000000000001 NULL +-0.0000000000000000000000000000000000001 0 0 0 -0.0000000000000000000000000000000000001 NULL +0.0000000000000000000000000000000000001 0 0.123456789012345 0.015241578753238669120562399025 -0.1234567890123456789012345678901234578 NULL +0.1234567890123456789012345678901234578 0.0152415787532388367504953515625666822 1.234567890123456 1.524157875323881726870921383936 -1.2345678901234567890123456789012345678 NULL +1.2345678901234567890123456789012345678 1.5241578753238836750495351562566681943 12.34567890123456 152.4157875323881726870921383936 -12.345678901234567890123456789012345678 NULL +12.345678901234567890123456789012345678 152.41578753238836750495351562566681943 123.4567890123456 15241.57875323881726870921383936 -123.45678901234567890123456789012345678 NULL +123.45678901234567890123456789012345678 15241.578753238836750495351562566681943 1234.567890123456 1524157.875323881726870921383936 -1234.5678901234567890123456789012345678 NULL +1234.5678901234567890123456789012345678 1524157.8753238836750495351562566681943 12345.67890123456 152415787.5323881726870921383936 -12345.678901234567890123456789012345678 NULL +12345.678901234567890123456789012345678 152415787.53238836750495351562566681943 123456.7890123456 15241578753.23881726870921383936 -123456.78901234567890123456789012345678 NULL +123456.78901234567890123456789012345678 15241578753.238836750495351562566681943 1234567.890123456 1524157875323.881726870921383936 -1234567.8901234567890123456789012345678 NULL +1234567.8901234567890123456789012345678 1524157875323.8836750495351562566681943 12345678.90123456 152415787532388.1726870921383936 -12345678.901234567890123456789012345678 NULL +12345678.901234567890123456789012345678 152415787532388.36750495351562566681943 123456789.0123456 15241578753238817.26870921383936 -123456789.01234567890123456789012345678 NULL +123456789.01234567890123456789012345678 15241578753238836.750495351562566681943 1234567890.123456 1524157875323881726.870921383936 -1234567890.1234567890123456789012345678 NULL +1234567890.1234567890123456789012345678 1524157875323883675.0495351562566681943 12345678901.23456 152415787532388172687.0921383936 -12345678901.234567890123456789012345678 NULL +12345678901.234567890123456789012345678 152415787532388367504.95351562566681943 123456789012.3456 15241578753238817268709.21383936 -123456789012.34567890123456789012345678 NULL +123456789012.34567890123456789012345678 15241578753238836750495.351562566681943 1234567890123.456 1524157875323881726870921.383936 -1234567890123.4567890123456789012345678 NULL +1234567890123.4567890123456789012345678 1524157875323883675049535.1562566681943 12345678901234.56 152415787532388172687092138.3936 -12345678901234.567890123456789012345678 NULL +12345678901234.567890123456789012345678 152415787532388367504953515.62566681943 123456789012345.6 15241578753238817268709213839.36 -123456789012345.67890123456789012345678 NULL -1234567890123456.7890123456789012345678 NULL -12345678901234567.890123456789012345678 NULL -123456789012345678.90123456789012345678 NULL -1234567890123456789.0123456789012345678 NULL +123456789012345.67890123456789012345678 15241578753238836750495351562.566681943 +1234567890123456.7890123456789012345678 1524157875323883675049535156256.6681943 +12345678901234567.890123456789012345678 152415787532388367504953515625666.81943 +123456789012345678.90123456789012345678 15241578753238836750495351562566681.943 +1234567890123456789.0123456789012345678 1524157875323883675049535156256668194.3 12345678901234567890.123456789012345678 NULL 123456789012345678901.23456789012345678 NULL 1234567890123456789012.3456789012345678 NULL diff --git ql/src/test/results/clientpositive/decimal_udf.q.out ql/src/test/results/clientpositive/decimal_udf.q.out index 2627b71..fa5a2c2 100644 --- ql/src/test/results/clientpositive/decimal_udf.q.out +++ ql/src/test/results/clientpositive/decimal_udf.q.out @@ -751,7 +751,7 @@ NULL 9.8596 9.8596 9.8596 -NULL +0.9999999999999999999999998 1524157875323883675.019051998750190521 1524157875323883652.7968299765279684 PREHOOK: query: EXPLAIN SELECT key * value FROM DECIMAL_UDF @@ -2110,7 +2110,7 @@ NULL 9.8596 9.8596 9.8596 -NULL +0.9999999999999999999999998 1524157875323883675.019051998750190521 1524157875323883652.7968299765279684 PREHOOK: query: -- modulo