maropu commented on a change in pull request #25461: [SPARK-28741][SQL]Throw exceptions when casting to integers causes overflow URL: https://github.com/apache/spark/pull/25461#discussion_r316970371
########## File path: sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/Cast.scala ########## @@ -1182,6 +1233,78 @@ case class Cast(child: Expression, dataType: DataType, timeZoneId: Option[String (c, evPrim, evNull) => code"$evPrim = $c != 0;" } + private[this] def castTimestampToIntegerCode( + ctx: CodegenContext, + integralType: String): CastFunction = { + if (failOnIntegerOverflow) { + val longValue = ctx.freshName("longValue") + (c, evPrim, evNull) => + code""" + long $longValue = ${timestampToLongCode(c)}; + if ($longValue == ($integralType) $longValue) { + $evPrim = ($integralType) $longValue; + } else { + throw new ArithmeticException("Casting $c to $integralType causes overflow"); + } + """ + } else { + (c, evPrim, evNull) => code"$evPrim = ($integralType) ${timestampToLongCode(c)};" + } + } + + private[this] def castDecimalToIntegerCode( + ctx: CodegenContext, + integralType: String): CastFunction = { + (c, evPrim, evNull) => code"$evPrim = $c.to${integralType.capitalize}($failOnIntegerOverflow);" + } + + private[this] def castIntegerToIntegerExactCode(integralType: String): CastFunction = { Review comment: (oh, I didn't know this functionality, ```suggestion, cool) ---------------------------------------------------------------- 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