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-8810] [SQL] Added several UDF unit tests for Spark SQL #7207

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
11 changes: 11 additions & 0 deletions sql/core/src/test/scala/org/apache/spark/sql/TestData.scala
Original file line number Diff line number Diff line change
Expand Up @@ -196,4 +196,15 @@ object TestData {
:: ComplexData(Map("2" -> 2), TestData(2, "2"), Seq(2), false)
:: Nil).toDF()
complexData.registerTempTable("complexData")

case class GroupData(g: String, v: Int)
val groupData =
TestSQLContext.sparkContext.parallelize(
GroupData("red", 1) ::
GroupData("red", 2) ::
GroupData("blue", 10) ::
GroupData("green", 100) ::
GroupData("green", 200) :: Nil).toDF()
groupData.registerTempTable("groupData")

}
43 changes: 43 additions & 0 deletions sql/core/src/test/scala/org/apache/spark/sql/UDFSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package org.apache.spark.sql
case class FunctionResult(f1: String, f2: String)

class UDFSuite extends QueryTest {
import org.apache.spark.sql.TestData._

private lazy val ctx = org.apache.spark.sql.test.TestSQLContext
import ctx.implicits._
Expand Down Expand Up @@ -82,6 +83,48 @@ class UDFSuite extends QueryTest {
assert(ctx.sql("SELECT strLenScala('test', 1)").head().getInt(0) === 5)
}

test("UDF in a WHERE") {
testData.sqlContext.udf.register("oneArgFilter", (n:Int) => { n > 80 })

val result =
testData.sqlContext.sql("SELECT * FROM testData WHERE oneArgFilter(key)")
assert(result.count() === 20)
}

test("UDF in a HAVING") {
testData.sqlContext.udf.register("havingFilter", (n:Long) => { n > 5 })

val result =
testData.sqlContext.sql("SELECT g, SUM(v) as s FROM groupData GROUP BY g HAVING havingFilter(s)")
Copy link
Contributor

Choose a reason for hiding this comment

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

we now prefer just having the dataset closer to the test case rather than putting them in TestData.

You can do something like this easily in each test case itself

val df = Seq(("red", 1), ("red", 2), ("blue", 10), ("green", 100), ("green", 200)).toDF("g", "v")
df.registerTempTable("groupData")

Copy link
Member

Choose a reason for hiding this comment

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

This line exceeds 100 characters and the last test failure is due to this. @spirom , Could you add proper indentation here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed.

assert(result.count() === 2)
}

test("UDF in a GROUP BY") {
testData.sqlContext.udf.register("groupFunction", (n:Int) => { n > 10 })

val result =
testData.sqlContext.sql("SELECT SUM(v) FROM groupData GROUP BY groupFunction(v)")
assert(result.count() === 2)
}

test("UDFs everywhere") {
ctx.udf.register("groupFunction", (n:Int) => { n > 10 })
Copy link
Contributor

Choose a reason for hiding this comment

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

add a space after colon, i.e.

(n: Int) => { n > 10 }

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed.

ctx.udf.register("havingFilter", (n:Long) => { n > 2000 })
ctx.udf.register("whereFilter", (n:Int) => { n < 150 })
ctx.udf.register("timesHundred", (n:Long) => { n * 100 })

val result =
testData.sqlContext.sql(
"""
| SELECT timesHundred(SUM(v)) as v100
| FROM groupData
| WHERE whereFilter(v)
| GROUP BY groupFunction(v)
| HAVING havingFilter(v100)
""".stripMargin)
assert(result.count() === 1)
}

test("struct UDF") {
ctx.udf.register("returnStruct", (f1: String, f2: String) => FunctionResult(f1, f2))

Expand Down