-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-29364][SQL] Return an interval from datediff() in the ANSI mode and Spark dialect
#26034
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,7 +19,7 @@ package org.apache.spark.sql.catalyst.expressions | |
|
|
||
| import java.sql.{Date, Timestamp} | ||
| import java.text.SimpleDateFormat | ||
| import java.time.{Instant, LocalDateTime, ZoneId, ZoneOffset} | ||
| import java.time.{Instant, LocalDate, LocalDateTime, ZoneId, ZoneOffset} | ||
| import java.util.{Calendar, Locale, TimeZone} | ||
| import java.util.concurrent.TimeUnit | ||
| import java.util.concurrent.TimeUnit._ | ||
|
|
@@ -836,18 +836,51 @@ class DateExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper { | |
| } | ||
| } | ||
|
|
||
| test("datediff") { | ||
| checkEvaluation( | ||
| DateDiff(Literal(Date.valueOf("2015-07-24")), Literal(Date.valueOf("2015-07-21"))), 3) | ||
| checkEvaluation( | ||
| DateDiff(Literal(Date.valueOf("2015-07-21")), Literal(Date.valueOf("2015-07-24"))), -3) | ||
| checkEvaluation(DateDiff(Literal.create(null, DateType), Literal(Date.valueOf("2015-07-24"))), | ||
| null) | ||
| checkEvaluation(DateDiff(Literal(Date.valueOf("2015-07-24")), Literal.create(null, DateType)), | ||
| null) | ||
| checkEvaluation( | ||
| DateDiff(Literal.create(null, DateType), Literal.create(null, DateType)), | ||
| null) | ||
| test("datediff returns an integer") { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Although we didn't touch the other test cases, but at least for this, we had better have
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In that case, it will not test
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry, I blindly copied. What I mean was setting
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it would be better to add asserts to check this assumption otherwise if defaults for the SQL config will be changed in the future, this test will not check default behavior. |
||
| Seq( | ||
| (true, "PostgreSQL"), | ||
| (false, "PostgreSQL"), | ||
| (false, "Spark")).foreach { case (ansi, dialect) => | ||
| withSQLConf( | ||
| SQLConf.ANSI_ENABLED.key -> ansi.toString, | ||
| SQLConf.DIALECT.key -> dialect.toString) { | ||
| checkEvaluation( | ||
| DateDiff(Literal(Date.valueOf("2015-07-24")), Literal(Date.valueOf("2015-07-21"))), 3) | ||
| checkEvaluation( | ||
| DateDiff(Literal(Date.valueOf("2015-07-21")), Literal(Date.valueOf("2015-07-24"))), -3) | ||
| checkEvaluation( | ||
| DateDiff(Literal.create(null, DateType), Literal(Date.valueOf("2015-07-24"))), | ||
| null) | ||
| checkEvaluation( | ||
| DateDiff(Literal(Date.valueOf("2015-07-24")), Literal.create(null, DateType)), | ||
| null) | ||
| checkEvaluation( | ||
| DateDiff(Literal.create(null, DateType), Literal.create(null, DateType)), | ||
| null) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| test("datediff returns an interval") { | ||
| withSQLConf(SQLConf.ANSI_ENABLED.key -> "true", SQLConf.DIALECT.key -> "Spark") { | ||
| val end = LocalDate.of(2019, 10, 5) | ||
| checkEvaluation(DateDiff(Literal(end), Literal(end)), | ||
| new CalendarInterval(0, 0)) | ||
| checkEvaluation(DateDiff(Literal(end.plusDays(1)), Literal(end)), | ||
| CalendarInterval.fromString("interval 1 days")) | ||
| checkEvaluation(DateDiff(Literal(end.minusDays(1)), Literal(end)), | ||
| CalendarInterval.fromString("interval -1 days")) | ||
| val epochDate = Literal(LocalDate.ofEpochDay(0)) | ||
| checkEvaluation(DateDiff(Literal(end), epochDate), | ||
| CalendarInterval.fromString("interval 49 years 9 months 4 days")) | ||
| checkEvaluation(DateDiff(epochDate, Literal(end)), | ||
| CalendarInterval.fromString("interval -49 years -9 months -4 days")) | ||
| checkEvaluation( | ||
| DateDiff( | ||
| Literal(LocalDate.of(10000, 1, 1)), | ||
| Literal(LocalDate.of(1, 1, 1))), | ||
| CalendarInterval.fromString("interval 9999 years")) | ||
| } | ||
| } | ||
|
|
||
| test("to_utc_timestamp") { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe, one-liner
SQLConf.get.ansiEnabled && SQLConf.get.getConf(DIALECT) == Dialect.POSTGRESQL.toStringis enough. Please note that I suggestedDialect.POSTGRESQL.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you do what you suggest, this will change results of
date.sql:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That test suite aims to test
ANSIandPostgreSQLdialect. Given that, the regeneration will be a correct solution for the question. Or, if we want to test onlySparkdialect there, we can turn offANSImode before that test case by adding one-line configuration. (Also, turn on back after that test case).cc @maropu , @gatorsmile , @gengliangwang .