Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SPARK-27898][SQL] Support 4 date operators(date + integer, integer + date, date - integer and date - date) #24755

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -820,8 +820,10 @@ object TypeCoercion {
}

/**
* Turns Add/Subtract of DateType/TimestampType/StringType and CalendarIntervalType
* to TimeAdd/TimeSub
* 1. Turns Add/Subtract of DateType/TimestampType/StringType and CalendarIntervalType
* to TimeAdd/TimeSub.
* 2. Turns Add/Subtract of DateType/IntegerType and IntegerType/DateType
* to DateAdd/DateSub/DateDiff.
*/
object DateTimeOperations extends Rule[LogicalPlan] {

Expand All @@ -837,6 +839,11 @@ object TypeCoercion {
Cast(TimeAdd(l, r), l.dataType)
case Subtract(l, r @ CalendarIntervalType()) if acceptedTypes.contains(l.dataType) =>
Cast(TimeSub(l, r), l.dataType)

case Add(l @ DateType(), r @ IntegerType()) => DateAdd(l, r)
case Add(l @ IntegerType(), r @ DateType()) => DateAdd(r, l)
case Subtract(l @ DateType(), r @ IntegerType()) => DateSub(l, r)
case Subtract(l @ DateType(), r @ DateType()) => DateDiff(l, r)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1406,6 +1406,7 @@ class TypeCoercionSuite extends AnalysisTest {
val timestamp = Literal(new Timestamp(0L))
val interval = Literal(new CalendarInterval(0, 0))
val str = Literal("2015-01-01")
val intValue = Literal(0, IntegerType)

ruleTest(dateTimeOperations, Add(date, interval), Cast(TimeAdd(date, interval), DateType))
ruleTest(dateTimeOperations, Add(interval, date), Cast(TimeAdd(date, interval), DateType))
Expand All @@ -1424,6 +1425,11 @@ class TypeCoercionSuite extends AnalysisTest {
// interval operations should not be effected
ruleTest(dateTimeOperations, Add(interval, interval), Add(interval, interval))
ruleTest(dateTimeOperations, Subtract(interval, interval), Subtract(interval, interval))

ruleTest(dateTimeOperations, Add(date, intValue), DateAdd(date, intValue))
ruleTest(dateTimeOperations, Add(intValue, date), DateAdd(date, intValue))
ruleTest(dateTimeOperations, Subtract(date, intValue), DateSub(date, intValue))
ruleTest(dateTimeOperations, Subtract(date, date), DateDiff(date, date))
}

/**
Expand Down
7 changes: 6 additions & 1 deletion sql/core/src/test/resources/sql-tests/inputs/datetime.sql
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,9 @@ select a, b from ttf2 order by a, current_date;

select weekday('2007-02-03'), weekday('2009-07-30'), weekday('2017-05-27'), weekday(null), weekday('1582-10-15 13:10:15');

select year('1500-01-01'), month('1500-01-01'), dayOfYear('1500-01-01');
select year('1500-01-01'), month('1500-01-01'), dayOfYear('1500-01-01');

select date '2001-09-28' + 7;
select 7 + date '2001-09-28';
select date '2001-10-01' - 7;
select date '2001-10-01' - date '2001-09-28';
34 changes: 33 additions & 1 deletion sql/core/src/test/resources/sql-tests/results/datetime.sql.out
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
-- Automatically generated by SQLQueryTestSuite
-- Number of queries: 11
-- Number of queries: 15


-- !query 0
Expand Down Expand Up @@ -97,3 +97,35 @@ select year('1500-01-01'), month('1500-01-01'), dayOfYear('1500-01-01')
struct<year(CAST(1500-01-01 AS DATE)):int,month(CAST(1500-01-01 AS DATE)):int,dayofyear(CAST(1500-01-01 AS DATE)):int>
-- !query 10 output
1500 1 1


-- !query 11
select date '2001-09-28' + 7
-- !query 11 schema
struct<date_add(DATE '2001-09-28', 7):date>
-- !query 11 output
2001-10-05


-- !query 12
select 7 + date '2001-09-28'
-- !query 12 schema
struct<date_add(DATE '2001-09-28', 7):date>
-- !query 12 output
2001-10-05


-- !query 13
select date '2001-10-01' - 7
-- !query 13 schema
struct<date_sub(DATE '2001-10-01', 7):date>
-- !query 13 output
2001-09-24


-- !query 14
select date '2001-10-01' - date '2001-09-28'
-- !query 14 schema
struct<datediff(DATE '2001-10-01', DATE '2001-09-28'):int>
-- !query 14 output
3