diff --git a/common/src/java/org/apache/hadoop/hive/common/type/Timestamp.java b/common/src/java/org/apache/hadoop/hive/common/type/Timestamp.java index a8b7b6d186..4859025e1e 100644 --- a/common/src/java/org/apache/hadoop/hive/common/type/Timestamp.java +++ b/common/src/java/org/apache/hadoop/hive/common/type/Timestamp.java @@ -19,6 +19,7 @@ import java.time.Instant; import java.time.LocalDateTime; +import java.time.OffsetDateTime; import java.time.ZoneOffset; import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatterBuilder; @@ -160,7 +161,12 @@ public static Timestamp valueOf(String s) { try { localDateTime = LocalDateTime.parse(s); } catch (DateTimeParseException e2) { - throw new IllegalArgumentException("Cannot create timestamp, parsing error"); + // Try ISO-8601 format with Zones + try { + localDateTime = OffsetDateTime.parse(s).toLocalDateTime(); + } catch (DateTimeParseException e3) { + throw new IllegalArgumentException("Cannot create timestamp, parsing error"); + } } } return new Timestamp(localDateTime); diff --git a/ql/src/test/org/apache/hadoop/hive/ql/udf/generic/TestGenericUDFDate.java b/ql/src/test/org/apache/hadoop/hive/ql/udf/generic/TestGenericUDFDate.java index dcb4d9c53b..bf0fb49c90 100644 --- a/ql/src/test/org/apache/hadoop/hive/ql/udf/generic/TestGenericUDFDate.java +++ b/ql/src/test/org/apache/hadoop/hive/ql/udf/generic/TestGenericUDFDate.java @@ -112,4 +112,42 @@ public void testVoidToDate() throws HiveException { } } + public void testValidToDate() throws HiveException { + runAndVerifyString("2015-04-22", "2015-04-22"); + runAndVerifyTs("2015-04-23T10:11", "2015-04-23"); + runAndVerifyTs("2015-04-23 10:11:45", "2015-04-23"); + runAndVerifyTs("2015-04-23T10:11:45Z", "2015-04-23"); + runAndVerifyTs("2015-04-23T10:11:45+01:00", "2015-04-23"); + runAndVerifyTs("2015-04-23T10:11:45-01:00", "2015-04-23"); + } + + private void runAndVerifyTs(String string, String expected) + throws HiveException { + GenericUDFDate udf = new GenericUDFDate(); + ObjectInspector valueOI = PrimitiveObjectInspectorFactory.writableTimestampObjectInspector; + ObjectInspector[] arguments = {valueOI}; + + udf.initialize(arguments); + DeferredObject valueObj0 = new DeferredJavaObject(string != null ? new TimestampWritableV2( + Timestamp.valueOf(string)) : null); + DeferredObject[] args = { valueObj0}; + DateWritableV2 output = (DateWritableV2) udf.evaluate(args); + + assertEquals(expected, output.toString()); + } + + private void runAndVerifyString(String string, String expected) + throws HiveException { + GenericUDFDate udf = new GenericUDFDate(); + ObjectInspector valueOI = PrimitiveObjectInspectorFactory.writableStringObjectInspector; + ObjectInspector[] arguments = {valueOI}; + + udf.initialize(arguments); + DeferredObject valueObj0 = new DeferredJavaObject(string != null ? new Text(string) : null); + DeferredObject[] args = { valueObj0}; + DateWritableV2 output = (DateWritableV2) udf.evaluate(args); + + assertEquals(expected, output.toString()); + } + } diff --git a/ql/src/test/org/apache/hadoop/hive/ql/udf/generic/TestGenericUDFDateFormat.java b/ql/src/test/org/apache/hadoop/hive/ql/udf/generic/TestGenericUDFDateFormat.java index 6a3cdda48a..81c75d2222 100644 --- a/ql/src/test/org/apache/hadoop/hive/ql/udf/generic/TestGenericUDFDateFormat.java +++ b/ql/src/test/org/apache/hadoop/hive/ql/udf/generic/TestGenericUDFDateFormat.java @@ -61,6 +61,9 @@ public void testDateFormatStr() throws HiveException { runAndVerifyStr("2015-04-09 10:30", fmtText, "Thursday", udf); runAndVerifyStr("2015-04-10 10:30:45.123", fmtText, "Friday", udf); runAndVerifyStr("2015-04-11T10:30:45", fmtText, "Saturday", udf); + runAndVerifyStr("2015-04-11T10:30:45Z", fmtText, "Saturday", udf); + runAndVerifyStr("2015-04-11T10:30:45+01:00", fmtText, "Saturday", udf); + runAndVerifyStr("2015-04-11T10:30:45-01:00", fmtText, "Saturday", udf); runAndVerifyStr("2015-04-12 10", fmtText, "Sunday", udf); }