Github user yhuai commented on a diff in the pull request:
https://github.com/apache/spark/pull/9393#discussion_r43577572
--- Diff: sql/core/src/test/scala/org/apache/spark/sql/UDFSuite.scala ---
@@ -191,4 +193,86 @@ class UDFSuite extends QueryTest with SharedSQLContext
{
// pass a decimal to intExpected.
assert(sql("SELECT intExpected(1.0)").head().getInt(0) === 1)
}
+
+ private def checkNumUDFs(df: DataFrame, expectedNumUDFs: Int): Unit = {
+ val udfs = df.queryExecution.optimizedPlan.collect {
+ case p: logical.Project => p.projectList.flatMap {
+ case e => e.collect {
+ case udf: ScalaUDF => udf
+ }
+ }
+ }.flatMap(functions => functions)
+ assert(udfs.length === expectedNumUDFs)
+ }
+
+ test("nondeterministic udf: using UDFRegistration") {
+ import org.apache.spark.sql.functions._
+
+ val deterministicUDF = sqlContext.udf.register("plusOne1", (x: Int) =>
x + 1)
+ val nondeterministicUDF = deterministicUDF.nonDeterministic
+ sqlContext.udf.register("plusOne2", nondeterministicUDF)
+
+ {
+ val df = sql("SELECT 1 as a")
+ .select(col("a"), deterministicUDF(col("a")).as("b"))
+ .select(col("a"), col("b"), deterministicUDF(col("b")).as("c"))
+ checkNumUDFs(df, 3)
+ checkAnswer(df, Row(1, 2, 3))
+ }
+
+ {
+ val df = sql("SELECT 1 as a")
+ .select(col("a"), callUDF("plusOne1", col("a")).as("b"))
+ .select(col("a"), col("b"), callUDF("plusOne1", col("b")).as("c"))
+ checkNumUDFs(df, 3)
+ checkAnswer(df, Row(1, 2, 3))
+ }
+
+ {
+ val df = sql("SELECT 1 as a")
+ .select(col("a"), nondeterministicUDF(col("a")).as("b"))
+ .select(col("a"), col("b"), nondeterministicUDF(col("b")).as("c"))
+ checkNumUDFs(df, 2)
+ checkAnswer(df, Row(1, 2, 3))
+ }
+
+ {
+ val df = sql("SELECT 1 as a")
+ .select(col("a"), callUDF("plusOne2", col("a")).as("b"))
+ .select(col("a"), col("b"), callUDF("plusOne2", col("b")).as("c"))
+ checkNumUDFs(df, 2)
+ checkAnswer(df, Row(1, 2, 3))
+ }
+ }
+
+ test("nondeterministic udf: using udf function") {
+ import org.apache.spark.sql.functions._
+
+ val deterministicUDF = udf((x: Int) => x + 1)
+ val nondeterministicUDF = deterministicUDF.nonDeterministic
+
+ {
+ val df = sql("SELECT 1 as a")
+ .select(col("a"), deterministicUDF(col("a")).as("b"))
+ .select(col("a"), col("b"), deterministicUDF(col("b")).as("c"))
+ checkNumUDFs(df, 3)
+ checkAnswer(df, Row(1, 2, 3))
+ }
+
+ {
+ val df = sql("SELECT 1 as a")
+ .select(col("a"), nondeterministicUDF(col("a")).as("b"))
+ .select(col("a"), col("b"), nondeterministicUDF(col("b")).as("c"))
+ checkNumUDFs(df, 2)
+ checkAnswer(df, Row(1, 2, 3))
+ }
+ }
--- End diff --
@mengxr Here are two examples to define nondeterministic UDFs.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]