-
Notifications
You must be signed in to change notification settings - Fork 28.9k
[SPARK-23903][SQL] Add support for date extract #21479
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,21 @@ | ||
CREATE TEMPORARY VIEW t AS select '2011-05-06 07:08:09.1234567' as c; | ||
|
||
select extract(year from c) from t; | ||
|
||
select extract(quarter from c) from t; | ||
|
||
select extract(month from c) from t; | ||
|
||
select extract(week from c) from t; | ||
|
||
select extract(day from c) from t; | ||
|
||
select extract(dayofweek from c) from t; | ||
|
||
select extract(hour from c) from t; | ||
|
||
select extract(minute from c) from t; | ||
|
||
select extract(second from c) from t; | ||
|
||
select extract(not_supported from c) from t; |
96 changes: 96 additions & 0 deletions
96
sql/core/src/test/resources/sql-tests/results/extract.sql.out
This file contains hidden or 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,96 @@ | ||
-- Automatically generated by SQLQueryTestSuite | ||
-- Number of queries: 11 | ||
|
||
|
||
-- !query 0 | ||
CREATE TEMPORARY VIEW t AS select '2011-05-06 07:08:09.1234567' as c | ||
-- !query 0 schema | ||
struct<> | ||
-- !query 0 output | ||
|
||
|
||
|
||
-- !query 1 | ||
select extract(year from c) from t | ||
-- !query 1 schema | ||
struct<year(CAST(c AS DATE)):int> | ||
-- !query 1 output | ||
2011 | ||
|
||
|
||
-- !query 2 | ||
select extract(quarter from c) from t | ||
-- !query 2 schema | ||
struct<quarter(CAST(c AS DATE)):int> | ||
-- !query 2 output | ||
2 | ||
|
||
|
||
-- !query 3 | ||
select extract(month from c) from t | ||
-- !query 3 schema | ||
struct<month(CAST(c AS DATE)):int> | ||
-- !query 3 output | ||
5 | ||
|
||
|
||
-- !query 4 | ||
select extract(week from c) from t | ||
-- !query 4 schema | ||
struct<weekofyear(CAST(c AS DATE)):int> | ||
-- !query 4 output | ||
18 | ||
|
||
|
||
-- !query 5 | ||
select extract(day from c) from t | ||
-- !query 5 schema | ||
struct<dayofmonth(CAST(c AS DATE)):int> | ||
-- !query 5 output | ||
6 | ||
|
||
|
||
-- !query 6 | ||
select extract(dayofweek from c) from t | ||
-- !query 6 schema | ||
struct<dayofweek(CAST(c AS DATE)):int> | ||
-- !query 6 output | ||
6 | ||
|
||
|
||
-- !query 7 | ||
select extract(hour from c) from t | ||
-- !query 7 schema | ||
struct<hour(CAST(c AS TIMESTAMP)):int> | ||
-- !query 7 output | ||
7 | ||
|
||
|
||
-- !query 8 | ||
select extract(minute from c) from t | ||
-- !query 8 schema | ||
struct<minute(CAST(c AS TIMESTAMP)):int> | ||
-- !query 8 output | ||
8 | ||
|
||
|
||
-- !query 9 | ||
select extract(second from c) from t | ||
-- !query 9 schema | ||
struct<second(CAST(c AS TIMESTAMP)):int> | ||
-- !query 9 output | ||
9 | ||
|
||
|
||
-- !query 10 | ||
select extract(not_supported from c) from t | ||
-- !query 10 schema | ||
struct<> | ||
-- !query 10 output | ||
org.apache.spark.sql.catalyst.parser.ParseException | ||
|
||
Literals of type 'NOT_SUPPORTED' are currently not supported.(line 1, pos 7) | ||
|
||
== SQL == | ||
select extract(not_supported from c) from t | ||
-------^^^ |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@HyukjinKwon @maropu @wangyum @huaxingao Just realized EXTRACT is not included in https://spark.apache.org/docs/latest/api/sql/index.html Could we fix it in the upcoming built-in function doc page updates?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, I see. Nice catch! The python script that we are now working on (#28224) just dumps the entries of
ExpressionDescription(ExpressionInfo)
, so the output unfortunately cannot include a doc entry forEXTRACT
now. To document it, there are the three options that I can think of;(the simplest fix) Add some description about
EXTRACT
in the SELECT syntax page (e.g., thenamed_expression
section), then add a link todate_part
in the built-in function page.Add a dummy
ExpressionDescription
forEXTRACT
like this;ExpressionDescription
like this;Which one is preferred, or any other smarter idea?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
EXTRACT is not an alias as it has different syntax. The second approach looks good.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the check, @cloud-fan. ok, I'll open a PR to follow that approach.