Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Special pattern for min-sec
  • Loading branch information
MaxGekk committed Oct 31, 2019
commit 140a26fbe9836908bd3094d15a9f4dc234346fc8
Original file line number Diff line number Diff line change
Expand Up @@ -196,20 +196,13 @@ object IntervalUtils {
UnitName.day -> (0, Integer.MAX_VALUE, Math.multiplyExact(_, DateTimeUtils.MICROS_PER_DAY))
)

private val signRe = "(?<sign>[+|-])?"
private val dayRe = "((?<day>\\d+)\\s+)?"
private val signRe = "(?<sign>[+|-])"
private val dayRe = "((?<day>\\d+)\\s+)"
private val hourRe = "(?<hour>\\d{1,2}+)"
private val minuteRe = "(?<minute>\\d{1,2}+)"
private val secondRe = "(?<second>(\\d{1,2}+)(\\.(\\d{1,9}+))?)"

private val dayTimeRe = Map(
(UnitName.minute, UnitName.second) -> (s"^$signRe$minuteRe:$secondRe$$").r,
(UnitName.hour, UnitName.minute) -> (s"^$signRe$hourRe:$minuteRe$$").r,
(UnitName.hour, UnitName.second) -> (s"^$signRe$hourRe:$minuteRe:$secondRe$$").r,
(UnitName.day, UnitName.hour) -> (s"^$signRe$dayRe$hourRe$$").r,
(UnitName.day, UnitName.minute) -> (s"^$signRe$dayRe$hourRe:$minuteRe$$").r,
(UnitName.day, UnitName.second) -> (s"^$signRe$dayRe$hourRe:$minuteRe:$secondRe$$").r
)
private val minsecRe = (s"^$signRe?$dayRe?($hourRe:)?$minuteRe:$secondRe$$").r
private val daysecRe = (s"^$signRe?$dayRe?$hourRe(:$minuteRe(:$secondRe)?)?$$").r

private def unitsRange(start: UnitName.Value, end: UnitName.Value): Seq[UnitName.Value] = {
(start.id to end.id).map(UnitName(_))
Expand All @@ -230,9 +223,10 @@ object IntervalUtils {
to: UnitName.Value): CalendarInterval = {
require(input != null, "Interval day-time string must be not null")
assert(input.length == input.trim.length)
require(dayTimeRe.contains(from -> to),
s"Cannot support (interval '$input' $from to $to) expression")
val pattern = dayTimeRe(from, to).pattern
val pattern = (from, to) match {
case (UnitName.minute, UnitName.second) => minsecRe.pattern
case _ => daysecRe.pattern
}
val m = pattern.matcher(input)
require(m.matches, s"Interval string must match day-time format of '$pattern': $input")

Expand Down Expand Up @@ -327,6 +321,7 @@ object IntervalUtils {
Long.MaxValue / DateTimeUtils.MICROS_PER_SECOND) * DateTimeUtils.MICROS_PER_SECOND
}

if (secondNano == null) return 0L
secondNano.split("\\.") match {
case Array(secondsStr) => parseSeconds(secondsStr)
case Array("", nanosStr) => parseNanos(nanosStr, false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,46 +149,46 @@ interval 1 days 2 hours 3 minutes 4 seconds
-- !query 18
SELECT interval '1 2:03' hour to minute
-- !query 18 schema
struct<interval 1 days 2 hours 3 minutes:interval>
struct<interval 2 hours 3 minutes:interval>
-- !query 18 output
interval 1 days 2 hours 3 minutes
interval 2 hours 3 minutes


-- !query 19
SELECT interval '1 2:03:04' hour to minute
-- !query 19 schema
struct<interval 1 days 2 hours 3 minutes:interval>
struct<interval 2 hours 3 minutes:interval>
-- !query 19 output
interval 1 days 2 hours 3 minutes
interval 2 hours 3 minutes


-- !query 20
SELECT interval '1 2:03' hour to second
-- !query 20 schema
struct<interval 1 days 2 hours 3 minutes:interval>
struct<interval 2 hours 3 minutes:interval>
-- !query 20 output
interval 1 days 2 hours 3 minutes
interval 2 hours 3 minutes


-- !query 21
SELECT interval '1 2:03:04' hour to second
-- !query 21 schema
struct<interval 1 days 2 hours 3 minutes 4 seconds:interval>
struct<interval 2 hours 3 minutes 4 seconds:interval>
-- !query 21 output
interval 1 days 2 hours 3 minutes 4 seconds
interval 2 hours 3 minutes 4 seconds


-- !query 22
SELECT interval '1 2:03' minute to second
-- !query 22 schema
struct<interval 1 days 2 minutes 3 seconds:interval>
struct<interval 2 minutes 3 seconds:interval>
-- !query 22 output
interval 1 days 2 minutes 3 seconds
interval 2 minutes 3 seconds


-- !query 23
SELECT interval '1 2:03:04' minute to second
-- !query 23 schema
struct<interval 1 days 2 hours 3 minutes 4 seconds:interval>
struct<interval 3 minutes 4 seconds:interval>
-- !query 23 output
interval 1 days 2 hours 3 minutes 4 seconds
interval 3 minutes 4 seconds