Github user hvanhovell commented on a diff in the pull request: https://github.com/apache/spark/pull/14788#discussion_r77796067 --- Diff: sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/datetimeExpressions.scala --- @@ -72,57 +72,76 @@ case class CurrentTimestamp() extends LeafExpression with CodegenFallback { } /** - * Adds a number of days to startdate. + * Adds a number of days to date/timestamp. */ @ExpressionDescription( - usage = "_FUNC_(start_date, num_days) - Returns the date that is num_days after start_date.", + usage = "_FUNC_(instant, num_days) - Returns the date/timestamp that is num_days after instant.", extended = "> SELECT _FUNC_('2016-07-30', 1);\n '2016-07-31'") -case class DateAdd(startDate: Expression, days: Expression) +case class DateAdd(instant: Expression, days: Expression) extends BinaryExpression with ImplicitCastInputTypes { - override def left: Expression = startDate + override def left: Expression = instant override def right: Expression = days - override def inputTypes: Seq[AbstractDataType] = Seq(DateType, IntegerType) + override def inputTypes: Seq[AbstractDataType] = + Seq(TypeCollection(DateType, TimestampType), IntegerType) - override def dataType: DataType = DateType + override def dataType: DataType = instant.dataType - override def nullSafeEval(start: Any, d: Any): Any = { - start.asInstanceOf[Int] + d.asInstanceOf[Int] + override def nullSafeEval(start: Any, days: Any): Any = { + (instant.dataType, start, days) match { + case (_: DateType, startDate: Int, days: Int) => + startDate + days + case (_: TimestampType, startTimestamp: Long, days: Int) => + DateTimeUtils.timestampAddDays(startTimestamp, days) + } } override def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = { - nullSafeCodeGen(ctx, ev, (sd, d) => { - s"""${ev.value} = $sd + $d;""" - }) + instant.dataType match { + case DateType => nullSafeCodeGen(ctx, ev, (sd, d) => s"""${ev.value} = $sd + $d;""") + case TimestampType => + val dtu = DateTimeUtils.getClass.getName.stripSuffix("$") + defineCodeGen(ctx, ev, (sd, d) => s"""$dtu.timestampAddDays($sd, $d)""") + } } override def prettyName: String = "date_add" } /** - * Subtracts a number of days to startdate. + * Subtracts a number of days to date/timestamp. */ @ExpressionDescription( - usage = "_FUNC_(start_date, num_days) - Returns the date that is num_days before start_date.", + usage = "_FUNC_(instant, num_days) - Returns the date/timestamp that is num_days before instant.", extended = "> SELECT _FUNC_('2016-07-30', 1);\n '2016-07-29'") -case class DateSub(startDate: Expression, days: Expression) +case class DateSub(instant: Expression, days: Expression) --- End diff -- How much work would it be to make AddDays/SubDays have a common parent? It is a bit beyond the scope of the PR.
--- 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 infrastruct...@apache.org or file a JIRA ticket with INFRA. --- --------------------------------------------------------------------- To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org For additional commands, e-mail: reviews-h...@spark.apache.org