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

Add TIMEDIFF and DATEDIFF functions. (#131) #1195

Merged

Conversation

Yury-Fridlyand
Copy link
Collaborator

Signed-off-by: Yury-Fridlyand yuryf@bitquilltech.com

Description

I referred to MySQL docs and tried to reproduce MySQL v.8.0.30 behavior as a reference.

See team review and discussion on Bit-Quill#131

New function: TIMEDIFF.

Changes

Signature

(TIME, TIME) -> TIME

Future changes (TODOs):

  1. Accept more/different types if and after [Discussion] Do we need to support more than 24 hours in TIME? #852
  2. Accept strings

Test queries:

-- OpenSearch
SELECT TIMEDIFF('23:59:59', '13:00:00');
-- MySQL
SELECT TIMEDIFF('2007-12-31 23:59:59', '13:00:00');
SELECT TIMEDIFF('2007-12-31 23:59:59', '2004-01-01 13:00:00');
SELECT TIMEDIFF('2000-01-01 00:00:00', now());
SELECT TIMEDIFF(DATE('2008-12-12'), DATE('2008-11-15'));

New function: DATEDIFF.

Changes

Calculates the difference of date part of given values.

Fully compliant with MySQL.

Signature

(DATE/DATETIME/TIMESTAMP/TIME, DATE/DATETIME/TIMESTAMP/TIME) -> LONG

Future changes (TODOs):

Accept strings if it gives performance gain

Test queries:

SELECT DATEDIFF(TIMESTAMP('2000-01-02 00:00:00'), TIMESTAMP('2000-01-01 23:59:59'));
SELECT DATEDIFF(TIME('23:59:59'), TIMESTAMP('2004-01-01 00:00:00'));
SELECT DATEDIFF(TIME('23:59:59'), TIME('00:00:00'));
SELECT DATEDIFF(TIMESTAMP('2000-01-02 00:00:00'), DATETIME('2000-01-01 23:59:59'));

Test data

I found that first 6 rows from date0, time0, time1, datetime0 are good for testing - these columns have different data types in MySQL. In OpenSearch SQL all [date][time] columns have timestamp type, so I use CAST for clear testing.

data
mysql> show fields from Calcs where field IN ('date0', 'time0', 'time1', 'datetime0');
+-----------+-----------+------+-----+---------+-------+
| Field     | Type      | Null | Key | Default | Extra |
+-----------+-----------+------+-----+---------+-------+
| date0     | date      | YES  |     | NULL    |       |
| time0     | datetime  | YES  |     | NULL    |       |
| time1     | time      | YES  |     | NULL    |       |
| datetime0 | timestamp | YES  |     | NULL    |       |
+-----------+-----------+------+-----+---------+-------+
4 rows in set (0.00 sec)
mysql> select date0, time0, time1, datetime0 from calcs;
+------------+---------------------+----------+---------------------+
| date0      | time0               | time1    | datetime0           |
+------------+---------------------+----------+---------------------+
| 2004-04-15 | 1899-12-30 21:07:32 | 19:36:22 | 2004-07-09 10:17:35 |
| 1972-07-04 | 1900-01-01 13:48:48 | 02:05:25 | 2004-07-26 12:30:34 |
| 1975-11-12 | 1900-01-01 18:21:08 | 09:33:31 | 2004-08-02 07:59:23 |
| 2004-06-04 | 1900-01-01 18:51:48 | 22:50:16 | 2004-07-05 13:14:20 |
| 2004-06-19 | 1900-01-01 15:01:19 | NULL     | 2004-07-28 23:30:22 |
| NULL       | 1900-01-01 08:59:39 | 19:57:33 | 2004-07-22 00:30:23 |
| NULL       | 1900-01-01 07:37:48 | NULL     | 2004-07-28 06:54:50 |
| NULL       | 1900-01-01 19:45:54 | 19:48:23 | 2004-07-12 17:30:16 |
| NULL       | 1900-01-01 09:00:59 | 22:20:14 | 2004-07-04 22:49:28 |
| NULL       | 1900-01-01 20:36:00 | NULL     | 2004-07-23 21:13:37 |
| NULL       | 1900-01-01 01:31:32 | 00:05:57 | 2004-07-14 08:16:44 |
| NULL       | 1899-12-30 22:15:40 | 04:40:49 | 2004-07-25 15:22:26 |
| NULL       | 1900-01-01 13:53:46 | 04:48:07 | 2004-07-17 14:01:56 |
| NULL       | 1900-01-01 04:57:51 | NULL     | 2004-07-19 22:21:31 |
| NULL       | 1899-12-30 22:42:43 | 18:58:41 | 2004-07-31 11:57:52 |
| NULL       | 1899-12-30 22:24:08 | NULL     | 2004-07-14 07:43:00 |
| NULL       | 1900-01-01 11:58:29 | 12:33:57 | 2004-07-28 12:34:28 |
+------------+---------------------+----------+---------------------+
17 rows in set (0.00 sec)

Check List

  • New functionality includes testing.
    • All tests pass, including unit test, integration test and doctest
  • New functionality has been documented.
    • New functionality has javadoc added
    • New functionality has user manual doc added
  • Commits are signed per the DCO using --signoff

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
For more information on following Developer Certificate of Origin and signing off your commits, please check here.

* Add `TIMEDIFF` and `DATEDIFF` functions.

Signed-off-by: Yury-Fridlyand <yuryf@bitquilltech.com>
Signed-off-by: Yury-Fridlyand <yury.fridlyand@improving.com>
Co-authored-by: Max Ksyunz <maxk@bitquilltech.com>
@codecov-commenter
Copy link

codecov-commenter commented Dec 19, 2022

Codecov Report

Merging #1195 (9ed9529) into main (2d34d9e) will not change coverage.
The diff coverage is n/a.

@@           Coverage Diff           @@
##             main    #1195   +/-   ##
=======================================
  Coverage   62.76%   62.76%           
=======================================
  Files          10       10           
  Lines         658      658           
  Branches      119      119           
=======================================
  Hits          413      413           
  Misses        192      192           
  Partials       53       53           
Flag Coverage Δ
query-workbench 62.76% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

@dai-chen dai-chen added the enhancement New feature or request label Dec 20, 2022
@dai-chen dai-chen merged commit 438c44d into opensearch-project:main Jan 6, 2023
opensearch-trigger-bot bot pushed a commit that referenced this pull request Jan 6, 2023
* Add `TIMEDIFF` and `DATEDIFF` functions.

Signed-off-by: Yury-Fridlyand <yuryf@bitquilltech.com>
Signed-off-by: Yury-Fridlyand <yury.fridlyand@improving.com>
Co-authored-by: Max Ksyunz <maxk@bitquilltech.com>

Signed-off-by: Yury-Fridlyand <yuryf@bitquilltech.com>
Signed-off-by: Yury-Fridlyand <yury.fridlyand@improving.com>
Co-authored-by: Max Ksyunz <maxk@bitquilltech.com>
(cherry picked from commit 438c44d)
dai-chen pushed a commit that referenced this pull request Jan 6, 2023
* Add `TIMEDIFF` and `DATEDIFF` functions.

Signed-off-by: Yury-Fridlyand <yuryf@bitquilltech.com>
Signed-off-by: Yury-Fridlyand <yury.fridlyand@improving.com>
Co-authored-by: Max Ksyunz <maxk@bitquilltech.com>

Signed-off-by: Yury-Fridlyand <yuryf@bitquilltech.com>
Signed-off-by: Yury-Fridlyand <yury.fridlyand@improving.com>
Co-authored-by: Max Ksyunz <maxk@bitquilltech.com>
(cherry picked from commit 438c44d)

Co-authored-by: Yury-Fridlyand <yury.fridlyand@improving.com>
@Yury-Fridlyand Yury-Fridlyand deleted the integ-datetime-diff-functions branch February 15, 2023 01:17
@VenkatManuru
Copy link

Hi

I don't see these functions available in latest version https://opensearch.org/docs/latest/search-plugins/sql/functions/#date-and-time. Can you please confirm if these are available? If not, by when these will be made available?

@dai-chen dai-chen added the v2.5.0 'Issues and PRs related to version v2.5.0' label Mar 13, 2023
@Yury-Fridlyand
Copy link
Collaborator Author

@VenkatManuru,
It was released in 2.5.0. Updates to documentation on the website are delivered slowly, it is not yet updated, unfortunately.
Please, refer to docs in the SQL plugin repo, it is always up-to-date.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport 2.x enhancement New feature or request v2.5.0 'Issues and PRs related to version v2.5.0'
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants