Github user ueshin commented on a diff in the pull request: https://github.com/apache/spark/pull/22419#discussion_r218531021 --- Diff: sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/mathExpressions.scala --- @@ -1245,3 +1245,65 @@ case class BRound(child: Expression, scale: Expression) with Serializable with ImplicitCastInputTypes { def this(child: Expression) = this(child, Literal(0)) } + +/** + * The number truncated to scale decimal places. + */ +// scalastyle:off line.size.limit +@ExpressionDescription( + usage = "_FUNC_(number, scale) - Returns number truncated to scale decimal places. " + + "If scale is omitted, then number is truncated to 0 places. " + + "scale can be negative to truncate (make zero) scale digits left of the decimal point.", + examples = """ + Examples: + > SELECT _FUNC_(1234567891.1234567891, 4); + 1234567891.1234 + > SELECT _FUNC_(1234567891.1234567891, -4); + 1234560000 + > SELECT _FUNC_(1234567891.1234567891); + 1234567891 + """) +// scalastyle:on line.size.limit +case class Truncate(number: Expression, scale: Expression) + extends BinaryExpression with ImplicitCastInputTypes { + + override def left: Expression = number + override def right: Expression = scale + + override def inputTypes: Seq[AbstractDataType] = + Seq(TypeCollection(DoubleType, DecimalType), IntegerType) + + override def dataType: DataType = left.dataType + + private lazy val foldableTruncScale: Int = scale.eval().asInstanceOf[Int] + + protected override def nullSafeEval(input1: Any, input2: Any): Any = { + val truncScale = if (scale.foldable) { + foldableTruncScale + } else { + scale.eval().asInstanceOf[Int] + } + number.dataType match { + case DoubleType => MathUtils.trunc(input1.asInstanceOf[Double], truncScale) + case DecimalType.Fixed(_, _) => + MathUtils.trunc(input1.asInstanceOf[Decimal].toJavaBigDecimal, truncScale) --- End diff -- I guess we have to return `Decimal` instead of `java.math.BigDecimal`?
--- --------------------------------------------------------------------- To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org For additional commands, e-mail: reviews-h...@spark.apache.org