[SPARK-9151][SQL] Implement code generation for Abs

JIRA: https://issues.apache.org/jira/browse/SPARK-9151

Add codegen support for `Abs`.

Author: Liang-Chi Hsieh <viirya@appier.com>

Closes #7498 from viirya/abs_codegen and squashes the following commits:

0c8410f [Liang-Chi Hsieh] Implement code generation for Abs.
This commit is contained in:
Liang-Chi Hsieh 2015-07-18 12:11:37 -07:00 committed by Reynold Xin
parent 86c50bf72c
commit 225de8da2b
2 changed files with 9 additions and 0 deletions

View file

@ -73,6 +73,13 @@ case class Abs(child: Expression) extends UnaryExpression with ExpectsInputTypes
private lazy val numeric = TypeUtils.getNumeric(dataType)
override def genCode(ctx: CodeGenContext, ev: GeneratedExpressionCode): String = dataType match {
case dt: DecimalType =>
defineCodeGen(ctx, ev, c => s"$c.abs()")
case dt: NumericType =>
defineCodeGen(ctx, ev, c => s"(${ctx.javaType(dt)})(java.lang.Math.abs($c))")
}
protected override def nullSafeEval(input: Any): Any = numeric.abs(input)
}

View file

@ -278,6 +278,8 @@ final class Decimal extends Ordered[Decimal] with Serializable {
Decimal(-longVal, precision, scale)
}
}
def abs: Decimal = if (this.compare(Decimal(0)) < 0) this.unary_- else this
}
object Decimal {