Skip to content

Commit 4e64409

Browse files
committed
[SPARK-10617] [SQL] Fixed AddMonths leap year calculations
1 parent d323e5e commit 4e64409

File tree

3 files changed

+7
-4
lines changed

3 files changed

+7
-4
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -674,7 +674,7 @@ object DateTimeUtils {
674674
val leapDay = if (currentMonthInYear == 1 && isLeapYear(currentYear + YearZero)) 1 else 0
675675
val lastDayOfMonth = monthDays(currentMonthInYear) + leapDay
676676

677-
val currentDayInMonth = if (daysToMonthEnd == 0 || dayOfMonth >= lastDayOfMonth) {
677+
val currentDayInMonth = if (dayOfMonth >= lastDayOfMonth) {
678678
// last day of the month
679679
lastDayOfMonth
680680
} else {

sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/DateExpressionsSuite.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,8 @@ class DateExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper {
295295
test("add_months") {
296296
checkEvaluation(AddMonths(Literal(Date.valueOf("2015-01-30")), Literal(1)),
297297
DateTimeUtils.fromJavaDate(Date.valueOf("2015-02-28")))
298+
checkEvaluation(AddMonths(Literal(Date.valueOf("2015-02-28")), Literal(12)),
299+
DateTimeUtils.fromJavaDate(Date.valueOf("2016-02-28")))
298300
checkEvaluation(AddMonths(Literal(Date.valueOf("2016-03-30")), Literal(-1)),
299301
DateTimeUtils.fromJavaDate(Date.valueOf("2016-02-29")))
300302
checkEvaluation(

sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/util/DateTimeUtilsSuite.scala

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -371,10 +371,11 @@ class DateTimeUtilsSuite extends SparkFunSuite {
371371
c1.set(1997, 1, 28, 10, 30, 0)
372372
val days1 = millisToDays(c1.getTimeInMillis)
373373
val c2 = Calendar.getInstance()
374-
c2.set(2000, 1, 29)
374+
c2.set(2000, 1, 28)
375375
assert(dateAddMonths(days1, 36) === millisToDays(c2.getTimeInMillis))
376-
c2.set(1996, 0, 31)
376+
c2.set(1996, 0, 28)
377377
assert(dateAddMonths(days1, -13) === millisToDays(c2.getTimeInMillis))
378+
378379
}
379380

380381
test("timestamp add months") {
@@ -383,7 +384,7 @@ class DateTimeUtilsSuite extends SparkFunSuite {
383384
c1.set(Calendar.MILLISECOND, 0)
384385
val ts1 = c1.getTimeInMillis * 1000L
385386
val c2 = Calendar.getInstance()
386-
c2.set(2000, 1, 29, 10, 30, 0)
387+
c2.set(2000, 1, 28, 10, 30, 0)
387388
c2.set(Calendar.MILLISECOND, 123)
388389
val ts2 = c2.getTimeInMillis * 1000L
389390
assert(timestampAddInterval(ts1, 36, 123000) === ts2)

0 commit comments

Comments
 (0)