-
Notifications
You must be signed in to change notification settings - Fork 25.2k
SQL: Make parsing of date more lenient #52137
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
32850d4
SQL: Make parsing of date more lenient
matriv 381741f
Address comments
matriv 6e6ef0b
Merge remote-tracking branch 'upstream/master' into fix-49379
matriv 89bf5e8
address comments
matriv 7241b91
Merge remote-tracking branch 'upstream/master' into fix-49379
matriv 62f5919
Add more invalid date parsing tests
matriv File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -172,19 +172,37 @@ public void testConversionToDate() { | |
Converter conversion = converterFor(KEYWORD, to); | ||
assertNull(conversion.convert(null)); | ||
|
||
assertEquals(date(0L), conversion.convert("1970-01-01")); | ||
assertEquals(date(1483228800000L), conversion.convert("2017-01-01")); | ||
assertEquals(date(-1672531200000L), conversion.convert("1917-01-01")); | ||
assertEquals(date(18000000L), conversion.convert("1970-01-01")); | ||
assertEquals(date(1581292800000L), conversion.convert("2020-02-10T10:20")); | ||
assertEquals(date(-125908819200000L), conversion.convert("-2020-02-10T10:20:30.123")); | ||
assertEquals(date(1581292800000L), conversion.convert("2020-02-10T10:20:30.123456789")); | ||
|
||
// double check back and forth conversion | ||
assertEquals(date(1581292800000L), conversion.convert("2020-02-10 10:20")); | ||
assertEquals(date(-125908819200000L), conversion.convert("-2020-02-10 10:20:30.123")); | ||
assertEquals(date(1581292800000L), conversion.convert("2020-02-10 10:20:30.123456789")); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For sake of completeness, I'd suggest a test without the duration separator, but with a timezone (or a |
||
|
||
assertEquals(date(1581292800000L), conversion.convert("2020-02-10T10:20+05:00")); | ||
assertEquals(date(-125908819200000L), conversion.convert("-2020-02-10T10:20:30.123-06:00")); | ||
assertEquals(date(1581292800000L), conversion.convert("2020-02-10T10:20:30.123456789+03:00")); | ||
|
||
assertEquals(date(1581292800000L), conversion.convert("2020-02-10 10:20+05:00")); | ||
assertEquals(date(-125908819200000L), conversion.convert("-2020-02-10 10:20:30.123-06:00")); | ||
assertEquals(date(1581292800000L), conversion.convert("2020-02-10 10:20:30.123456789+03:00")); | ||
|
||
// double check back and forth conversion | ||
ZonedDateTime zdt = org.elasticsearch.common.time.DateUtils.nowWithMillisResolution(); | ||
Converter forward = converterFor(DATE, KEYWORD); | ||
Converter back = converterFor(KEYWORD, DATE); | ||
assertEquals(asDateOnly(zdt), back.convert(forward.convert(zdt))); | ||
Exception e = expectThrows(QlIllegalArgumentException.class, () -> conversion.convert("0xff")); | ||
assertEquals("cannot cast [0xff] to [date]: Text '0xff' could not be parsed at index 0", e.getMessage()); | ||
e = expectThrows(QlIllegalArgumentException.class, () -> conversion.convert("2020-02-")); | ||
assertEquals("cannot cast [2020-02-] to [date]: Text '2020-02-' could not be parsed at index 8", e.getMessage()); | ||
e = expectThrows(QlIllegalArgumentException.class, () -> conversion.convert("2020-")); | ||
assertEquals("cannot cast [2020-] to [date]: Text '2020-' could not be parsed at index 5", e.getMessage()); | ||
e = expectThrows(QlIllegalArgumentException.class, () -> conversion.convert("-2020-02-")); | ||
assertEquals("cannot cast [-2020-02-] to [date]: Text '-2020-02-' could not be parsed at index 9", e.getMessage()); | ||
e = expectThrows(QlIllegalArgumentException.class, () -> conversion.convert("-2020-")); | ||
assertEquals("cannot cast [-2020-] to [date]: Text '-2020-' could not be parsed at index 6", e.getMessage()); | ||
} | ||
} | ||
|
||
|
@@ -285,7 +303,6 @@ public void testConversionToDateTime() { | |
assertEquals(dateTime(18000000L), conversion.convert("1970-01-01T00:00:00-05:00")); | ||
|
||
// double check back and forth conversion | ||
|
||
ZonedDateTime dt = org.elasticsearch.common.time.DateUtils.nowWithMillisResolution(); | ||
Converter forward = converterFor(DATETIME, KEYWORD); | ||
Converter back = converterFor(KEYWORD, DATETIME); | ||
|
@@ -692,4 +709,4 @@ static ZonedDateTime date(long millisSinceEpoch) { | |
static OffsetTime time(long millisSinceEpoch) { | ||
return DateUtils.asTimeOnly(millisSinceEpoch); | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In which situation is the
>
valid here? (I refer toseparatorIdx >= dateFormat.length()
) Isn't==
enough to detect a date-only string?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
2 lines above we do
+3
to move directly after the day part in the string.If the string passed is something like:
2020-12-
then we have moved passed the string's length anddateFormat.charAt()
will throw OutOfBounds exception.I forgot to add a test for that, will do.