-
Notifications
You must be signed in to change notification settings - Fork 28.3k
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
[SPARK-8944][SQL] Support casting between IntervalType and StringType #7355
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -56,4 +56,53 @@ public void toStringTest() { | |
i = new Interval(34, 3 * MICROS_PER_WEEK + 13 * MICROS_PER_HOUR + 123); | ||
assertEquals(i.toString(), "interval 2 years 10 months 3 weeks 13 hours 123 microseconds"); | ||
} | ||
|
||
@Test | ||
public void fromStringTest() { | ||
testSingleUnit("year", 3, 36, 0); | ||
testSingleUnit("month", 3, 3, 0); | ||
testSingleUnit("week", 3, 0, 3 * MICROS_PER_WEEK); | ||
testSingleUnit("day", 3, 0, 3 * MICROS_PER_DAY); | ||
testSingleUnit("hour", 3, 0, 3 * MICROS_PER_HOUR); | ||
testSingleUnit("minute", 3, 0, 3 * MICROS_PER_MINUTE); | ||
testSingleUnit("second", 3, 0, 3 * MICROS_PER_SECOND); | ||
testSingleUnit("millisecond", 3, 0, 3 * MICROS_PER_MILLI); | ||
testSingleUnit("microsecond", 3, 0, 3); | ||
|
||
String s; | ||
Interval i; | ||
|
||
s = "interval -5 years 23 month"; | ||
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. s -> input 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. and i -> interval |
||
i = new Interval(-5 * 12 + 23, 0); | ||
assertEquals(Interval.fromString(s), i); | ||
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. do we need to test all the combinations of units? 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. probably not - but we should definitely test each unit at least once. |
||
|
||
// Error cases | ||
i = null; | ||
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. remove i and use null directly. "i" is a bad name since it is usually used for iterating over an array. |
||
|
||
s = "interval 3month 1 hour"; | ||
assertEquals(Interval.fromString(s), i); | ||
|
||
s = "interval 3 moth 1 hour"; | ||
assertEquals(Interval.fromString(s), i); | ||
|
||
s = "interval"; | ||
assertEquals(Interval.fromString(s), i); | ||
|
||
s = "int"; | ||
assertEquals(Interval.fromString(s), i); | ||
|
||
s = ""; | ||
assertEquals(Interval.fromString(s), i); | ||
|
||
s = null; | ||
assertEquals(Interval.fromString(s), i); | ||
} | ||
|
||
private void testSingleUnit(String unit, int number, int months, long microseconds) { | ||
String s1 = "interval " + number + " " + unit; | ||
String s2 = "interval " + number + " " + unit + "s"; | ||
Interval i = new Interval(months, microseconds); | ||
assertEquals(Interval.fromString(s1), i); | ||
assertEquals(Interval.fromString(s2), i); | ||
} | ||
} |
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.
add more comments explaining the regex, since most people would need to look up documentation on the regex.