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

SQL: Enhance timestamp escaped literal parsing #52097

Merged
merged 5 commits into from
Feb 10, 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 @@ -32,11 +32,16 @@ public final class DateUtils {
public static final LocalDate EPOCH = LocalDate.of(1970, 1, 1);
public static final long DAY_IN_MILLIS = 60 * 60 * 24 * 1000L;

private static final DateTimeFormatter DATE_TIME_ESCAPED_LITERAL_FORMATTER = new DateTimeFormatterBuilder()
.append(ISO_LOCAL_DATE)
.appendLiteral(" ")
.append(ISO_LOCAL_TIME)
.toFormatter().withZone(UTC);
private static final DateTimeFormatter DATE_TIME_ESCAPED_LITERAL_FORMATTER_WHITESPACE = new DateTimeFormatterBuilder()
.append(ISO_LOCAL_DATE)
.appendLiteral(' ')
.append(ISO_LOCAL_TIME)
.toFormatter().withZone(UTC);
private static final DateTimeFormatter DATE_TIME_ESCAPED_LITERAL_FORMATTER_T_LITERAL = new DateTimeFormatterBuilder()
.append(ISO_LOCAL_DATE)
.appendLiteral('T')
.append(ISO_LOCAL_TIME)
.toFormatter().withZone(UTC);

private static final DateFormatter UTC_DATE_TIME_FORMATTER = DateFormatter.forPattern("date_optional_time").withZone(UTC);
private static final int DEFAULT_PRECISION_FOR_CURRENT_FUNCTIONS = 3;
Expand Down Expand Up @@ -105,7 +110,13 @@ public static ZonedDateTime asDateTime(String dateFormat) {
}

public static ZonedDateTime ofEscapedLiteral(String dateFormat) {
return ZonedDateTime.parse(dateFormat, DATE_TIME_ESCAPED_LITERAL_FORMATTER.withZone(UTC));
int separatorIdx = dateFormat.lastIndexOf('-') + 3;
// Avoid index out of bounds - it will lead to DateTimeParseException anyways
if (separatorIdx >= dateFormat.length() || dateFormat.charAt(separatorIdx) == 'T') {
return ZonedDateTime.parse(dateFormat, DATE_TIME_ESCAPED_LITERAL_FORMATTER_T_LITERAL.withZone(UTC));
} else {
return ZonedDateTime.parse(dateFormat, DATE_TIME_ESCAPED_LITERAL_FORMATTER_WHITESPACE.withZone(UTC));
}
}

public static String toString(ZonedDateTime dateTime) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,33 @@ private Literal timestampLiteral(String date) {
return (Literal) exp;
}

private String buildDate() {
StringBuilder sb = new StringBuilder();
int length = randomIntBetween(4, 9);

if (randomBoolean()) {
sb.append('-');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand this. Why is the date starting with -?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To test negative Year.

} else {
if (length > 4) {
sb.append('-');
}
}

for (int i = 1; i <= length; i++) {
sb.append(i);
}
sb.append("-05-10");
return sb.toString();
}

private String buildSecsAndFractional() {
if (randomBoolean()) {
return ":55" + randomFrom("", ".1", ".12", ".123", ".1234", ".12345", ".123456",
".1234567", ".12345678", ".123456789");
}
return "";
}

private Literal guidLiteral(String guid) {
Expression exp = parser.createExpression(buildExpression("guid", "'%s'", guid));
assertThat(exp, instanceOf(Expression.class));
Expand Down Expand Up @@ -185,7 +212,7 @@ public void testFunctionWithFunctionWithArgAndParams() {
}

public void testDateLiteral() {
Literal l = dateLiteral("2012-01-01");
Literal l = dateLiteral(buildDate());
assertThat(l.dataType(), is(DATE));
}

Expand All @@ -197,7 +224,7 @@ public void testDateLiteralValidation() {
}

public void testTimeLiteral() {
Literal l = timeLiteral("12:23:56");
Literal l = timeLiteral("12:23" + buildSecsAndFractional());
assertThat(l.dataType(), is(TIME));
}

Expand All @@ -209,14 +236,27 @@ public void testTimeLiteralValidation() {
}

public void testTimestampLiteral() {
Literal l = timestampLiteral("2012-01-01 10:01:02.3456");
Literal l = timestampLiteral(buildDate() + " 10:20" + buildSecsAndFractional());
assertThat(l.dataType(), is(DATETIME));
l = timestampLiteral(buildDate() + "T11:22" + buildSecsAndFractional());
assertThat(l.dataType(), is(DATETIME));
}

public void testTimestampLiteralValidation() {
ParsingException ex = expectThrows(ParsingException.class, () -> timestampLiteral("2012-01-01T10:01:02.3456"));
String date = buildDate();
ParsingException ex = expectThrows(ParsingException.class, () -> timestampLiteral(date+ "_AB 10:01:02.3456"));
assertEquals(
"line 1:2: Invalid timestamp received; Text '" + date + "_AB 10:01:02.3456' could not be parsed at index " +
date.length(),
ex.getMessage());
ex = expectThrows(ParsingException.class, () -> timestampLiteral("20120101_AB 10:01:02.3456"));
assertEquals(
"line 1:2: Invalid timestamp received; Text '20120101_AB 10:01:02.3456' could not be parsed at index 0",
ex.getMessage());

ex = expectThrows(ParsingException.class, () -> timestampLiteral(date));
assertEquals(
"line 1:2: Invalid timestamp received; Text '2012-01-01T10:01:02.3456' could not be parsed at index 10",
"line 1:2: Invalid timestamp received; Text '" + date + "' could not be parsed at index " + date.length(),
ex.getMessage());
}

Expand Down