forked from apache/spark
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SPARK-28690][SQL] Add
date_part
function for timestamps/dates
## What changes were proposed in this pull request? In the PR, I propose new function `date_part()`. The function is modeled on the traditional Ingres equivalent to the SQL-standard function `extract`: ``` date_part('field', source) ``` and added for feature parity with PostgreSQL (https://www.postgresql.org/docs/11/functions-datetime.html#FUNCTIONS-DATETIME-EXTRACT). The `source` can have `DATE` or `TIMESTAMP` type. Supported string values of `'field'` are: - `millennium` - the current millennium for given date (or a timestamp implicitly casted to a date). For example, years in the 1900s are in the second millennium. The third millennium started _January 1, 2001_. - `century` - the current millennium for given date (or timestamp). The first century starts at 0001-01-01 AD. - `decade` - the current decade for given date (or timestamp). Actually, this is the year field divided by 10. - isoyear` - the ISO 8601 week-numbering year that the date falls in. Each ISO 8601 week-numbering year begins with the Monday of the week containing the 4th of January. - `year`, `month`, `day`, `hour`, `minute`, `second` - `week` - the number of the ISO 8601 week-numbering week of the year. By definition, ISO weeks start on Mondays and the first week of a year contains January 4 of that year. - `quarter` - the quarter of the year (1 - 4) - `dayofweek` - the day of the week for date/timestamp (1 = Sunday, 2 = Monday, ..., 7 = Saturday) - `dow` - the day of the week as Sunday (0) to Saturday (6) - `isodow` - the day of the week as Monday (1) to Sunday (7) - `doy` - the day of the year (1 - 365/366) - `milliseconds` - the seconds field including fractional parts multiplied by 1,000. - `microseconds` - the seconds field including fractional parts multiplied by 1,000,000. - `epoch` - the number of seconds since 1970-01-01 00:00:00 local time in microsecond precision. Here are examples: ```sql spark-sql> select date_part('year', timestamp'2019-08-12 01:00:00.123456'); 2019 spark-sql> select date_part('week', timestamp'2019-08-12 01:00:00.123456'); 33 spark-sql> select date_part('doy', timestamp'2019-08-12 01:00:00.123456'); 224 ``` I changed implementation of `extract` to re-use `date_part()` internally. ## How was this patch tested? Added `date_part.sql` and regenerated results of `extract.sql`. Closes apache#25410 from MaxGekk/date_part. Lead-authored-by: Maxim Gekk <max.gekk@gmail.com> Co-authored-by: Takeshi Yamamuro <yamamuro@apache.org> Signed-off-by: Takeshi Yamamuro <yamamuro@apache.org>
- Loading branch information
1 parent
4284696
commit 3789eb1
Showing
9 changed files
with
727 additions
and
153 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
sql/core/src/test/resources/sql-tests/inputs/date_part.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
CREATE TEMPORARY VIEW t AS select '2011-05-06 07:08:09.1234567' as c; | ||
|
||
select date_part('millennium', c) from t; | ||
select date_part('millennia', c) from t; | ||
select date_part('mil', c) from t; | ||
select date_part('mils', c) from t; | ||
|
||
select date_part('century', c) from t; | ||
select date_part('centuries', c) from t; | ||
select date_part('c', c) from t; | ||
select date_part('cent', c) from t; | ||
|
||
select date_part('decade', c) from t; | ||
select date_part('decades', c) from t; | ||
select date_part('dec', c) from t; | ||
select date_part('decs', c) from t; | ||
|
||
select date_part('year', c) from t; | ||
select date_part('y', c) from t; | ||
select date_part('years', c) from t; | ||
select date_part('yr', c) from t; | ||
select date_part('yrs', c) from t; | ||
|
||
select date_part('quarter', c) from t; | ||
select date_part('qtr', c) from t; | ||
|
||
select date_part('month', c) from t; | ||
select date_part('mon', c) from t; | ||
select date_part('mons', c) from t; | ||
select date_part('months', c) from t; | ||
|
||
select date_part('week', c) from t; | ||
select date_part('w', c) from t; | ||
select date_part('weeks', c) from t; | ||
|
||
select date_part('day', c) from t; | ||
select date_part('d', c) from t; | ||
select date_part('days', c) from t; | ||
|
||
select date_part('dayofweek', c) from t; | ||
|
||
select date_part('dow', c) from t; | ||
|
||
select date_part('isodow', c) from t; | ||
|
||
select date_part('doy', c) from t; | ||
|
||
select date_part('hour', c) from t; | ||
select date_part('h', c) from t; | ||
select date_part('hours', c) from t; | ||
select date_part('hr', c) from t; | ||
select date_part('hrs', c) from t; | ||
|
||
select date_part('minute', c) from t; | ||
select date_part('m', c) from t; | ||
select date_part('min', c) from t; | ||
select date_part('mins', c) from t; | ||
select date_part('minutes', c) from t; | ||
|
||
select date_part('second', c) from t; | ||
select date_part('s', c) from t; | ||
select date_part('sec', c) from t; | ||
select date_part('seconds', c) from t; | ||
select date_part('secs', c) from t; | ||
|
||
select date_part('not_supported', c) from t; | ||
|
||
select date_part(c, c) from t; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.