gengliangwang commented on code in PR #55938:
URL: https://github.com/apache/spark/pull/55938#discussion_r3269035728


##########
sql/catalyst/src/main/java/org/apache/spark/sql/catalyst/expressions/ArithmeticUtils.java:
##########
@@ -0,0 +1,85 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.spark.sql.catalyst.expressions;
+
+import org.apache.spark.sql.errors.QueryExecutionErrors;
+
+/**
+ * Static helpers used by {@code BinaryArithmetic.doGenCode} (and
+ * corresponding eval paths) for ANSI overflow-checked {@code byte} and
+ * {@code short} arithmetic. Primitive {@code int} / {@code long} /
+ * {@code float} / {@code double} arithmetic stays inline -- routing those
+ * single-bytecode operations through a static method would be a runtime
+ * regression.
+ */
+public final class ArithmeticUtils {

Review Comment:
   Good catch. The new commit adds a class-javadoc note explicitly calling out 
that these helpers mirror `ByteExactNumeric` / `ShortExactNumeric` and exist 
only because Scala `object` methods can't be called from generated code without 
going through `references[]`. I also dropped the redundant eval-path branches 
(see other replies), so `numerics.scala` is now the sole eval-path 
implementation.



##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/arithmetic.scala:
##########
@@ -301,31 +301,22 @@ 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 opName = symbol match {
+        case "+" => "Add"
+        case "-" => "Subtract"
+        case "*" => "Multiply"
+        case _ =>
+          throw 
QueryExecutionErrors.notOverrideExpectedMethodsError(this.getClass.getName,
+            s"genCode for Byte/Short with symbol '$symbol'", "genCode")

Review Comment:
   Done — switched to `SparkException.internalError(s"Unexpected symbol 
'$symbol' for Byte/Short BinaryArithmetic")`.



##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/arithmetic.scala:
##########
@@ -458,6 +449,10 @@ case class Add(
       MathUtils.addExact(input1.asInstanceOf[Int], input2.asInstanceOf[Int], 
getContextOrNull())
     case _: LongType if failOnError =>
       MathUtils.addExact(input1.asInstanceOf[Long], input2.asInstanceOf[Long], 
getContextOrNull())
+    case _: ByteType if failOnError =>
+      ArithmeticUtils.byteAddExact(input1.asInstanceOf[Byte], 
input2.asInstanceOf[Byte])
+    case _: ShortType if failOnError =>
+      ArithmeticUtils.shortAddExact(input1.asInstanceOf[Short], 
input2.asInstanceOf[Short])

Review Comment:
   Agreed — reverted. `Add.nullSafeEval` now falls through to `numeric.plus` 
for byte/short (which already routes through 
`ByteExactNumeric`/`ShortExactNumeric` and throws the same SQL-formatted 
overflow error). The pre-PR statement in the description about a generic 
`ArithmeticException` was wrong; sorry for the noise.



##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/arithmetic.scala:
##########
@@ -555,6 +550,10 @@ case class Subtract(
         input1.asInstanceOf[Long],
         input2.asInstanceOf[Long],
         getContextOrNull())
+    case _: ByteType if failOnError =>
+      ArithmeticUtils.byteSubtractExact(input1.asInstanceOf[Byte], 
input2.asInstanceOf[Byte])
+    case _: ShortType if failOnError =>
+      ArithmeticUtils.shortSubtractExact(input1.asInstanceOf[Short], 
input2.asInstanceOf[Short])

Review Comment:
   Reverted — `Subtract.nullSafeEval` now falls through to `numeric.minus` for 
byte/short.



##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/arithmetic.scala:
##########
@@ -625,6 +624,10 @@ case class Multiply(
         input1.asInstanceOf[Long],
         input2.asInstanceOf[Long],
         getContextOrNull())
+    case _: ByteType if failOnError =>
+      ArithmeticUtils.byteMultiplyExact(input1.asInstanceOf[Byte], 
input2.asInstanceOf[Byte])
+    case _: ShortType if failOnError =>
+      ArithmeticUtils.shortMultiplyExact(input1.asInstanceOf[Short], 
input2.asInstanceOf[Short])

Review Comment:
   Reverted — `Multiply.nullSafeEval` now falls through to `numeric.times` for 
byte/short.



-- 
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]

Reply via email to