-
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 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -592,7 +592,7 @@ primaryExpression | |
| | identifier #columnReference | ||
| | base=primaryExpression '.' fieldName=identifier #dereference | ||
| | '(' expression ')' #parenthesizedExpression | ||
| | EXTRACT '(' field=(YEAR | QUARTER | MONTH | WEEK | DAY | HOUR | MINUTE | SECOND) FROM source=valueExpression ')' #extract | ||
| | EXTRACT '(' field=identifier FROM source=valueExpression ')' #extract | ||
| ; | ||
|
|
||
| constant | ||
|
|
@@ -740,7 +740,7 @@ nonReserved | |
| | VIEW | REPLACE | ||
| | IF | ||
| | POSITION | ||
| | EXTRACT | YEAR | QUARTER | MONTH | WEEK | DAY | HOUR | MINUTE | SECOND | ||
| | 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 | ||
|
|
@@ -886,6 +886,7 @@ QUARTER: 'QUARTER'; | |
| MONTH: 'MONTH'; | ||
| WEEK: 'WEEK'; | ||
| DAY: 'DAY'; | ||
| DOW: 'DOW'; | ||
| HOUR: 'HOUR'; | ||
| MINUTE: 'MINUTE'; | ||
| SECOND: 'SECOND'; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1210,23 +1210,34 @@ class AstBuilder(conf: SQLConf) extends SqlBaseBaseVisitor[AnyRef] with Logging | |
| * Create a Extract expression. | ||
| */ | ||
| override def visitExtract(ctx: ExtractContext): Expression = withOrigin(ctx) { | ||
| ctx.field.getType match { | ||
| case SqlBaseParser.YEAR => | ||
| Year(expression(ctx.source)) | ||
| case SqlBaseParser.QUARTER => | ||
| Quarter(expression(ctx.source)) | ||
| case SqlBaseParser.MONTH => | ||
| Month(expression(ctx.source)) | ||
| case SqlBaseParser.WEEK => | ||
| WeekOfYear(expression(ctx.source)) | ||
| case SqlBaseParser.DAY => | ||
| DayOfMonth(expression(ctx.source)) | ||
| case SqlBaseParser.HOUR => | ||
| Hour(expression(ctx.source)) | ||
| case SqlBaseParser.MINUTE => | ||
| Minute(expression(ctx.source)) | ||
| case SqlBaseParser.SECOND => | ||
| Second(expression(ctx.source)) | ||
| 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) | ||
| } | ||
| } | ||
|
|
||
|
|
||
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.