-
Notifications
You must be signed in to change notification settings - Fork 28.4k
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
[SPARK-8186] [SPARK-8187] [SQL] datetime function: date_add, date_sub #6782
Closed
Closed
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5a50edc
datetime function: date_add, date_sub, datediff
adrian-wang a6243f7
remove datediff because datediff use GTC time, do that in another pr
adrian-wang ea9fde0
fix rebase
adrian-wang f89bf4a
chenghao's comments
adrian-wang 2ac75f7
add codegen
adrian-wang 0944f03
rebase and several update
adrian-wang 95b3b58
fix test
adrian-wang 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
remove datediff because datediff use GTC time, do that in another pr
- Loading branch information
commit a6243f76862355b8088f76addae3d21318b8eb43
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,65 +17,99 @@ | |
|
||
package org.apache.spark.sql.catalyst.expressions | ||
|
||
import org.apache.spark.sql.types._ | ||
|
||
case class DateAdd(left: Expression, right: Expression) | ||
extends BinaryExpression with ExpectsInputTypes { | ||
|
||
override def children: Seq[Expression] = left :: right :: Nil | ||
override def expectedChildTypes: Seq[DataType] = DateType :: IntegerType :: Nil | ||
|
||
override def dataType: DataType = DateType | ||
import java.util.{Calendar, TimeZone} | ||
|
||
override def toString: String = s"DateAdd($left, $right)" | ||
|
||
override def eval(input: Row): Any = { | ||
val startDate = left.eval(input) | ||
val days = right.eval(input) | ||
if (startDate == null || days == null) { | ||
null | ||
import org.apache.spark.sql.catalyst.analysis.TypeCheckResult | ||
import org.apache.spark.sql.catalyst.util.DateUtils | ||
import org.apache.spark.sql.types._ | ||
import org.apache.spark.unsafe.types.UTF8String | ||
|
||
case class DateAdd(startDate: Expression, days: Expression) extends Expression { | ||
override def children: Seq[Expression] = startDate :: days :: Nil | ||
|
||
override def foldable: Boolean = startDate.foldable && days.foldable | ||
override def nullable: Boolean = startDate.nullable || days.nullable | ||
|
||
override def checkInputDataTypes(): TypeCheckResult = { | ||
val supportedLeftType = Seq(StringType, DateType, TimestampType, NullType) | ||
if (!supportedLeftType.contains(startDate.dataType)) { | ||
TypeCheckResult.TypeCheckFailure( | ||
s"type of startdate expression in DateAdd should be string/timestamp/date," + | ||
s" not ${startDate.dataType}") | ||
} else if (days.dataType != IntegerType && days.dataType != NullType) { | ||
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. Could we also support integral types here for For example, we always infer integer from Python and Json as LongType. |
||
TypeCheckResult.TypeCheckFailure( | ||
s"type of days expression in DateAdd should be int, not ${days.dataType}.") | ||
} else { | ||
startDate.asInstanceOf[Int] + days.asInstanceOf[Int] | ||
TypeCheckResult.TypeCheckSuccess | ||
} | ||
} | ||
} | ||
|
||
case class DateSub(left: Expression, right: Expression) | ||
extends BinaryExpression with ExpectsInputTypes { | ||
|
||
override def expectedChildTypes: Seq[DataType] = DateType :: IntegerType :: Nil | ||
override def dataType: DataType = StringType | ||
|
||
override def dataType: DataType = DateType | ||
|
||
override def toString: String = s"DateSub($left, $right)" | ||
override def toString: String = s"DateAdd($startDate, $days)" | ||
|
||
override def eval(input: Row): Any = { | ||
val startDate = left.eval(input) | ||
val days = right.eval(input) | ||
if (startDate == null || days == null) { | ||
val start = startDate.eval(input) | ||
val d = days.eval(input) | ||
if (start == null || d == null) { | ||
null | ||
} else { | ||
startDate.asInstanceOf[Int] - days.asInstanceOf[Int] | ||
val offset = d.asInstanceOf[Int] | ||
val resultDays = startDate.dataType match { | ||
case StringType => | ||
DateUtils.millisToDays(DateUtils.stringToTime( | ||
start.asInstanceOf[UTF8String].toString).getTime) + offset | ||
case TimestampType => | ||
DateUtils.millisToDays(DateUtils.toJavaTimestamp( | ||
start.asInstanceOf[Long]).getTime) + offset | ||
case DateType => start.asInstanceOf[Int] + offset | ||
} | ||
UTF8String.fromString(DateUtils.toString(resultDays)) | ||
} | ||
} | ||
} | ||
|
||
case class DateDiff(left: Expression, right: Expression) | ||
extends BinaryExpression with ExpectsInputTypes { | ||
|
||
override def expectedChildTypes: Seq[DataType] = DateType :: DateType :: Nil | ||
case class DateSub(startDate: Expression, days: Expression) extends Expression { | ||
override def children: Seq[Expression] = startDate :: days :: Nil | ||
|
||
override def foldable: Boolean = startDate.foldable && days.foldable | ||
override def nullable: Boolean = startDate.nullable || days.nullable | ||
|
||
override def checkInputDataTypes(): TypeCheckResult = { | ||
val supportedLeftType = Seq(StringType, DateType, TimestampType, NullType) | ||
if (!supportedLeftType.contains(startDate.dataType)) { | ||
TypeCheckResult.TypeCheckFailure( | ||
s"type of startdate expression in DateSub should be string/timestamp/date," + | ||
s" not ${startDate.dataType}") | ||
} else if (days.dataType != IntegerType && days.dataType != NullType) { | ||
TypeCheckResult.TypeCheckFailure( | ||
s"type of days expression in DateSub should be int, not ${days.dataType}.") | ||
} else { | ||
TypeCheckResult.TypeCheckSuccess | ||
} | ||
} | ||
|
||
override def dataType: DataType = IntegerType | ||
override def dataType: DataType = StringType | ||
|
||
override def toString: String = s"DateDiff($left, $right)" | ||
override def toString: String = s"DateSub($startDate, $days)" | ||
|
||
override def eval(input: Row): Any = { | ||
val startDate = left.eval(input) | ||
val endDate = right.eval(input) | ||
if (startDate == null || endDate == null) { | ||
val start = startDate.eval(input) | ||
val d = days.eval(input) | ||
if (start == null || d == null) { | ||
null | ||
} else { | ||
startDate.asInstanceOf[Int] - endDate.asInstanceOf[Int] | ||
val offset = d.asInstanceOf[Int] | ||
val resultDays = startDate.dataType match { | ||
case StringType => | ||
DateUtils.millisToDays(DateUtils.stringToTime( | ||
start.asInstanceOf[UTF8String].toString).getTime) - offset | ||
case TimestampType => | ||
DateUtils.millisToDays(DateUtils.toJavaTimestamp( | ||
start.asInstanceOf[Long]).getTime) - offset | ||
case DateType => start.asInstanceOf[Int] - offset | ||
} | ||
UTF8String.fromString(DateUtils.toString(resultDays)) | ||
} | ||
} | ||
} |
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
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.
i think it'd be helpful to define the semantics here (basically what eval is doing)