Skip to content

Commit 9be894e

Browse files
committed
add round functions in o.a.s.sql.functions
1 parent 7c83e13 commit 9be894e

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

sql/core/src/main/scala/org/apache/spark/sql/functions.scala

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1385,6 +1385,30 @@ object functions {
13851385
*/
13861386
def rint(columnName: String): Column = rint(Column(columnName))
13871387

1388+
/**
1389+
* Returns the value of the `e` rounded to 0 decimal places.
1390+
*
1391+
* @group math_funcs
1392+
* @since 1.5.0
1393+
*/
1394+
def round(e: Column): Column = Round(Seq(e.expr))
1395+
1396+
/**
1397+
* Returns the value of `e` rounded to the value of `scale` decimal places.
1398+
*
1399+
* @group math_funcs
1400+
* @since 1.5.0
1401+
*/
1402+
def round(e: Column, scale: Column): Column = Round(Seq(e.expr, scale.expr))
1403+
1404+
/**
1405+
* Returns the value of `e` rounded to `scale` decimal places.
1406+
*
1407+
* @group math_funcs
1408+
* @since 1.5.0
1409+
*/
1410+
def round(e: Column, scale: Int): Column = round(e, lit(scale))
1411+
13881412
/**
13891413
* Shift the the given value numBits left. If the given value is a long value, this function
13901414
* will return a long value else it will return an integer value.

sql/core/src/test/scala/org/apache/spark/sql/MathExpressionsSuite.scala

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,16 @@ class MathExpressionsSuite extends QueryTest {
198198
testOneToOneMathFunction(rint, math.rint)
199199
}
200200

201+
test("round") {
202+
checkAnswer(
203+
ctx.sql("SELECT round(-32768), round(1809242.3151111344, 9), round(1809242.3151111344, 9)"),
204+
Seq((1, 2)).toDF().select(
205+
round(lit(-32768)),
206+
round(lit(1809242.3151111344), lit(9)),
207+
round(lit(1809242.3151111344), 9))
208+
)
209+
}
210+
201211
test("exp") {
202212
testOneToOneMathFunction(exp, math.exp)
203213
}

0 commit comments

Comments
 (0)