Skip to content

Commit

Permalink
[SPARK-38604][SQL] Keep ceil and floor with only a single argument th…
Browse files Browse the repository at this point in the history
…e same as before

This is just the fix. I didn't add any tests yet. I am happy to do it, I just wasn't sure where the right place to put the tests would be. Once I have tests I will cherry-pick this back to the 3.3 branch and put up a PR for that too. I am also happy to update the comments because this is a bit confusing that there is no indication that things have changed.

Closes #35913 from revans2/ceil_floor_no_arg_behavior.

Authored-by: Robert (Bobby) Evans <bobby@apache.org>
Signed-off-by: Hyukjin Kwon <gurwls223@apache.org>
  • Loading branch information
revans2 authored and HyukjinKwon committed Mar 22, 2022
1 parent a876f00 commit 692e4b0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
8 changes: 6 additions & 2 deletions sql/core/src/main/scala/org/apache/spark/sql/functions.scala
Expand Up @@ -1783,7 +1783,9 @@ object functions {
* @group math_funcs
* @since 1.4.0
*/
def ceil(e: Column): Column = ceil(e, lit(0))
def ceil(e: Column): Column = withExpr {
UnresolvedFunction(Seq("ceil"), Seq(e.expr), isDistinct = false)
}

/**
* Computes the ceiling of the given value of `e` to 0 decimal places.
Expand Down Expand Up @@ -1913,7 +1915,9 @@ object functions {
* @group math_funcs
* @since 1.4.0
*/
def floor(e: Column): Column = floor(e, lit(0))
def floor(e: Column): Column = withExpr {
UnresolvedFunction(Seq("floor"), Seq(e.expr), isDistinct = false)
}

/**
* Computes the floor of the given column value to 0 decimal places.
Expand Down
Expand Up @@ -202,6 +202,13 @@ class MathFunctionsSuite extends QueryTest with SharedSparkSession {

test("ceil and ceiling") {
testOneToOneMathFunction(ceil, (d: Double) => math.ceil(d).toLong)
// testOneToOneMathFunction does not validate the resulting data type
assert(
spark.range(1).select(ceil(col("id")).alias("a")).schema ==
types.StructType(Seq(types.StructField("a", types.LongType))))
assert(
spark.range(1).select(ceil(col("id"), lit(0)).alias("a")).schema ==
types.StructType(Seq(types.StructField("a", types.DecimalType(20, 0)))))
checkAnswer(
sql("SELECT ceiling(0), ceiling(1), ceiling(1.5)"),
Row(0L, 1L, 2L))
Expand Down Expand Up @@ -250,6 +257,13 @@ class MathFunctionsSuite extends QueryTest with SharedSparkSession {

test("floor") {
testOneToOneMathFunction(floor, (d: Double) => math.floor(d).toLong)
// testOneToOneMathFunction does not validate the resulting data type
assert(
spark.range(1).select(floor(col("id")).alias("a")).schema ==
types.StructType(Seq(types.StructField("a", types.LongType))))
assert(
spark.range(1).select(floor(col("id"), lit(0)).alias("a")).schema ==
types.StructType(Seq(types.StructField("a", types.DecimalType(20, 0)))))
}

test("factorial") {
Expand Down

0 comments on commit 692e4b0

Please sign in to comment.