MaxGekk commented on a change in pull request #26034: [SPARK-29364][SQL] Return
an interval from date subtract according to SQL standard
URL: https://github.com/apache/spark/pull/26034#discussion_r331764340
##########
File path:
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/datetimeExpressions.scala
##########
@@ -1672,33 +1673,59 @@ case class TruncTimestamp(
}
/**
- * Returns the number of days from startDate to endDate.
+ * Returns the number of days from startDate to endDate or an interval between
the dates.
*/
+// scalastyle:off line.size.limit line.contains.tab
@ExpressionDescription(
- usage = "_FUNC_(endDate, startDate) - Returns the number of days from
`startDate` to `endDate`.",
+ usage = "_FUNC_(endDate, startDate) - Returns the number of days from
`startDate` to `endDate`." +
+ "When `spark.sql.ansi.enabled` is set to `true` and `spark.sql.dialect` is
`Spark`, it returns " +
+ "an interval between `startDate` (inclusive) and `endDate` (exclusive).",
examples = """
Examples:
> SELECT _FUNC_('2009-07-31', '2009-07-30');
1
-
> SELECT _FUNC_('2009-07-30', '2009-07-31');
-1
+ > SET spark.sql.ansi.enabled=true;
+ spark.sql.ansi.enabled true
+ > SET spark.sql.dialect=Spark;
+ spark.sql.dialect Spark
+ > select _FUNC_(date'tomorrow', date'yesterday');
+ interval 2 days
""",
since = "1.5.0")
+// scalastyle:on line.size.limit line.contains.tab
case class DateDiff(endDate: Expression, startDate: Expression)
extends BinaryExpression with ImplicitCastInputTypes {
override def left: Expression = endDate
override def right: Expression = startDate
override def inputTypes: Seq[AbstractDataType] = Seq(DateType, DateType)
- override def dataType: DataType = IntegerType
+ private val returnInterval: Boolean = {
+ val isSparkDialect = SQLConf.get.getConf(DIALECT) ==
Dialect.SPARK.toString()
+ SQLConf.get.ansiEnabled && isSparkDialect
Review comment:
> Please note that I suggested `Dialect.POSTGRESQL`
If you do what you suggest, this will change results of `date.sql`:
```diff
-- !query 46
SELECT f1 - date '2000-01-01' AS `Days From 2K` FROM DATE_TBL
-- !query 46 schema
-struct<Days From 2K:int>
+struct<Days From 2K:interval>
-- !query 46 output
--1035
--1036
--1037
--1400
--1401
--1402
--1403
--15542
--15607
-13977
-14343
-14710
-91
-92
-93
+interval -2 years -10 months
+interval -2 years -10 months -1 days
+interval -2 years -9 months -4 weeks -2 days
+interval -3 years -10 months
+interval -3 years -10 months -1 days
+interval -3 years -10 months -2 days
+interval -3 years -9 months -4 weeks -2 days
+interval -42 years -6 months -2 weeks -4 days
+interval -42 years -8 months -3 weeks -1 days
+interval 3 months
+interval 3 months 1 days
+interval 3 months 2 days
+interval 38 years 3 months 1 weeks
+interval 39 years 3 months 1 weeks 1 days
+interval 40 years 3 months 1 weeks 2 days
```
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]