Skip to content

Commit f3c207d

Browse files
committed
SQL: Implement CURRENT_TIME/CURTIME functions (#40662)
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 (cherry picked from commit 9feede7)
1 parent 670e766 commit f3c207d

File tree

21 files changed

+1635
-1291
lines changed

21 files changed

+1635
-1291
lines changed

docs/reference/sql/appendix/syntax-reserved.asciidoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ s|SQL-92
3535
|`CONVERT` |reserved |reserved
3636
|`CURRENT_DATE` |reserved |reserved
3737
|`CURRENT_TIMESTAMP` |reserved |reserved
38+
|`CURRENT_TIME` |reserved |reserved
3839
|`DAY` |reserved |reserved
3940
|`DAYS` | |
4041
|`DESC` |reserved |reserved

docs/reference/sql/functions/date-time.asciidoc

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,63 @@ is used for relative date filtering:
139139
include-tagged::{sql-specs}/docs/docs.csv-spec[filterToday]
140140
--------------------------------------------------
141141

142+
[[sql-functions-current-time]]
143+
==== `CURRENT_TIME/CURTIME`
144+
145+
.Synopsis:
146+
[source, sql]
147+
--------------------------------------------------
148+
CURRENT_TIME
149+
CURRENT_TIME([precision <1>])
150+
CURTIME
151+
--------------------------------------------------
152+
153+
*Input*:
154+
155+
<1> fractional digits; optional
156+
157+
*Output*: time
158+
159+
.Description:
160+
161+
Returns the time when the current query reached the server.
162+
As a function, `CURRENT_TIME()` accepts _precision_ as an optional
163+
parameter for rounding the second fractional digits (nanoseconds). The default _precision_ is 3,
164+
meaning a milliseconds precision current time will be returned.
165+
166+
This method always returns the same value for its every occurrence within the same query.
167+
168+
["source","sql",subs="attributes,callouts,macros"]
169+
--------------------------------------------------
170+
include-tagged::{sql-specs}/docs/docs.csv-spec[currentTime]
171+
--------------------------------------------------
172+
173+
["source","sql",subs="attributes,callouts,macros"]
174+
--------------------------------------------------
175+
include-tagged::{sql-specs}/docs/docs.csv-spec[currentTimeFunction]
176+
--------------------------------------------------
177+
178+
["source","sql",subs="attributes,callouts,macros"]
179+
--------------------------------------------------
180+
include-tagged::{sql-specs}/docs/docs.csv-spec[curTimeFunction]
181+
--------------------------------------------------
182+
183+
["source","sql",subs="attributes,callouts,macros"]
184+
--------------------------------------------------
185+
include-tagged::{sql-specs}/docs/docs.csv-spec[currentTimeFunctionPrecision]
186+
--------------------------------------------------
187+
188+
Typically, this function is used for relative date/time filtering:
189+
190+
["source","sql",subs="attributes,callouts,macros"]
191+
--------------------------------------------------
192+
include-tagged::{sql-specs}/docs/docs.csv-spec[filterCurrentTime]
193+
--------------------------------------------------
194+
195+
[IMPORTANT]
196+
Currently, using a _precision_ greater than 3 doesn't make any difference to the output of the
197+
function as the maximum number of second fractional digits returned is 3 (milliseconds).
198+
142199
[[sql-functions-current-timestamp]]
143200
==== `CURRENT_TIMESTAMP`
144201

docs/reference/sql/functions/index.asciidoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
* <<sql-functions-datetime-interval, Date-Time Operators>>
5050
* <<sql-functions-current-date, Date-Time Functions>>
5151
** <<sql-functions-current-date>>
52+
** <<sql-functions-current-time>>
5253
** <<sql-functions-current-timestamp>>
5354
** <<sql-functions-datetime-day>>
5455
** <<sql-functions-datetime-dow>>

x-pack/plugin/sql/qa/src/main/resources/command.csv-spec

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ NULLIF |CONDITIONAL
3434
NVL |CONDITIONAL
3535
CURDATE |SCALAR
3636
CURRENT_DATE |SCALAR
37+
CURRENT_TIME |SCALAR
3738
CURRENT_TIMESTAMP|SCALAR
39+
CURTIME |SCALAR
3840
DAY |SCALAR
3941
DAYNAME |SCALAR
4042
DAYOFMONTH |SCALAR

x-pack/plugin/sql/qa/src/main/resources/docs/docs.csv-spec

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,9 @@ NULLIF |CONDITIONAL
211211
NVL |CONDITIONAL
212212
CURDATE |SCALAR
213213
CURRENT_DATE |SCALAR
214+
CURRENT_TIME |SCALAR
214215
CURRENT_TIMESTAMP|SCALAR
216+
CURTIME |SCALAR
215217
DAY |SCALAR
216218
DAYNAME |SCALAR
217219
DAYOFMONTH |SCALAR
@@ -2412,6 +2414,63 @@ Mingsen
24122414
// end::filterToday
24132415
;
24142416

2417+
2418+
currentTime-Ignore
2419+
// tag::currentTime
2420+
SELECT CURRENT_TIME AS result;
2421+
2422+
result
2423+
------------------------
2424+
12:31:27.237Z
2425+
// end::currentTime
2426+
;
2427+
2428+
currentTimeFunction-Ignore
2429+
// tag::currentTimeFunction
2430+
SELECT CURRENT_TIME() AS result;
2431+
2432+
result
2433+
------------------------
2434+
12:31:27.237Z
2435+
// end::currentTimeFunction
2436+
;
2437+
2438+
curTimeFunction-Ignore
2439+
// tag::curTimeFunction
2440+
SELECT CURTIME() AS result;
2441+
2442+
result
2443+
------------------------
2444+
12:31:27.237Z
2445+
// end::curTimeFunction
2446+
;
2447+
2448+
currentTimeFunctionPrecision-Ignore
2449+
// tag::currentTimeFunctionPrecision
2450+
SELECT CURRENT_TIME(1) AS result;
2451+
2452+
result
2453+
------------------------
2454+
12:31:27.2Z
2455+
// end::currentTimeFunctionPrecision
2456+
;
2457+
2458+
2459+
filterCurrentTime-Ignore
2460+
// tag::filterCurrentTime
2461+
SELECT first_name FROM emp WHERE CAST(hire_date AS TIME) > CURRENT_TIME() - INTERVAL 20 MINUTES ORDER BY first_name ASC LIMIT 5;
2462+
2463+
first_name
2464+
---------------
2465+
Alejandro
2466+
Amabile
2467+
Anneke
2468+
Anoosh
2469+
Arumugam
2470+
// end::filterCurrentTime
2471+
;
2472+
2473+
24152474
currentTimestamp-Ignore
24162475
// tag::curTs
24172476
SELECT CURRENT_TIMESTAMP AS result;

x-pack/plugin/sql/qa/src/main/resources/time.csv-spec

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,24 @@ SELECT HOUR(CAST('10:11:12.345' AS TIME) + INTERVAL '20' HOURS) AS h, SECOND(INT
8181
h:i | m:i
8282
6 | 52
8383
;
84+
85+
orderByCurrentTime
86+
SELECT first_name FROM test_emp ORDER BY CURRENT_TIME(), first_name LIMIT 5;
87+
88+
first_name
89+
---------------
90+
Alejandro
91+
Amabile
92+
Anneke
93+
Anoosh
94+
Arumugam
95+
;
96+
97+
// Awaits Fix https://github.com/elastic/elasticsearch/issues/40639
98+
groupByCurrentTime-Ignore
99+
SELECT MAX(salary) FROM test_emp GROUP BY CURRENT_TIME;
100+
101+
MAX(salary)
102+
---------------
103+
74999
104+
;

x-pack/plugin/sql/src/main/antlr/SqlBase.g4

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,8 +226,9 @@ primaryExpression
226226
;
227227

228228
builtinDateTimeFunction
229-
: name=CURRENT_DATE ('(' ')')?
230-
| name=CURRENT_TIMESTAMP ('(' precision=INTEGER_VALUE? ')')?
229+
: name=CURRENT_TIMESTAMP ('(' precision=INTEGER_VALUE? ')')?
230+
| name=CURRENT_DATE ('(' ')')?
231+
| name=CURRENT_TIME ('(' precision=INTEGER_VALUE? ')')?
231232
;
232233

233234
castExpression
@@ -373,6 +374,7 @@ CATALOGS: 'CATALOGS';
373374
COLUMNS: 'COLUMNS';
374375
CONVERT: 'CONVERT';
375376
CURRENT_DATE : 'CURRENT_DATE';
377+
CURRENT_TIME : 'CURRENT_TIME';
376378
CURRENT_TIMESTAMP : 'CURRENT_TIMESTAMP';
377379
DAY: 'DAY';
378380
DAYS: 'DAYS';

0 commit comments

Comments
 (0)