drill-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From prog...@apache.org
Subject drill git commit: DRILL-5002: Using hive's date functions on top of date column gives wrong results for local time-zone
Date Wed, 20 Sep 2017 05:14:22 GMT
Repository: drill
Updated Branches:
  refs/heads/master b2744a2ab -> 4c99f0cdd


DRILL-5002: Using hive's date functions on top of date column gives wrong results for local
time-zone

closes #937


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

Branch: refs/heads/master
Commit: 4c99f0cdd98b1a0e789c5d11b353f88a2f6d54aa
Parents: b2744a2
Author: Vitalii Diravka <vitalii.diravka@gmail.com>
Authored: Mon Aug 21 20:47:13 2017 +0300
Committer: Paul Rogers <progers@maprtech.com>
Committed: Tue Sep 19 14:07:00 2017 -0700

----------------------------------------------------------------------
 .../templates/ObjectInspectorHelper.java        |  6 ++-
 .../codegen/templates/ObjectInspectors.java     | 20 +++++++--
 .../drill/exec/fn/hive/TestInbuiltHiveUDFs.java | 43 +++++++++++++++++++-
 3 files changed, 63 insertions(+), 6 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/drill/blob/4c99f0cd/contrib/storage-hive/core/src/main/codegen/templates/ObjectInspectorHelper.java
----------------------------------------------------------------------
diff --git a/contrib/storage-hive/core/src/main/codegen/templates/ObjectInspectorHelper.java
b/contrib/storage-hive/core/src/main/codegen/templates/ObjectInspectorHelper.java
index 5d14f81..0ee433b 100644
--- a/contrib/storage-hive/core/src/main/codegen/templates/ObjectInspectorHelper.java
+++ b/contrib/storage-hive/core/src/main/codegen/templates/ObjectInspectorHelper.java
@@ -204,7 +204,11 @@ public class ObjectInspectorHelper {
           <#elseif entry.hiveType == "TIMESTAMP">
             JVar tsVar = jc._else().decl(m.directClass(java.sql.Timestamp.class.getCanonicalName()),
"ts",
               castedOI.invoke("getPrimitiveJavaObject").arg(returnValue));
-            jc._else().assign(returnValueHolder.ref("value"), tsVar.invoke("getTime"));
+            // Bringing relative timestamp value without timezone info to timestamp value
in UTC, since Drill keeps date-time values in UTC
+            JVar localDateTimeVar = jc._else().decl(m.directClass(org.joda.time.LocalDateTime.class.getCanonicalName()),
"localDateTime",
+                JExpr._new(m.directClass(org.joda.time.LocalDateTime.class.getCanonicalName())).arg(tsVar));
+            jc._else().assign(returnValueHolder.ref("value"), localDateTimeVar.invoke("toDateTime")
+                .arg(m.directClass(org.joda.time.DateTimeZone.class.getCanonicalName()).staticRef("UTC")).invoke("getMillis"));
           <#elseif entry.hiveType == "DATE">
             JVar dVar = jc._else().decl(m.directClass(java.sql.Date.class.getCanonicalName()),
"d",
               castedOI.invoke("getPrimitiveJavaObject").arg(returnValue));

http://git-wip-us.apache.org/repos/asf/drill/blob/4c99f0cd/contrib/storage-hive/core/src/main/codegen/templates/ObjectInspectors.java
----------------------------------------------------------------------
diff --git a/contrib/storage-hive/core/src/main/codegen/templates/ObjectInspectors.java b/contrib/storage-hive/core/src/main/codegen/templates/ObjectInspectors.java
index ffd3a56..f79f4b1 100644
--- a/contrib/storage-hive/core/src/main/codegen/templates/ObjectInspectors.java
+++ b/contrib/storage-hive/core/src/main/codegen/templates/ObjectInspectors.java
@@ -213,7 +213,10 @@ public class Drill${entry.drillType}${entry.hiveOI} {
     <#else>
       final TimeStampHolder h = (TimeStampHolder) o;
     </#if>
-      return new java.sql.Timestamp(h.value);
+      org.joda.time.LocalDateTime dateTime = new org.joda.time.LocalDateTime(h.value, org.joda.time.DateTimeZone.UTC);
+      // use "toDate()" to get java.util.Date object with exactly the same fields as this
Joda date-time.
+      // See more in Javadoc for "LocalDateTime#toDate()"
+      return new java.sql.Timestamp(dateTime.toDate().getTime());
     }
 
     @Override
@@ -226,7 +229,10 @@ public class Drill${entry.drillType}${entry.hiveOI} {
     <#else>
       final TimeStampHolder h = (TimeStampHolder) o;
     </#if>
-      return new TimestampWritable(new java.sql.Timestamp(h.value));
+      org.joda.time.LocalDateTime dateTime = new org.joda.time.LocalDateTime(h.value, org.joda.time.DateTimeZone.UTC);
+      // use "toDate()" to get java.util.Date object with exactly the same fields as this
Joda date-time.
+      // See more in Javadoc for "LocalDateTime#toDate()"
+      return new TimestampWritable(new java.sql.Timestamp(dateTime.toDate().getTime()));
     }
 
 <#elseif entry.drillType == "Date">
@@ -240,7 +246,10 @@ public class Drill${entry.drillType}${entry.hiveOI} {
     <#else>
       final DateHolder h = (DateHolder) o;
     </#if>
-      return new java.sql.Date(h.value);
+      org.joda.time.LocalDate localDate = new org.joda.time.LocalDate(h.value, org.joda.time.DateTimeZone.UTC);
+      // Use "toDate()" to get java.util.Date object with exactly the same year the same
year, month and day as Joda date.
+      // See more in Javadoc for "LocalDate#toDate()"
+      return new java.sql.Date(localDate.toDate().getTime());
     }
 
     @Override
@@ -253,7 +262,10 @@ public class Drill${entry.drillType}${entry.hiveOI} {
     <#else>
       final DateHolder h = (DateHolder) o;
     </#if>
-      return new DateWritable(new java.sql.Date(h.value));
+      org.joda.time.LocalDate localDate = new org.joda.time.LocalDate(h.value, org.joda.time.DateTimeZone.UTC);
+      // Use "toDate()" to get java.util.Date object with exactly the same year the same
year, month and day as Joda date.
+      // See more in Javadoc for "LocalDate#toDate()"
+      return new DateWritable(new java.sql.Date(localDate.toDate().getTime()));
     }
 
 <#else>

http://git-wip-us.apache.org/repos/asf/drill/blob/4c99f0cd/contrib/storage-hive/core/src/test/java/org/apache/drill/exec/fn/hive/TestInbuiltHiveUDFs.java
----------------------------------------------------------------------
diff --git a/contrib/storage-hive/core/src/test/java/org/apache/drill/exec/fn/hive/TestInbuiltHiveUDFs.java
b/contrib/storage-hive/core/src/test/java/org/apache/drill/exec/fn/hive/TestInbuiltHiveUDFs.java
index 88200ec..1f00ab4 100644
--- a/contrib/storage-hive/core/src/test/java/org/apache/drill/exec/fn/hive/TestInbuiltHiveUDFs.java
+++ b/contrib/storage-hive/core/src/test/java/org/apache/drill/exec/fn/hive/TestInbuiltHiveUDFs.java
@@ -1,4 +1,4 @@
-/**
+/*
  * Licensed to the Apache Software Foundation (ASF) under one
  * or more contributor license agreements.  See the NOTICE file
  * distributed with this work for additional information
@@ -26,6 +26,7 @@ import org.apache.drill.common.types.TypeProtos;
 import org.apache.drill.exec.compile.ClassTransformer;
 import org.apache.drill.exec.hive.HiveTestBase;
 import org.apache.drill.exec.server.options.OptionValue;
+import org.joda.time.DateTime;
 import org.junit.Test;
 
 import java.util.List;
@@ -138,4 +139,44 @@ public class TestInbuiltHiveUDFs extends HiveTestBase {
     }
   }
 
+  @Test
+  public void testLastDay() throws Exception {
+    testBuilder()
+        .sqlQuery("select last_day(to_date('1994-02-01','yyyy-MM-dd')) as `LAST_DAY` from
(VALUES(1))")
+        .unOrdered()
+        .baselineColumns("LAST_DAY")
+        .baselineValues("1994-02-28")
+        .go();
+  }
+
+  @Test
+  public void testDatediff() throws Exception {
+    testBuilder()
+        .sqlQuery("select datediff(date '1996-03-01', timestamp '1997-02-10 17:32:00.0')
as `DATEDIFF` from (VALUES(1))")
+        .unOrdered()
+        .baselineColumns("DATEDIFF")
+        .baselineValues(-346)
+        .go();
+  }
+
+  @Test
+  public void testFromUTCTimestamp() throws Exception {
+    testBuilder()
+        .sqlQuery("select from_utc_timestamp('1970-01-01 08:00:00','PST') as PST_TIMESTAMP
from (VALUES(1))")
+        .unOrdered()
+        .baselineColumns("PST_TIMESTAMP")
+        .baselineValues(DateTime.parse("1970-01-01T00:00:00.0"))
+        .go();
+  }
+
+  @Test
+  public void testToUTCTimestamp() throws Exception {
+    testBuilder()
+        .sqlQuery("select to_utc_timestamp('1970-01-01 00:00:00','PST') as UTC_TIMESTAMP
from (VALUES(1))")
+        .unOrdered()
+        .baselineColumns("UTC_TIMESTAMP")
+        .baselineValues(DateTime.parse("1970-01-01T08:00:00.0"))
+        .go();
+  }
+
 }


Mime
View raw message