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 a5d7399..1d9efd8 100644 --- common/src/java/org/apache/hadoop/hive/common/type/Decimal128.java +++ common/src/java/org/apache/hadoop/hive/common/type/Decimal128.java @@ -1576,28 +1576,24 @@ public int hashCode() { */ @Override public long longValue() { + + // Avoid allocating temporary variables for special cases: signum or scale is zero if (signum == 0) { return 0L; } - - long ret; - UnsignedInt128 tmp; if (scale == 0) { + long ret; ret = this.unscaledValue.getV1(); ret <<= 32L; ret |= SqlMathUtil.LONG_MASK & this.unscaledValue.getV0(); + if (signum >= 0) { + return ret; + } else { + return -ret; + } } else { - tmp = new UnsignedInt128(this.unscaledValue); - tmp.scaleDownTenDestructive(scale); - ret = tmp.getV1(); - ret <<= 32L; - ret |= SqlMathUtil.LONG_MASK & tmp.getV0(); - } - - if (signum >= 0) { - return ret; - } else { - return -ret; + HiveDecimal hd = HiveDecimal.create(this.toBigDecimal()); + return hd.longValue(); } } 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 426c03d..91bb8b5 100644 --- common/src/test/org/apache/hadoop/hive/common/type/TestDecimal128.java +++ common/src/test/org/apache/hadoop/hive/common/type/TestDecimal128.java @@ -786,5 +786,11 @@ public void testToLong() { assertEquals(4294967295L, d.longValue()); d.update("4294967296.01", (short) 2); // 2^32 + .01 assertEquals(4294967296L, d.longValue()); + + // Compare long value with HiveDecimal#longValue + d.update(37.678, (short)5); + HiveDecimal hd = HiveDecimal.create(BigDecimal.valueOf(37.678)); + assertEquals(hd.longValue(), d.longValue()); } + }