Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix DateTimeException error classification in varchar casts. #2

Merged
merged 3 commits into from
Nov 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import io.prestosql.spi.type.LongTimestamp;
import io.prestosql.type.DateTimes;

import java.time.DateTimeException;
import java.time.ZonedDateTime;
import java.util.regex.Matcher;

Expand Down Expand Up @@ -53,6 +54,11 @@ public static long castToShort(@LiteralParameter("p") long precision, @SqlType("
catch (IllegalArgumentException e) {
throw new PrestoException(INVALID_CAST_ARGUMENT, "Value cannot be cast to timestamp: " + value.toStringUtf8(), e);
}
catch (DateTimeException e) {
//Leverage highly specific error message from the source exception.
throw new PrestoException(INVALID_CAST_ARGUMENT,
String.format("Value cannot be cast to timestamp; %s.", e.getMessage()), e);
}
}

@LiteralParameters({"x", "p"})
Expand All @@ -65,6 +71,11 @@ public static LongTimestamp castToLong(@LiteralParameter("p") long precision, @S
catch (IllegalArgumentException e) {
throw new PrestoException(INVALID_CAST_ARGUMENT, "Value cannot be cast to timestamp: " + value.toStringUtf8(), e);
}
catch (DateTimeException e) {
//Leverage highly specific error message from the source exception.
throw new PrestoException(INVALID_CAST_ARGUMENT,
String.format("Value cannot be cast to timestamp; %s. ", e.getMessage()), e);
}
}

@VisibleForTesting
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,15 @@ public void testCastToTimestampWithTimeZone()
"2001-01-22 03:04:05.321 " + DATE_TIME_ZONE.getID());
}

@Test
public void testCastInvalidVarcharToTimestamp()
{
assertInvalidCast("cast('2020-13-01 23:59:01' as timestamp)",
"Value cannot be cast to timestamp; Invalid value for MonthOfYear (valid values 1 - 12): 13.");
assertInvalidCast("cast('2020-12-01 24:00:00' as timestamp)",
"Value cannot be cast to timestamp; Invalid value for HourOfDay (valid values 0 - 23): 24.");
}

@Test
public void testCastToSlice()
{
Expand Down