Repository: hive
Updated Branches:
  refs/heads/master 68b66a64f -> fb7987059


HIVE-13745: UDF current_date、current_timestamp、unix_timestamp NPE (Biao Wu, 
reviewed by Yongzhi Chen)


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

Branch: refs/heads/master
Commit: fb79870592d775cd836d5611e21ab1c7030aadba
Parents: 68b66a6
Author: Yongzhi Chen <[email protected]>
Authored: Fri May 11 06:30:30 2018 -0400
Committer: Yongzhi Chen <[email protected]>
Committed: Fri May 11 06:30:30 2018 -0400

----------------------------------------------------------------------
 .../org/apache/hadoop/hive/conf/HiveConf.java   |  1 +
 .../hadoop/hive/ql/session/SessionState.java    |  1 +
 .../ql/udf/generic/GenericUDFCurrentDate.java   | 26 ++++++++++++++++++-
 .../udf/generic/GenericUDFCurrentTimestamp.java | 26 ++++++++++++++++++-
 .../ql/udf/generic/GenericUDFUnixTimeStamp.java | 27 +++++++++++++++++++-
 5 files changed, 78 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/hive/blob/fb798705/common/src/java/org/apache/hadoop/hive/conf/HiveConf.java
----------------------------------------------------------------------
diff --git a/common/src/java/org/apache/hadoop/hive/conf/HiveConf.java 
b/common/src/java/org/apache/hadoop/hive/conf/HiveConf.java
index 33c863d..44b9eb2 100644
--- a/common/src/java/org/apache/hadoop/hive/conf/HiveConf.java
+++ b/common/src/java/org/apache/hadoop/hive/conf/HiveConf.java
@@ -1843,6 +1843,7 @@ public class HiveConf extends Configuration {
     TESTMODE_BUCKET_CODEC_VERSION("hive.test.bucketcodec.version", 1,
       "For testing only.  Will make ACID subsystem write 
RecordIdentifier.bucketId in specified\n" +
         "format", false),
+    HIVE_QUERY_TIMESTAMP("hive.query.timestamp", System.currentTimeMillis(), 
"query execute time."),
 
     HIVEMERGEMAPFILES("hive.merge.mapfiles", true,
         "Merge small files at the end of a map-only job"),

http://git-wip-us.apache.org/repos/asf/hive/blob/fb798705/ql/src/java/org/apache/hadoop/hive/ql/session/SessionState.java
----------------------------------------------------------------------
diff --git a/ql/src/java/org/apache/hadoop/hive/ql/session/SessionState.java 
b/ql/src/java/org/apache/hadoop/hive/ql/session/SessionState.java
index 6bb756c..9f65a77 100644
--- a/ql/src/java/org/apache/hadoop/hive/ql/session/SessionState.java
+++ b/ql/src/java/org/apache/hadoop/hive/ql/session/SessionState.java
@@ -1924,6 +1924,7 @@ public class SessionState {
    */
   public void setupQueryCurrentTimestamp() {
     queryCurrentTimestamp = new Timestamp(System.currentTimeMillis());
+    sessionConf.setLongVar(ConfVars.HIVE_QUERY_TIMESTAMP, 
queryCurrentTimestamp.getTime());
 
     // Provide a facility to set current timestamp during tests
     if (sessionConf.getBoolVar(ConfVars.HIVE_IN_TEST)) {

http://git-wip-us.apache.org/repos/asf/hive/blob/fb798705/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFCurrentDate.java
----------------------------------------------------------------------
diff --git 
a/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFCurrentDate.java 
b/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFCurrentDate.java
index 7d3c3f4..91fd08f 100644
--- 
a/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFCurrentDate.java
+++ 
b/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFCurrentDate.java
@@ -18,8 +18,12 @@
 package org.apache.hadoop.hive.ql.udf.generic;
 
 import java.sql.Date;
+import java.sql.Timestamp;
 
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.hive.conf.HiveConf;
 import org.apache.hadoop.hive.ql.exec.Description;
+import org.apache.hadoop.hive.ql.exec.MapredContext;
 import org.apache.hadoop.hive.ql.exec.UDFArgumentException;
 import org.apache.hadoop.hive.ql.exec.UDFArgumentLengthException;
 import org.apache.hadoop.hive.ql.metadata.HiveException;
@@ -39,6 +43,13 @@ import 
org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectIn
 public class GenericUDFCurrentDate extends GenericUDF {
 
   protected DateWritable currentDate;
+  private Configuration conf;
+
+  @Override
+  public void configure(MapredContext context) {
+    super.configure(context);
+    conf = context.getJobConf();
+  }
 
   @Override
   public ObjectInspector initialize(ObjectInspector[] arguments)
@@ -50,8 +61,21 @@ public class GenericUDFCurrentDate extends GenericUDF {
     }
 
     if (currentDate == null) {
+      SessionState ss = SessionState.get();
+      Timestamp queryTimestamp;
+      if (ss == null) {
+        if (conf == null) {
+          queryTimestamp = new Timestamp(System.currentTimeMillis());
+        } else {
+          queryTimestamp = new Timestamp(
+                  HiveConf.getLongVar(conf, 
HiveConf.ConfVars.HIVE_QUERY_TIMESTAMP));
+        }
+      } else {
+        queryTimestamp = ss.getQueryCurrentTimestamp();
+      }
+
       Date dateVal =
-          
Date.valueOf(SessionState.get().getQueryCurrentTimestamp().toString().substring(0,
 10));
+              Date.valueOf(queryTimestamp.toString().substring(0, 10));
       currentDate = new DateWritable(dateVal);
     }
 

http://git-wip-us.apache.org/repos/asf/hive/blob/fb798705/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFCurrentTimestamp.java
----------------------------------------------------------------------
diff --git 
a/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFCurrentTimestamp.java
 
b/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFCurrentTimestamp.java
index 9da51c8..ca43840 100644
--- 
a/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFCurrentTimestamp.java
+++ 
b/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFCurrentTimestamp.java
@@ -17,7 +17,12 @@
  */
 package org.apache.hadoop.hive.ql.udf.generic;
 
+import java.sql.Timestamp;
+
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.hive.conf.HiveConf;
 import org.apache.hadoop.hive.ql.exec.Description;
+import org.apache.hadoop.hive.ql.exec.MapredContext;
 import org.apache.hadoop.hive.ql.exec.UDFArgumentException;
 import org.apache.hadoop.hive.ql.exec.UDFArgumentLengthException;
 import org.apache.hadoop.hive.ql.metadata.HiveException;
@@ -37,6 +42,13 @@ import 
org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectIn
 public class GenericUDFCurrentTimestamp extends GenericUDF {
 
   protected TimestampWritable currentTimestamp;
+  private Configuration conf;
+
+  @Override
+  public void configure(MapredContext context) {
+    super.configure(context);
+    conf = context.getJobConf();
+  }
 
   @Override
   public ObjectInspector initialize(ObjectInspector[] arguments)
@@ -48,7 +60,19 @@ public class GenericUDFCurrentTimestamp extends GenericUDF {
     }
 
     if (currentTimestamp == null) {
-      currentTimestamp = new 
TimestampWritable(SessionState.get().getQueryCurrentTimestamp());
+      SessionState ss = SessionState.get();
+      Timestamp queryTimestamp;
+      if (ss == null) {
+        if (conf == null) {
+          queryTimestamp = new Timestamp(System.currentTimeMillis());
+        } else {
+          queryTimestamp = new Timestamp(
+                  HiveConf.getLongVar(conf, 
HiveConf.ConfVars.HIVE_QUERY_TIMESTAMP));
+        }
+      } else {
+        queryTimestamp = ss.getQueryCurrentTimestamp();
+      }
+      currentTimestamp = new TimestampWritable(queryTimestamp);
     }
 
     return PrimitiveObjectInspectorFactory.writableTimestampObjectInspector;

http://git-wip-us.apache.org/repos/asf/hive/blob/fb798705/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFUnixTimeStamp.java
----------------------------------------------------------------------
diff --git 
a/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFUnixTimeStamp.java
 
b/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFUnixTimeStamp.java
index 8329831..6ce72f7 100644
--- 
a/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFUnixTimeStamp.java
+++ 
b/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFUnixTimeStamp.java
@@ -18,6 +18,11 @@
 
 package org.apache.hadoop.hive.ql.udf.generic;
 
+import java.sql.Timestamp;
+
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.hive.conf.HiveConf;
+import org.apache.hadoop.hive.ql.exec.MapredContext;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.apache.hadoop.hive.ql.exec.Description;
@@ -37,6 +42,14 @@ import org.apache.hadoop.io.LongWritable;
 public class GenericUDFUnixTimeStamp extends GenericUDFToUnixTimeStamp {
   private static final Logger LOG = 
LoggerFactory.getLogger(GenericUDFUnixTimeStamp.class);
   private LongWritable currentTimestamp; // retValue is transient so store 
this separately.
+  private Configuration conf;
+
+  @Override
+  public void configure(MapredContext context) {
+    super.configure(context);
+    conf = context.getJobConf();
+  }
+
   @Override
   protected void initializeInput(ObjectInspector[] arguments) throws 
UDFArgumentException {
     if (arguments.length > 0) {
@@ -44,7 +57,19 @@ public class GenericUDFUnixTimeStamp extends 
GenericUDFToUnixTimeStamp {
     } else {
       if (currentTimestamp == null) {
         currentTimestamp = new LongWritable(0);
-        setValueFromTs(currentTimestamp, 
SessionState.get().getQueryCurrentTimestamp());
+        SessionState ss = SessionState.get();
+        Timestamp queryTimestamp;
+        if (ss == null) {
+          if (conf == null) {
+            queryTimestamp = new Timestamp(System.currentTimeMillis());
+          } else {
+            queryTimestamp = new Timestamp(
+                    HiveConf.getLongVar(conf, 
HiveConf.ConfVars.HIVE_QUERY_TIMESTAMP));
+          }
+        } else {
+          queryTimestamp = ss.getQueryCurrentTimestamp();
+        }
+        setValueFromTs(currentTimestamp, queryTimestamp);
         String msg = "unix_timestamp(void) is deprecated. Use 
current_timestamp instead.";
         SessionState.getConsole().printInfo(msg, false);
       }

Reply via email to