You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hive.apache.org by om...@apache.org on 2017/05/26 17:32:09 UTC

[2/4] hive git commit: HIVE-15335: Fast Decimal (addendum)

HIVE-15335: Fast Decimal (addendum)


Project: http://git-wip-us.apache.org/repos/asf/hive/repo
Commit: http://git-wip-us.apache.org/repos/asf/hive/commit/c55ef22e
Tree: http://git-wip-us.apache.org/repos/asf/hive/tree/c55ef22e
Diff: http://git-wip-us.apache.org/repos/asf/hive/diff/c55ef22e

Branch: refs/heads/branch-2.2
Commit: c55ef22e5eb7868e9c4fe8d5977c6c5124e36ef9
Parents: 9f8141b
Author: Owen O'Malley <om...@apache.org>
Authored: Thu May 25 09:29:03 2017 -0700
Committer: Owen O'Malley <om...@apache.org>
Committed: Thu May 25 09:33:32 2017 -0700

----------------------------------------------------------------------
 .../orc/impl/ConvertTreeReaderFactory.java      | 50 ++------------------
 .../hadoop/hive/ql/util/TimestampUtils.java     | 36 ++++++--------
 2 files changed, 19 insertions(+), 67 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/hive/blob/c55ef22e/orc/src/java/org/apache/orc/impl/ConvertTreeReaderFactory.java
----------------------------------------------------------------------
diff --git a/orc/src/java/org/apache/orc/impl/ConvertTreeReaderFactory.java b/orc/src/java/org/apache/orc/impl/ConvertTreeReaderFactory.java
index a7c3380..2d293b5 100644
--- a/orc/src/java/org/apache/orc/impl/ConvertTreeReaderFactory.java
+++ b/orc/src/java/org/apache/orc/impl/ConvertTreeReaderFactory.java
@@ -613,53 +613,13 @@ public class ConvertTreeReaderFactory extends TreeReaderFactory {
 
     @Override
     public void setConvertVectorElement(int elementNum) throws IOException {
-      HiveDecimalWritable decWritable = decimalColVector.vector[elementNum];
-      long[] vector = longColVector.vector;
-      Category readerCategory = readerType.getCategory();
-
-      // Check to see if the decimal will fit in the Hive integer data type.
-      // If not, set the element to null.
-      boolean isInRange;
-      switch (readerCategory) {
-      case BOOLEAN:
-        // No data loss for boolean.
-        vector[elementNum] = decWritable.signum() == 0 ? 0 : 1;
-        return;
-      case BYTE:
-        isInRange = decWritable.isByte();
-        break;
-      case SHORT:
-        isInRange = decWritable.isShort();
-        break;
-      case INT:
-        isInRange = decWritable.isInt();
-        break;
-      case LONG:
-        isInRange = decWritable.isLong();
-        break;
-      default:
-        throw new RuntimeException("Unexpected type kind " + readerCategory.name());
-      }
-      if (!isInRange) {
+      HiveDecimal decimalValue = decimalColVector.vector[elementNum].getHiveDecimal();
+      if (decimalValue.compareTo(DECIMAL_MAX_LONG) > 0 ||
+          decimalValue.compareTo(DECIMAL_MIN_LONG) < 0) {
         longColVector.isNull[elementNum] = true;
         longColVector.noNulls = false;
-        return;
-      }
-      switch (readerCategory) {
-      case BYTE:
-        vector[elementNum] = decWritable.byteValue();
-        break;
-      case SHORT:
-        vector[elementNum] = decWritable.shortValue();
-        break;
-      case INT:
-        vector[elementNum] = decWritable.intValue();
-        break;
-      case LONG:
-        vector[elementNum] = decWritable.longValue();
-        break;
-      default:
-        throw new RuntimeException("Unexpected type kind " + readerCategory.name());
+      } else {
+        downCastAnyInteger(longColVector, elementNum, decimalValue.longValue(), readerType);
       }
     }
 

http://git-wip-us.apache.org/repos/asf/hive/blob/c55ef22e/storage-api/src/java/org/apache/hadoop/hive/ql/util/TimestampUtils.java
----------------------------------------------------------------------
diff --git a/storage-api/src/java/org/apache/hadoop/hive/ql/util/TimestampUtils.java b/storage-api/src/java/org/apache/hadoop/hive/ql/util/TimestampUtils.java
index dfc7272..c0e8a2e 100644
--- a/storage-api/src/java/org/apache/hadoop/hive/ql/util/TimestampUtils.java
+++ b/storage-api/src/java/org/apache/hadoop/hive/ql/util/TimestampUtils.java
@@ -76,32 +76,24 @@ public class TimestampUtils {
    * @param dec
    * @return
    */
-  public static Timestamp decimalToTimestamp(HiveDecimal dec) {
+  public static Timestamp decimalToTimestamp(HiveDecimal d) {
+    try {
+      BigDecimal nanoInstant = d.bigDecimalValue().multiply(BILLION_BIG_DECIMAL);
+      int nanos = nanoInstant.remainder(BILLION_BIG_DECIMAL).intValue();
+      if (nanos < 0) {
+        nanos += 1000000000;
+      }
+      long seconds =
+          nanoInstant.subtract(new BigDecimal(nanos)).divide(BILLION_BIG_DECIMAL).longValue();
+      Timestamp t = new Timestamp(seconds * 1000);
+      t.setNanos(nanos);
 
-    HiveDecimalWritable nanosWritable = new HiveDecimalWritable(dec);
-    nanosWritable.mutateFractionPortion();               // Clip off seconds portion.
-    nanosWritable.mutateScaleByPowerOfTen(9);            // Bring nanoseconds into integer portion.
-    if (!nanosWritable.isSet() || !nanosWritable.isInt()) {
+      return t;
+    } catch (NumberFormatException nfe) {
       return null;
-    }
-    int nanos = nanosWritable.intValue();
-    if (nanos < 0) {
-      nanos += 1000000000;
-    }
-    nanosWritable.setFromLong(nanos);
-
-    HiveDecimalWritable nanoInstant = new HiveDecimalWritable(dec);
-    nanoInstant.mutateScaleByPowerOfTen(9);
-
-    nanoInstant.mutateSubtract(nanosWritable);
-    nanoInstant.mutateScaleByPowerOfTen(-9);              // Back to seconds.
-    if (!nanoInstant.isSet() || !nanoInstant.isLong()) {
+    } catch (IllegalArgumentException iae) {
       return null;
     }
-    long seconds = nanoInstant.longValue();
-    Timestamp t = new Timestamp(seconds * 1000);
-    t.setNanos(nanos);
-    return t;
   }
 
   /**