diff --git a/presto-main/src/main/java/com/facebook/presto/operator/scalar/DateTimeFunctions.java b/presto-main/src/main/java/com/facebook/presto/operator/scalar/DateTimeFunctions.java index 1762597fdccf4..d8a8500379cc0 100644 --- a/presto-main/src/main/java/com/facebook/presto/operator/scalar/DateTimeFunctions.java +++ b/presto-main/src/main/java/com/facebook/presto/operator/scalar/DateTimeFunctions.java @@ -119,7 +119,15 @@ public static long currentTime(SqlFunctionProperties properties) // of TIME WITH TIME ZONE millis -= valueToSessionTimeZoneOffsetDiff(properties.getSessionStartTime(), getDateTimeZone(properties.getTimeZoneKey())); } - return packDateTimeWithZone(millis, properties.getTimeZoneKey()); + try { + return packDateTimeWithZone(millis, properties.getTimeZoneKey()); + } + catch (NotSupportedException | TimeZoneNotSupportedException e) { + throw new PrestoException(NOT_SUPPORTED, e.getMessage(), e); + } + catch (IllegalArgumentException e) { + throw new PrestoException(INVALID_FUNCTION_ARGUMENT, e.getMessage(), e); + } } @Description("current time without time zone") @@ -147,7 +155,15 @@ public static Slice currentTimeZone(SqlFunctionProperties properties) @SqlType(StandardTypes.TIMESTAMP_WITH_TIME_ZONE) public static long currentTimestamp(SqlFunctionProperties properties) { - return packDateTimeWithZone(properties.getSessionStartTime(), properties.getTimeZoneKey()); + try { + return packDateTimeWithZone(properties.getSessionStartTime(), properties.getTimeZoneKey()); + } + catch (NotSupportedException | TimeZoneNotSupportedException e) { + throw new PrestoException(NOT_SUPPORTED, e.getMessage(), e); + } + catch (IllegalArgumentException e) { + throw new PrestoException(INVALID_FUNCTION_ARGUMENT, e.getMessage(), e); + } } @Description("current timestamp without time zone") @@ -183,7 +199,16 @@ public static long fromUnixTime(@SqlType(StandardTypes.DOUBLE) double unixTime, catch (IllegalArgumentException e) { throw new PrestoException(INVALID_FUNCTION_ARGUMENT, e.getMessage(), e); } - return packDateTimeWithZone(Math.round(unixTime * 1000), timeZoneKey); + + try { + return packDateTimeWithZone(Math.round(unixTime * 1000), timeZoneKey); + } + catch (NotSupportedException | TimeZoneNotSupportedException e) { + throw new PrestoException(NOT_SUPPORTED, e.getMessage(), e); + } + catch (IllegalArgumentException e) { + throw new PrestoException(INVALID_FUNCTION_ARGUMENT, e.getMessage(), e); + } } @ScalarFunction("from_unixtime") @@ -268,7 +293,15 @@ public static long fromISO8601Timestamp(SqlFunctionProperties properties, @SqlTy DateTimeFormatter formatter = ISODateTimeFormat.dateTimeParser() .withChronology(getChronology(properties.getTimeZoneKey())) .withOffsetParsed(); - return packDateTimeWithZone(parseDateTimeHelper(formatter, iso8601DateTime.toStringUtf8())); + try { + return packDateTimeWithZone(parseDateTimeHelper(formatter, iso8601DateTime.toStringUtf8())); + } + catch (NotSupportedException | TimeZoneNotSupportedException e) { + throw new PrestoException(NOT_SUPPORTED, e.getMessage(), e); + } + catch (IllegalArgumentException e) { + throw new PrestoException(INVALID_FUNCTION_ARGUMENT, e.getMessage(), e); + } } @ScalarFunction("from_iso8601_date") @@ -304,7 +337,15 @@ public static long timeAtTimeZone(SqlFunctionProperties properties, @SqlType(Sta @SqlType(StandardTypes.TIMESTAMP_WITH_TIME_ZONE) public static long timestampAtTimeZone(@SqlType(StandardTypes.TIMESTAMP_WITH_TIME_ZONE) long timestampWithTimeZone, @SqlType("varchar(x)") Slice zoneId) { - return packDateTimeWithZone(unpackMillisUtc(timestampWithTimeZone), zoneId.toStringUtf8()); + try { + return packDateTimeWithZone(unpackMillisUtc(timestampWithTimeZone), zoneId.toStringUtf8()); + } + catch (NotSupportedException | TimeZoneNotSupportedException e) { + throw new PrestoException(NOT_SUPPORTED, e.getMessage(), e); + } + catch (IllegalArgumentException e) { + throw new PrestoException(INVALID_FUNCTION_ARGUMENT, e.getMessage(), e); + } } @ScalarFunction(value = "at_timezone", visibility = HIDDEN) @@ -313,7 +354,15 @@ public static long timestampAtTimeZone(@SqlType(StandardTypes.TIMESTAMP_WITH_TIM { checkCondition((zoneOffset % 60_000L) == 0L, INVALID_FUNCTION_ARGUMENT, "Invalid time zone offset interval: interval contains seconds"); long zoneOffsetMinutes = zoneOffset / 60_000L; - return packDateTimeWithZone(unpackMillisUtc(timestampWithTimeZone), getTimeZoneKeyForOffset(zoneOffsetMinutes)); + try { + return packDateTimeWithZone(unpackMillisUtc(timestampWithTimeZone), getTimeZoneKeyForOffset(zoneOffsetMinutes)); + } + catch (NotSupportedException | TimeZoneNotSupportedException e) { + throw new PrestoException(NOT_SUPPORTED, e.getMessage(), e); + } + catch (IllegalArgumentException e) { + throw new PrestoException(INVALID_FUNCTION_ARGUMENT, e.getMessage(), e); + } } @Description("truncate to the specified precision in the session timezone") @@ -581,6 +630,9 @@ public static long parseDatetime(SqlFunctionProperties properties, @SqlType("var .withLocale(properties.getSessionLocale()), datetime.toStringUtf8())); } + catch (NotSupportedException | TimeZoneNotSupportedException e) { + throw new PrestoException(NOT_SUPPORTED, e.getMessage(), e); + } catch (IllegalArgumentException e) { throw new PrestoException(INVALID_FUNCTION_ARGUMENT, e); } @@ -1384,7 +1436,15 @@ private static long timeAtTimeZone(SqlFunctionProperties properties, long timeWi localMillis += TimeUnit.DAYS.toMillis(1); } - return packDateTimeWithZone(millis, timeZoneKey); + try { + return packDateTimeWithZone(millis, timeZoneKey); + } + catch (NotSupportedException | TimeZoneNotSupportedException e) { + throw new PrestoException(NOT_SUPPORTED, e.getMessage(), e); + } + catch (IllegalArgumentException e) { + throw new PrestoException(INVALID_FUNCTION_ARGUMENT, e.getMessage(), e); + } } // HACK WARNING! diff --git a/presto-main/src/test/java/com/facebook/presto/operator/scalar/TestDateTimeFunctions.java b/presto-main/src/test/java/com/facebook/presto/operator/scalar/TestDateTimeFunctions.java index 33529708d119d..1a939a01b3461 100644 --- a/presto-main/src/test/java/com/facebook/presto/operator/scalar/TestDateTimeFunctions.java +++ b/presto-main/src/test/java/com/facebook/presto/operator/scalar/TestDateTimeFunctions.java @@ -99,5 +99,8 @@ public void testNotSupportedTimeZone() { String sql = "FROM_UNIXTIME(a.column1, 'yyyy-mm-dd') FROM (VALUES (1)) a (column1)"; assertNotSupported(sql, "Time zone not supported: yyyy-mm-dd"); + + sql = "FROM_UNIXTIME(a.column1) AT TIME ZONE 'PDT','yyyy-MM-dd HH:mm:ss' FROM (VALUES (1)) a (column1)"; + assertNotSupported(sql, "Time zone not supported: PDT"); } }