cloud-fan commented on code in PR #55938:
URL: https://github.com/apache/spark/pull/55938#discussion_r3279943747
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/arithmetic.scala:
##########
@@ -301,31 +301,21 @@ abstract class BinaryArithmetic extends BinaryOperator
with SupportQueryContext
val mathUtils =
IntervalMathUtils.getClass.getCanonicalName.stripSuffix("$")
defineCodeGen(ctx, ev, (eval1, eval2) =>
s"$mathUtils.${exactMathMethod.get}($eval1, $eval2)")
// byte and short are casted into int when add, minus, times or divide
+ case ByteType | ShortType if failOnError =>
+ val methodName = symbol match {
+ case "+" => "plus"
+ case "-" => "minus"
+ case "*" => "times"
+ case _ =>
+ throw SparkException.internalError(
+ s"Unexpected symbol '$symbol' for Byte/Short BinaryArithmetic")
+ }
+ val numericObj = (if (dataType == ByteType) ByteExactNumeric else
ShortExactNumeric)
+ .getClass.getCanonicalName.stripSuffix("$")
+ defineCodeGen(ctx, ev, (eval1, eval2) =>
s"$numericObj.$methodName($eval1, $eval2)")
case ByteType | ShortType =>
nullSafeCodeGen(ctx, ev, (eval1, eval2) => {
- val tmpResult = ctx.freshName("tmpResult")
- val try_suggestion = symbol match {
- case "+" => "try_add"
- case "-" => "try_subtract"
- case "*" => "try_multiply"
- case _ => "unknown_function"
- }
- val overflowCheck = if (failOnError) {
- val javaType = CodeGenerator.boxedType(dataType)
- s"""
- |if ($tmpResult < $javaType.MIN_VALUE || $tmpResult >
$javaType.MAX_VALUE) {
- | throw QueryExecutionErrors.binaryArithmeticCauseOverflowError(
- | $eval1, "$symbol", $eval2, "$try_suggestion");
- |}
- """.stripMargin
- } else {
- ""
- }
- s"""
- |${CodeGenerator.JAVA_INT} $tmpResult = $eval1 $symbol $eval2;
- |$overflowCheck
- |${ev.value} = (${CodeGenerator.javaType(dataType)})($tmpResult);
- """.stripMargin
+ s"${ev.value} = (${CodeGenerator.javaType(dataType)})($eval1 $symbol
$eval2);"
})
Review Comment:
nit (optional): now that the body is a single `ev.value = …;` assignment,
this branch could also use `defineCodeGen` for visual symmetry with the new
`failOnError` branch directly above (and with the `AnsiIntervalType` branch on
lines 297–302):
```suggestion
case ByteType | ShortType =>
defineCodeGen(ctx, ev, (eval1, eval2) =>
s"(${CodeGenerator.javaType(dataType)})($eval1 $symbol $eval2)")
```
--
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.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]