Github user rxin commented on a diff in the pull request:
https://github.com/apache/spark/pull/6938#discussion_r34404086
--- Diff:
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/math.scala
---
@@ -520,3 +522,127 @@ case class Logarithm(left: Expression, right:
Expression)
"""
}
}
+
+case class Round(child: Expression, scale: Expression)
+ extends BinaryExpression with ExpectsInputTypes {
+
+ import BigDecimal.RoundingMode.HALF_UP
+
+ def this(child: Expression) = this(child, Literal(0))
+
+ override def left: Expression = child
+ override def right: Expression = scale
+
+ override def children: Seq[Expression] = Seq(child, scale)
+
+ // round of Decimal would eval to null if it fails to `changePrecision`
+ override def nullable: Boolean = true
+
+ override def foldable: Boolean = child.foldable
+
+ override lazy val dataType: DataType = child.dataType match {
+ case DecimalType.Fixed(p, s) => DecimalType(p, _scale)
+ case t => t
+ }
+
+ override def inputTypes: Seq[AbstractDataType] = Seq(NumericType,
IntegerType)
+
+ override def checkInputDataTypes(): TypeCheckResult = {
+ super.checkInputDataTypes() match {
+ case TypeCheckSuccess =>
+ if (scale.foldable) {
+ TypeCheckSuccess
+ } else {
+ TypeCheckFailure("Only foldable Expression is allowed for scale
arguments")
+ }
+ case f => f
+ }
+ }
+
+ private lazy val scaleV = scale.eval(EmptyRow)
+ private lazy val _scale = if (scaleV != null) scaleV.asInstanceOf[Int]
else 0
+
+ protected override def nullSafeEval(input1: Any, input2: Any): Any = {
+ child.dataType match {
+ case _: DecimalType =>
+ val decimal = input1.asInstanceOf[Decimal]
+ if (decimal.changePrecision(decimal.precision, _scale)) decimal
else null
+ case ByteType =>
+ BigDecimal(input1.asInstanceOf[Byte]).setScale(_scale,
HALF_UP).toByte
+ case ShortType =>
+ BigDecimal(input1.asInstanceOf[Short]).setScale(_scale,
HALF_UP).toShort
+ case IntegerType =>
+ BigDecimal(input1.asInstanceOf[Int]).setScale(_scale,
HALF_UP).toInt
+ case LongType =>
+ BigDecimal(input1.asInstanceOf[Long]).setScale(_scale,
HALF_UP).toLong
+ case FloatType =>
+ val f = input1.asInstanceOf[Float]
+ if (f.isNaN || f.isInfinite) {
+ f
+ } else {
+ BigDecimal(f).setScale(_scale, HALF_UP).toFloat
+ }
+ case DoubleType =>
+ val d = input1.asInstanceOf[Double]
+ if (d.isNaN || d.isInfinite) {
+ d
+ } else {
+ BigDecimal(d).setScale(_scale, HALF_UP).toDouble
+ }
+ }
+ }
+
+ override def genCode(ctx: CodeGenContext, ev: GeneratedExpressionCode):
String = {
+ val ce = child.gen(ctx)
+
+ val evaluationCode = child.dataType match {
--- End diff --
for integral types, can you special case scale <= 0? in those cases, you
can just return the value.
for round, special case scale == 0, which you can just call java math round?
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]