Skip to content

Commit

Permalink
SQL: Implement CURRENT_DATE (#38175)
Browse files Browse the repository at this point in the history
Since DATE data type is now available, this implements the
`CURRENT_DATE/CURRENT_DATE()/TODAY()` similar to `CURRENT_TIMESTAMP`.

Closes: #38160
  • Loading branch information
matriv committed Feb 5, 2019
1 parent 887fa2c commit c9701be
Show file tree
Hide file tree
Showing 21 changed files with 1,161 additions and 764 deletions.
79 changes: 77 additions & 2 deletions docs/reference/sql/functions/date-time.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,48 @@ include-tagged::{sql-specs}/docs.csv-spec[dtIntervalMul]

beta[]

[[sql-functions-current-date]]
==== `CURRENT_DATE/CURDATE`

.Synopsis:
[source, sql]
--------------------------------------------------
CURRENT_DATE
CURRENT_DATE()
--------------------------------------------------

*Input*: _none_

*Output*: date

.Description:

Returns the date (no time part) when the current query reached the server.
It can be used both as a keyword: `CURRENT_DATE` or as a function with no arguments: `CURRENT_DATE()`.

[NOTE]
Unlike CURRENT_DATE, `CURDATE()` can only be used as a function with no arguments and not as a keyword.

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.csv-spec[curDate]
--------------------------------------------------

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

Typically, this function (as well as its twin <<sql-functions-today,TODAY())>> function
is used for relative date filtering:

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

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

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

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

["source","sql",subs="attributes,callouts,macros"]
--------------------------------------------------
Expand Down Expand Up @@ -422,7 +464,8 @@ NOW()
.Description:

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

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

[[sql-functions-today]]
==== `TODAY`

.Synopsis:
[source, sql]
--------------------------------------------------
TODAY()
--------------------------------------------------

*Input*: _none_

*Output*: date

.Description:

This function offers the same functionality as <<sql-functions-current-date,CURRENT_DATE()>> function: returns
the date when the current query reached the server. 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.csv-spec[todayFunction]
--------------------------------------------------

Typically, this function (as well as its twin <<sql-functions-current-timestamp,CURRENT_TIMESTAMP())>> function is used
for relative date filtering:

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

[[sql-functions-datetime-week]]
==== `WEEK_OF_YEAR/WEEK`

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ public void testShowFunctionsLikeInfix() throws IOException {
assertThat(readLine(), RegexMatcher.matches("\\s*ISODAYOFWEEK\\s*\\|\\s*SCALAR\\s*"));
assertThat(readLine(), RegexMatcher.matches("\\s*ISO_DAY_OF_WEEK\\s*\\|\\s*SCALAR\\s*"));
assertThat(readLine(), RegexMatcher.matches("\\s*MINUTE_OF_DAY\\s*\\|\\s*SCALAR\\s*"));
assertThat(readLine(), RegexMatcher.matches("\\s*TODAY\\s*\\|\\s*SCALAR\\s*"));
assertEquals("", readLine());
}
}
6 changes: 5 additions & 1 deletion x-pack/plugin/sql/qa/src/main/resources/command.csv-spec
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ ISNULL |CONDITIONAL
LEAST |CONDITIONAL
NULLIF |CONDITIONAL
NVL |CONDITIONAL
CURDATE |SCALAR
CURRENT_DATE |SCALAR
CURRENT_TIMESTAMP|SCALAR
DAY |SCALAR
DAYNAME |SCALAR
Expand Down Expand Up @@ -65,7 +67,8 @@ MONTH_OF_YEAR |SCALAR
NOW |SCALAR
QUARTER |SCALAR
SECOND |SCALAR
SECOND_OF_MINUTE |SCALAR
SECOND_OF_MINUTE |SCALAR
TODAY |SCALAR
WEEK |SCALAR
WEEK_OF_YEAR |SCALAR
YEAR |SCALAR
Expand Down Expand Up @@ -175,6 +178,7 @@ HOUR_OF_DAY |SCALAR
ISODAYOFWEEK |SCALAR
ISO_DAY_OF_WEEK|SCALAR
MINUTE_OF_DAY |SCALAR
TODAY |SCALAR
;

showTables
Expand Down
87 changes: 87 additions & 0 deletions x-pack/plugin/sql/qa/src/main/resources/date.csv-spec
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,73 @@
// Date
//

currentDateKeywordWithDivision
SELECT YEAR(CURRENT_TIMESTAMP) / 1000 AS result;

result
---------------
2
;

currentDateFunctionNoArgsWithDivision
SELECT YEAR(CURRENT_TIMESTAMP()) / 1000 AS result;

result
---------------
2
;

todayWithDivision
SELECT YEAR(TODAY()) / 1000 AS result;

result
---------------
2
;

todayIntervalSubstraction
SELECT TRUNCATE(YEAR(TODAY() - INTERVAL 50 YEARS) / 1000) AS result;

result
---------------
1
;


currentDateFilter
SELECT first_name FROM test_emp WHERE hire_date > CURRENT_DATE() - INTERVAL 25 YEARS ORDER BY first_name ASC LIMIT 10;

first_name
-----------------
Kazuhito
Kenroku
Lillian
Mayumi
Mingsen
Sailaja
Saniya
Shahaf
Suzette
Tuval
;

currentDateFilterScript
SELECT first_name, TRUNCATE(YEAR(hire_date) - YEAR(TODAY()) / 1000) AS filter FROM test_emp
WHERE TRUNCATE(YEAR(hire_date) - YEAR(TODAY()) / 1000) > 1990 ORDER BY first_name ASC LIMIT 10;

first_name | filter
Cristinel |1991
Kazuhito |1993
Kenroku |1992
Lillian |1997
Magy |1991
Mayumi |1993
Mingsen |1992
Sailaja |1994
Saniya |1992
Shahaf |1993
;

dateExtractDateParts
SELECT
DAY(CAST(birth_date AS DATE)) d,
Expand Down Expand Up @@ -75,3 +142,23 @@ SELECT YEAR(CAST('2019-01-21' AS DATE) + INTERVAL '1-2' YEAR TO MONTH) AS y, MON
y:i | m:i
2020 | 3
;

orderByCurrentDate
SELECT first_name FROM test_emp ORDER BY TODAY(), first_name LIMIT 5;

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

groupByCurrentDate
SELECT MAX(salary) FROM test_emp GROUP BY TODAY();

MAX(salary)
---------------
74999
;
7 changes: 7 additions & 0 deletions x-pack/plugin/sql/qa/src/main/resources/datetime.sql-spec
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,12 @@ SELECT DAY_OF_WEEK(birth_date) day, COUNT(*) c FROM test_emp WHERE DAY_OF_WEEK(b
currentTimestampYear
SELECT YEAR(CURRENT_TIMESTAMP()) AS result;

orderByCurrentTimestamp
SELECT first_name FROM test_emp ORDER BY NOW(), first_name NULLS LAST LIMIT 5;

groupByCurrentTimestamp
SELECT MAX(salary) AS max FROM test_emp GROUP BY NOW();

//
// H2 uses the local timezone instead of the specified one
//
Expand All @@ -131,3 +137,4 @@ SELECT HOUR(CURRENT_TIMESTAMP()) AS result;

currentTimestampMinute-Ignore
SELECT MINUTE(CURRENT_TIMESTAMP()) AS result;

74 changes: 62 additions & 12 deletions x-pack/plugin/sql/qa/src/main/resources/docs.csv-spec
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,8 @@ ISNULL |CONDITIONAL
LEAST |CONDITIONAL
NULLIF |CONDITIONAL
NVL |CONDITIONAL
CURDATE |SCALAR
CURRENT_DATE |SCALAR
CURRENT_TIMESTAMP|SCALAR
DAY |SCALAR
DAYNAME |SCALAR
Expand Down Expand Up @@ -242,7 +244,8 @@ MONTH_OF_YEAR |SCALAR
NOW |SCALAR
QUARTER |SCALAR
SECOND |SCALAR
SECOND_OF_MINUTE |SCALAR
SECOND_OF_MINUTE |SCALAR
TODAY |SCALAR
WEEK |SCALAR
WEEK_OF_YEAR |SCALAR
YEAR |SCALAR
Expand Down Expand Up @@ -365,6 +368,7 @@ HOUR_OF_DAY |SCALAR
ISODAYOFWEEK |SCALAR
ISO_DAY_OF_WEEK|SCALAR
MINUTE_OF_DAY |SCALAR
TODAY |SCALAR

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


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

first_name
---------------
Alejandro
Amabile
Anneke
Anoosh
Arumugam
// end::filterNow

currentDate-Ignore
// tag::curDate
SELECT CURRENT_TIMESTAMP AS result;

result
------------------------
2018-12-12
// end::curDate
;

currentDateFunction-Ignore
// tag::curDateFunction
SELECT CURRENT_TIMESTAMP() AS result;

result
------------------------
2018-12-12
// end::curDateFunction
;

todayFunction-Ignore
// tag::todayFunction
SELECT TODAY() AS result;

result
------------------------
2018-12-12
// end::todayFunction
;

filterToday
// tag::filterToday
SELECT first_name FROM emp WHERE hire_date > TODAY() - INTERVAL 25 YEARS ORDER BY first_name ASC LIMIT 5;

first_name
------------
Kazuhito
Kenroku
Lillian
Mayumi
Mingsen
// end::filterToday
;

currentTimestamp-Ignore
Expand Down Expand Up @@ -2282,6 +2318,20 @@ SELECT NOW() AS result;
// end::nowFunction
;

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

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

////////////
// Next two queries need to have the same output, as they should be equivalent.
// They are used in the "SQL Limitations" page.
Expand Down
7 changes: 4 additions & 3 deletions x-pack/plugin/sql/src/main/antlr/SqlBase.g4
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,8 @@ castTemplate
;

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

convertTemplate
Expand Down Expand Up @@ -337,7 +338,7 @@ string
// http://developer.mimer.se/validator/sql-reserved-words.tml
nonReserved
: ANALYZE | ANALYZED
| CATALOGS | COLUMNS | CURRENT
| CATALOGS | COLUMNS
| DAY | DEBUG
| EXECUTABLE | EXPLAIN
| FIRST | FORMAT | FULL | FUNCTIONS
Expand Down Expand Up @@ -370,7 +371,7 @@ CATALOG: 'CATALOG';
CATALOGS: 'CATALOGS';
COLUMNS: 'COLUMNS';
CONVERT: 'CONVERT';
CURRENT: 'CURRENT';
CURRENT_DATE : 'CURRENT_DATE';
CURRENT_TIMESTAMP : 'CURRENT_TIMESTAMP';
DAY: 'DAY';
DAYS: 'DAYS';
Expand Down
Loading

0 comments on commit c9701be

Please sign in to comment.