diff --git a/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFDateFormat.java b/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFDateFormat.java index 6d3e86f9212fd3c082390790b73667f74f925c3b..54b4504f576e6897109e650a45baf1c5d42e0cb2 100644 --- a/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFDateFormat.java +++ b/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFDateFormat.java @@ -20,8 +20,7 @@ import static org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorUtils.PrimitiveGrouping.DATE_GROUP; import static org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorUtils.PrimitiveGrouping.STRING_GROUP; -import java.text.SimpleDateFormat; -import java.util.TimeZone; +import java.time.format.DateTimeFormatter; import org.apache.hadoop.hive.common.type.Date; import org.apache.hadoop.hive.common.type.Timestamp; @@ -45,8 +44,8 @@ */ @Description(name = "date_format", value = "_FUNC_(date/timestamp/string, fmt) - converts a date/timestamp/string " + "to a value of string in the format specified by the date format fmt.", - extended = "Supported formats are SimpleDateFormat formats - " - + "https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html. " + extended = "Supported formats are DateTimeFormatter formats - " + + "https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html. " + "Second argument fmt should be constant.\n" + "Example: > SELECT _FUNC_('2015-04-08', 'y');\n '2015'") public class GenericUDFDateFormat extends GenericUDF { @@ -54,9 +53,8 @@ private transient PrimitiveCategory[] tsInputTypes = new PrimitiveCategory[2]; private transient Converter[] dtConverters = new Converter[2]; private transient PrimitiveCategory[] dtInputTypes = new PrimitiveCategory[2]; - private final java.util.Date date = new java.util.Date(); private final Text output = new Text(); - private transient SimpleDateFormat formatter; + private transient DateTimeFormatter formatter; @Override public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException { @@ -79,8 +77,7 @@ public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumen String fmtStr = getConstantStringValue(arguments, 1); if (fmtStr != null) { try { - formatter = new SimpleDateFormat(fmtStr); - formatter.setTimeZone(TimeZone.getTimeZone("UTC")); + formatter = DateTimeFormatter.ofPattern(fmtStr); } catch (IllegalArgumentException e) { // ignore } @@ -110,8 +107,8 @@ public Object evaluate(DeferredObject[] arguments) throws HiveException { ts = Timestamp.ofEpochMilli(d.toEpochMilli()); } - date.setTime(ts.toEpochMilli()); - String res = formatter.format(date); + String res = ts.format(formatter); + if (res == null) { return null; } 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 f0a5d3f19ef5b1f7b6aaa3eba662eb8c3d73a78f..45f6325c69c63524d73d24560f9e40abff0260f5 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 @@ -147,7 +147,7 @@ public void testNullFmt() throws HiveException { public void testWrongFmt() throws HiveException { GenericUDFDateFormat udf = new GenericUDFDateFormat(); ObjectInspector valueOI0 = PrimitiveObjectInspectorFactory.writableStringObjectInspector; - Text fmtText = new Text("Q"); + Text fmtText = new Text("R"); ObjectInspector valueOI1 = PrimitiveObjectInspectorFactory .getPrimitiveWritableConstantObjectInspector(TypeInfoFactory.stringTypeInfo, fmtText); ObjectInspector[] arguments = { valueOI0, valueOI1 }; @@ -157,6 +157,19 @@ public void testWrongFmt() throws HiveException { runAndVerifyStr("2015-04-05", fmtText, null, udf); } + public void testJulianDates() throws HiveException { + GenericUDFDateFormat udf = new GenericUDFDateFormat(); + ObjectInspector valueOI0 = PrimitiveObjectInspectorFactory.writableStringObjectInspector; + Text fmtText = new Text("dd---MM--yyyy"); + ObjectInspector valueOI1 = PrimitiveObjectInspectorFactory + .getPrimitiveWritableConstantObjectInspector(TypeInfoFactory.stringTypeInfo, fmtText); + ObjectInspector[] arguments = { valueOI0, valueOI1 }; + + udf.initialize(arguments); + + runAndVerifyStr("1001-01-05", fmtText, "05---01--1001", udf); + } + private void runAndVerifyStr(String str, Text fmtText, String expResult, GenericUDF udf) throws HiveException { DeferredObject valueObj0 = new DeferredJavaObject(str != null ? new Text(str) : null);