Skip to content

Commit

Permalink
add round functions in o.a.s.sql.functions
Browse files Browse the repository at this point in the history
  • Loading branch information
yjshen committed Jul 14, 2015
1 parent 7c83e13 commit 9be894e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
24 changes: 24 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 @@ -1385,6 +1385,30 @@ object functions {
*/
def rint(columnName: String): Column = rint(Column(columnName))

/**
* Returns the value of the `e` rounded to 0 decimal places.
*
* @group math_funcs
* @since 1.5.0
*/
def round(e: Column): Column = Round(Seq(e.expr))

/**
* Returns the value of `e` rounded to the value of `scale` decimal places.
*
* @group math_funcs
* @since 1.5.0
*/
def round(e: Column, scale: Column): Column = Round(Seq(e.expr, scale.expr))

/**
* Returns the value of `e` rounded to `scale` decimal places.
*
* @group math_funcs
* @since 1.5.0
*/
def round(e: Column, scale: Int): Column = round(e, lit(scale))

/**
* Shift the the given value numBits left. If the given value is a long value, this function
* will return a long value else it will return an integer value.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,16 @@ class MathExpressionsSuite extends QueryTest {
testOneToOneMathFunction(rint, math.rint)
}

test("round") {
checkAnswer(
ctx.sql("SELECT round(-32768), round(1809242.3151111344, 9), round(1809242.3151111344, 9)"),
Seq((1, 2)).toDF().select(
round(lit(-32768)),
round(lit(1809242.3151111344), lit(9)),
round(lit(1809242.3151111344), 9))
)
}

test("exp") {
testOneToOneMathFunction(exp, math.exp)
}
Expand Down

0 comments on commit 9be894e

Please sign in to comment.