cloud-fan commented on a change in pull request #34494:
URL: https://github.com/apache/spark/pull/34494#discussion_r749068269



##########
File path: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/IntervalUtils.scala
##########
@@ -1253,4 +1255,50 @@ object IntervalUtils {
     }
     intervalString
   }
+
+  def integralToYearMonthInterval(v: Long, endField: Byte): Int = {
+    if (v != v.toInt) {
+      throw QueryExecutionErrors.castingCauseOverflowError(v, 
YM(endField).catalogString)
+    }
+    endField match {
+      case YEAR =>
+        try {
+          Math.multiplyExact(v.toInt, MONTHS_PER_YEAR)
+        } catch {
+          case _: ArithmeticException =>
+            throw QueryExecutionErrors.castingCauseOverflowError(v, 
YM(endField).catalogString)
+        }
+      case MONTH => v.toInt
+    }
+  }
+
+  def yearMonthIntervalToInt(v: Int, endFiled: Byte): Int = {
+    endFiled match {
+      case YEAR => v / MONTHS_PER_YEAR
+      case MONTH => v
+    }
+  }
+
+  def integralToDayTimeInterval(v: Long, endField: Byte): Long = {
+    try {
+      endField match {
+        case DAY => Math.multiplyExact(v, MICROS_PER_DAY)
+        case HOUR => Math.multiplyExact(v, MICROS_PER_HOUR)
+        case MINUTE => Math.multiplyExact(v, MICROS_PER_MINUTE)
+        case SECOND => Math.multiplyExact(v, MICROS_PER_SECOND)
+      }
+    } catch {
+      case _: ArithmeticException =>
+        throw QueryExecutionErrors.castingCauseOverflowError(v, 
DT(endField).catalogString)
+    }
+  }
+
+  def dayTimeIntervalToLong(v: Long, endFiled: Byte): Long = {
+    endFiled match {
+      case DAY => v / MICROS_PER_DAY
+      case HOUR => v / MICROS_PER_HOUR
+      case MINUTE => v / MICROS_PER_MINUTE
+      case SECOND => v / MICROS_PER_SECOND
+    }
+  }

Review comment:
       Thinking about it more, I think a better approach is
   ```
   def intToYearMonthInterval...
   def longToYearMonthInterval... (has extra overflow check and call 
intToYearMonthInterval at the end)
   def longToDayTimeInterval...
   
   def yearMonthIntervalToInt... (no overflow check)
   def yearMonthIntervalToShort... (has overflow check and call 
yearMonthIntervalToInt at the beginning)
   def yearMonthIntervalToByte... (ditto)
   
   def dayTimeIntervalToLong... (no overflow check)
   def dayTimeIntervalToInt... (has overflow check and call 
dayTimeIntervalToLong at the beginning)
   def dayTimeIntervalToShort... (ditto)
   def dayTimeIntervalToByte... (ditto)
   ```
   
   The benefits are:
   1. Codegen code path can call the best version to get the best performance 
(e.g. no `int -> long -> int` roundtrip)
   2. interpreted code path can avoid duplicating the overflow checking logic.




-- 
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.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to