You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by GitBox <gi...@apache.org> on 2019/10/26 10:25:28 UTC

[GitHub] [spark] MaxGekk commented on a change in pull request #26261: [SPARK-29607][SQL] Move static methods from CalendarInterval to IntervalUtils

MaxGekk 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_r339295170
 
 

 ##########
 File path: sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/IntervalUtils.scala
 ##########
 @@ -119,4 +121,198 @@ object IntervalUtils {
       case _: IllegalArgumentException => null
     }
   }
+
+  private val yearMonthPattern = Pattern.compile("^([+|-])?(\\d+)-(\\d+)$")
+
+  private val dayTimePattern = Pattern.compile(
+    "^([+|-])?((\\d+) )?((\\d+):)?(\\d+):(\\d+)(\\.(\\d+))?$")
+
+  def toLongWithRange(fieldName: String, s: String, minValue: Long, maxValue: Long): Long = {
+    var result = 0L
+    if (s != null) {
+      result = java.lang.Long.parseLong(s)
+      if (result < minValue || result > maxValue) {
+        throw new IllegalArgumentException(
+          s"$fieldName $result outside range [$minValue, $maxValue]")
+      }
+    }
+    result
+  }
+
+  /**
+   * Parse YearMonth string in form: [-]YYYY-MM
+   *
+   * adapted from HiveIntervalYearMonth.valueOf
+   */
+  def fromYearMonthString(input: String): CalendarInterval = {
+    if (input == null) throw new IllegalArgumentException("Interval year-month string was null")
+    val s = input.trim
+    val m = yearMonthPattern.matcher(s)
+    if (!m.matches) {
+      throw new IllegalArgumentException(
+        "Interval string does not match year-month format of 'y-m': " + s)
+    }
+
+    try {
+      val sign = if (m.group(1) != null && m.group(1) == "-") -1 else 1
+      val years = toLongWithRange("year", m.group(2), 0, Integer.MAX_VALUE).toInt
+      val months = toLongWithRange("month", m.group(3), 0, 11).toInt
+      new CalendarInterval(sign * (years * 12 + months), 0)
+    } catch {
+      case e: Exception =>
+        throw new IllegalArgumentException(
+          "Error parsing interval year-month string: " + e.getMessage, e)
+    }
+  }
+
+  /**
+   * 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")
+  }
+
+  /**
+   * 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 = {
+    if (input == null) {
+      throw new IllegalArgumentException("Interval day-time string was null")
+    }
+    val s = input.trim
+    val m = dayTimePattern.matcher(s)
+    if (!m.matches) {
+      throw new IllegalArgumentException(
+        "Interval string does not 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")
+      }
+      var micros = days * DateTimeUtils.MICROS_PER_DAY
+      micros += hours * MICROS_PER_HOUR
+      micros += minutes * MICROS_PER_MINUTE
+      micros += seconds * DateTimeUtils.MICROS_PER_SECOND
+      micros += nanos / DateTimeUtils.NANOS_PER_MICROS
 
 Review comment:
   because of line length limit and to replace magic constant by `DateTimeUtils.NANOS_PER_MICROS`

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org