MaxGekk commented on a change in pull request #26132: [SPARK-29387][SQL]
Support `*` and `/` operators for intervals
URL: https://github.com/apache/spark/pull/26132#discussion_r342489283
##########
File path:
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/intervalExpressions.scala
##########
@@ -109,3 +109,55 @@ object ExtractIntervalPart {
case _ => errorHandleFunc
}
}
+
+abstract class IntervalNumOperation(
+ interval: Expression,
+ num: Expression,
+ operation: (CalendarInterval, Double) => CalendarInterval,
+ operationName: String)
+ extends BinaryExpression with ImplicitCastInputTypes with Serializable {
+ override def left: Expression = interval
+ override def right: Expression = num
+
+ override def inputTypes: Seq[AbstractDataType] = Seq(CalendarIntervalType,
DoubleType)
+ override def dataType: DataType = CalendarIntervalType
+
+ override def nullable: Boolean = true
+
+ override def nullSafeEval(interval: Any, num: Any): Any = {
+ try {
+ operation(interval.asInstanceOf[CalendarInterval],
num.asInstanceOf[Double])
+ } catch {
+ case _: java.lang.ArithmeticException => null
+ }
+ }
+
+ override def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = {
+ nullSafeCodeGen(ctx, ev, (interval, num) => {
+ val iu = IntervalUtils.getClass.getName.stripSuffix("$")
+ s"""
+ try {
+ ${ev.value} = $iu.$operationName($interval, $num);
+ } catch (java.lang.ArithmeticException e) {
+ ${ev.isNull} = true;
+ }
+ """
+ })
+ }
+
+ override def prettyName: String = operationName + "_interval"
+}
+
+case class MultiplyInterval(interval: Expression, num: Expression)
Review comment:
This should be added for only expressions registered as functions.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]