diff --git common/src/java/org/apache/hadoop/hive/common/type/Decimal128.java common/src/java/org/apache/hadoop/hive/common/type/Decimal128.java index 9fe3dcf..d4cc32d 100644 --- common/src/java/org/apache/hadoop/hive/common/type/Decimal128.java +++ common/src/java/org/apache/hadoop/hive/common/type/Decimal128.java @@ -261,7 +261,9 @@ public boolean isZero() { * object to copy from */ public Decimal128 update(Decimal128 o) { - update(o, o.scale); + this.unscaledValue.update(o.unscaledValue); + this.scale = o.scale; + this.signum = o.signum; return this; } @@ -272,9 +274,8 @@ public Decimal128 update(Decimal128 o) { * object to copy from */ public Decimal128 update(Decimal128 o, short scale) { - this.unscaledValue.update(o.unscaledValue); - this.scale = scale; - this.signum = o.signum; + update(o); + this.changeScaleDestructive(scale); return this; } diff --git common/src/test/org/apache/hadoop/hive/common/type/TestDecimal128.java common/src/test/org/apache/hadoop/hive/common/type/TestDecimal128.java index a4bac33..46236a5 100644 --- common/src/test/org/apache/hadoop/hive/common/type/TestDecimal128.java +++ common/src/test/org/apache/hadoop/hive/common/type/TestDecimal128.java @@ -859,4 +859,12 @@ public void testToHiveDecimalString() { assertEquals(hd8.toString(), d13.getHiveDecimalString()); assertEquals("1234123000", d13.getHiveDecimalString()); } + + @Test + public void testUpdateWithScale() { + Decimal128 d1 = new Decimal128(1234.123, (short)4); + Decimal128 d2 = new Decimal128(0, (short)3); + d2.update(d1, (short)3); + assertEquals(0, d1.compareTo(d2)); + } } diff --git ql/src/java/org/apache/hadoop/hive/ql/exec/vector/expressions/MathExpr.java ql/src/java/org/apache/hadoop/hive/ql/exec/vector/expressions/MathExpr.java index ca26a09..74e3de3 100644 --- ql/src/java/org/apache/hadoop/hive/ql/exec/vector/expressions/MathExpr.java +++ ql/src/java/org/apache/hadoop/hive/ql/exec/vector/expressions/MathExpr.java @@ -46,11 +46,23 @@ public static long abs(long v) { } public static double sign(double v) { - return v >= 0 ? 1.0 : -1.0; + if (v == 0) { + return 0; + } else if (v > 0) { + return 1.0; + } else { + return -1.0; + } } public static double sign(long v) { - return v >= 0 ? 1.0 : -1.0; + if (v == 0) { + return 0; + } else if (v > 0) { + return 1.0; + } else { + return -1.0; + } } // for casting integral types to boolean diff --git ql/src/java/org/apache/hadoop/hive/ql/exec/vector/expressions/aggregates/VectorUDAFSumDecimal.java ql/src/java/org/apache/hadoop/hive/ql/exec/vector/expressions/aggregates/VectorUDAFSumDecimal.java index 0089ad3..efc23f8 100644 --- ql/src/java/org/apache/hadoop/hive/ql/exec/vector/expressions/aggregates/VectorUDAFSumDecimal.java +++ ql/src/java/org/apache/hadoop/hive/ql/exec/vector/expressions/aggregates/VectorUDAFSumDecimal.java @@ -303,15 +303,15 @@ public void aggregateInput(AggregationBuffer agg, VectorizedRowBatch batch) Decimal128[] vector = inputVector.vector; if (inputVector.isRepeating) { - if (inputVector.noNulls) { - if (myagg.isNull) { - myagg.isNull = false; - myagg.sum.zeroClear(); + if ((inputVector.noNulls) || !inputVector.isNull[0]) { + if (myagg.isNull) { + myagg.isNull = false; + myagg.sum.zeroClear(); + } + scratchDecimal.update(batchSize); + scratchDecimal.multiplyDestructive(vector[0], inputVector.scale); + myagg.sum.addDestructive(scratchDecimal, inputVector.scale); } - scratchDecimal.update(batchSize); - scratchDecimal.multiplyDestructive(vector[0], inputVector.scale); - myagg.sum.update(scratchDecimal); - } return; }