diff --git a/common/src/java/org/apache/hadoop/hive/common/type/TimestampTZ.java b/common/src/java/org/apache/hadoop/hive/common/type/TimestampTZ.java index b5bd80bda8..af181ba124 100644 --- a/common/src/java/org/apache/hadoop/hive/common/type/TimestampTZ.java +++ b/common/src/java/org/apache/hadoop/hive/common/type/TimestampTZ.java @@ -92,6 +92,10 @@ public long getEpochSecond() { return zonedDateTime.toInstant().getEpochSecond(); } + public long toEpochMilli() { + return zonedDateTime.toInstant().toEpochMilli(); + } + public int getNanos() { return zonedDateTime.toInstant().getNano(); } diff --git a/ql/src/java/org/apache/hadoop/hive/ql/parse/SemanticAnalyzer.java b/ql/src/java/org/apache/hadoop/hive/ql/parse/SemanticAnalyzer.java index b330d710a1..c2e734c8d5 100644 --- a/ql/src/java/org/apache/hadoop/hive/ql/parse/SemanticAnalyzer.java +++ b/ql/src/java/org/apache/hadoop/hive/ql/parse/SemanticAnalyzer.java @@ -14964,7 +14964,7 @@ private void useCachedResult(QueryResultsCache.CacheEntry cacheEntry, boolean ne } private QueryResultsCache.QueryInfo createCacheQueryInfoForQuery(QueryResultsCache.LookupInfo lookupInfo) { - long queryTime = SessionState.get().getQueryCurrentTimestamp().getTime(); + long queryTime = SessionState.get().getQueryCurrentTimestamp().toEpochMilli(); return new QueryResultsCache.QueryInfo(queryTime, lookupInfo, queryState.getHiveOperation(), resultSchema, getTableAccessInfo(), getColumnAccessInfo(), inputs); } 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 ff523cc686..e406060642 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 @@ -28,7 +28,7 @@ import java.net.URI; import java.net.URISyntaxException; import java.net.URLClassLoader; -import java.sql.Timestamp; +import java.time.Instant; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; @@ -56,6 +56,9 @@ import org.apache.hadoop.hive.common.FileUtils; import org.apache.hadoop.hive.common.JavaUtils; import org.apache.hadoop.hive.common.log.ProgressMonitor; +import org.apache.hadoop.hive.common.type.Timestamp; +import org.apache.hadoop.hive.common.type.TimestampTZ; +import org.apache.hadoop.hive.common.type.TimestampTZUtil; import org.apache.hadoop.hive.conf.HiveConf; import org.apache.hadoop.hive.conf.HiveConf.ConfVars; import org.apache.hadoop.hive.conf.HiveConfUtil; @@ -305,7 +308,7 @@ /** * CURRENT_TIMESTAMP value for query */ - private Timestamp queryCurrentTimestamp; + private Instant queryCurrentTimestamp; private final ResourceMaps resourceMaps; @@ -1960,14 +1963,16 @@ public String getNextValuesTempTableSuffix() { * Initialize current timestamp, other necessary query initialization. */ public void setupQueryCurrentTimestamp() { - queryCurrentTimestamp = new Timestamp(System.currentTimeMillis()); + queryCurrentTimestamp = Instant.now(); // Provide a facility to set current timestamp during tests if (sessionConf.getBoolVar(ConfVars.HIVE_IN_TEST)) { String overrideTimestampString = HiveConf.getVar(sessionConf, HiveConf.ConfVars.HIVETESTCURRENTTIMESTAMP, (String)null); if (overrideTimestampString != null && overrideTimestampString.length() > 0) { - queryCurrentTimestamp = Timestamp.valueOf(overrideTimestampString); + TimestampTZ zonedDateTime = TimestampTZUtil.convert( + Timestamp.valueOf(overrideTimestampString), sessionConf.getLocalTimeZone()); + queryCurrentTimestamp = zonedDateTime.getZonedDateTime().toInstant(); } } } @@ -1976,7 +1981,7 @@ public void setupQueryCurrentTimestamp() { * Get query current timestamp * @return */ - public Timestamp getQueryCurrentTimestamp() { + public Instant getQueryCurrentTimestamp() { return queryCurrentTimestamp; } 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 cffd10beee..0e58dd0d08 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 @@ -17,6 +17,7 @@ */ package org.apache.hadoop.hive.ql.udf.generic; +import java.time.ZonedDateTime; import org.apache.hadoop.hive.common.type.Date; import org.apache.hadoop.hive.ql.exec.Description; import org.apache.hadoop.hive.ql.exec.UDFArgumentException; @@ -49,8 +50,11 @@ public ObjectInspector initialize(ObjectInspector[] arguments) } if (currentDate == null) { - Date dateVal = - Date.valueOf(SessionState.get().getQueryCurrentTimestamp().toString().substring(0, 10)); + SessionState ss = SessionState.get(); + ZonedDateTime dateTime = ss.getQueryCurrentTimestamp().atZone( + ss.getConf().getLocalTimeZone()); + Date dateVal = Date.valueOf( + dateTime.toString().substring(0, 10)); currentDate = new DateWritableV2(dateVal); } 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 d9447f126b..281294c6d7 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,6 +17,7 @@ */ package org.apache.hadoop.hive.ql.udf.generic; +import java.time.ZonedDateTime; import org.apache.hadoop.hive.common.type.Timestamp; import org.apache.hadoop.hive.ql.exec.Description; import org.apache.hadoop.hive.ql.exec.UDFArgumentException; @@ -49,9 +50,11 @@ public ObjectInspector initialize(ObjectInspector[] arguments) } if (currentTimestamp == null) { - java.sql.Timestamp ts = SessionState.get().getQueryCurrentTimestamp(); + SessionState ss = SessionState.get(); + ZonedDateTime dateTime = ss.getQueryCurrentTimestamp().atZone( + ss.getConf().getLocalTimeZone()); currentTimestamp = new TimestampWritableV2( - Timestamp.ofEpochMilli(ts.getTime(), ts.getNanos())); + Timestamp.valueOf(dateTime.toLocalDateTime().toString())); } return PrimitiveObjectInspectorFactory.writableTimestampObjectInspector; 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 557ab792ea..d560c62adb 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 @@ -45,7 +45,7 @@ protected void initializeInput(ObjectInspector[] arguments) throws UDFArgumentEx } else { if (currentTimestamp == null) { currentTimestamp = new LongWritable(0); - setValueFromTs(currentTimestamp, Timestamp.ofEpochMilli(SessionState.get().getQueryCurrentTimestamp().getTime())); + setValueFromTs(currentTimestamp, Timestamp.ofEpochMilli(SessionState.get().getQueryCurrentTimestamp().toEpochMilli())); String msg = "unix_timestamp(void) is deprecated. Use current_timestamp instead."; SessionState.getConsole().printInfo(msg, false); }