Peng-Lei commented on a change in pull request #34494:
URL: https://github.com/apache/spark/pull/34494#discussion_r749091569
##########
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:
>
> The benefits are:
>
> Codegen code path can call the best version to get the best performance
(e.g. no int -> long -> int roundtrip)
> interpreted code path can avoid duplicating the overflow checking logic.
I agree with benefits you said. Let me thinking more about it. I want to
embed function instead of define the function. because the funciton just be
used only once except `intToYearMonthInterval` `longToDayTimeInterval`
`yearMonthIntervalToInt` `dayTimeIntervalToLong`.
--
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]