Skip to content

Commit 62933db

Browse files
committed
SQL: Implement CURRENT_DATE (#38175)
Since DATE data type is now available, this implements the `CURRENT_DATE/CURRENT_DATE()/TODAY()` similar to `CURRENT_TIMESTAMP`. Closes: #38160
1 parent 80ae5da commit 62933db

File tree

21 files changed

+1161
-764
lines changed

21 files changed

+1161
-764
lines changed

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

+77-2
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,48 @@ include-tagged::{sql-specs}/docs.csv-spec[dtIntervalMul]
9393

9494
beta[]
9595

96+
[[sql-functions-current-date]]
97+
==== `CURRENT_DATE/CURDATE`
98+
99+
.Synopsis:
100+
[source, sql]
101+
--------------------------------------------------
102+
CURRENT_DATE
103+
CURRENT_DATE()
104+
--------------------------------------------------
105+
106+
*Input*: _none_
107+
108+
*Output*: date
109+
110+
.Description:
111+
112+
Returns the date (no time part) when the current query reached the server.
113+
It can be used both as a keyword: `CURRENT_DATE` or as a function with no arguments: `CURRENT_DATE()`.
114+
115+
[NOTE]
116+
Unlike CURRENT_DATE, `CURDATE()` can only be used as a function with no arguments and not as a keyword.
117+
118+
This method always returns the same value for its every occurrence within the same query.
119+
120+
["source","sql",subs="attributes,callouts,macros"]
121+
--------------------------------------------------
122+
include-tagged::{sql-specs}/docs.csv-spec[curDate]
123+
--------------------------------------------------
124+
125+
["source","sql",subs="attributes,callouts,macros"]
126+
--------------------------------------------------
127+
include-tagged::{sql-specs}/docs.csv-spec[curDateFunction]
128+
--------------------------------------------------
129+
130+
Typically, this function (as well as its twin <<sql-functions-today,TODAY())>> function
131+
is used for relative date filtering:
132+
133+
["source","sql",subs="attributes,callouts,macros"]
134+
--------------------------------------------------
135+
include-tagged::{sql-specs}/docs.csv-spec[filterToday]
136+
--------------------------------------------------
137+
96138
[[sql-functions-current-timestamp]]
97139
==== `CURRENT_TIMESTAMP`
98140

@@ -115,7 +157,7 @@ Returns the date/time when the current query reached the server.
115157
As a function, `CURRENT_TIMESTAMP()` accepts _precision_ as an optional
116158
parameter for rounding the second fractional digits (nanoseconds).
117159

118-
This method always returns the same value within a query.
160+
This method always returns the same value for its every occurrence within the same query.
119161

120162
["source","sql",subs="attributes,callouts,macros"]
121163
--------------------------------------------------
@@ -422,7 +464,8 @@ NOW()
422464
.Description:
423465

424466
This function offers the same functionality as <<sql-functions-current-timestamp,CURRENT_TIMESTAMP()>> function: returns
425-
the datetime when the current query reached the server. This method always returns the same value within a query.
467+
the datetime when the current query reached the server. This method always returns the same value for its every
468+
occurrence within the same query.
426469

427470
["source","sql",subs="attributes,callouts,macros"]
428471
--------------------------------------------------
@@ -485,6 +528,38 @@ Extract the year quarter the date/datetime falls in.
485528
include-tagged::{sql-specs}/docs.csv-spec[quarter]
486529
--------------------------------------------------
487530

531+
[[sql-functions-today]]
532+
==== `TODAY`
533+
534+
.Synopsis:
535+
[source, sql]
536+
--------------------------------------------------
537+
TODAY()
538+
--------------------------------------------------
539+
540+
*Input*: _none_
541+
542+
*Output*: date
543+
544+
.Description:
545+
546+
This function offers the same functionality as <<sql-functions-current-date,CURRENT_DATE()>> function: returns
547+
the date when the current query reached the server. This method always returns the same value for its every occurrence
548+
within the same query.
549+
550+
["source","sql",subs="attributes,callouts,macros"]
551+
--------------------------------------------------
552+
include-tagged::{sql-specs}/docs.csv-spec[todayFunction]
553+
--------------------------------------------------
554+
555+
Typically, this function (as well as its twin <<sql-functions-current-timestamp,CURRENT_TIMESTAMP())>> function is used
556+
for relative date filtering:
557+
558+
["source","sql",subs="attributes,callouts,macros"]
559+
--------------------------------------------------
560+
include-tagged::{sql-specs}/docs.csv-spec[filterToday]
561+
--------------------------------------------------
562+
488563
[[sql-functions-datetime-week]]
489564
==== `WEEK_OF_YEAR/WEEK`
490565

x-pack/plugin/sql/qa/src/main/java/org/elasticsearch/xpack/sql/qa/cli/ShowTestCase.java

+1
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ public void testShowFunctionsLikeInfix() throws IOException {
9191
assertThat(readLine(), RegexMatcher.matches("\\s*ISODAYOFWEEK\\s*\\|\\s*SCALAR\\s*"));
9292
assertThat(readLine(), RegexMatcher.matches("\\s*ISO_DAY_OF_WEEK\\s*\\|\\s*SCALAR\\s*"));
9393
assertThat(readLine(), RegexMatcher.matches("\\s*MINUTE_OF_DAY\\s*\\|\\s*SCALAR\\s*"));
94+
assertThat(readLine(), RegexMatcher.matches("\\s*TODAY\\s*\\|\\s*SCALAR\\s*"));
9495
assertEquals("", readLine());
9596
}
9697
}

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

+5-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ ISNULL |CONDITIONAL
3131
LEAST |CONDITIONAL
3232
NULLIF |CONDITIONAL
3333
NVL |CONDITIONAL
34+
CURDATE |SCALAR
35+
CURRENT_DATE |SCALAR
3436
CURRENT_TIMESTAMP|SCALAR
3537
DAY |SCALAR
3638
DAYNAME |SCALAR
@@ -65,7 +67,8 @@ MONTH_OF_YEAR |SCALAR
6567
NOW |SCALAR
6668
QUARTER |SCALAR
6769
SECOND |SCALAR
68-
SECOND_OF_MINUTE |SCALAR
70+
SECOND_OF_MINUTE |SCALAR
71+
TODAY |SCALAR
6972
WEEK |SCALAR
7073
WEEK_OF_YEAR |SCALAR
7174
YEAR |SCALAR
@@ -175,6 +178,7 @@ HOUR_OF_DAY |SCALAR
175178
ISODAYOFWEEK |SCALAR
176179
ISO_DAY_OF_WEEK|SCALAR
177180
MINUTE_OF_DAY |SCALAR
181+
TODAY |SCALAR
178182
;
179183

180184
showTables

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

+87
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,73 @@
22
// Date
33
//
44

5+
currentDateKeywordWithDivision
6+
SELECT YEAR(CURRENT_TIMESTAMP) / 1000 AS result;
7+
8+
result
9+
---------------
10+
2
11+
;
12+
13+
currentDateFunctionNoArgsWithDivision
14+
SELECT YEAR(CURRENT_TIMESTAMP()) / 1000 AS result;
15+
16+
result
17+
---------------
18+
2
19+
;
20+
21+
todayWithDivision
22+
SELECT YEAR(TODAY()) / 1000 AS result;
23+
24+
result
25+
---------------
26+
2
27+
;
28+
29+
todayIntervalSubstraction
30+
SELECT TRUNCATE(YEAR(TODAY() - INTERVAL 50 YEARS) / 1000) AS result;
31+
32+
result
33+
---------------
34+
1
35+
;
36+
37+
38+
currentDateFilter
39+
SELECT first_name FROM test_emp WHERE hire_date > CURRENT_DATE() - INTERVAL 25 YEARS ORDER BY first_name ASC LIMIT 10;
40+
41+
first_name
42+
-----------------
43+
Kazuhito
44+
Kenroku
45+
Lillian
46+
Mayumi
47+
Mingsen
48+
Sailaja
49+
Saniya
50+
Shahaf
51+
Suzette
52+
Tuval
53+
;
54+
55+
currentDateFilterScript
56+
SELECT first_name, TRUNCATE(YEAR(hire_date) - YEAR(TODAY()) / 1000) AS filter FROM test_emp
57+
WHERE TRUNCATE(YEAR(hire_date) - YEAR(TODAY()) / 1000) > 1990 ORDER BY first_name ASC LIMIT 10;
58+
59+
first_name | filter
60+
Cristinel |1991
61+
Kazuhito |1993
62+
Kenroku |1992
63+
Lillian |1997
64+
Magy |1991
65+
Mayumi |1993
66+
Mingsen |1992
67+
Sailaja |1994
68+
Saniya |1992
69+
Shahaf |1993
70+
;
71+
572
dateExtractDateParts
673
SELECT
774
DAY(CAST(birth_date AS DATE)) d,
@@ -75,3 +142,23 @@ SELECT YEAR(CAST('2019-01-21' AS DATE) + INTERVAL '1-2' YEAR TO MONTH) AS y, MON
75142
y:i | m:i
76143
2020 | 3
77144
;
145+
146+
orderByCurrentDate
147+
SELECT first_name FROM test_emp ORDER BY TODAY(), first_name LIMIT 5;
148+
149+
first_name
150+
---------------
151+
Alejandro
152+
Amabile
153+
Anneke
154+
Anoosh
155+
Arumugam
156+
;
157+
158+
groupByCurrentDate
159+
SELECT MAX(salary) FROM test_emp GROUP BY TODAY();
160+
161+
MAX(salary)
162+
---------------
163+
74999
164+
;

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

+7
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,12 @@ SELECT DAY_OF_WEEK(birth_date) day, COUNT(*) c FROM test_emp WHERE DAY_OF_WEEK(b
120120
currentTimestampYear
121121
SELECT YEAR(CURRENT_TIMESTAMP()) AS result;
122122

123+
orderByCurrentTimestamp
124+
SELECT first_name FROM test_emp ORDER BY NOW(), first_name NULLS LAST LIMIT 5;
125+
126+
groupByCurrentTimestamp
127+
SELECT MAX(salary) AS max FROM test_emp GROUP BY NOW();
128+
123129
//
124130
// H2 uses the local timezone instead of the specified one
125131
//
@@ -131,3 +137,4 @@ SELECT HOUR(CURRENT_TIMESTAMP()) AS result;
131137

132138
currentTimestampMinute-Ignore
133139
SELECT MINUTE(CURRENT_TIMESTAMP()) AS result;
140+

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

+62-12
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,8 @@ ISNULL |CONDITIONAL
208208
LEAST |CONDITIONAL
209209
NULLIF |CONDITIONAL
210210
NVL |CONDITIONAL
211+
CURDATE |SCALAR
212+
CURRENT_DATE |SCALAR
211213
CURRENT_TIMESTAMP|SCALAR
212214
DAY |SCALAR
213215
DAYNAME |SCALAR
@@ -242,7 +244,8 @@ MONTH_OF_YEAR |SCALAR
242244
NOW |SCALAR
243245
QUARTER |SCALAR
244246
SECOND |SCALAR
245-
SECOND_OF_MINUTE |SCALAR
247+
SECOND_OF_MINUTE |SCALAR
248+
TODAY |SCALAR
246249
WEEK |SCALAR
247250
WEEK_OF_YEAR |SCALAR
248251
YEAR |SCALAR
@@ -365,6 +368,7 @@ HOUR_OF_DAY |SCALAR
365368
ISODAYOFWEEK |SCALAR
366369
ISO_DAY_OF_WEEK|SCALAR
367370
MINUTE_OF_DAY |SCALAR
371+
TODAY |SCALAR
368372

369373
// end::showFunctionsWithPattern
370374
;
@@ -2227,18 +2231,50 @@ SELECT WEEK(CAST('1988-01-05T09:22:10Z' AS TIMESTAMP)) AS week, ISOWEEK(CAST('19
22272231
;
22282232

22292233

2230-
currentNow
2231-
// tag::filterNow
2232-
SELECT first_name FROM emp WHERE hire_date > NOW() - INTERVAL 100 YEARS ORDER BY first_name ASC LIMIT 5;
22332234

2234-
first_name
2235-
---------------
2236-
Alejandro
2237-
Amabile
2238-
Anneke
2239-
Anoosh
2240-
Arumugam
2241-
// end::filterNow
2235+
2236+
currentDate-Ignore
2237+
// tag::curDate
2238+
SELECT CURRENT_TIMESTAMP AS result;
2239+
2240+
result
2241+
------------------------
2242+
2018-12-12
2243+
// end::curDate
2244+
;
2245+
2246+
currentDateFunction-Ignore
2247+
// tag::curDateFunction
2248+
SELECT CURRENT_TIMESTAMP() AS result;
2249+
2250+
result
2251+
------------------------
2252+
2018-12-12
2253+
// end::curDateFunction
2254+
;
2255+
2256+
todayFunction-Ignore
2257+
// tag::todayFunction
2258+
SELECT TODAY() AS result;
2259+
2260+
result
2261+
------------------------
2262+
2018-12-12
2263+
// end::todayFunction
2264+
;
2265+
2266+
filterToday
2267+
// tag::filterToday
2268+
SELECT first_name FROM emp WHERE hire_date > TODAY() - INTERVAL 25 YEARS ORDER BY first_name ASC LIMIT 5;
2269+
2270+
first_name
2271+
------------
2272+
Kazuhito
2273+
Kenroku
2274+
Lillian
2275+
Mayumi
2276+
Mingsen
2277+
// end::filterToday
22422278
;
22432279

22442280
currentTimestamp-Ignore
@@ -2282,6 +2318,20 @@ SELECT NOW() AS result;
22822318
// end::nowFunction
22832319
;
22842320

2321+
filterNow
2322+
// tag::filterNow
2323+
SELECT first_name FROM emp WHERE hire_date > NOW() - INTERVAL 100 YEARS ORDER BY first_name ASC LIMIT 5;
2324+
2325+
first_name
2326+
---------------
2327+
Alejandro
2328+
Amabile
2329+
Anneke
2330+
Anoosh
2331+
Arumugam
2332+
// end::filterNow
2333+
;
2334+
22852335
////////////
22862336
// Next two queries need to have the same output, as they should be equivalent.
22872337
// They are used in the "SQL Limitations" page.

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

+4-3
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,8 @@ castTemplate
236236
;
237237

238238
builtinDateTimeFunction
239-
: name=CURRENT_TIMESTAMP ('(' precision=INTEGER_VALUE? ')')?
239+
: name=CURRENT_DATE ('(' ')')?
240+
| name=CURRENT_TIMESTAMP ('(' precision=INTEGER_VALUE? ')')?
240241
;
241242

242243
convertTemplate
@@ -337,7 +338,7 @@ string
337338
// http://developer.mimer.se/validator/sql-reserved-words.tml
338339
nonReserved
339340
: ANALYZE | ANALYZED
340-
| CATALOGS | COLUMNS | CURRENT
341+
| CATALOGS | COLUMNS
341342
| DAY | DEBUG
342343
| EXECUTABLE | EXPLAIN
343344
| FIRST | FORMAT | FULL | FUNCTIONS
@@ -370,7 +371,7 @@ CATALOG: 'CATALOG';
370371
CATALOGS: 'CATALOGS';
371372
COLUMNS: 'COLUMNS';
372373
CONVERT: 'CONVERT';
373-
CURRENT: 'CURRENT';
374+
CURRENT_DATE : 'CURRENT_DATE';
374375
CURRENT_TIMESTAMP : 'CURRENT_TIMESTAMP';
375376
DAY: 'DAY';
376377
DAYS: 'DAYS';

0 commit comments

Comments
 (0)