-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-28107][SQL] Support 'DAY TO (HOUR|MINUTE|SECOND)', 'HOUR TO (MINUTE|SECOND)' and 'MINUTE TO SECOND' #25000
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
Changes from 1 commit
8c99767
ed4b3cf
3edfed6
fa68a41
2b5408d
c41dccc
d68ea26
7fd926a
bf78d73
ebe917c
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 |
|---|---|---|
|
|
@@ -54,8 +54,8 @@ private static String unitRegex(String unit) { | |
| private static Pattern yearMonthPattern = | ||
| Pattern.compile("^(?:['|\"])?([+|-])?(\\d+)-(\\d+)(?:['|\"])?$"); | ||
|
|
||
| private static Pattern dayTimePattern = | ||
| Pattern.compile("^(?:['|\"])?([+|-])?((\\d+) )?(\\d+):(\\d+):(\\d+)(\\.(\\d+))?(?:['|\"])?$"); | ||
| private static Pattern dayTimePattern = Pattern.compile( | ||
| "^(?:['|\"])?([+|-])?((\\d+) )?(\\d+):(\\d+)(:(\\d+))?(\\.(\\d+))?(?:['|\"])?$"); | ||
|
|
||
| private static Pattern quoteTrimPattern = Pattern.compile("^(?:['|\"])?(.*?)(?:['|\"])?$"); | ||
|
|
||
|
|
@@ -159,8 +159,13 @@ public static CalendarInterval fromYearMonthString(String s) throws IllegalArgum | |
| * | ||
| * adapted from HiveIntervalDayTime.valueOf | ||
| */ | ||
| public static CalendarInterval fromDayTimeString(String s) throws IllegalArgumentException { | ||
| CalendarInterval result = null; | ||
| public static CalendarInterval fromDayTimeString(String s) { | ||
| return fromDayTimeString(s, "day", "second"); | ||
| } | ||
|
|
||
| public static CalendarInterval fromDayTimeString(String s, String from, String to) | ||
dongjoon-hyun marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| throws IllegalArgumentException { | ||
dongjoon-hyun marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| CalendarInterval result; | ||
| if (s == null) { | ||
| throw new IllegalArgumentException("Interval day-time string was null"); | ||
| } | ||
|
|
@@ -174,11 +179,41 @@ public static CalendarInterval fromDayTimeString(String s) throws IllegalArgumen | |
| int sign = m.group(1) != null && m.group(1).equals("-") ? -1 : 1; | ||
| long days = m.group(2) == null ? 0 : toLongWithRange("day", m.group(3), | ||
dongjoon-hyun marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 0, Integer.MAX_VALUE); | ||
| long hours = toLongWithRange("hour", m.group(4), 0, 23); | ||
| long minutes = toLongWithRange("minute", m.group(5), 0, 59); | ||
| long seconds = toLongWithRange("second", m.group(6), 0, 59); | ||
| long hours; | ||
| long minutes; | ||
| long seconds; | ||
| switch (from) { | ||
| case "minute": | ||
| hours = m.group(6) == null ? 0 : toLongWithRange("hour", m.group(4), 0, 23); | ||
| minutes = m.group(6) == null ? toLongWithRange("minute", m.group(4), 0, 59) | ||
| : toLongWithRange("minute", m.group(5), 0, 59); | ||
| seconds = m.group(6) == null ? toLongWithRange("minute", m.group(5), 0, 59) | ||
| : toLongWithRange("second", m.group(7), 0, 59); | ||
| break; | ||
| default: | ||
|
||
| if(m.group(8) != null && m.group(6) == null) { | ||
| hours = 0; | ||
| minutes = toLongWithRange("minute", m.group(4), 0, 59); | ||
| seconds = toLongWithRange("second", m.group(5), 0, 59); | ||
| } else { | ||
| hours = toLongWithRange("hour", m.group(4), 0, 23); | ||
| minutes = toLongWithRange("minute", m.group(5), 0, 59); | ||
| seconds = toLongWithRange("second", m.group(7), 0, 59); | ||
| } | ||
| } | ||
| // Hive allow nanosecond precision interval | ||
| long nanos = toLongWithRange("nanosecond", m.group(8), 0L, 999999999L); | ||
| long nanos = toLongWithRange("nanosecond", m.group(9), 0L, 999999999L); | ||
| switch (to) { | ||
| case "minute": | ||
| seconds = 0; | ||
| nanos = 0; | ||
| break; | ||
| case "hour": | ||
| minutes = 0; | ||
| seconds = 0; | ||
| nanos = 0; | ||
| break; | ||
dongjoon-hyun marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| result = new CalendarInterval(0, sign * ( | ||
| days * MICROS_PER_DAY + hours * MICROS_PER_HOUR + minutes * MICROS_PER_MINUTE + | ||
| seconds * MICROS_PER_SECOND + nanos / 1000L)); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1180,12 +1180,34 @@ class SQLQuerySuite extends QueryTest with SQLTestUtils with TestHiveSingleton { | |
| test("Convert hive interval term into Literal of CalendarIntervalType") { | ||
| checkAnswer(sql("select interval '10-9' year to month"), | ||
| Row(CalendarInterval.fromString("interval 10 years 9 months"))) | ||
| checkAnswer(sql("select interval '20 15:40:32.99899999' day to hour"), | ||
|
Member
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. All these test cases are positive ones. Could you add the negative cases too? Do they throw an exception? Exception messages make sense? |
||
| Row(CalendarInterval.fromString("interval 2 weeks 6 days 15 hours"))) | ||
|
||
| checkAnswer(sql("select interval '20 15:40:32.99899999' day to minute"), | ||
| Row(CalendarInterval.fromString("interval 2 weeks 6 days 15 hours 40 minutes"))) | ||
|
||
| checkAnswer(sql("select interval '20 15:40:32.99899999' day to second"), | ||
| Row(CalendarInterval.fromString("interval 2 weeks 6 days 15 hours 40 minutes " + | ||
| "32 seconds 99 milliseconds 899 microseconds"))) | ||
| checkAnswer(sql("select interval '15:40:32.99899999' hour to minute"), | ||
| Row(CalendarInterval.fromString("interval 15 hours 40 minutes"))) | ||
| checkAnswer(sql("select interval '15:40.99899999' hour to second"), | ||
| Row(CalendarInterval.fromString("interval 15 minutes 40 seconds 99 milliseconds " + | ||
| "899 microseconds"))) | ||
| checkAnswer(sql("select interval '15:40' hour to second"), | ||
| Row(CalendarInterval.fromString("interval 15 hours 40 minutes"))) | ||
| checkAnswer(sql("select interval '15:40:32.99899999' hour to second"), | ||
| Row(CalendarInterval.fromString("interval 15 hours 40 minutes 32 seconds 99 milliseconds " + | ||
| "899 microseconds"))) | ||
| checkAnswer(sql("select interval '20 40:32.99899999' minute to second"), | ||
| Row(CalendarInterval.fromString("interval 2 weeks 6 days 40 minutes 32 seconds " + | ||
| "99 milliseconds 899 microseconds"))) | ||
| checkAnswer(sql("select interval '40:32.99899999' minute to second"), | ||
| Row(CalendarInterval.fromString("interval 40 minutes 32 seconds 99 milliseconds " + | ||
| "899 microseconds"))) | ||
| checkAnswer(sql("select interval '40:32.99899999' minute to second"), | ||
| Row(CalendarInterval.fromString("interval 40 minutes 32 seconds 99 milliseconds " + | ||
| "899 microseconds"))) | ||
|
||
| checkAnswer(sql("select interval '40:32' minute to second"), | ||
| Row(CalendarInterval.fromString("interval 40 minutes 32 seconds"))) | ||
| checkAnswer(sql("select interval '30' year"), | ||
| Row(CalendarInterval.fromString("interval 30 years"))) | ||
| checkAnswer(sql("select interval '25' month"), | ||
|
|
||
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.
We still need
throws IllegalArgumentExceptionhere.