Skip to content

Commit 9f248f1

Browse files
committed
The function of month and day return the value which is not we
expected.
1 parent e9804b3 commit 9f248f1

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,14 @@ object DateTimeUtils {
511511
*/
512512
private[this] def getYearAndDayInYear(daysSince1970: SQLDate): (Int, Int) = {
513513
// add the difference (in days) between 1.1.1970 and the artificial year 0 (-17999)
514-
val daysNormalized = daysSince1970 + toYearZero
514+
var daysSince1970Tmp = daysSince1970
515+
// Since Julian calendar was replaced with the Gregorian calendar,
516+
// the 10 days after Oct. 4 were skipped.
517+
// (1582-10-04) -141428 days since 1970-01-01
518+
if (daysSince1970 <= -141428) {
519+
daysSince1970Tmp -= 10
520+
}
521+
val daysNormalized = daysSince1970Tmp + toYearZero
515522
val numOfQuarterCenturies = daysNormalized / daysIn400Years
516523
val daysInThis400 = daysNormalized % daysIn400Years + 1
517524
val (years, dayInYear) = numYears(daysInThis400)

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ class DateExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper {
6060
}
6161
}
6262
checkEvaluation(DayOfYear(Literal.create(null, DateType)), null)
63+
checkEvaluation(DayOfYear(Literal(new Date(sdf.parse("1582-10-15 13:10:15").getTime))), 288)
64+
checkEvaluation(DayOfYear(Literal(new Date(sdf.parse("1582-10-04 13:10:15").getTime))), 277)
6365
checkConsistencyBetweenInterpretedAndCodegen(DayOfYear, DateType)
6466
}
6567

@@ -80,6 +82,8 @@ class DateExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper {
8082
}
8183
}
8284
}
85+
checkEvaluation(Year(Literal(new Date(sdf.parse("1582-01-01 13:10:15").getTime))), 1582)
86+
checkEvaluation(Year(Literal(new Date(sdf.parse("1581-12-31 13:10:15").getTime))), 1581)
8387
checkConsistencyBetweenInterpretedAndCodegen(Year, DateType)
8488
}
8589

@@ -100,6 +104,8 @@ class DateExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper {
100104
}
101105
}
102106
}
107+
checkEvaluation(Quarter(Literal(new Date(sdf.parse("1582-10-01 13:10:15").getTime))), 4)
108+
checkEvaluation(Quarter(Literal(new Date(sdf.parse("1582-09-30 13:10:15").getTime))), 3)
103109
checkConsistencyBetweenInterpretedAndCodegen(Quarter, DateType)
104110
}
105111

@@ -109,6 +115,9 @@ class DateExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper {
109115
checkEvaluation(Month(Cast(Literal(sdfDate.format(d)), DateType)), 4)
110116
checkEvaluation(Month(Cast(Literal(ts), DateType)), 11)
111117

118+
checkEvaluation(Month(Literal(new Date(sdf.parse("1582-04-28 13:10:15").getTime))), 4)
119+
checkEvaluation(Month(Literal(new Date(sdf.parse("1582-10-04 13:10:15").getTime))), 10)
120+
checkEvaluation(Month(Literal(new Date(sdf.parse("1582-10-15 13:10:15").getTime))), 10)
112121
(2003 to 2004).foreach { y =>
113122
(0 to 3).foreach { m =>
114123
(0 to 2 * 24).foreach { i =>
@@ -130,6 +139,9 @@ class DateExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper {
130139
checkEvaluation(DayOfMonth(Cast(Literal(sdfDate.format(d)), DateType)), 8)
131140
checkEvaluation(DayOfMonth(Cast(Literal(ts), DateType)), 8)
132141

142+
checkEvaluation(DayOfMonth(Literal(new Date(sdf.parse("1582-04-28 13:10:15").getTime))), 28)
143+
checkEvaluation(DayOfMonth(Literal(new Date(sdf.parse("1582-10-15 13:10:15").getTime))), 15)
144+
checkEvaluation(DayOfMonth(Literal(new Date(sdf.parse("1582-10-04 13:10:15").getTime))), 4)
133145
(1999 to 2000).foreach { y =>
134146
val c = Calendar.getInstance()
135147
c.set(y, 0, 1, 0, 0, 0)
@@ -163,6 +175,8 @@ class DateExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper {
163175
checkEvaluation(WeekOfYear(Cast(Literal(sdfDate.format(d)), DateType)), 15)
164176
checkEvaluation(WeekOfYear(Cast(Literal(ts), DateType)), 45)
165177
checkEvaluation(WeekOfYear(Cast(Literal("2011-05-06"), DateType)), 18)
178+
checkEvaluation(WeekOfYear(Literal(new Date(sdf.parse("1582-10-15 13:10:15").getTime))), 40)
179+
checkEvaluation(WeekOfYear(Literal(new Date(sdf.parse("1582-10-04 13:10:15").getTime))), 40)
166180
checkConsistencyBetweenInterpretedAndCodegen(WeekOfYear, DateType)
167181
}
168182

0 commit comments

Comments
 (0)