-
Notifications
You must be signed in to change notification settings - Fork 28.9k
[SPARK-29532][SQL] Simplify interval string parsing #26190
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 all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
c8d81e3
simplify interval string parsing
cloud-fan 87417d9
address comments
cloud-fan a5f355f
fix test
cloud-fan 10aa751
re-generate golden file
cloud-fan 5c5e251
address comments
cloud-fan 03e90e4
update benchmark
cloud-fan 836e868
fix
cloud-fan 33ceedc
update
cloud-fan f759fd5
Regenerated result on EC2 (#14)
dongjoon-hyun 77f69a1
Merge remote-tracking branch 'origin/master' into parser
cloud-fan 48b7ef4
more tests
cloud-fan 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 |
|---|---|---|
|
|
@@ -32,94 +32,11 @@ public final class CalendarInterval implements Serializable { | |
| public static final long MICROS_PER_DAY = MICROS_PER_HOUR * 24; | ||
| public static final long MICROS_PER_WEEK = MICROS_PER_DAY * 7; | ||
|
|
||
| /** | ||
| * A function to generate regex which matches interval string's unit part like "3 years". | ||
| * | ||
| * First, we can leave out some units in interval string, and we only care about the value of | ||
| * unit, so here we use non-capturing group to wrap the actual regex. | ||
| * At the beginning of the actual regex, we should match spaces before the unit part. | ||
| * Next is the number part, starts with an optional "-" to represent negative value. We use | ||
| * capturing group to wrap this part as we need the value later. | ||
| * Finally is the unit name, ends with an optional "s". | ||
| */ | ||
| private static String unitRegex(String unit) { | ||
| return "(?:\\s+(-?\\d+)\\s+" + unit + "s?)?"; | ||
| } | ||
|
|
||
| private static Pattern p = Pattern.compile("interval" + unitRegex("year") + unitRegex("month") + | ||
| unitRegex("week") + unitRegex("day") + unitRegex("hour") + unitRegex("minute") + | ||
| unitRegex("second") + unitRegex("millisecond") + unitRegex("microsecond"), | ||
| Pattern.CASE_INSENSITIVE); | ||
|
|
||
| private static Pattern yearMonthPattern = | ||
| Pattern.compile("^(?:['|\"])?([+|-])?(\\d+)-(\\d+)(?:['|\"])?$"); | ||
| private static Pattern yearMonthPattern = Pattern.compile( | ||
| "^([+|-])?(\\d+)-(\\d+)$"); | ||
|
|
||
| private static Pattern dayTimePattern = Pattern.compile( | ||
| "^(?:['|\"])?([+|-])?((\\d+) )?((\\d+):)?(\\d+):(\\d+)(\\.(\\d+))?(?:['|\"])?$"); | ||
|
|
||
| private static Pattern quoteTrimPattern = Pattern.compile("^(?:['|\"])?(.*?)(?:['|\"])?$"); | ||
|
|
||
| private static long toLong(String s) { | ||
| if (s == null) { | ||
| return 0; | ||
| } else { | ||
| return Long.parseLong(s); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Convert a string to CalendarInterval. Return null if the input string is not a valid interval. | ||
| * This method is case-insensitive. | ||
| */ | ||
| public static CalendarInterval fromString(String s) { | ||
| try { | ||
| return fromCaseInsensitiveString(s); | ||
| } catch (IllegalArgumentException e) { | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * 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) { | ||
| throw new IllegalArgumentException("Interval cannot be null"); | ||
| } | ||
| String trimmed = s.trim(); | ||
| if (trimmed.isEmpty()) { | ||
| throw new IllegalArgumentException("Interval cannot be blank"); | ||
| } | ||
| String prefix = "interval"; | ||
| String intervalStr = trimmed; | ||
| // Checks the given interval string does not start with the `interval` prefix | ||
| if (!intervalStr.regionMatches(true, 0, prefix, 0, prefix.length())) { | ||
| // Prepend `interval` if it does not present because | ||
| // the regular expression strictly require it. | ||
| 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); | ||
| } | ||
|
|
||
| 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); | ||
| } | ||
| "^([+|-])?((\\d+) )?((\\d+):)?(\\d+):(\\d+)(\\.(\\d+))?$"); | ||
|
|
||
| public static long toLongWithRange(String fieldName, | ||
|
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. Maybe, throw an exception if 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. maybe it's better, but I'd like to avoid changing unrelated code. |
||
| String s, long minValue, long maxValue) throws IllegalArgumentException { | ||
|
|
@@ -242,72 +159,59 @@ public static CalendarInterval fromDayTimeString(String s, String from, String t | |
| return result; | ||
| } | ||
|
|
||
| public static CalendarInterval fromSingleUnitString(String unit, String s) | ||
| public static CalendarInterval fromUnitStrings(String[] units, String[] values) | ||
| throws IllegalArgumentException { | ||
| assert units.length == values.length; | ||
| int months = 0; | ||
| long microseconds = 0; | ||
|
|
||
| CalendarInterval result = null; | ||
| if (s == null) { | ||
| throw new IllegalArgumentException(String.format("Interval %s string was null", unit)); | ||
| } | ||
| s = s.trim(); | ||
| Matcher m = quoteTrimPattern.matcher(s); | ||
| if (!m.matches()) { | ||
| throw new IllegalArgumentException( | ||
| "Interval string does not match day-time format of 'd h:m:s.n': " + s); | ||
| } else { | ||
| for (int i = 0; i < units.length; i++) { | ||
| try { | ||
| switch (unit) { | ||
| switch (units[i]) { | ||
| case "year": | ||
| int year = (int) toLongWithRange("year", m.group(1), | ||
| Integer.MIN_VALUE / 12, Integer.MAX_VALUE / 12); | ||
| result = new CalendarInterval(year * 12, 0L); | ||
| months = Math.addExact(months, Math.multiplyExact(Integer.parseInt(values[i]), 12)); | ||
| break; | ||
| case "month": | ||
| int month = (int) toLongWithRange("month", m.group(1), | ||
| Integer.MIN_VALUE, Integer.MAX_VALUE); | ||
| result = new CalendarInterval(month, 0L); | ||
| months = Math.addExact(months, Integer.parseInt(values[i])); | ||
| break; | ||
| case "week": | ||
| long week = toLongWithRange("week", m.group(1), | ||
| Long.MIN_VALUE / MICROS_PER_WEEK, Long.MAX_VALUE / MICROS_PER_WEEK); | ||
| result = new CalendarInterval(0, week * MICROS_PER_WEEK); | ||
| microseconds = Math.addExact( | ||
| microseconds, | ||
| Math.multiplyExact(Long.parseLong(values[i]), MICROS_PER_WEEK)); | ||
| break; | ||
| case "day": | ||
| long day = toLongWithRange("day", m.group(1), | ||
| Long.MIN_VALUE / MICROS_PER_DAY, Long.MAX_VALUE / MICROS_PER_DAY); | ||
| result = new CalendarInterval(0, day * MICROS_PER_DAY); | ||
| microseconds = Math.addExact( | ||
| microseconds, | ||
| Math.multiplyExact(Long.parseLong(values[i]), MICROS_PER_DAY)); | ||
| break; | ||
| case "hour": | ||
| long hour = toLongWithRange("hour", m.group(1), | ||
| Long.MIN_VALUE / MICROS_PER_HOUR, Long.MAX_VALUE / MICROS_PER_HOUR); | ||
| result = new CalendarInterval(0, hour * MICROS_PER_HOUR); | ||
| microseconds = Math.addExact( | ||
| microseconds, | ||
| Math.multiplyExact(Long.parseLong(values[i]), MICROS_PER_HOUR)); | ||
| break; | ||
| case "minute": | ||
| long minute = toLongWithRange("minute", m.group(1), | ||
| Long.MIN_VALUE / MICROS_PER_MINUTE, Long.MAX_VALUE / MICROS_PER_MINUTE); | ||
| result = new CalendarInterval(0, minute * MICROS_PER_MINUTE); | ||
| microseconds = Math.addExact( | ||
| microseconds, | ||
| Math.multiplyExact(Long.parseLong(values[i]), MICROS_PER_MINUTE)); | ||
| break; | ||
| case "second": { | ||
| long micros = parseSecondNano(m.group(1)); | ||
| result = new CalendarInterval(0, micros); | ||
| microseconds = Math.addExact(microseconds, parseSecondNano(values[i])); | ||
| break; | ||
| } | ||
| case "millisecond": | ||
| long millisecond = toLongWithRange("millisecond", m.group(1), | ||
| Long.MIN_VALUE / MICROS_PER_MILLI, Long.MAX_VALUE / MICROS_PER_MILLI); | ||
| result = new CalendarInterval(0, millisecond * MICROS_PER_MILLI); | ||
| microseconds = Math.addExact( | ||
| microseconds, | ||
| Math.multiplyExact(Long.parseLong(values[i]), MICROS_PER_MILLI)); | ||
| break; | ||
| case "microsecond": { | ||
| long micros = Long.parseLong(m.group(1)); | ||
| result = new CalendarInterval(0, micros); | ||
| case "microsecond": | ||
| microseconds = Math.addExact(microseconds, Long.parseLong(values[i])); | ||
| break; | ||
| } | ||
| } | ||
| } catch (Exception e) { | ||
| throw new IllegalArgumentException("Error parsing interval string: " + e.getMessage(), e); | ||
| } | ||
| } | ||
| return result; | ||
| return new CalendarInterval(months, microseconds); | ||
| } | ||
|
|
||
| /** | ||
|
|
||
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.