cloud-fan commented on a change in pull request #32314:
URL: https://github.com/apache/spark/pull/32314#discussion_r620467267
##########
File path:
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/intervalExpressions.scala
##########
@@ -418,20 +430,42 @@ case class DivideYMInterval(
}
override def nullSafeEval(interval: Any, num: Any): Any = {
+ checkDivideOverflow(interval.asInstanceOf[Int], num)
evalFunc(interval.asInstanceOf[Int], num)
}
override def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode =
right.dataType match {
case LongType =>
val math = classOf[LongMath].getName
val javaType = CodeGenerator.javaType(dataType)
- defineCodeGen(ctx, ev, (m, n) =>
+ val micros = left.genCode(ctx)
+ val num = right.genCode(ctx)
+ val checkIntegralDivideOverflow =
+ s"""
+ |if (${micros.value} == ${Int.MinValue}L && ${num.value} == -1L)
+ | throw QueryExecutionErrors.overflowInIntegralDivideError();
+ |""".stripMargin
+ nullSafeCodeGen(ctx, ev, (m, n) =>
// Similarly to non-codegen code. The result of `divide(Int, Long,
...)` must fit to `Int`.
// Casting to `Int` is safe here.
- s"($javaType)($math.divide($m, $n, java.math.RoundingMode.HALF_UP))")
+ s"""
+ |$checkIntegralDivideOverflow
+ |${ev.value} = ($javaType)($math.divide($m, $n,
java.math.RoundingMode.HALF_UP));
+ """.stripMargin)
case _: IntegralType =>
val math = classOf[IntMath].getName
Review comment:
can we merge `case LongType` and `case _: IntegralType` to save
duplicated code?
--
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]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]