diff --git ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFEpochMilli.java ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFEpochMilli.java index a239cbb42d..d8e822ae97 100644 --- ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFEpochMilli.java +++ ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFEpochMilli.java @@ -23,6 +23,7 @@ import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector; import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory; import org.apache.hadoop.hive.serde2.objectinspector.primitive.TimestampLocalTZObjectInspector; +import org.apache.hadoop.hive.serde2.objectinspector.primitive.TimestampObjectInspector; import org.apache.hadoop.io.LongWritable; /** @@ -31,7 +32,8 @@ public class GenericUDFEpochMilli extends GenericUDF { private transient final LongWritable result = new LongWritable(); - private transient TimestampLocalTZObjectInspector oi; + private transient TimestampLocalTZObjectInspector tsWithLocalTzOi = null; + private transient TimestampObjectInspector tsOi = null; @Override public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException { @@ -39,7 +41,11 @@ public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumen throw new UDFArgumentLengthException( "The operator GenericUDFEpochMilli only accepts 1 argument."); } - oi = (TimestampLocalTZObjectInspector) arguments[0]; + if (arguments[0] instanceof TimestampObjectInspector) { + tsOi = (TimestampObjectInspector) arguments[0]; + } else { + tsWithLocalTzOi = (TimestampLocalTZObjectInspector) arguments[0]; + } return PrimitiveObjectInspectorFactory.writableLongObjectInspector; } @@ -49,7 +55,10 @@ public Object evaluate(DeferredObject[] arguments) throws HiveException { if (a0 == null) { return null; } - result.set(oi.getPrimitiveJavaObject(a0).getZonedDateTime().toInstant().toEpochMilli()); + + result.set(tsOi == null ? + tsWithLocalTzOi.getPrimitiveJavaObject(a0).getZonedDateTime().toInstant().toEpochMilli() : + tsOi.getPrimitiveJavaObject(a0).toEpochMilli()); return result; } diff --git ql/src/test/queries/clientpositive/udf_from_utc_timestamp.q ql/src/test/queries/clientpositive/udf_from_utc_timestamp.q index ca0a6a80c8..fc83722d9d 100644 --- ql/src/test/queries/clientpositive/udf_from_utc_timestamp.q +++ ql/src/test/queries/clientpositive/udf_from_utc_timestamp.q @@ -27,3 +27,7 @@ select from_utc_timestamp('2012-02-11-04:30:00', 'UTC'), from_utc_timestamp('2012-02-11-04:30:00', 'PST'); +select +to_epoch_milli(cast (1536449552291 as timestamp )), +to_epoch_milli(cast('2012-02-11 04:30:00' as timestamp)), +cast(to_epoch_milli(cast('2012-02-11 04:30:00' as timestamp)) as timestamp ); \ No newline at end of file diff --git ql/src/test/results/clientpositive/udf_from_utc_timestamp.q.out ql/src/test/results/clientpositive/udf_from_utc_timestamp.q.out index 7294680e70..b39677349d 100644 --- ql/src/test/results/clientpositive/udf_from_utc_timestamp.q.out +++ ql/src/test/results/clientpositive/udf_from_utc_timestamp.q.out @@ -99,3 +99,18 @@ POSTHOOK: type: QUERY POSTHOOK: Input: _dummy_database@_dummy_table #### A masked pattern was here #### 2012-02-11 00:00:00 2012-02-10 16:00:00 +PREHOOK: query: select +to_epoch_milli(cast (1536449552291 as timestamp )), +to_epoch_milli(cast('2012-02-11 04:30:00' as timestamp)), +cast(to_epoch_milli(cast('2012-02-11 04:30:00' as timestamp)) as timestamp ) +PREHOOK: type: QUERY +PREHOOK: Input: _dummy_database@_dummy_table +#### A masked pattern was here #### +POSTHOOK: query: select +to_epoch_milli(cast (1536449552291 as timestamp )), +to_epoch_milli(cast('2012-02-11 04:30:00' as timestamp)), +cast(to_epoch_milli(cast('2012-02-11 04:30:00' as timestamp)) as timestamp ) +POSTHOOK: type: QUERY +POSTHOOK: Input: _dummy_database@_dummy_table +#### A masked pattern was here #### +1536449552291 1328934600000 2012-02-11 04:30:00