-
Notifications
You must be signed in to change notification settings - Fork 29.3k
[SPARK-20909][SQL] Add build-int SQL function - DAYOFWEEK #18134
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
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -402,6 +402,44 @@ case class DayOfMonth(child: Expression) extends UnaryExpression with ImplicitCa | |
| } | ||
| } | ||
|
|
||
| // scalastyle:off line.size.limit | ||
| @ExpressionDescription( | ||
| usage = "_FUNC_(date) - Returns the weekday index for date/timestamp (1 = Sunday, 2 = Monday, ..., 7 = Saturday).", | ||
| extended = """ | ||
| Examples: | ||
| > SELECT _FUNC_('2009-07-30'); | ||
| 5 | ||
| """) | ||
| // scalastyle:on line.size.limit | ||
| case class DayOfWeek(child: Expression) extends UnaryExpression with ImplicitCastInputTypes { | ||
|
|
||
| override def inputTypes: Seq[AbstractDataType] = Seq(DateType) | ||
|
|
||
| override def dataType: DataType = IntegerType | ||
|
|
||
| @transient private lazy val c = { | ||
| Calendar.getInstance(DateTimeUtils.getTimeZone("UTC")) | ||
| } | ||
|
|
||
| override protected def nullSafeEval(date: Any): Any = { | ||
| c.setTimeInMillis(date.asInstanceOf[Int] * 1000L * 3600L * 24L) | ||
| c.get(Calendar.DAY_OF_WEEK) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Keep pace with Hive's DayOfWeek |
||
| } | ||
|
|
||
| override protected def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = { | ||
| nullSafeCodeGen(ctx, ev, time => { | ||
| val cal = classOf[Calendar].getName | ||
| val c = ctx.freshName("cal") | ||
| val dtu = DateTimeUtils.getClass.getName.stripSuffix("$") | ||
| ctx.addMutableState(cal, c, s"""$c = $cal.getInstance($dtu.getTimeZone("UTC"));""") | ||
| s""" | ||
| $c.setTimeInMillis($time * 1000L * 3600L * 24L); | ||
| ${ev.value} = $c.get($cal.DAY_OF_WEEK); | ||
| """ | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| @ExpressionDescription( | ||
| usage = "_FUNC_(date) - Returns the week of the year of the given date.", | ||
| extended = """ | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,3 +6,5 @@ select current_date = current_date(), current_timestamp = current_timestamp(); | |
| select to_date(null), to_date('2016-12-31'), to_date('2016-12-31', 'yyyy-MM-dd'); | ||
|
|
||
| select to_timestamp(null), to_timestamp('2016-12-31 00:12:00'), to_timestamp('2016-12-31', 'yyyy-MM-dd'); | ||
|
|
||
| select dayofweek('2007-02-03'), dayofweek('2009-07-30'), dayofweek('2017-05-27'), dayofweek(null), dayofweek('1582-10-15 13:10:15'); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: Can you add a line break at the end of file? |
||
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.
As Sunday, Saturday are included, it is not only weekday.
Returns the day of the week ....