Skip to content

Commit 3430e94

Browse files
committed
Fix:add unit test cases
1 parent 7d6385f commit 3430e94

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -604,8 +604,9 @@ object DateTimeUtils {
604604
private[this] def getYearAndDayInYear(daysSince1970: SQLDate): (Int, Int) = {
605605
// add the difference (in days) between 1.1.1970 and the artificial year 0 (-17999)
606606
var daysSince1970Tmp = daysSince1970
607-
// In history,the period(5.10.1582 ~ 14.10.1582) is not exist
608-
// (4.10.1582) -141428 days since 1.1.1970
607+
// Since Julian calendar was replaced with the Gregorian calendar,
608+
// the 10 days after Oct. 4 were skipped.
609+
// (1582-10-04) -141428 days since 1970-01-01
609610
if (daysSince1970 <= -141428) {
610611
daysSince1970Tmp -= 10
611612
}
@@ -616,6 +617,7 @@ object DateTimeUtils {
616617
val year: Int = (2001 - 20000) + 400 * numOfQuarterCenturies + years
617618
(year, dayInYear)
618619
}
620+
619621
/**
620622
* Returns the 'day in year' value for the given date. The date is expressed in days
621623
* since 1.1.1970.

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ class DateExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper {
7676
}
7777
}
7878
checkEvaluation(DayOfYear(Literal.create(null, DateType)), null)
79+
80+
checkEvaluation(DayOfYear(Literal(new Date(sdf.parse("1582-10-15 13:10:15").getTime))), 288)
81+
checkEvaluation(DayOfYear(Literal(new Date(sdf.parse("1582-10-04 13:10:15").getTime))), 277)
7982
checkConsistencyBetweenInterpretedAndCodegen(DayOfYear, DateType)
8083
}
8184

@@ -96,6 +99,8 @@ class DateExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper {
9699
}
97100
}
98101
}
102+
checkEvaluation(Year(Literal(new Date(sdf.parse("1582-01-01 13:10:15").getTime))), 1582)
103+
checkEvaluation(Year(Literal(new Date(sdf.parse("1581-12-31 13:10:15").getTime))), 1581)
99104
checkConsistencyBetweenInterpretedAndCodegen(Year, DateType)
100105
}
101106

@@ -116,6 +121,9 @@ class DateExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper {
116121
}
117122
}
118123
}
124+
125+
checkEvaluation(Quarter(Literal(new Date(sdf.parse("1582-10-01 13:10:15").getTime))), 4)
126+
checkEvaluation(Quarter(Literal(new Date(sdf.parse("1582-09-30 13:10:15").getTime))), 3)
119127
checkConsistencyBetweenInterpretedAndCodegen(Quarter, DateType)
120128
}
121129

@@ -125,6 +133,10 @@ class DateExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper {
125133
checkEvaluation(Month(Cast(Literal(sdfDate.format(d)), DateType, gmtId)), 4)
126134
checkEvaluation(Month(Cast(Literal(ts), DateType, gmtId)), 11)
127135

136+
checkEvaluation(Month(Literal(new Date(sdf.parse("1582-04-28 13:10:15").getTime))), 4)
137+
checkEvaluation(Month(Literal(new Date(sdf.parse("1582-10-04 13:10:15").getTime))), 10)
138+
checkEvaluation(Month(Literal(new Date(sdf.parse("1582-10-15 13:10:15").getTime))), 10)
139+
128140
val c = Calendar.getInstance()
129141
(2003 to 2004).foreach { y =>
130142
(0 to 3).foreach { m =>
@@ -146,6 +158,10 @@ class DateExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper {
146158
checkEvaluation(DayOfMonth(Cast(Literal(sdfDate.format(d)), DateType, gmtId)), 8)
147159
checkEvaluation(DayOfMonth(Cast(Literal(ts), DateType, gmtId)), 8)
148160

161+
checkEvaluation(DayOfMonth(Literal(new Date(sdf.parse("1582-04-28 13:10:15").getTime))), 28)
162+
checkEvaluation(DayOfMonth(Literal(new Date(sdf.parse("1582-10-15 13:10:15").getTime))), 15)
163+
checkEvaluation(DayOfMonth(Literal(new Date(sdf.parse("1582-10-04 13:10:15").getTime))), 4)
164+
149165
val c = Calendar.getInstance()
150166
(1999 to 2000).foreach { y =>
151167
c.set(y, 0, 1, 0, 0, 0)
@@ -186,6 +202,8 @@ class DateExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper {
186202
checkEvaluation(WeekOfYear(Cast(Literal(sdfDate.format(d)), DateType, gmtId)), 15)
187203
checkEvaluation(WeekOfYear(Cast(Literal(ts), DateType, gmtId)), 45)
188204
checkEvaluation(WeekOfYear(Cast(Literal("2011-05-06"), DateType, gmtId)), 18)
205+
checkEvaluation(WeekOfYear(Literal(new Date(sdf.parse("1582-10-15 13:10:15").getTime))), 40)
206+
checkEvaluation(WeekOfYear(Literal(new Date(sdf.parse("1582-10-04 13:10:15").getTime))), 40)
189207
checkConsistencyBetweenInterpretedAndCodegen(WeekOfYear, DateType)
190208
}
191209

0 commit comments

Comments
 (0)