gengliangwang commented on a change in pull request #31836: URL: https://github.com/apache/spark/pull/31836#discussion_r595684119
########## File path: sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/arithmetic.scala ########## @@ -145,18 +145,39 @@ 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 | ShortType if failOnError => + val javaBoxedType = CodeGenerator.boxedType(dataType) + val javaType = CodeGenerator.javaType(dataType) + nullSafeCodeGen(ctx, ev, eval => + s""" + |if ($eval == $javaBoxedType.MIN_VALUE) { + | throw QueryExecutionErrors.unaryMinusCauseOverflowError($eval); + |} else if ($eval < 0) { + | ${ev.value} = ($javaType)-$eval; Review comment: You are right. Added test for this. ---------------------------------------------------------------- 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 --------------------------------------------------------------------- To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org For additional commands, e-mail: reviews-h...@spark.apache.org