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

Conversation

wangyum
Copy link
Member

@wangyum wangyum commented May 31, 2019

What changes were proposed in this pull request?

This pr add support 4 PostgreSQL's date operators(date + integer, integer + date, date - integer and date - date):

Operator Example Result
+ date '2001-09-28' + 7 date '2001-10-05'
+ 7 + date '2001-09-28' date '2001-10-05'
- date '2001-10-01' - 7 date '2001-09-24'
- date '2001-10-01' - date '2001-09-28' integer '3' (days)

Most databases support date - date operation, where PostgreSQL, Vertica, Teradata, Oracle and DB2 returns Integer type, Hive and Presto returns Interval type, MySQL returns unexpected value, and SQL Server does not support date - date operation.

PostgreSQL:

postgres=# select substr(version(), 0, 16), date '2001-09-28' + 7, 7 + date '2001-09-28', date '2001-10-01' - 7, date '2001-10-01' - date '2001-09-28';
     substr      |  ?column?  |  ?column?  |  ?column?  | ?column?
-----------------+------------+------------+------------+----------
 PostgreSQL 11.3 | 2001-10-05 | 2001-10-05 | 2001-09-24 |        3
(1 row)

Vertica:

dbadmin=> select version(), date '2001-09-28' + 7, 7 + date '2001-09-28', date '2001-10-01' - 7, date '2001-10-01' - date '2001-09-28';
              version               |  ?column?  |  ?column?  |  ?column?  | ?column?
------------------------------------+------------+------------+------------+----------
 Vertica Analytic Database v9.1.1-0 | 2001-10-05 | 2001-10-05 | 2001-09-24 |        3
(1 row)

Teradata:
image

Oracle:
image
DB2:
image
Hive:

hive> select version(),  date '2001-10-01' - date '2001-09-28';
OK
3.1.1 rf4e0529634b6231a0072295da48af466cf2f10b7	3 00:00:00.000000000
Time taken: 2.038 seconds, Fetched: 1 row(s)

Presto:

presto> select  date '2001-10-01' - date '2001-09-28';
     _col0
----------------
 3 00:00:00.000
(1 row)

MySQL:

mysql> SELECT version(), date '2001-10-01' - date '2001-09-28';
+-----------+---------------------------------------+
| version() | date '2001-10-01' - date '2001-09-28' |
+-----------+---------------------------------------+
| 5.7.26    |                                    73 |
+-----------+---------------------------------------+
1 row in set (0.00 sec)

More details:
https://www.postgresql.org/docs/12/functions-datetime.html

How was this patch tested?

unit tests

1. date + integer
2. integer + date
3. date - integer
4. date - date
@MaxGekk
Copy link
Member

MaxGekk commented May 31, 2019

According to SQL standard, date '2001-10-01' - date '2001-09-28' must return the Interval type instead of IntegerType as DateDiff does.

@SparkQA
Copy link

SparkQA commented May 31, 2019

Test build #106016 has finished for PR 24755 at commit c10533d.

  • This patch passes all tests.
  • This patch merges cleanly.
  • This patch adds no public classes.

@wangyum
Copy link
Member Author

wangyum commented Jun 16, 2019

@MaxGekk I prefer to return the Integer type. PostgreSQL, Vertica, Teradata, Oracle, and DB2 all return Integer types, and only Hive and Presto return Interval types.

@gatorsmile
Copy link
Member

What is the current behavior of Spark SQL?

@wangyum
Copy link
Member Author

wangyum commented Jun 23, 2019

Our DateDiff returns integer:

@dongjoon-hyun
Copy link
Member

Retest this please.

@SparkQA
Copy link

SparkQA commented Jul 5, 2019

Test build #107272 has finished for PR 24755 at commit c10533d.

  • This patch passes all tests.
  • This patch merges cleanly.
  • This patch adds no public classes.

Copy link
Member

@dongjoon-hyun dongjoon-hyun left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although I have been preferring Hive/PrestoSQL compatibility over the others, but I'm +1 for this PR since this is a new feature and we are working on PostgreSQL compatibility improvement in 3.0. We can add an additional configuration later if we need Hive/PrestoSQL compatibility, too.

+1, LGTM. Merged to master. Let's move forward.

Thank you so much, @wangyum , @gatorsmile , @MaxGekk .

@wangyum wangyum deleted the Add4DateOperators branch July 6, 2019 02:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
5 participants