Github user davies commented on a diff in the pull request:
https://github.com/apache/spark/pull/7589#discussion_r35693133
--- Diff:
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/datetimeFunctions.scala
---
@@ -258,3 +305,125 @@ case class DateFormatClass(left: Expression, right:
Expression) extends BinaryEx
})
}
}
+
+/**
+ * Time Adds Interval.
+ */
+case class TimeAdd(left: Expression, right: Expression)
+ extends BinaryExpression with ExpectsInputTypes {
+
+ override def toString: String = s"$left + $right"
+ override def inputTypes: Seq[AbstractDataType] =
+ Seq(TypeCollection(DateType, TimestampType), IntervalType)
+
+ override def dataType: DataType = TimestampType
+
+ override def nullSafeEval(start: Any, interval: Any): Any = {
+ val itvl = interval.asInstanceOf[Interval]
+ left.dataType match {
+ case DateType =>
+ DateTimeUtils.dateAddFullInterval(
+ start.asInstanceOf[Int], itvl.months, itvl.microseconds)
+ case TimestampType =>
+ DateTimeUtils.timestampAddFullInterval(
+ start.asInstanceOf[Long], itvl.months, itvl.microseconds)
+ }
+ }
+
+ override def genCode(ctx: CodeGenContext, ev: GeneratedExpressionCode):
String = {
+ val dtu = DateTimeUtils.getClass.getName.stripSuffix("$")
+ left.dataType match {
+ case DateType =>
+ defineCodeGen(ctx, ev, (sd, i) => {
+ s"""$dtu.dateAddFullInterval($sd, $i.months, $i.microseconds)"""
+ })
+ case TimestampType => // TimestampType
+ defineCodeGen(ctx, ev, (sd, i) => {
+ s"""$dtu.timestampAddFullInterval($sd, $i.months,
$i.microseconds)"""
+ })
+ }
+ }
+}
+
+/**
+ * Time Subtracts Interval.
+ */
+case class TimeSub(left: Expression, right: Expression)
+ extends BinaryExpression with ExpectsInputTypes {
+
+ override def toString: String = s"$left - $right"
+ override def inputTypes: Seq[AbstractDataType] =
+ Seq(TypeCollection(DateType, TimestampType), IntervalType)
+
+ override def dataType: DataType = TimestampType
+
+ override def nullSafeEval(start: Any, interval: Any): Any = {
+ val itvl = interval.asInstanceOf[Interval]
+ left.dataType match {
+ case DateType =>
+ DateTimeUtils.dateAddFullInterval(
+ start.asInstanceOf[Int], 0 - itvl.months, 0 - itvl.microseconds)
+ case TimestampType =>
+ DateTimeUtils.timestampAddFullInterval(
+ start.asInstanceOf[Long], 0 - itvl.months, 0 - itvl.microseconds)
+ }
+ }
+
+ override def genCode(ctx: CodeGenContext, ev: GeneratedExpressionCode):
String = {
+ val dtu = DateTimeUtils.getClass.getName.stripSuffix("$")
+ left.dataType match {
+ case DateType =>
+ defineCodeGen(ctx, ev, (sd, i) => {
+ s"""$dtu.dateAddFullInterval($sd, 0 - $i.months, 0 -
$i.microseconds)"""
+ })
+ case TimestampType => // TimestampType
+ defineCodeGen(ctx, ev, (sd, i) => {
+ s"""$dtu.timestampAddFullInterval($sd, 0 - $i.months, 0 -
$i.microseconds)"""
+ })
+ }
+ }
+}
+
+/**
+ * Returns the date that is num_months after start_date.
+ */
+case class AddMonths(left: Expression, right: Expression)
+ extends BinaryExpression with ImplicitCastInputTypes {
+
+ override def inputTypes: Seq[AbstractDataType] = Seq(DateType,
IntegerType)
+
+ override def dataType: DataType = DateType
+
+ override def nullSafeEval(start: Any, months: Any): Any = {
+ DateTimeUtils.dateAddYearMonthInterval(start.asInstanceOf[Int],
months.asInstanceOf[Int])
+ }
+
+ override def genCode(ctx: CodeGenContext, ev: GeneratedExpressionCode):
String = {
+ val dtu = DateTimeUtils.getClass.getName.stripSuffix("$")
+ defineCodeGen(ctx, ev, (sd, m) => {
+ s"""$dtu.dateAddYearMonthInterval($sd, $m)"""
+ })
+ }
+}
+
+/**
+ * Returns number of months between dates date1 and date2.
+ */
+case class MonthsBetween(left: Expression, right: Expression)
+ extends BinaryExpression with ImplicitCastInputTypes {
+
+ override def inputTypes: Seq[AbstractDataType] = Seq(TimestampType,
TimestampType)
--- End diff --
Why not use DateType?
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]