This is an automated email from the ASF dual-hosted git repository.

dkuzmenko pushed a commit to branch branch-4.0
in repository https://gitbox.apache.org/repos/asf/hive.git


The following commit(s) were added to refs/heads/branch-4.0 by this push:
     new da8957dd06e HIVE-28337: Process timestamps at UTC timezone instead of 
local timezone in MetaStoreUtils (Kiran Velumuri reviewed by Stamatis 
Zampetakis, Wechar Yu)
da8957dd06e is described below

commit da8957dd06ee88d8144179dce6ea266b199b8f39
Author: Kiran Velumuri <[email protected]>
AuthorDate: Tue Jun 18 12:46:14 2024 +0530

    HIVE-28337: Process timestamps at UTC timezone instead of local timezone in 
MetaStoreUtils (Kiran Velumuri reviewed by Stamatis Zampetakis, Wechar Yu)
    
    Processing timestamps in UTC avoids problems related to string values 
falling into DST intervals.
    
    Close apache/hive#5309
    
    (cherry picked from commit e31811bb7c6670ab1f725adde3aa2b012ca64415)
---
 .../hive/metastore/utils/MetaStoreUtils.java       |  8 +++---
 .../hive/metastore/utils/TestMetaStoreUtils.java   | 33 +++++++++++++++++-----
 2 files changed, 30 insertions(+), 11 deletions(-)

diff --git 
a/standalone-metastore/metastore-common/src/main/java/org/apache/hadoop/hive/metastore/utils/MetaStoreUtils.java
 
b/standalone-metastore/metastore-common/src/main/java/org/apache/hadoop/hive/metastore/utils/MetaStoreUtils.java
index c12c009b81c..895e2c9cafd 100644
--- 
a/standalone-metastore/metastore-common/src/main/java/org/apache/hadoop/hive/metastore/utils/MetaStoreUtils.java
+++ 
b/standalone-metastore/metastore-common/src/main/java/org/apache/hadoop/hive/metastore/utils/MetaStoreUtils.java
@@ -25,8 +25,8 @@ import java.security.AccessController;
 import java.security.PrivilegedAction;
 import java.sql.Date;
 import java.sql.Timestamp;
+import java.time.Instant;
 import java.time.LocalDate;
-import java.time.LocalDateTime;
 import java.time.format.DateTimeFormatter;
 import java.time.format.ResolverStyle;
 import java.util.ArrayList;
@@ -123,7 +123,7 @@ public class MetaStoreUtils {
    * @return Timestamp in string format.
    */
   public static String convertTimestampToString(Timestamp timestamp) {
-    return TIMESTAMP_FORMATTER.format(timestamp.toLocalDateTime());
+    return TIMESTAMP_FORMATTER.format(timestamp.toInstant());
   }
 
   /**
@@ -132,8 +132,8 @@ public class MetaStoreUtils {
    * @return java.sql.Timestamp object.
    */
   public static Timestamp convertStringToTimestamp(String timestamp) {
-    LocalDateTime val = 
LocalDateTime.from(TIMESTAMP_FORMATTER.parse(timestamp));
-    return Timestamp.valueOf(val);
+    Instant instant = Instant.from(TIMESTAMP_FORMATTER.parse(timestamp));
+    return Timestamp.from(instant);
   }
 
   // Indicates a type was derived from the deserializer rather than Hive's 
metadata.
diff --git 
a/standalone-metastore/metastore-server/src/test/java/org/apache/hadoop/hive/metastore/utils/TestMetaStoreUtils.java
 
b/standalone-metastore/metastore-server/src/test/java/org/apache/hadoop/hive/metastore/utils/TestMetaStoreUtils.java
index 8632f233e04..4a938f1b8a5 100644
--- 
a/standalone-metastore/metastore-server/src/test/java/org/apache/hadoop/hive/metastore/utils/TestMetaStoreUtils.java
+++ 
b/standalone-metastore/metastore-server/src/test/java/org/apache/hadoop/hive/metastore/utils/TestMetaStoreUtils.java
@@ -47,16 +47,18 @@ public class TestMetaStoreUtils {
   private static final TimeZone DEFAULT = TimeZone.getDefault();
   private static final DateTimeFormatter FORMATTER = 
DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss");
   private final TimeZone timezone;
+  private final Timestamp timestamp;
   private final String date;
-  private final String timestamp;
+  private final String timestampString;
 
-  public TestMetaStoreUtils(String zoneId, LocalDateTime timestamp) {
+  public TestMetaStoreUtils(String zoneId, LocalDateTime localDateTime) {
     this.timezone = TimeZone.getTimeZone(zoneId);
-    this.timestamp = timestamp.format(FORMATTER);
-    this.date = 
timestamp.toLocalDate().format(DateTimeFormatter.ISO_LOCAL_DATE);
+    this.timestamp = Timestamp.from(localDateTime.toInstant(ZoneOffset.UTC));
+    this.timestampString = localDateTime.format(FORMATTER);
+    this.date = 
timestamp.toLocalDateTime().format(DateTimeFormatter.ISO_LOCAL_DATE);
   }
 
-  @Parameterized.Parameters(name = "zoneId={0}, timestamp={1}")
+  @Parameterized.Parameters(name = "zoneId={0}, localDateTime={1}")
   public static Collection<Object[]> generateZoneTimestampPairs() {
     List<Object[]> params = new ArrayList<>();
     long minDate = LocalDate.of(0, 1, 
1).atStartOfDay().toEpochSecond(ZoneOffset.UTC);
@@ -67,9 +69,26 @@ public class TestMetaStoreUtils {
         params.add(new Object[] { zone, datetime });
       }
     });
+    // Timestamp and Date do not have the year 0000. So, the year 0000 gets 
converted to year 0001.
+    params.add(new Object[] {"Asia/Kolkata", LocalDateTime.of(0, 1, 
7,22,44,36)});
+    generateDaylightSavingTimestampPairs(params);
     return params;
   }
 
+  public static void generateDaylightSavingTimestampPairs(List<Object[]> 
params) {
+    params.add(new Object[] { "America/Anchorage", LocalDateTime.of(2024, 3, 
10, 2, 1, 0) });
+    params.add(new Object[] { "America/St_Johns", LocalDateTime.of(2024, 3, 
10, 2, 1, 0) });
+    params.add(new Object[] { "America/Chicago", LocalDateTime.of(2024, 3, 10, 
2, 1, 0) });
+    params.add(new Object[] { "America/Indiana/Indianapolis", 
LocalDateTime.of(2024, 3, 10, 2, 1, 0) });
+    params.add(new Object[] { "America/Los_Angeles", LocalDateTime.of(2024, 3, 
10, 2, 1, 0) });
+
+    params.add(new Object[] { "Europe/Paris", LocalDateTime.of(2024, 3, 31, 2, 
2, 2) });
+
+    params.add(new Object[] { "Pacific/Auckland", LocalDateTime.of(2024, 9, 
29, 2, 3, 4) });
+
+    params.add(new Object[] { "Australia/Sydney", LocalDateTime.of(2024, 10, 
6, 2, 4, 6) });
+  }
+
   @Before
   public void setup() {
     TimeZone.setDefault(timezone);
@@ -82,7 +101,7 @@ public class TestMetaStoreUtils {
 
   @Test
   public void testTimestampToString() {
-    assertEquals(timestamp, 
MetaStoreUtils.convertTimestampToString(Timestamp.valueOf(timestamp)));
+    assertEquals(timestampString, 
MetaStoreUtils.convertTimestampToString(timestamp));
   }
 
   @Test
@@ -92,7 +111,7 @@ public class TestMetaStoreUtils {
 
   @Test
   public void testStringToTimestamp() {
-    assertEquals(Timestamp.valueOf(timestamp), 
MetaStoreUtils.convertStringToTimestamp(timestamp));
+    assertEquals(timestamp, 
MetaStoreUtils.convertStringToTimestamp(timestampString));
   }
 
   @AfterClass

Reply via email to