-
Notifications
You must be signed in to change notification settings - Fork 29.3k
[SPARK-29369][SQL] Support string intervals without the interval prefix
#26079
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
Closed
Closed
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
6d0564c
Make fromString tolerant to prefix
MaxGekk 60d7d34
Use Arrays.asList
MaxGekk 9253cec
Add test for missing prefix
MaxGekk eecfbf4
Test for cast
MaxGekk e254ee5
Test for the interval constructor
MaxGekk 813897b
Add comments
MaxGekk e2c1352
Regenerate literals.sql.out
MaxGekk 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,7 +18,6 @@ | |
| package org.apache.spark.unsafe.types; | ||
|
|
||
| import java.io.Serializable; | ||
| import java.util.Locale; | ||
| import java.util.regex.Matcher; | ||
| import java.util.regex.Pattern; | ||
|
|
||
|
|
@@ -73,45 +72,50 @@ private static long toLong(String s) { | |
| * This method is case-insensitive. | ||
| */ | ||
| public static CalendarInterval fromString(String s) { | ||
| if (s == null) { | ||
| return null; | ||
| } | ||
| s = s.trim(); | ||
| Matcher m = p.matcher(s); | ||
| if (!m.matches() || s.compareToIgnoreCase("interval") == 0) { | ||
| try { | ||
| return fromCaseInsensitiveString(s); | ||
| } catch (IllegalArgumentException e) { | ||
| return null; | ||
| } else { | ||
| long months = toLong(m.group(1)) * 12 + toLong(m.group(2)); | ||
| long microseconds = toLong(m.group(3)) * MICROS_PER_WEEK; | ||
| microseconds += toLong(m.group(4)) * MICROS_PER_DAY; | ||
| microseconds += toLong(m.group(5)) * MICROS_PER_HOUR; | ||
| microseconds += toLong(m.group(6)) * MICROS_PER_MINUTE; | ||
| microseconds += toLong(m.group(7)) * MICROS_PER_SECOND; | ||
| microseconds += toLong(m.group(8)) * MICROS_PER_MILLI; | ||
| microseconds += toLong(m.group(9)); | ||
| return new CalendarInterval((int) months, microseconds); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Convert a string to CalendarInterval. Unlike fromString, this method can handle | ||
| * Convert a string to CalendarInterval. This method can handle | ||
| * strings without the `interval` prefix and throws IllegalArgumentException | ||
| * when the input string is not a valid interval. | ||
| * | ||
| * @throws IllegalArgumentException if the string is not a valid internal. | ||
| */ | ||
| public static CalendarInterval fromCaseInsensitiveString(String s) { | ||
| if (s == null || s.trim().isEmpty()) { | ||
| throw new IllegalArgumentException("Interval cannot be null or blank."); | ||
| if (s == null) { | ||
| throw new IllegalArgumentException("Interval cannot be null"); | ||
| } | ||
| String sInLowerCase = s.trim().toLowerCase(Locale.ROOT); | ||
| String interval = | ||
| sInLowerCase.startsWith("interval ") ? sInLowerCase : "interval " + sInLowerCase; | ||
| CalendarInterval cal = fromString(interval); | ||
| if (cal == null) { | ||
| String trimmed = s.trim(); | ||
| if (trimmed.isEmpty()) { | ||
| throw new IllegalArgumentException("Interval cannot be blank"); | ||
| } | ||
| String prefix = "interval"; | ||
| String intervalStr = trimmed; | ||
| if (!intervalStr.regionMatches(true, 0, prefix, 0, prefix.length())) { | ||
|
Contributor
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. what does this condition mean?
Member
Author
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. I added comments about this. |
||
| intervalStr = prefix + " " + trimmed; | ||
| } else if (intervalStr.length() == prefix.length()) { | ||
| throw new IllegalArgumentException("Interval string must have time units"); | ||
| } | ||
|
|
||
| Matcher m = p.matcher(intervalStr); | ||
| if (!m.matches()) { | ||
| throw new IllegalArgumentException("Invalid interval: " + s); | ||
| } | ||
| return cal; | ||
|
|
||
| long months = toLong(m.group(1)) * 12 + toLong(m.group(2)); | ||
| long microseconds = toLong(m.group(3)) * MICROS_PER_WEEK; | ||
| microseconds += toLong(m.group(4)) * MICROS_PER_DAY; | ||
| microseconds += toLong(m.group(5)) * MICROS_PER_HOUR; | ||
| microseconds += toLong(m.group(6)) * MICROS_PER_MINUTE; | ||
| microseconds += toLong(m.group(7)) * MICROS_PER_SECOND; | ||
| microseconds += toLong(m.group(8)) * MICROS_PER_MILLI; | ||
| microseconds += toLong(m.group(9)); | ||
| return new CalendarInterval((int) months, microseconds); | ||
| } | ||
|
|
||
| public static long toLongWithRange(String fieldName, | ||
|
|
||
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
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.
Is it the only place we parse interval string? I thought we parse it with antlr parser.
Uh oh!
There was an error while loading. Please reload this page.
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.
antlr parser does this as well but it parses sql elements like
here is only the place where we parse string values:
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.
This looks duplicated. Shall we add a
parseIntervalmethod to theParserInterfaceinterface and call the parser here?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.
Maybe something has been duplicated, and can be reused but this is heavy refactoring for this PR.
For instance,
AstBuilder.visitIntervalgets already split interval units butCalendarInterval.fromString()uses regular expression to parse & split:spark/common/unsafe/src/main/java/org/apache/spark/unsafe/types/CalendarInterval.java
Lines 50 to 53 in b103449
If you don't mind, I would try to do that in a separate PR.
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.
This PR introduced code duplication #8034 for your code #7355 5 years ago.
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.
And your regexp is not tolerant to the order of interval units, see:
Uh oh!
There was an error while loading. Please reload this page.
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.
Let's keep them separate so far. And I will try to write flexible and common code in the near future for parsing string intervals that could handle other features found in #26055
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.
SGTM