Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SPARK-35051][SQL] Support add/subtract of a day-time interval to/fro…
…m a date ### What changes were proposed in this pull request? Support `date +/- day-time interval`. In the PR, I propose to update the binary arithmetic rules, and cast an input date to a timestamp at the session time zone, and then add a day-time interval to it. ### Why are the changes needed? 1. To conform the ANSI SQL standard which requires to support such operation over dates and intervals: <img width="811" alt="Screenshot 2021-03-12 at 11 36 14" src="https://user-images.githubusercontent.com/1580697/111081674-865d4900-8515-11eb-86c8-3538ecaf4804.png"> 2. To fix the regression comparing to the recent Spark release 3.1 with default settings. Before the changes: ```sql spark-sql> select date'now' + (timestamp'now' - timestamp'yesterday'); Error in query: cannot resolve 'DATE '2021-04-14' + subtracttimestamps(TIMESTAMP '2021-04-14 18:14:56.497', TIMESTAMP '2021-04-13 00:00:00')' due to data type mismatch: argument 1 requires timestamp type, however, 'DATE '2021-04-14'' is of date type.; line 1 pos 7; 'Project [unresolvedalias(cast(2021-04-14 + subtracttimestamps(2021-04-14 18:14:56.497, 2021-04-13 00:00:00, false, Some(Europe/Moscow)) as date), None)] +- OneRowRelation ``` Spark 3.1: ```sql spark-sql> select date'now' + (timestamp'now' - timestamp'yesterday'); 2021-04-15 ``` Hive: ```sql 0: jdbc:hive2://localhost:10000/default> select date'2021-04-14' + (timestamp'2020-04-14 18:15:30' - timestamp'2020-04-13 00:00:00'); +------------------------+ | _c0 | +------------------------+ | 2021-04-15 18:15:30.0 | +------------------------+ ``` ### Does this PR introduce _any_ user-facing change? Should not since new intervals have not been released yet. After the changes: ```sql spark-sql> select date'now' + (timestamp'now' - timestamp'yesterday'); 2021-04-15 18:13:16.555 ``` ### How was this patch tested? By running new tests: ``` $ build/sbt "test:testOnly *ColumnExpressionSuite" ``` Closes #32170 from MaxGekk/date-add-day-time-interval. Authored-by: Max Gekk <max.gekk@gmail.com> Signed-off-by: Max Gekk <max.gekk@gmail.com>
- Loading branch information