Peng-Lei commented on a change in pull request #34494:
URL: https://github.com/apache/spark/pull/34494#discussion_r749941998



##########
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:
       I thought about it. Finally, as you said, the corresponding convert 
function is defined even if it will be used infrequently. So the codegen just 
call the corresponding convert function that is better match and the overflow 
check is performed only when necessary for the corresponding convert function.




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