-
Notifications
You must be signed in to change notification settings - Fork 29k
[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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -592,6 +592,7 @@ primaryExpression | |
| | identifier #columnReference | ||
| | base=primaryExpression '.' fieldName=identifier #dereference | ||
| | '(' expression ')' #parenthesizedExpression | ||
| | EXTRACT '(' field=identifier FROM source=valueExpression ')' #extract | ||
| ; | ||
|
|
||
| constant | ||
|
|
@@ -739,6 +740,7 @@ nonReserved | |
| | VIEW | REPLACE | ||
| | IF | ||
| | POSITION | ||
| | EXTRACT | YEAR | QUARTER | MONTH | WEEK | DAY | DOW | HOUR | MINUTE | SECOND | ||
|
||
| | NO | DATA | ||
| | START | TRANSACTION | COMMIT | ROLLBACK | IGNORE | ||
| | SORT | CLUSTER | DISTRIBUTE | UNSET | TBLPROPERTIES | SKEWED | STORED | DIRECTORIES | LOCATION | ||
|
|
@@ -878,6 +880,16 @@ TRAILING: 'TRAILING'; | |
|
|
||
| IF: 'IF'; | ||
| POSITION: 'POSITION'; | ||
| EXTRACT: 'EXTRACT'; | ||
| YEAR: 'YEAR'; | ||
| QUARTER: 'QUARTER'; | ||
| MONTH: 'MONTH'; | ||
| WEEK: 'WEEK'; | ||
| DAY: 'DAY'; | ||
| DOW: 'DOW'; | ||
| HOUR: 'HOUR'; | ||
| MINUTE: 'MINUTE'; | ||
| SECOND: 'SECOND'; | ||
|
|
||
| EQ : '=' | '=='; | ||
| NSEQ: '<=>'; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1206,6 +1206,41 @@ class AstBuilder(conf: SQLConf) extends SqlBaseBaseVisitor[AnyRef] with Logging | |
| new StringLocate(expression(ctx.substr), expression(ctx.str)) | ||
| } | ||
|
|
||
| /** | ||
| * Create a Extract expression. | ||
| */ | ||
| override def visitExtract(ctx: ExtractContext): Expression = withOrigin(ctx) { | ||
| val extractType = ctx.field.getText.toUpperCase(Locale.ROOT) | ||
| try { | ||
| extractType match { | ||
| case "YEAR" => | ||
| Year(expression(ctx.source)) | ||
| case "QUARTER" => | ||
| Quarter(expression(ctx.source)) | ||
| case "MONTH" => | ||
| Month(expression(ctx.source)) | ||
| case "WEEK" => | ||
| WeekOfYear(expression(ctx.source)) | ||
| case "DAY" => | ||
| DayOfMonth(expression(ctx.source)) | ||
| case "DOW" => | ||
|
||
| DayOfWeek(expression(ctx.source)) | ||
| case "HOUR" => | ||
| Hour(expression(ctx.source)) | ||
| case "MINUTE" => | ||
| Minute(expression(ctx.source)) | ||
| case "SECOND" => | ||
| Second(expression(ctx.source)) | ||
| case other => | ||
| throw new ParseException(s"Literals of type '$other' are currently not supported.", ctx) | ||
| } | ||
| } catch { | ||
| case e: IllegalArgumentException => | ||
|
||
| val message = Option(e.getMessage).getOrElse(s"Exception parsing $extractType") | ||
| throw new ParseException(message, ctx) | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Create a (windowed) Function expression. | ||
| */ | ||
|
|
||
| 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(dow 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; |
| 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(dow 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 | ||
| -------^^^ |
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 forEXTRACTnow. To document it, there are the three options that I can think of;(the simplest fix) Add some description about
EXTRACTin the SELECT syntax page (e.g., thenamed_expressionsection), then add a link todate_partin the built-in function page.Add a dummy
ExpressionDescriptionforEXTRACTlike this;ExpressionDescriptionlike 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.