Skip to content
Closed
Prev Previous commit
Next Next commit
Merge branch 'master' of github.com:apache/spark into date_add
Conflicts:
	sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/DateExpressionsSuite.scala
  • Loading branch information
Davies Liu committed Jul 30, 2015
commit 9e8e085ab866e2bc9f6588f2db7029c9adcefe29
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,9 @@ import java.text.SimpleDateFormat
import java.util.Calendar

import org.apache.spark.SparkFunSuite
import org.apache.spark.sql.catalyst.InternalRow
import org.apache.spark.sql.catalyst.util.DateTimeUtils
import org.apache.spark.sql.types.{StringType, TimestampType, DateType}
import org.apache.spark.unsafe.types.CalendarInterval
import org.apache.spark.sql.types._

class DateExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper {

Expand Down Expand Up @@ -210,6 +209,11 @@ class DateExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper {
checkEvaluation(
DateAdd(Literal(Date.valueOf("2016-02-28")), Literal(-365)),
DateTimeUtils.fromJavaDate(Date.valueOf("2015-02-28")))
checkEvaluation(DateAdd(Literal.create(null, DateType), Literal(1)), null)
checkEvaluation(DateAdd(Literal(Date.valueOf("2016-02-28")), Literal.create(null, IntegerType)),
null)
checkEvaluation(DateAdd(Literal.create(null, DateType), Literal.create(null, IntegerType)),
null)
}

test("date_sub") {
Expand All @@ -219,13 +223,29 @@ class DateExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper {
checkEvaluation(
DateSub(Literal(Date.valueOf("2015-01-01")), Literal(-1)),
DateTimeUtils.fromJavaDate(Date.valueOf("2015-01-02")))
checkEvaluation(DateSub(Literal.create(null, DateType), Literal(1)), null)
checkEvaluation(DateSub(Literal(Date.valueOf("2016-02-28")), Literal.create(null, IntegerType)),
null)
checkEvaluation(DateSub(Literal.create(null, DateType), Literal.create(null, IntegerType)),
null)
}

test("time_add") {
checkEvaluation(
TimeAdd(Literal(Timestamp.valueOf("2016-01-29 10:00:00")),
Literal(new CalendarInterval(1, 123000L))),
DateTimeUtils.fromJavaTimestamp(Timestamp.valueOf("2016-02-29 10:00:00.123")))

checkEvaluation(
TimeAdd(Literal.create(null, TimestampType), Literal(new CalendarInterval(1, 123000L))),
null)
checkEvaluation(
TimeAdd(Literal(Timestamp.valueOf("2016-01-29 10:00:00")),
Literal.create(null, CalendarIntervalType)),
null)
checkEvaluation(
TimeAdd(Literal.create(null, TimestampType), Literal.create(null, CalendarIntervalType)),
null)
}

test("time_sub") {
Expand All @@ -238,24 +258,54 @@ class DateExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper {
Literal(Timestamp.valueOf("2016-03-30 00:00:01")),
Literal(new CalendarInterval(1, 2000000.toLong))),
DateTimeUtils.fromJavaTimestamp(Timestamp.valueOf("2016-02-28 23:59:59")))

checkEvaluation(
TimeSub(Literal.create(null, TimestampType), Literal(new CalendarInterval(1, 123000L))),
null)
checkEvaluation(
TimeSub(Literal(Timestamp.valueOf("2016-01-29 10:00:00")),
Literal.create(null, CalendarIntervalType)),
null)
checkEvaluation(
TimeSub(Literal.create(null, TimestampType), Literal.create(null, CalendarIntervalType)),
null)
}

test("add_months") {
checkEvaluation(AddMonths(Literal(Date.valueOf(
"2015-01-30")), Literal(1)), DateTimeUtils.fromJavaDate(Date.valueOf("2015-02-28")))
checkEvaluation(AddMonths(Literal(Date.valueOf(
"2016-03-30")), Literal(-1)), DateTimeUtils.fromJavaDate(Date.valueOf("2016-02-29")))
checkEvaluation(AddMonths(Literal(Date.valueOf("2015-01-30")), Literal(1)),
DateTimeUtils.fromJavaDate(Date.valueOf("2015-02-28")))
checkEvaluation(AddMonths(Literal(Date.valueOf("2016-03-30")), Literal(-1)),
DateTimeUtils.fromJavaDate(Date.valueOf("2016-02-29")))
checkEvaluation(
AddMonths(Literal(Date.valueOf("2015-01-30")), Literal.create(null, IntegerType)),
null)
checkEvaluation(AddMonths(Literal.create(null, DateType), Literal(1)), null)
checkEvaluation(AddMonths(Literal.create(null, DateType), Literal.create(null, IntegerType)),
null)
}

test("months_between") {
checkEvaluation(MonthsBetween(Literal(Timestamp.valueOf(
"1997-02-28 10:30:00")), Literal(Timestamp.valueOf("1996-10-30 00:00:00"))), 3.94959677)
checkEvaluation(MonthsBetween(Literal(Timestamp.valueOf(
"2015-01-30 11:52:00")), Literal(Timestamp.valueOf("2015-01-30 11:50:00"))), 0.0)
checkEvaluation(MonthsBetween(Literal(Timestamp.valueOf(
"2015-01-31 00:00:00")), Literal(Timestamp.valueOf("2015-03-31 22:00:00"))), -2.0)
checkEvaluation(MonthsBetween(Literal(Timestamp.valueOf(
"2015-03-31 22:00:00")), Literal(Timestamp.valueOf("2015-02-28 00:00:00"))), 1.0)
checkEvaluation(
MonthsBetween(Literal(Timestamp.valueOf("1997-02-28 10:30:00")),
Literal(Timestamp.valueOf("1996-10-30 00:00:00"))),
3.94959677)
checkEvaluation(
MonthsBetween(Literal(Timestamp.valueOf("2015-01-30 11:52:00")),
Literal(Timestamp.valueOf("2015-01-30 11:50:00"))),
0.0)
checkEvaluation(
MonthsBetween(Literal(Timestamp.valueOf("2015-01-31 00:00:00")),
Literal(Timestamp.valueOf("2015-03-31 22:00:00"))),
-2.0)
checkEvaluation(
MonthsBetween(Literal(Timestamp.valueOf("2015-03-31 22:00:00")),
Literal(Timestamp.valueOf("2015-02-28 00:00:00"))),
1.0)
val t = Literal(Timestamp.valueOf("2015-03-31 22:00:00"))
val tnull = Literal.create(null, TimestampType)
checkEvaluation(MonthsBetween(t, tnull), null)
checkEvaluation(MonthsBetween(tnull, t), null)
checkEvaluation(MonthsBetween(tnull, tnull), null)
}

test("last_day") {
Expand Down
You are viewing a condensed version of this merge commit. You can view the full changes here.