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 implementation of now, sysdate, localtime and similar functions #754

Merged
merged 3 commits into from
Sep 23, 2022

Conversation

Yury-Fridlyand
Copy link
Collaborator

@Yury-Fridlyand Yury-Fridlyand commented Aug 12, 2022

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

Description

New functions added:

  • now()
  • current_timestamp()
  • localtimestamp()
  • localtime()
  • sysdate()
  • curtime()
  • current_time()
  • curdate()
  • current_date()

New signatures:

  • now() : datetime
  • current_timestamp() : datetime
  • current_timestamp : datetime
  • localtimestamp() : datetime
  • localtimestamp : datetime
  • localtime() : datetime
  • localtime : datetime
  • sysdate() : datetime
  • sysdate(int) : datetime
  • curtime() : time
  • current_time() : time
  • current_time : time
  • curdate() : date
  • current_date() : date
  • current_date : date
Not added signatures
  • now(int) : datetime
  • current_timestamp(int) : datetime
  • localtimestamp(int) : datetime
  • localtime(int) : datetime
  • curtime(int) : time
  • current_time(int) : time

Tests

SQL
select now(), current_timestamp(), localtimestamp(), localtime(), sysdate(), sysdate(3), curdate(), curtime(), current_time(), current_date(), CURRENT_DATE, CURRENT_TIME, CURRENT_TIMESTAMP, LOCALTIME, LOCALTIMESTAMP;
fetched rows / total rows = 1/1
-[ RECORD 1 ]-------------------------
now()                   | 2022-07-20 20:15:25.262409
current_timestamp()     | 2022-07-20 20:15:25.262409
localtimestamp()        | 2022-07-20 20:15:25.262409
localtime()             | 2022-07-20 20:15:25.262409
sysdate()               | 2022-07-20 20:15:26.468210
sysdate(3)              | 2022-07-20 20:15:26.468000
curdate()               | 2022-07-20
curtime()               | 20:15:26.469603
current_time()          | 20:15:26.469746
current_date()          | 2022-07-20
CURRENT_DATE            | 2022-07-20
CURRENT_TIME            | 20:15:26.469869
CURRENT_TIMESTAMP       | 2022-07-20 20:15:25.262409
LOCALTIME               | 2022-07-20 20:15:25.262409
LOCALTIMESTAMP          | 2022-07-20 20:15:25.262409
PPL
source=calcs | top 1 zzz | eval `now()` = now(), `current_timestamp()` = current_timestamp(), `localtimestamp()` = localtimestamp(), `localtime()` = localtime(), `sysdate()` = sysdate(), `sysdate(3)` = sysdate(3), `curdate()` = curdate(), `curtime()` = curtime(), `current_time()` = current_time(), `current_date()` = current_date(), `CURRENT_DATE` = CURRENT_DATE, `CURRENT_TIME` = CURRENT_TIME, `CURRENT_TIMESTAMP` = CURRENT_TIMESTAMP, `LOCALTIME` = LOCALTIME, `LOCALTIMESTAMP` = LOCALTIMESTAMP;
fetched rows / total rows = 1/1
-[ RECORD 1 ]-------------------------
zzz                     | f
now()                   | 2022-07-20 20:17:22.451338
current_timestamp()     | 2022-07-20 20:17:22.451338
localtimestamp()        | 2022-07-20 20:17:22.451338
localtime()             | 2022-07-20 20:17:22.451338
sysdate()               | 2022-07-20 20:17:22.760505
sysdate(3)              | 2022-07-20 20:17:22.760000
curdate()               | 2022-07-20
curtime()               | 20:17:22.760584
current_time()          | 20:17:22.760626
current_date()          | 2022-07-20
CURRENT_DATE            | 2022-07-20
CURRENT_TIME            | 20:17:22.760693
CURRENT_TIMESTAMP       | 2022-07-20 20:17:22.451338
LOCALTIME               | 2022-07-20 20:17:22.451338
LOCALTIMESTAMP          | 2022-07-20 20:17:22.451338

Implementation details

NOW and SYSDATE difference
According to MySQL standard

NOW() returns a constant time that indicates the time at which the statement began to execute.

SYSDATE() returns the time at which it executes.

I had to save and then to pick time when plugin receives a query. QueryContext was chosen to store this unless a better candidate introduced.

Parser update

  1. datetimeConstantLiteral was added to SQL and PPL parsers. It lists pseudo-constants which actually invoke corresponding functions without (). So CURRENT_DATE is shortcut for CURRENT_DATE() and so on.
  2. AstExpressionBuilder were updated to call a function when datetimeConstantLiteral met.
  3. AstExpressionBuilder in PPL is updated a bit by adding visitFunction method and removing some duplicated code. Copied from AstExpressionBuilder in SQL.
  4. ANTLR rules were copied from SQL to PPL.
  5. ConstantFunction entity added to represent functions which values should be cached in scope of the current query. Cache is stored in AnalysisContext. This includes changes in:
  • SQL parser
  • PPL parser
  • ExpressionAnalyzer
  1. All functions are cached (are ConstantFunction) except:
  • sysdate
  • curdate
  • current_date
  1. Since now() and now(int) are different functions, they can't use common cache. To avoid these functions being executed more than once, func(int) signatures of them were removed.

Please see PR Bit-Quill#113 which adds caching mechanism.

Doctests
Doctests for new functions always fail, because test validate exact string match for result. I added docs, but disabled doctests for them.

Issues Resolved

See also

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.

@Yury-Fridlyand Yury-Fridlyand requested a review from a team as a code owner August 12, 2022 18:45
@codecov-commenter

This comment was marked as spam.

@Yury-Fridlyand Yury-Fridlyand force-pushed the integ-datetime-now branch 2 times, most recently from 4a6a9be to d5e8a21 Compare August 17, 2022 20:23
MitchellGale
MitchellGale previously approved these changes Aug 22, 2022
forestmvey
forestmvey previously approved these changes Aug 22, 2022
dai-chen
dai-chen previously approved these changes Aug 23, 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.

Thanks for the changes!

@MaxKsyunz
Copy link
Collaborator

@Yury-Fridlyand please update the target branch from main to 2.x

MitchellGale
MitchellGale previously approved these changes Sep 19, 2022
@@ -85,6 +89,84 @@ public void register(BuiltinFunctionRepository repository) {
repository.register(to_days());
repository.register(week());
repository.register(year());

repository.register(now());
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: should have inserted these alphabetically into the above list

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I'll update in next PR

acarbonetto
acarbonetto previously approved these changes Sep 19, 2022
Signed-off-by: Yury-Fridlyand <[email protected]>
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.

LGTM. Thanks for the changes!

@dai-chen dai-chen added enhancement New feature or request SQL PPL Piped processing language labels Sep 21, 2022
@dai-chen dai-chen requested a review from MaxKsyunz September 21, 2022 18:41
@dai-chen dai-chen merged commit e28ac78 into opensearch-project:2.x Sep 23, 2022
@Yury-Fridlyand Yury-Fridlyand deleted the integ-datetime-now branch September 24, 2022 00:09
MitchellGale added a commit to Bit-Quill/opensearch-project-sql that referenced this pull request Oct 3, 2022
dai-chen pushed a commit that referenced this pull request Oct 11, 2022
* Update docs.

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

* Reorder functions in docs aplhabetically.

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

Signed-off-by: Yury-Fridlyand <[email protected]>
GabeFernandez310 pushed a commit to Bit-Quill/opensearch-project-sql that referenced this pull request Oct 19, 2022
…-project#884)

* Update docs.

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

* Reorder functions in docs aplhabetically.

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
enhancement New feature or request PPL Piped processing language SQL
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants