Skip to content

Commit

Permalink
SQL: Implement CURRENT_TIME/CURTIME functions (#40662)
Browse files Browse the repository at this point in the history
After `TIME` SQL data type is introduced, implement
`CURRENT_TIME/CURTIME` functions similarly to CURRENT_TIMESTAMP
that return the system's current time (only, without the date part).

Closes: #40468
  • Loading branch information
matriv committed Apr 3, 2019
1 parent cb5ec67 commit 9feede7
Show file tree
Hide file tree
Showing 21 changed files with 1,635 additions and 1,291 deletions.
1 change: 1 addition & 0 deletions docs/reference/sql/appendix/syntax-reserved.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ s|SQL-92
|`CONVERT` |reserved |reserved
|`CURRENT_DATE` |reserved |reserved
|`CURRENT_TIMESTAMP` |reserved |reserved
|`CURRENT_TIME` |reserved |reserved
|`DAY` |reserved |reserved
|`DAYS` | |
|`DESC` |reserved |reserved
Expand Down
57 changes: 57 additions & 0 deletions docs/reference/sql/functions/date-time.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,63 @@ is used for relative date filtering:
include-tagged::{sql-specs}/docs/docs.csv-spec[filterToday]
--------------------------------------------------

[[sql-functions-current-time]]
==== `CURRENT_TIME/CURTIME`

.Synopsis:
[source, sql]
--------------------------------------------------
CURRENT_TIME
CURRENT_TIME([precision <1>])
CURTIME
--------------------------------------------------

*Input*:

<1> fractional digits; optional

*Output*: time

.Description:

Returns the time when the current query reached the server.
As a function, `CURRENT_TIME()` accepts _precision_ as an optional
parameter for rounding the second fractional digits (nanoseconds). The default _precision_ is 3,
meaning a milliseconds precision current time will be returned.

This method always returns the same value for its every occurrence within the same query.

["source","sql",subs="attributes,callouts,macros"]
--------------------------------------------------
include-tagged::{sql-specs}/docs/docs.csv-spec[currentTime]
--------------------------------------------------

["source","sql",subs="attributes,callouts,macros"]
--------------------------------------------------
include-tagged::{sql-specs}/docs/docs.csv-spec[currentTimeFunction]
--------------------------------------------------

["source","sql",subs="attributes,callouts,macros"]
--------------------------------------------------
include-tagged::{sql-specs}/docs/docs.csv-spec[curTimeFunction]
--------------------------------------------------

["source","sql",subs="attributes,callouts,macros"]
--------------------------------------------------
include-tagged::{sql-specs}/docs/docs.csv-spec[currentTimeFunctionPrecision]
--------------------------------------------------

Typically, this function is used for relative date/time filtering:

["source","sql",subs="attributes,callouts,macros"]
--------------------------------------------------
include-tagged::{sql-specs}/docs/docs.csv-spec[filterCurrentTime]
--------------------------------------------------

[IMPORTANT]
Currently, using a _precision_ greater than 3 doesn't make any difference to the output of the
function as the maximum number of second fractional digits returned is 3 (milliseconds).

[[sql-functions-current-timestamp]]
==== `CURRENT_TIMESTAMP`

Expand Down
1 change: 1 addition & 0 deletions docs/reference/sql/functions/index.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
* <<sql-functions-datetime-interval, Date-Time Operators>>
* <<sql-functions-current-date, Date-Time Functions>>
** <<sql-functions-current-date>>
** <<sql-functions-current-time>>
** <<sql-functions-current-timestamp>>
** <<sql-functions-datetime-day>>
** <<sql-functions-datetime-dow>>
Expand Down
2 changes: 2 additions & 0 deletions x-pack/plugin/sql/qa/src/main/resources/command.csv-spec
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ NULLIF |CONDITIONAL
NVL |CONDITIONAL
CURDATE |SCALAR
CURRENT_DATE |SCALAR
CURRENT_TIME |SCALAR
CURRENT_TIMESTAMP|SCALAR
CURTIME |SCALAR
DAY |SCALAR
DAYNAME |SCALAR
DAYOFMONTH |SCALAR
Expand Down
59 changes: 59 additions & 0 deletions x-pack/plugin/sql/qa/src/main/resources/docs/docs.csv-spec
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,9 @@ NULLIF |CONDITIONAL
NVL |CONDITIONAL
CURDATE |SCALAR
CURRENT_DATE |SCALAR
CURRENT_TIME |SCALAR
CURRENT_TIMESTAMP|SCALAR
CURTIME |SCALAR
DAY |SCALAR
DAYNAME |SCALAR
DAYOFMONTH |SCALAR
Expand Down Expand Up @@ -2412,6 +2414,63 @@ Mingsen
// end::filterToday
;


currentTime-Ignore
// tag::currentTime
SELECT CURRENT_TIME AS result;

result
------------------------
12:31:27.237Z
// end::currentTime
;

currentTimeFunction-Ignore
// tag::currentTimeFunction
SELECT CURRENT_TIME() AS result;

result
------------------------
12:31:27.237Z
// end::currentTimeFunction
;

curTimeFunction-Ignore
// tag::curTimeFunction
SELECT CURTIME() AS result;

result
------------------------
12:31:27.237Z
// end::curTimeFunction
;

currentTimeFunctionPrecision-Ignore
// tag::currentTimeFunctionPrecision
SELECT CURRENT_TIME(1) AS result;

result
------------------------
12:31:27.2Z
// end::currentTimeFunctionPrecision
;


filterCurrentTime-Ignore
// tag::filterCurrentTime
SELECT first_name FROM emp WHERE CAST(hire_date AS TIME) > CURRENT_TIME() - INTERVAL 20 MINUTES ORDER BY first_name ASC LIMIT 5;

first_name
---------------
Alejandro
Amabile
Anneke
Anoosh
Arumugam
// end::filterCurrentTime
;


currentTimestamp-Ignore
// tag::curTs
SELECT CURRENT_TIMESTAMP AS result;
Expand Down
21 changes: 21 additions & 0 deletions x-pack/plugin/sql/qa/src/main/resources/time.csv-spec
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,24 @@ SELECT HOUR(CAST('10:11:12.345' AS TIME) + INTERVAL '20' HOURS) AS h, SECOND(INT
h:i | m:i
6 | 52
;

orderByCurrentTime
SELECT first_name FROM test_emp ORDER BY CURRENT_TIME(), first_name LIMIT 5;

first_name
---------------
Alejandro
Amabile
Anneke
Anoosh
Arumugam
;

// Awaits Fix https://github.com/elastic/elasticsearch/issues/40639
groupByCurrentTime-Ignore
SELECT MAX(salary) FROM test_emp GROUP BY CURRENT_TIME;

MAX(salary)
---------------
74999
;
6 changes: 4 additions & 2 deletions x-pack/plugin/sql/src/main/antlr/SqlBase.g4
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,9 @@ primaryExpression
;

builtinDateTimeFunction
: name=CURRENT_DATE ('(' ')')?
| name=CURRENT_TIMESTAMP ('(' precision=INTEGER_VALUE? ')')?
: name=CURRENT_TIMESTAMP ('(' precision=INTEGER_VALUE? ')')?
| name=CURRENT_DATE ('(' ')')?
| name=CURRENT_TIME ('(' precision=INTEGER_VALUE? ')')?
;

castExpression
Expand Down Expand Up @@ -373,6 +374,7 @@ CATALOGS: 'CATALOGS';
COLUMNS: 'COLUMNS';
CONVERT: 'CONVERT';
CURRENT_DATE : 'CURRENT_DATE';
CURRENT_TIME : 'CURRENT_TIME';
CURRENT_TIMESTAMP : 'CURRENT_TIMESTAMP';
DAY: 'DAY';
DAYS: 'DAYS';
Expand Down
Loading

0 comments on commit 9feede7

Please sign in to comment.