cloud-fan commented on a change in pull request #27672: [SPARK-30919][SQL] Make
interval multiply and divide's overflow behavior consistent with other
operations
URL: https://github.com/apache/spark/pull/27672#discussion_r383153661
##########
File path:
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/IntervalUtils.scala
##########
@@ -404,22 +404,44 @@ object IntervalUtils {
}
/**
- * Makes an interval from months, days and micros with the fractional part by
- * adding the month fraction to days and the days fraction to micros.
- *
- * @throws ArithmeticException if the result overflows any field value
+ * Makes an interval from months, days and micros with the fractional part.
+ * The overflow style here follows the way of ansi sql standard and the
natural rules for
+ * intervals as defined in the Gregorian calendar. Thus, the days fraction
will be added
+ * to microseconds but the months fraction will not be added to days, and it
will throw
+ * exception if any part overflows.
*/
private def fromDoubles(
monthsWithFraction: Double,
daysWithFraction: Double,
microsWithFraction: Double): CalendarInterval = {
val truncatedMonths = Math.toIntExact(monthsWithFraction.toLong)
- val days = daysWithFraction + DAYS_PER_MONTH * (monthsWithFraction -
truncatedMonths)
- val truncatedDays = Math.toIntExact(days.toLong)
- val micros = microsWithFraction + MICROS_PER_DAY * (days - truncatedDays)
+ val truncatedDays = Math.toIntExact(daysWithFraction.toLong)
+ val micros = microsWithFraction + MICROS_PER_DAY * (daysWithFraction -
truncatedDays)
new CalendarInterval(truncatedMonths, truncatedDays, micros.round)
}
+ /**
+ * Makes an interval from months, days and micros with the fractional part.
+ * The overflow style here follows the way of casting [[java.lang.Double]]
to integrals and the
+ * natural rules for intervals as defined in the Gregorian calendar. Thus,
the days fraction
+ * will be added to microseconds but the months fraction will not be added
to days, and there may
+ * be rounding or truncation in months(or day and microseconds) part.
+ */
+ private def safeFromDoubles(
+ monthsWithFraction: Double,
+ daysWithFraction: Double,
+ microsWithFraction: Double): CalendarInterval = {
+ val truncatedDays = if (daysWithFraction > Int.MaxValue) {
+ Int.MaxValue
+ } else if (daysWithFraction < Int.MinValue) {
+ Int.MinValue
+ } else {
+ daysWithFraction.toInt
Review comment:
what if we just call `daysWithFraction.toInt`?
----------------------------------------------------------------
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]