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 functions PERIOD_ADD and PERIOD_DIFF. #933

Merged

Conversation

Yury-Fridlyand
Copy link
Collaborator

@Yury-Fridlyand Yury-Fridlyand commented Oct 19, 2022

Signed-off-by: Yury-Fridlyand [email protected]

Description

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

New function: PERIOD_ADD.

Changes

Adds N months to period P (in the format YYMM or YYYYMM). Returns a value in the format YYYYMM.

Fully compliant with MySQL.

Signature

updated LONG to INT

(INTEGER, INTEGER) -> INTEGER

Future changes (TODOs):

Update if and after #862

Notes

Not documented, but with experiments I discovered that

  • 1 or 2 digits year is interpreted as 2000 + x.
  • 2 digit year lower that 70 interpreted as 20xx, higher than 70 - as 19xx

Test queries:

SELECT PERIOD_ADD(200801, 2), PERIOD_ADD(200801, -12);
SELECT PERIOD_ADD(111, 2), PERIOD_ADD(1111, 2), PERIOD_ADD(1111, 2);
SELECT PERIOD_ADD(7011, 2), PERIOD_ADD(6911, 2);

New function: PERIOD_DIFF.

Changes

Returns the number of months between periods P1 and P2. P1 and P2 should be in the format YYMM or YYYYMM.

Fully compliant with MySQL.

Signature

updated LONG to INT

(INTEGER, INTEGER) -> INTEGER

Future changes (TODOs):

Update if and after #862

Notes

Not documented, but with experiments I discovered that

  • 1 or 2 digits year is interpreted as 2000 + x.
  • 2 digit year lower that 70 interpreted as 20xx, higher than 70 - as 19xx

Test queries:

SELECT PERIOD_DIFF(200802, 200703), PERIOD_DIFF(200802, 201003);
SELECT PERIOD_DIFF(7011, 6911);

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)

See team review in Bit-Quill#130

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.

MaxKsyunz
MaxKsyunz previously approved these changes Oct 20, 2022
@dai-chen dai-chen added the enhancement New feature or request label Oct 20, 2022
@Yury-Fridlyand
Copy link
Collaborator Author

Rebased to resolve conflicts.

dai-chen
dai-chen previously approved these changes Oct 27, 2022
Copy link
Collaborator

@dai-chen dai-chen left a comment

Choose a reason for hiding this comment

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

Just minor comment on the function signature. Thanks for the changes!

@codecov-commenter

This comment was marked as spam.

@dai-chen dai-chen merged commit d46b389 into opensearch-project:2.x Oct 28, 2022
@Yury-Fridlyand Yury-Fridlyand deleted the integ-datetime-period-functions branch October 28, 2022 18:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants