Skip to content

Commit 27de6b8

Browse files
committed
Add config spark.sql.legacy.datesSubtraction.enabled
1 parent 4a8173b commit 27de6b8

File tree

4 files changed

+22
-3
lines changed

4 files changed

+22
-3
lines changed

docs/sql-migration-guide.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ license: |
217217

218218
- Since Spark 3.0, the `size` function returns `NULL` for the `NULL` input. In Spark version 2.4 and earlier, this function gives `-1` for the same input. To restore the behavior before Spark 3.0, you can set `spark.sql.legacy.sizeOfNull` to `true`.
219219

220-
- In Spark version 2.4 and earlier, dates subtraction `date1` - `date2` gives the number of days from `date1` to `date2`. Since Spark 3.0, the expression has the `INTERVAL` type and returns an interval between two dates. To get the number of days, use the `datediff` function.
220+
- In Spark version 2.4 and earlier, dates subtraction `date1` - `date2` gives the number of days from `date1` to `date2`. Since Spark 3.0, the expression has the `INTERVAL` type and returns an interval between two dates. To get the number of days, you can set `spark.sql.legacy.datesSubtraction.enabled` to `true`.
221221

222222
## Upgrading from Spark SQL 2.4 to 2.4.1
223223

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/TypeCoercion.scala

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -849,7 +849,12 @@ object TypeCoercion {
849849
case Add(l @ DateType(), r @ IntegerType()) => DateAdd(l, r)
850850
case Add(l @ IntegerType(), r @ DateType()) => DateAdd(r, l)
851851
case Subtract(l @ DateType(), r @ IntegerType()) => DateSub(l, r)
852-
case Subtract(l @ DateType(), r @ DateType()) => SubtractDates(l, r)
852+
case Subtract(l @ DateType(), r @ DateType()) =>
853+
if (SQLConf.get.getConf(SQLConf.LEGACY_DATES_SUBTRACTION)) {
854+
DateDiff(l, r)
855+
} else {
856+
SubtractDates(l, r)
857+
}
853858
case Subtract(l @ TimestampType(), r @ TimestampType()) => TimestampDiff(l, r)
854859
case Subtract(l @ TimestampType(), r @ DateType()) =>
855860
TimestampDiff(l, Cast(r, TimestampType))

sql/catalyst/src/main/scala/org/apache/spark/sql/internal/SQLConf.scala

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2014,6 +2014,15 @@ object SQLConf {
20142014
.stringConf
20152015
.createWithDefault(
20162016
"https://maven-central.storage-download.googleapis.com/repos/central/data/")
2017+
2018+
val LEGACY_DATES_SUBTRACTION =
2019+
buildConf("spark.sql.legacy.datesSubtraction.enabled")
2020+
.doc("When true, date subtraction expressions have the INT type and return " +
2021+
"the number of days between the dates. If it is set to false, the expressions " +
2022+
"have the INTERVAL type and return an interval from the left date (inclusive) " +
2023+
"to the right date (exclusive).")
2024+
.booleanConf
2025+
.createWithDefault(false)
20172026
}
20182027

20192028
/**

sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/analysis/TypeCoercionSuite.scala

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1430,7 +1430,12 @@ class TypeCoercionSuite extends AnalysisTest {
14301430
ruleTest(dateTimeOperations, Add(date, intValue), DateAdd(date, intValue))
14311431
ruleTest(dateTimeOperations, Add(intValue, date), DateAdd(date, intValue))
14321432
ruleTest(dateTimeOperations, Subtract(date, intValue), DateSub(date, intValue))
1433-
ruleTest(dateTimeOperations, Subtract(date, date), SubtractDates(date, date))
1433+
withSQLConf(SQLConf.LEGACY_DATES_SUBTRACTION.key -> "false") {
1434+
ruleTest(dateTimeOperations, Subtract(date, date), SubtractDates(date, date))
1435+
}
1436+
withSQLConf(SQLConf.LEGACY_DATES_SUBTRACTION.key -> "true") {
1437+
ruleTest(dateTimeOperations, Subtract(date, date), DateDiff(date, date))
1438+
}
14341439
ruleTest(dateTimeOperations, Subtract(timestamp, timestamp),
14351440
TimestampDiff(timestamp, timestamp))
14361441
ruleTest(dateTimeOperations, Subtract(timestamp, date),

0 commit comments

Comments
 (0)