cloud-fan commented on a change in pull request #26261: [SPARK-29607][SQL] Move
static methods from CalendarInterval to IntervalUtils
URL: https://github.com/apache/spark/pull/26261#discussion_r339675874
##########
File path:
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/IntervalUtils.scala
##########
@@ -119,4 +123,195 @@ object IntervalUtils {
case _: IllegalArgumentException => null
}
}
+
+ private def toLongWithRange(
+ fieldName: String,
+ s: String,
+ minValue: Long,
+ maxValue: Long): Long = {
+ val result = if (s == null) 0L else s.toLong
+ require(minValue <= result && result <= maxValue,
+ s"$fieldName $result outside range [$minValue, $maxValue]")
+
+ result
+ }
+
+ private val yearMonthPattern = "^\\s*([+|-])?(\\d+)-(\\d+)\\s*$".r
+
+ /**
+ * Parse YearMonth string in form: [+|-]YYYY-MM
+ *
+ * adapted from HiveIntervalYearMonth.valueOf
+ */
+ def fromYearMonthString(input: String): CalendarInterval = {
+ require(input != null, "Interval year-month string must be not null")
+ def toInterval(yearStr: String, monthStr: String): CalendarInterval = {
+ try {
+ val years = toLongWithRange("year", yearStr, 0,
Integer.MAX_VALUE).toInt
+ val months = toLongWithRange("month", monthStr, 0, 11).toInt
+ val totalMonths = Math.addExact(Math.multiplyExact(years, 12), months)
+ new CalendarInterval(totalMonths, 0)
+ } catch {
+ case NonFatal(e) =>
+ throw new IllegalArgumentException(
+ s"Error parsing interval year-month string: ${e.getMessage}", e)
+ }
+ }
+ input match {
+ case yearMonthPattern("-", yearStr, monthStr) =>
+ toInterval(yearStr, monthStr).negate()
+ case yearMonthPattern(_, yearStr, monthStr) =>
+ toInterval(yearStr, monthStr)
+ case _ =>
+ throw new IllegalArgumentException(
+ s"Interval string does not match year-month format of 'y-m': $input")
+ }
+ }
+
+ /**
+ * Parse dayTime string in form: [-]d HH:mm:ss.nnnnnnnnn and
[-]HH:mm:ss.nnnnnnnnn
+ *
+ * adapted from HiveIntervalDayTime.valueOf
+ */
+ def fromDayTimeString(s: String): CalendarInterval = {
+ fromDayTimeString(s, "day", "second")
+ }
+
+ private val dayTimePattern =
+ "^([+|-])?((\\d+) )?((\\d+):)?(\\d+):(\\d+)(\\.(\\d+))?$".r
+
+ /**
+ * Parse dayTime string in form: [-]d HH:mm:ss.nnnnnnnnn and
[-]HH:mm:ss.nnnnnnnnn
+ *
+ * adapted from HiveIntervalDayTime.valueOf.
+ * Below interval conversion patterns are supported:
+ * - DAY TO (HOUR|MINUTE|SECOND)
+ * - HOUR TO (MINUTE|SECOND)
+ * - MINUTE TO SECOND
+ */
+ def fromDayTimeString(input: String, from: String, to: String):
CalendarInterval = {
+ require(input != null, "Interval day-time string must be not null")
+ val s = input.trim
+ val m = dayTimePattern.pattern.matcher(s)
+ require(m.matches, s"Interval string must match day-time format of 'd
h:m:s.n': $s")
+
+ try {
+ val sign = if (m.group(1) != null && m.group(1) == "-") -1 else 1
+ val days = if (m.group(2) == null) {
+ 0
+ } else {
+ toLongWithRange("day", m.group(3), 0, Integer.MAX_VALUE)
+ }
+ var hours: Long = 0L
+ var minutes: Long = 0L
+ var seconds: Long = 0L
+ if (m.group(5) != null || from == "minute") { // 'HH:mm:ss' or 'mm:ss
minute'
+ hours = toLongWithRange("hour", m.group(5), 0, 23)
+ minutes = toLongWithRange("minute", m.group(6), 0, 59)
+ seconds = toLongWithRange("second", m.group(7), 0, 59)
+ } else if (m.group(8) != null) { // 'mm:ss.nn'
+ minutes = toLongWithRange("minute", m.group(6), 0, 59)
+ seconds = toLongWithRange("second", m.group(7), 0, 59)
+ } else { // 'HH:mm'
+ hours = toLongWithRange("hour", m.group(6), 0, 23)
+ minutes = toLongWithRange("second", m.group(7), 0, 59)
+ }
+ // Hive allow nanosecond precision interval
+ val nanoStr = if (m.group(9) == null) {
+ null
+ } else {
+ (m.group(9) + "000000000").substring(0, 9)
+ }
+ var nanos = toLongWithRange("nanosecond", nanoStr, 0L, 999999999L)
+ to match {
+ case "hour" =>
+ minutes = 0
+ seconds = 0
+ nanos = 0
+ case "minute" =>
+ seconds = 0
+ nanos = 0
+ case "second" =>
+ // No-op
+ case _ =>
+ throw new IllegalArgumentException(
+ s"Cannot support (interval '$s' $from to $to) expression")
+ }
+ new CalendarInterval(0, sign * (
+ days * DateTimeUtils.MICROS_PER_DAY + hours * MICROS_PER_HOUR +
Review comment:
inspired by
https://github.com/apache/spark/pull/26261/files#diff-eba257f41b49f470321579875f054f00R152
, we should use exact math functions here too. This can be done in a followup.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]