MaxGekk commented on a change in pull request #31836:
URL: https://github.com/apache/spark/pull/31836#discussion_r594338622



##########
File path: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/arithmetic.scala
##########
@@ -145,18 +145,52 @@ case class UnaryPositive(child: Expression)
   """,
   since = "1.2.0",
   group = "math_funcs")
-case class Abs(child: Expression)
+case class Abs(child: Expression, failOnError: Boolean = 
SQLConf.get.ansiEnabled)
   extends UnaryExpression with ExpectsInputTypes with NullIntolerant {
 
+  def this(child: Expression) = this(child, SQLConf.get.ansiEnabled)
+
   override def inputTypes: Seq[AbstractDataType] = Seq(NumericType)
 
   override def dataType: DataType = child.dataType
 
-  private lazy val numeric = TypeUtils.getNumeric(dataType)
+  private lazy val numeric = TypeUtils.getNumeric(dataType, failOnError)
 
   override def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = 
dataType match {
     case _: DecimalType =>
       defineCodeGen(ctx, ev, c => s"$c.abs()")
+
+    case _: ByteType if failOnError =>
+      nullSafeCodeGen(ctx, ev, eval =>
+        s"""
+          |if ($eval == ${Byte.MinValue}) {
+          |  throw new ArithmeticException("byte overflow");
+          |} else if ($eval < 0) {
+          |  ${ev.value} = -$eval;
+          |} else {
+          |  ${ev.value} = $eval;
+          |}
+          |""".stripMargin)
+
+    case _: ShortType if failOnError =>
+      nullSafeCodeGen(ctx, ev, eval =>
+        s"""
+          |if ($eval == ${Short.MinValue}) {
+          |  throw new ArithmeticException("short overflow");
+          |} else if ($eval < 0) {
+          |  ${ev.value} = -$eval;
+          |} else {
+          |  ${ev.value} = $eval;
+          |}
+          |""".stripMargin)
+
+    case _: IntegerType if failOnError =>
+      defineCodeGen(ctx, ev, c => s"$c < 0 ? java.lang.Math.negateExact($c) : 
$c")
+
+    case _: LongType if failOnError =>
+      defineCodeGen(ctx, ev, c => s"$c < 0 ? java.lang.Math.negateExact($c) : 
$c")

Review comment:
       ```suggestion
       case IntegerType | LongType if failOnError =>
         defineCodeGen(ctx, ev, c => s"$c < 0 ? java.lang.Math.negateExact($c) 
: $c")
   ```




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

Reply via email to