diff --git a/serde/src/java/org/apache/hadoop/hive/serde2/objectinspector/primitive/PrimitiveObjectInspectorUtils.java b/serde/src/java/org/apache/hadoop/hive/serde2/objectinspector/primitive/PrimitiveObjectInspectorUtils.java index 8a057d1dab..61b12cdc15 100644 --- a/serde/src/java/org/apache/hadoop/hive/serde2/objectinspector/primitive/PrimitiveObjectInspectorUtils.java +++ b/serde/src/java/org/apache/hadoop/hive/serde2/objectinspector/primitive/PrimitiveObjectInspectorUtils.java @@ -1248,25 +1248,31 @@ public static Timestamp getTimestamp(Object o, PrimitiveObjectInspector inputOI, return result; } + final static int DATE_LENGTH = "YYYY-MM-DD".length(); + final static int TS_LENGTH = "yyyy-mm-dd hh:mm:ss".length(); + public static Timestamp getTimestampFromString(String s) { Timestamp result; s = s.trim(); s = trimNanoTimestamp(s); + // Handle simpler cases directly avoiding exceptions try { + if (s.length() == DATE_LENGTH) { + // Its a date! + return Timestamp.ofEpochMilli(Date.valueOf(s).toEpochMilli()); + } else if (s.length() == TS_LENGTH) { + return Timestamp.valueOf(s); + } + // Lets try other formats. result = Timestamp.valueOf(s); } catch (IllegalArgumentException e) { // Let's try to parse it as timestamp with time zone and transform try { result = Timestamp.valueOf(TimestampTZUtil.parse(s).getZonedDateTime() - .toLocalDateTime().toString()); + .toLocalDateTime().toString()); } catch (DateTimeException e2) { - // Last try: we try to parse it as date and transform - try { - result = Timestamp.ofEpochMilli(Date.valueOf(s).toEpochMilli()); - } catch (IllegalArgumentException e3) { - result = null; - } + result = null; } } return result;