Skip to content

Commit 9dfb45d

Browse files
committed
Rebase on interval with days
1 parent 8e8e539 commit 9dfb45d

File tree

1 file changed

+9
-10
lines changed

1 file changed

+9
-10
lines changed

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/IntervalUtils.scala

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,8 @@ object IntervalUtils {
403403
var state = PREFIX
404404
var i = 0
405405
var currentValue: Long = 0
406-
var months: Long = 0
406+
var months: Int = 0
407+
var days: Int = 0
407408
var microseconds: Long = 0
408409

409410
while (i < bytes.length) {
@@ -457,17 +458,15 @@ object IntervalUtils {
457458
try {
458459
b match {
459460
case 'y' if s.matchAt(yearStr, i) =>
460-
months = Math.addExact(months, Math.multiplyExact(12, currentValue))
461+
val monthsInYears = Math.multiplyExact(12, Math.toIntExact(currentValue))
462+
months = Math.addExact(months, monthsInYears)
461463
i += yearStr.numBytes()
462464
case 'w' if s.matchAt(weekStr, i) =>
463-
val daysUs = Math.multiplyExact(
464-
Math.multiplyExact(7, currentValue),
465-
DateTimeUtils.MICROS_PER_DAY)
466-
microseconds = Math.addExact(microseconds, daysUs)
465+
val daysInWeeks = Math.multiplyExact(7, Math.toIntExact(currentValue))
466+
days = Math.addExact(days, daysInWeeks)
467467
i += weekStr.numBytes()
468468
case 'd' if s.matchAt(dayStr, i) =>
469-
val daysUs = Math.multiplyExact(currentValue, DateTimeUtils.MICROS_PER_DAY)
470-
microseconds = Math.addExact(microseconds, daysUs)
469+
days = Math.addExact(days, Math.toIntExact(currentValue))
471470
i += dayStr.numBytes()
472471
case 'h' if s.matchAt(hourStr, i) =>
473472
val hoursUs = Math.multiplyExact(currentValue, MICROS_PER_HOUR)
@@ -479,7 +478,7 @@ object IntervalUtils {
479478
i += secondStr.numBytes()
480479
case 'm' =>
481480
if (s.matchAt(monthStr, i)) {
482-
months = Math.addExact(months, currentValue)
481+
months = Math.addExact(months, Math.toIntExact(currentValue))
483482
i += monthStr.numBytes()
484483
} else if (s.matchAt(minuteStr, i)) {
485484
val minutesUs = Math.multiplyExact(currentValue, MICROS_PER_MINUTE)
@@ -523,7 +522,7 @@ object IntervalUtils {
523522

524523
val result = state match {
525524
case UNIT_NAME_SUFFIX | END_UNIT_NAME | BEGIN_VALUE =>
526-
Some(new CalendarInterval(Math.toIntExact(months), microseconds))
525+
Some(new CalendarInterval(months, days, microseconds))
527526
case _ => None
528527
}
529528

0 commit comments

Comments
 (0)