yaooqinn commented on a change in pull request #26465: [SPARK-29390][SQL] Add
the justify_days(), justify_hours() and justif_interval() functions
URL: https://github.com/apache/spark/pull/26465#discussion_r345536254
##########
File path:
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/IntervalUtils.scala
##########
@@ -637,4 +637,40 @@ object IntervalUtils {
new CalendarInterval(totalMonths, totalDays, micros)
}
+
+ /**
+ * Adjust interval so 30-day time periods are represented as months
+ */
+ def justifyDays(interval: CalendarInterval): CalendarInterval = {
+ val monthToDays = interval.months * DAYS_PER_MONTH
+ val totalDays = Math.addExact(monthToDays, interval.days)
+ val months = Math.toIntExact(totalDays / DAYS_PER_MONTH)
+ val days = Math.toIntExact(totalDays % DAYS_PER_MONTH)
+ new CalendarInterval(months, days, interval.microseconds)
+ }
+
+ /**
+ * Adjust interval so 24-hour time periods are represented as days
+ */
+ def justifyHours(interval: CalendarInterval): CalendarInterval = {
+ val dayToUs = MICROS_PER_DAY * interval.days
+ val totalUs = Math.addExact(interval.microseconds, dayToUs)
+ val days = Math.toIntExact(totalUs / MICROS_PER_DAY)
+ val microseconds = totalUs % MICROS_PER_DAY
+ new CalendarInterval(interval.months, days, microseconds)
+ }
+
+ /**
+ * Adjust interval using justifyHours and justifyDays, with additional sign
adjustments
+ */
+ def justifyInterval(interval: CalendarInterval): CalendarInterval = {
+ val monthToDays = DAYS_PER_MONTH * interval.months
+ val dayToUs = Math.multiplyExact(monthToDays + interval.days,
MICROS_PER_DAY)
+ val totalUs = Math.addExact(interval.microseconds, dayToUs)
+ val microseconds = totalUs % MICROS_PER_DAY
+ val totalDays = totalUs / MICROS_PER_DAY
+ val days = totalDays % DAYS_PER_MONTH
+ val months = Math.toIntExact(totalDays / DAYS_PER_MONTH)
Review comment:
ok, i'd remove these useless overflow check
----------------------------------------------------------------
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]