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 datetime functions FROM_UNIXTIME and UNIX_TIMESTAMP #114

Merged
merged 13 commits into from
Sep 15, 2022

Conversation

Yury-Fridlyand
Copy link

FROM_UNIXTIME

  • (DOUBLE) -> DATETIME
  • (DOUBLE, STRING) -> STRING

Implementation details

  • For string formatting DATE_FORMAT function is used
  • Function returns 0 for values outside of range 1970-01-01 00:00:00 - 3001-01-18 23:59:59.999999

NOTES

Example
opensearchsql> select FROM_UNIXTIME(1220249547.12);
fetched rows / total rows = 1/1
+--------------------------------+
| FROM_UNIXTIME(1220249547.12)   |
|--------------------------------|
| 2008-09-01 06:12:27.119999885  |
+--------------------------------+

Test samples

SQL
select FROM_UNIXTIME(1337.42), FROM_UNIXTIME(123235435, '%T %M %D, %Y');
+--------------------------+--------------------------------------------+
| FROM_UNIXTIME(1337.42)   | FROM_UNIXTIME(123235435, '%T %M %D, %Y')   |
|--------------------------+--------------------------------------------|
| 1970-01-01 00:22:17.42   | 08:03:55 November 27th, 1973               |
+--------------------------+--------------------------------------------+
PPL
source=date | top 1 date_keyword | eval `FROM_UNIXTIME(1337.42)` = FROM_UNIXTIME(1337.42), `FROM_UNIXTIME(123235435, '%T %M %D, %Y')` = FROM_UNIXTIME(123235435, '%T %M %D, %Y') | fields `FROM_UNIXTIME(1337.42)`, `FROM_UNIXTIME(123235435, '%T %M %D, %Y')`;
+--------------------------+--------------------------------------------+
| FROM_UNIXTIME(1337.42)   | FROM_UNIXTIME(123235435, '%T %M %D, %Y')   |
|--------------------------+--------------------------------------------|
| 1970-01-01 00:22:17.42   | 08:03:55 November 27th, 1973               |
+--------------------------+--------------------------------------------+

UNIX_TIMESTAMP

  • () -> LONG
  • DATE -> DOUBLE
  • DATETIME -> DOUBLE
  • TIMESTAMP -> DOUBLE
  • DOUBLE -> DOUBLE

Implementation details

  • First function signature calls for LocalDateTime.now() without caching the value. Sequential calls in one query might return different values.
  • Function returns 0 for values outside of range 1970-01-01 00:00:00 - 3001-01-18 23:59:59.999999.

NOTES

Example
opensearchsql> select UNIX_TIMESTAMP(20771122143845.2345);
fetched rows / total rows = 1/1
+---------------------------------------+
| UNIX_TIMESTAMP(20771122143845.2345)   |
|---------------------------------------|
| 3404817524.234375                     |
+---------------------------------------+

Test samples

SQL
select UNIX_TIMESTAMP(), UNIX_TIMESTAMP(20771122143845), UNIX_TIMESTAMP(881226), UNIX_TIMESTAMP(881226231058.123), UNIX_TIMESTAMP(DATE('1995-05-06')), UNIX_TIMESTAMP(TIMESTAMP('2022-08-09 12:16:12')), UNIX_TIMESTAMP(ADDDATE(DATE('2020-08-26'), INTERVAL 1 HOUR));
UNIX_TIMESTAMP()                                             | 1662664900
UNIX_TIMESTAMP(20771122143845)                               | 3404817525.0
UNIX_TIMESTAMP(881226)                                       | 599097600.0
UNIX_TIMESTAMP(881226231058.123)                             | 599181057.1230469
UNIX_TIMESTAMP(DATE('1995-05-06'))                           | 799718400.0
UNIX_TIMESTAMP(TIMESTAMP('2022-08-09 12:16:12'))             | 1660047372.0
UNIX_TIMESTAMP(ADDDATE(DATE('2020-08-26'), INTERVAL 1 HOUR)) | 1598403600.0
PPL
source=date | eval `UNIX_TIMESTAMP()` = UNIX_TIMESTAMP(), `UNIX_TIMESTAMP(20771122143845)` = UNIX_TIMESTAMP(20771122143845), `UNIX_TIMESTAMP(881226)` = UNIX_TIMESTAMP(881226), `UNIX_TIMESTAMP(881226231058.123)` = UNIX_TIMESTAMP(881226231058.123), `UNIX_TIMESTAMP(DATE('1995-05-06'))` = UNIX_TIMESTAMP(DATE('1995-05-06')), `UNIX_TIMESTAMP(TIMESTAMP('2022-08-09 12:16:12'))` = UNIX_TIMESTAMP(TIMESTAMP('2022-08-09 12:16:12')), `UNIX_TIMESTAMP(ADDDATE(DATE('2020-08-26'), INTERVAL 1 HOUR))` = UNIX_TIMESTAMP(ADDDATE(DATE('2020-08-26'), INTERVAL 1 HOUR)) | fields `UNIX_TIMESTAMP()`, `UNIX_TIMESTAMP(20771122143845)`, `UNIX_TIMESTAMP(881226)`, `UNIX_TIMESTAMP(881226231058.123)`, `UNIX_TIMESTAMP(DATE('1995-05-06'))`, `UNIX_TIMESTAMP(TIMESTAMP('2022-08-09 12:16:12'))`, `UNIX_TIMESTAMP(ADDDATE(DATE('2020-08-26'), INTERVAL 1 HOUR))`;
UNIX_TIMESTAMP()                                             | 1662664900
UNIX_TIMESTAMP(20771122143845)                               | 3404817525.0
UNIX_TIMESTAMP(881226)                                       | 599097600.0
UNIX_TIMESTAMP(881226231058.123)                             | 599181057.1230469
UNIX_TIMESTAMP(DATE('1995-05-06'))                           | 799718400.0
UNIX_TIMESTAMP(TIMESTAMP('2022-08-09 12:16:12'))             | 1660047372.0
UNIX_TIMESTAMP(ADDDATE(DATE('2020-08-26'), INTERVAL 1 HOUR)) | 1598403600.0

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.

Signed-off-by: Yury-Fridlyand <[email protected]>
Signed-off-by: Yury-Fridlyand <[email protected]>
Signed-off-by: Yury-Fridlyand <[email protected]>
@Bit-Quill Bit-Quill deleted a comment from codecov bot Sep 8, 2022
@Bit-Quill Bit-Quill deleted a comment from codecov bot Sep 8, 2022
@@ -6,6 +6,7 @@

package org.opensearch.sql.expression.datetime;

import static java.time.temporal.ChronoField.YEAR;

Choose a reason for hiding this comment

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

is there no YEAR field in ExprCoreType?

Copy link
Author

Choose a reason for hiding this comment

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

No.. Why it should be? Aren't you messing ChronoField and ExprCoreType? ExprCoreType is a enum of STRING, DOUBLE, etc

@codecov

This comment was marked as spam.

docs/user/ppl/functions/datetime.rst Outdated Show resolved Hide resolved
docs/user/dql/functions.rst Show resolved Hide resolved
docs/user/dql/functions.rst Outdated Show resolved Hide resolved
docs/user/dql/functions.rst Outdated Show resolved Hide resolved
docs/user/dql/functions.rst Outdated Show resolved Hide resolved
docs/user/ppl/functions/datetime.rst Outdated Show resolved Hide resolved
Yury-Fridlyand and others added 2 commits September 12, 2022 18:52
Co-authored-by: MaxKsyunz <[email protected]>
Signed-off-by: Yury-Fridlyand <[email protected]>
@MitchellGale
Copy link

MySQL:

mysql> select FROM_UNIXTIME(1337.4200), FROM_UNIXTIME(123235435, '%T %M %D, %Y');
+--------------------------+------------------------------------------+
| FROM_UNIXTIME(1337.4200) | FROM_UNIXTIME(123235435, '%T %M %D, %Y') |
+--------------------------+------------------------------------------+
| 1970-01-01 00:22:17.4200 | 08:03:55 November 27th, 1973             |
+--------------------------+------------------------------------------+
1 row in set (0.00 sec)

OpenSearchSQL:

opensearchsql> select FROM_UNIXTIME(1337.4200), FROM_UNIXTIME(123235435, '%T %M %D, %Y');                                                                                                                                                                   
fetched rows / total rows = 1/1
+----------------------------+--------------------------------------------+
| FROM_UNIXTIME(1337.4200)   | FROM_UNIXTIME(123235435, '%T %M %D, %Y')   |
|----------------------------+--------------------------------------------|
| 1970-01-01 00:22:17.42     | 08:03:55 November 27th, 1973               |
+----------------------------+--------------------------------------------+

Missing trailing zeros in FROM_UNIXTIME

Copy link

@MitchellGale MitchellGale left a comment

Choose a reason for hiding this comment

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

select UNIX_TIMESTAMP(TIMESTAMP('1996-13-15 17:05:42')); 

Should return null (similar with other invalid dates (or times!)) instead of

TransportError(500, 'SemanticCheckException', {'error': {'type': 'SemanticCheckException', 'reason': 'Invalid Query', 'details': 'timestamp:1996-13-15 17:05:42 in unsupported format, please use yyyy-MM-dd HH:mm:ss[.SSSSSSSSS]'}, 'status': 400})

| 3404817525.0 |
+----------------------------------+

os> select UNIX_TIMESTAMP(TIMESTAMP('1996-11-15 17:05:42'))

Choose a reason for hiding this comment

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

MySQL shows this as returning without a floating point.

mysql> select UNIX_TIMESTAMP(TIMESTAMP('1996-11-15 17:05:42'));
+--------------------------------------------------+
| UNIX_TIMESTAMP(TIMESTAMP('1996-11-15 17:05:42')) |
+--------------------------------------------------+
|                                        848077542 |
+--------------------------------------------------+
1 row in set (0.00 sec)

Copy link
Author

Choose a reason for hiding this comment

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

It is added by the double serializer, it is out of my control. We can discuss to fix this globally in scope of another fix.

@@ -375,7 +375,7 @@ trigonometricFunctionName
dateAndTimeFunctionBase
: ADDDATE | DATE | DATE_ADD | DATE_SUB | DAY | DAYNAME | DAYOFMONTH | DAYOFWEEK | DAYOFYEAR | FROM_DAYS
| HOUR | MICROSECOND | MINUTE | MONTH | MONTHNAME | QUARTER | SECOND | SUBDATE | TIME | TIME_TO_SEC
| TIMESTAMP | TO_DAYS | YEAR | WEEK | DATE_FORMAT | MAKETIME | MAKEDATE
| TIMESTAMP | TO_DAYS | YEAR | WEEK | DATE_FORMAT | MAKETIME | MAKEDATE | FROM_UNIXTIME | UNIX_TIMESTAMP

Choose a reason for hiding this comment

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

Add in alphabetical order please.

Copy link
Author

Choose a reason for hiding this comment

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

Thank you, fixed in faf0372.

@@ -385,7 +385,7 @@ trigonometricFunctionName
dateTimeFunctionName
: ADDDATE | DATE | DATE_ADD | DATE_SUB | DAY | DAYNAME | DAYOFMONTH | DAYOFWEEK | DAYOFYEAR | FROM_DAYS
| HOUR | MICROSECOND | MINUTE | MONTH | MONTHNAME | QUARTER | SECOND | SUBDATE | TIME | TIME_TO_SEC
| TIMESTAMP | TO_DAYS | YEAR | WEEK | DATE_FORMAT | MAKETIME | MAKEDATE
| TIMESTAMP | TO_DAYS | YEAR | WEEK | DATE_FORMAT | MAKETIME | MAKEDATE | FROM_UNIXTIME | UNIX_TIMESTAMP

Choose a reason for hiding this comment

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

Add in alphabetical order.

Copy link
Author

Choose a reason for hiding this comment

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

Thank you, fixed in faf0372.

@Yury-Fridlyand
Copy link
Author

Missing trailing zeros in FROM_UNIXTIME

Interesting. It is impossible to track this in our framework. We convert input to Double, it doesn't store or serialize non-significant digits. The same applicable to the output value.

Copy link

@MaxKsyunz MaxKsyunz left a comment

Choose a reason for hiding this comment

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

@Yury-Fridlyand please update FromUnixTimeTest, UnixTimeStampTeset, and UnixTwoWayConversionTest to match existing expression tests in core/src/test/java/org/opensearch/sql/expression.

The following belong in core/src/main/java/org/opensearch/sql/expression/DSL.java

  • FromUnixTimeTest.fromUnixTime(Expression)
  • FromUnixTimeTest.fromUnixTime(Expression, Expression)
  • UnixTimeStampTest.unixTimeStampExpr()
  • UnixTimeStampTest.unixTimeStampOf(Expression)

Once you do that, duplicated functions from UnixTwoWayConversionTest can be removed.

…TimeTestBase.java


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

Co-authored-by: Max Ksyunz <[email protected]>
Signed-off-by: Yury-Fridlyand <[email protected]>
@acarbonetto acarbonetto self-requested a review September 15, 2022 16:39
@Yury-Fridlyand Yury-Fridlyand merged commit c01f1df into integ-datetime-unix-convert Sep 15, 2022
@Yury-Fridlyand Yury-Fridlyand deleted the dev-datetime-unix-convert branch September 15, 2022 23:27
Yury-Fridlyand added a commit that referenced this pull request Sep 15, 2022
* Add implementation for `FROM_UNIXTIME` and `UNIX_TIMESTAMP` functions, UT and IT.

Signed-off-by: Yury-Fridlyand <[email protected]>
Yury-Fridlyand added a commit that referenced this pull request Sep 24, 2022
* Add implementation for `FROM_UNIXTIME` and `UNIX_TIMESTAMP` functions, UT and IT.

Signed-off-by: Yury-Fridlyand <[email protected]>
forestmvey pushed a commit that referenced this pull request Sep 27, 2022
…ch-project#835)

* Add datetime functions `FROM_UNIXTIME` and `UNIX_TIMESTAMP` (#114)

* Add implementation for `FROM_UNIXTIME` and `UNIX_TIMESTAMP` functions, UT and IT.

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

* Collent all DateTime formatters into one place.

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

* Rename `DateFormatters` -> `DateTimeFormatters`.

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

Signed-off-by: Yury-Fridlyand <[email protected]>
MitchellGale pushed a commit that referenced this pull request Oct 3, 2022
…ch-project#835)

* Add datetime functions `FROM_UNIXTIME` and `UNIX_TIMESTAMP` (#114)

* Add implementation for `FROM_UNIXTIME` and `UNIX_TIMESTAMP` functions, UT and IT.

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

* Collent all DateTime formatters into one place.

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

* Rename `DateFormatters` -> `DateTimeFormatters`.

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

Signed-off-by: Yury-Fridlyand <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants