uros-b commented on code in PR #57476:
URL: https://github.com/apache/spark/pull/57476#discussion_r3700460927
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/mathExpressions.scala:
##########
@@ -1971,6 +1971,47 @@ case class BRound(
newLeft: Expression, newRight: Expression): BRound = copy(child = newLeft,
scale = newRight)
}
+/**
+ * Truncate an expression toward zero to `scale` decimal places.
+ * A negative `scale` truncates digits to the left of the decimal point.
+ * truncate(1234.5678, 2) = 1234.56, truncate(-1234.5678, 2) = -1234.56.
+ */
+// scalastyle:off line.size.limit
+@ExpressionDescription(
+ usage = "_FUNC_(expr[, scale]) - Returns `expr` truncated toward zero to
`scale` decimal places. `scale` defaults to 0. A negative `scale` truncates
digits to the left of the decimal point.",
+ arguments = """
+ Arguments:
+ * expr - The expression to truncate. An expression that evaluates to a
numeric.
+ * scale - The number of decimal places to keep. An expression that
evaluates to an integer, must be a constant, and defaults to 0. A negative
value truncates digits to the left of the decimal point.
+ """,
+ examples = """
+ Examples:
+ > SELECT _FUNC_(1234.5678, 2);
+ 1234.56
+ > SELECT _FUNC_(1234.5678, -2);
+ 1200
+ > SELECT _FUNC_(-1234.5678, 2);
+ -1234.56
+ """,
+ since = "4.3.0",
+ group = "math_funcs")
+// scalastyle:on line.size.limit
+case class Truncate(
+ child: Expression,
+ scale: Expression,
+ override val ansiEnabled: Boolean = SQLConf.get.ansiEnabled)
+ extends RoundBase(child, scale, BigDecimal.RoundingMode.DOWN, "ROUND_DOWN") {
Review Comment:
RoundBase.dataType widens decimals by one integral digit because rounding
can carry (ceil(9.9, 0) = 10). Truncation never can, so truncate(1234.5678, 2)
returns decimal(7,2) where decimal(6,2) would do. Inheriting this is the safe
choice, but a comment noting it's deliberate would help the next reader, since
it can matter at MAX_PRECISION.
--
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.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]