Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SPARK-23906][SQL] Add built-in UDF TRUNCATE(number) #22419

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ object FunctionRegistry {
expression[Tan]("tan"),
expression[Cot]("cot"),
expression[Tanh]("tanh"),
expression[Truncate]("truncate"),

expression[Add]("+"),
expression[Subtract]("-"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1245,3 +1245,27 @@ 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(child: Expression, scale: Expression)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am still preferring to extend trunc. Not straightforward to know the difference between truncate and trunc

Copy link
Member

@ueshin ueshin Oct 4, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @wangyum, can you still extend trunc?
If not, what were the major reasons you decided to separate these? Thanks!

Copy link
Member Author

@wangyum wangyum Oct 4, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't know we should trunc StringType to number or trunc to date.
For example:

SELECT trunc('2.5');
SELECT trunc('2009-02-12');

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In that case, its ok to handle the string as date. How about only accepting float, double, and decimal for number truncation?

extends RoundBase(child, scale, BigDecimal.RoundingMode.DOWN, "ROUND_DOWN")
with Serializable with ImplicitCastInputTypes {
def this(child: Expression) = this(child, Literal(0))
}
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@ final class Decimal extends Ordered[Decimal] with Serializable {
if (doubled > pow10diff || doubled == pow10diff && longVal % 2 != 0) {
longVal += (if (droppedDigits < 0) -1L else 1L)
}
case ROUND_DOWN =>
case _ =>
sys.error(s"Not supported rounding mode: $roundMode")
}
Expand Down Expand Up @@ -413,6 +414,7 @@ object Decimal {
val ROUND_HALF_EVEN = BigDecimal.RoundingMode.HALF_EVEN
val ROUND_CEILING = BigDecimal.RoundingMode.CEILING
val ROUND_FLOOR = BigDecimal.RoundingMode.FLOOR
val ROUND_DOWN = BigDecimal.RoundingMode.DOWN
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need this change, otherwise:

Caused by: org.codehaus.commons.compiler.CompileException: File 'generated.java', Line 41, Column 138: failed to compile: org.codehaus.commons.compiler.CompileException: File 'generated.java', Line 41, Column 138: A method named "ROUND_DOWN" is not declared in any enclosing class nor any supertype, nor through a static import

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess we need to modify Decimal.changePrecision() as well? Could you add it to DecimalSuite.scala#L207 to check?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added it and seem do not need change longVal. This test case can passed.


/** Maximum number of decimal digits an Int can represent */
val MAX_INT_DIGITS = 9
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -644,4 +644,59 @@ class MathExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper {
checkEvaluation(BRound(-0.35, 1), -0.4)
checkEvaluation(BRound(-35, -1), -40)
}

test("Truncate number") {
assert(Truncate(Literal.create(123.123, DoubleType),
NonFoldableLiteral.create(1, IntegerType)).checkInputDataTypes().isFailure)
assert(Truncate(Literal.create(123.123, DoubleType),
Literal.create(1, IntegerType)).checkInputDataTypes().isSuccess)

def testDouble(input: Any, scale: Any, expected: Any): Unit = {
checkEvaluation(Truncate(Literal.create(input, DoubleType),
Literal.create(scale, IntegerType)),
expected)
}

def testFloat(input: Any, scale: Any, expected: Any): Unit = {
checkEvaluation(Truncate(Literal.create(input, FloatType),
Literal.create(scale, IntegerType)),
expected)
}

def testDecimal(input: Any, scale: Any, expected: Any): Unit = {
checkEvaluation(Truncate(Literal.create(input, DecimalType.DoubleDecimal),
Literal.create(scale, IntegerType)),
expected)
}

testDouble(1234567891.1234567891D, 4, 1234567891.1234D)
testDouble(1234567891.1234567891D, -4, 1234560000D)
testDouble(1234567891.1234567891D, 0, 1234567891D)
testDouble(0.123D, -1, 0D)
testDouble(0.123D, 0, 0D)
testDouble(null, null, null)
testDouble(null, 0, null)
testDouble(1D, null, null)
testDouble(-1234567891.1234567891D, 4, -1234567891.1234D)

testFloat(1234567891.1234567891F, 4, 1234567891.1234F)
testFloat(1234567891.1234567891F, -4, 1234560000F)
testFloat(1234567891.1234567891F, 0, 1234567891F)
testFloat(0.123F, -1, 0F)
testFloat(0.123F, 0, 0F)
testFloat(null, null, null)
testFloat(null, 0, null)
testFloat(1F, null, null)
testFloat(-1234567891.1234567891F, 4, -1234567891.1234F)

testDecimal(Decimal(1234567891.1234567891), 4, Decimal(1234567891.1234))
testDecimal(Decimal(-1234567891.1234567891), 4, Decimal(-1234567891.1234))
testDecimal(Decimal(1234567891.1234567891), -4, Decimal(1234560000))
testDecimal(Decimal(1234567891.1234567891), 0, Decimal(1234567891))
testDecimal(Decimal(0.123), -1, Decimal(0))
testDecimal(Decimal(0.123), 0, Decimal(0))
testDecimal(null, null, null)
testDecimal(null, 0, null)
testDecimal(Decimal(1), null, null)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ class DecimalSuite extends SparkFunSuite with PrivateMethodTester {
}

test("changePrecision/toPrecision on compact decimal should respect rounding mode") {
Seq(ROUND_FLOOR, ROUND_CEILING, ROUND_HALF_UP, ROUND_HALF_EVEN).foreach { mode =>
Seq(ROUND_FLOOR, ROUND_CEILING, ROUND_HALF_UP, ROUND_HALF_EVEN, ROUND_DOWN).foreach { mode =>
Seq("0.4", "0.5", "0.6", "1.0", "1.1", "1.6", "2.5", "5.5").foreach { n =>
Seq("", "-").foreach { sign =>
val bd = BigDecimal(sign + n)
Expand Down
20 changes: 20 additions & 0 deletions sql/core/src/main/scala/org/apache/spark/sql/functions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2214,6 +2214,26 @@ object functions {
*/
def radians(columnName: String): Column = radians(Column(columnName))

/**
* Returns the value of the column `e` truncated to 0 places.
*
* @group math_funcs
* @since 2.5.0
*/
def truncate(e: Column): Column = truncate(e, 0)

/**
* Returns the value of column `e` truncated to the unit specified by the scale.
* If scale is omitted, then the value of column `e` is truncated to 0 places.
* Scale can be negative to truncate (make zero) scale digits left of the decimal point.
*
* @group math_funcs
* @since 2.5.0
*/
def truncate(e: Column, scale: Int): Column = withExpr {
Truncate(e.expr, Literal(scale))
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need def truncate(number: Column) to support omitting scale?


//////////////////////////////////////////////////////////////////////////////////////////////
// Misc functions
//////////////////////////////////////////////////////////////////////////////////////////////
Expand Down
9 changes: 9 additions & 0 deletions sql/core/src/test/resources/sql-tests/inputs/operators.sql
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,12 @@ select positive('-1.11'), positive(-1.11), negative('-1.11'), negative(-1.11);
-- pmod
select pmod(-7, 2), pmod(0, 2), pmod(7, 0), pmod(7, null), pmod(null, 2), pmod(null, null);
select pmod(cast(3.13 as decimal), cast(0 as decimal)), pmod(cast(2 as smallint), cast(0 as smallint));

-- truncate
select truncate(cast(1234567891.1234567891 as double), -4), truncate(cast(1234567891.1234567891 as double), 0), truncate(cast(1234567891.1234567891 as double), 4);
select truncate(cast(1234567891.1234567891 as float), -4), truncate(cast(1234567891.1234567891 as float), 0), truncate(cast(1234567891.1234567891 as float), 4);
select truncate(cast(1234567891.1234567891 as decimal), -4), truncate(cast(1234567891.1234567891 as decimal), 0), truncate(cast(1234567891.1234567891 as decimal), 4);
select truncate(cast(1234567891.1234567891 as long), -4), truncate(cast(1234567891.1234567891 as long), 0), truncate(cast(1234567891.1234567891 as long), 4);
select truncate(cast(1234567891.1234567891 as long), 9.03);
select truncate(cast(1234567891.1234567891 as double)), truncate(cast(1234567891.1234567891 as float)), truncate(cast(1234567891.1234567891 as decimal));
select truncate(cast(-1234567891.1234567891 as double), -4), truncate(cast(-1234567891.1234567891 as double), 4);
58 changes: 57 additions & 1 deletion sql/core/src/test/resources/sql-tests/results/operators.sql.out
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
-- Automatically generated by SQLQueryTestSuite
-- Number of queries: 55
-- Number of queries: 62


-- !query 0
Expand Down Expand Up @@ -452,3 +452,59 @@ select pmod(cast(3.13 as decimal), cast(0 as decimal)), pmod(cast(2 as smallint)
struct<pmod(CAST(3.13 AS DECIMAL(10,0)), CAST(0 AS DECIMAL(10,0))):decimal(10,0),pmod(CAST(2 AS SMALLINT), CAST(0 AS SMALLINT)):smallint>
-- !query 54 output
NULL NULL


-- !query 55
select truncate(cast(1234567891.1234567891 as double), -4), truncate(cast(1234567891.1234567891 as double), 0), truncate(cast(1234567891.1234567891 as double), 4)
-- !query 55 schema
struct<truncate(CAST(1234567891.1234567891 AS DOUBLE), -4):double,truncate(CAST(1234567891.1234567891 AS DOUBLE), 0):double,truncate(CAST(1234567891.1234567891 AS DOUBLE), 4):double>
-- !query 55 output
1.23456E9 1.234567891E9 1.2345678911234E9


-- !query 56
select truncate(cast(1234567891.1234567891 as float), -4), truncate(cast(1234567891.1234567891 as float), 0), truncate(cast(1234567891.1234567891 as float), 4)
-- !query 56 schema
struct<truncate(CAST(1234567891.1234567891 AS FLOAT), -4):float,truncate(CAST(1234567891.1234567891 AS FLOAT), 0):float,truncate(CAST(1234567891.1234567891 AS FLOAT), 4):float>
-- !query 56 output
1.23456E9 1.23456794E9 1.23456794E9


-- !query 57
select truncate(cast(1234567891.1234567891 as decimal), -4), truncate(cast(1234567891.1234567891 as decimal), 0), truncate(cast(1234567891.1234567891 as decimal), 4)
-- !query 57 schema
struct<truncate(CAST(1234567891.1234567891 AS DECIMAL(10,0)), -4):decimal(10,-4),truncate(CAST(1234567891.1234567891 AS DECIMAL(10,0)), 0):decimal(10,0),truncate(CAST(1234567891.1234567891 AS DECIMAL(10,0)), 4):decimal(10,0)>
-- !query 57 output
1234560000 1234567891 1234567891


-- !query 58
select truncate(cast(1234567891.1234567891 as long), -4), truncate(cast(1234567891.1234567891 as long), 0), truncate(cast(1234567891.1234567891 as long), 4)
-- !query 58 schema
struct<truncate(CAST(1234567891.1234567891 AS BIGINT), -4):bigint,truncate(CAST(1234567891.1234567891 AS BIGINT), 0):bigint,truncate(CAST(1234567891.1234567891 AS BIGINT), 4):bigint>
-- !query 58 output
1234560000 1234567891 1234567891


-- !query 59
select truncate(cast(1234567891.1234567891 as long), 9.03)
-- !query 59 schema
struct<truncate(CAST(1234567891.1234567891 AS BIGINT), CAST(9.03 AS INT)):bigint>
-- !query 59 output
1234567891


-- !query 60
select truncate(cast(1234567891.1234567891 as double)), truncate(cast(1234567891.1234567891 as float)), truncate(cast(1234567891.1234567891 as decimal))
-- !query 60 schema
struct<truncate(CAST(1234567891.1234567891 AS DOUBLE), 0):double,truncate(CAST(1234567891.1234567891 AS FLOAT), 0):float,truncate(CAST(1234567891.1234567891 AS DECIMAL(10,0)), 0):decimal(10,0)>
-- !query 60 output
1.234567891E9 1.23456794E9 1234567891


-- !query 61
select truncate(cast(-1234567891.1234567891 as double), -4), truncate(cast(-1234567891.1234567891 as double), 4)
-- !query 61 schema
struct<truncate(CAST(-1234567891.1234567891 AS DOUBLE), -4):double,truncate(CAST(-1234567891.1234567891 AS DOUBLE), 4):double>
-- !query 61 output
-1.23456E9 -1.2345678911234E9