Skip to content

Commit

Permalink
HIVE-28311: Backward compatibility of java.sql.Date and java.sql.Time…
Browse files Browse the repository at this point in the history
…stamp in hive-serde
  • Loading branch information
wecharyu committed Jun 7, 2024
1 parent c5d9577 commit de4053a
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 0 deletions.
4 changes: 4 additions & 0 deletions common/src/java/org/apache/hadoop/hive/common/type/Date.java
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,10 @@ public static Date valueOf(final String text) {
}
}

public static Date valueOf(java.sql.Date d) {
return new Date(d.toLocalDate());
}

public static Date ofEpochDay(int epochDay) {
return new Date(LocalDate.ofEpochDay(epochDay));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ protected JavaDateObjectInspector() {
}

public DateWritableV2 getPrimitiveWritableObject(Object o) {
if (o instanceof java.sql.Date) {
return new DateWritableV2(Date.valueOf((java.sql.Date) o));
}
return o == null ? null : new DateWritableV2((Date) o);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ protected JavaTimestampObjectInspector() {
}

public TimestampWritableV2 getPrimitiveWritableObject(Object o) {
if (o instanceof java.sql.Timestamp) {
return new TimestampWritableV2(Timestamp.ofEpochMilli(((java.sql.Timestamp) o).getTime()));
}
return o == null ? null : new TimestampWritableV2((Timestamp) o);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,20 @@ public void testGetPrimitiveGrouping() {
PrimitiveObjectInspectorUtils.getPrimitiveGrouping(PrimitiveCategory.VOID));
}

@Test
public void testGetDate() {
DateFormat gmtDateFormat = new SimpleDateFormat("yyyy-MM-dd");
gmtDateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
PrimitiveObjectInspector dateOI = PrimitiveObjectInspectorFactory
.getPrimitiveJavaObjectInspector(PrimitiveCategory.DATE);
assertEquals("1970-01-01", gmtDateFormat.format(PrimitiveObjectInspectorUtils.getDate(Date.ofEpochDay(0), dateOI).toEpochMilli()));
assertEquals("2024-06-07", gmtDateFormat.format(PrimitiveObjectInspectorUtils.getDate(Date.ofEpochMilli(1717752174344L), dateOI).toEpochMilli()));

// Test the compatibility of java.sql.Date that's removed in HIVE-20007
assertEquals("1970-01-01", gmtDateFormat.format(PrimitiveObjectInspectorUtils.getDate(java.sql.Date.valueOf("1970-01-01"), dateOI).toEpochMilli()));
assertEquals("2024-06-07", gmtDateFormat.format(PrimitiveObjectInspectorUtils.getDate(java.sql.Date.valueOf("2024-06-07"), dateOI).toEpochMilli()));
}

@Test
public void testgetTimestampWithMillisecondsInt() {
DateFormat gmtDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
Expand Down Expand Up @@ -148,6 +162,9 @@ public void testgetTimestampWithMillisecondsInt() {
PrimitiveObjectInspector timestampOI = PrimitiveObjectInspectorFactory
.getPrimitiveJavaObjectInspector(PrimitiveCategory.TIMESTAMP);
assertEquals("2015-02-07 15:01:22.123", gmtDateFormat.format(PrimitiveObjectInspectorUtils.getTimestamp(Timestamp.ofEpochMilli(1423321282123L), timestampOI).toSqlTimestamp()));

// Test the compatibility of java.sql.Timestamp that's removed in HIVE-20007
assertEquals("2024-06-07 09:22:54.344", gmtDateFormat.format(PrimitiveObjectInspectorUtils.getTimestamp(new java.sql.Timestamp(1717752174344L), timestampOI).toSqlTimestamp()));
}

@Test
Expand Down

0 comments on commit de4053a

Please sign in to comment.