cloud-fan commented on a change in pull request #26995: [SPARK-30341][SQL] Overflow check for interval arithmetic operations URL: https://github.com/apache/spark/pull/26995#discussion_r361636765
########## File path: sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/intervalExpressions.scala ########## @@ -112,49 +113,96 @@ object ExtractIntervalPart { } } -abstract class IntervalNumOperation( - interval: Expression, - num: Expression, - operation: (CalendarInterval, Double) => CalendarInterval, - operationName: String) +abstract class IntervalNumOperation(interval: Expression, num: Expression) extends BinaryExpression with ImplicitCastInputTypes with Serializable { + + protected val checkOverflow: Boolean = SQLConf.get.ansiEnabled + 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 +} + +case class MultiplyInterval(interval: Expression, num: Expression) + extends IntervalNumOperation(interval, num) { + + override def prettyName: String = "multiply_interval" override def nullSafeEval(interval: Any, num: Any): Any = { try { - operation(interval.asInstanceOf[CalendarInterval], num.asInstanceOf[Double]) + if (checkOverflow) { + multiplyExact(interval.asInstanceOf[CalendarInterval], num.asInstanceOf[Double]) + } else { + multiply(interval.asInstanceOf[CalendarInterval], num.asInstanceOf[Double]) + } } catch { - case _: java.lang.ArithmeticException => null + case _: ArithmeticException if !checkOverflow => null } } override def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = { nullSafeCodeGen(ctx, ev, (interval, num) => { val iu = IntervalUtils.getClass.getName.stripSuffix("$") + val operationName = if (checkOverflow) "multiplyExact" else "multiply" s""" try { ${ev.value} = $iu.$operationName($interval, $num); - } catch (java.lang.ArithmeticException e) { - ${ev.isNull} = true; + } catch (ArithmeticException e) { + if ($checkOverflow) { + throw e; + } else { + ${ev.isNull} = true; + } } """ }) } - - override def prettyName: String = operationName + "_interval" } -case class MultiplyInterval(interval: Expression, num: Expression) - extends IntervalNumOperation(interval, num, multiply, "multiply") - case class DivideInterval(interval: Expression, num: Expression) - extends IntervalNumOperation(interval, num, divide, "divide") + extends IntervalNumOperation(interval, num) { + + override def prettyName: String = "divide_interval" + + override def nullSafeEval(interval: Any, num: Any): Any = { + try { + if (num == 0) return null + if (checkOverflow) { + divideExact(interval.asInstanceOf[CalendarInterval], num.asInstanceOf[Double]) + } else { + divide(interval.asInstanceOf[CalendarInterval], num.asInstanceOf[Double]) + } + } catch { + case _: ArithmeticException if !checkOverflow => null Review comment: is it needed? ---------------------------------------------------------------- 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: us...@infra.apache.org With regards, Apache Git Services --------------------------------------------------------------------- To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org For additional commands, e-mail: reviews-h...@spark.apache.org