MaxGekk commented on code in PR #36064:
URL: https://github.com/apache/spark/pull/36064#discussion_r846764249


##########
sql/core/src/test/scala/org/apache/spark/sql/errors/QueryCompilationErrorsSuite.scala:
##########
@@ -171,4 +174,113 @@ class QueryCompilationErrorsSuite extends QueryTest with 
SharedSparkSession {
       "The feature is not supported: " +
       "Pandas UDF aggregate expressions don't support pivot.")
   }
+
+  test("NO_HANDLER_FOR_UDAF: No handler for UDAF error") {
+    val functionName = "myCast"
+    withUserDefinedFunction(functionName -> true) {
+      sql(
+        s"""
+          |CREATE TEMPORARY FUNCTION $functionName
+          |AS 'org.apache.spark.sql.errors.MyCastToString'
+          |""".stripMargin)
+
+      val e = intercept[AnalysisException] (
+        sql(s"SELECT $functionName(123) as value")
+      )
+
+      assert(e.errorClass === Some("NO_HANDLER_FOR_UDAF"))
+      assert(e.message ===
+        "No handler for UDAF 'org.apache.spark.sql.errors.MyCastToString'. " +
+        "Use sparkSession.udf.register(...) instead.")
+    }
+  }
+
+  test("UNTYPED_SCALA_UDF: use untyped Scala UDF should fail by default") {
+    val e = intercept[AnalysisException](udf((x: Int) => x, IntegerType))
+
+    assert(e.errorClass === Some("UNTYPED_SCALA_UDF"))
+    assert(e.message ===
+      "You're using untyped Scala UDF, which does not have the input type " +
+      "information. Spark may blindly pass null to the Scala closure with 
primitive-type " +
+      "argument, and the closure will see the default value of the Java type 
for the null " +
+      "argument, e.g. `udf((x: Int) => x, IntegerType)`, the result is 0 for 
null input. " +
+      "To get rid of this error, you could:\n" +
+      "1. use typed Scala UDF APIs(without return type parameter), e.g. 
`udf((x: Int) => x)`\n" +
+      "2. use Java UDF APIs, e.g. `udf(new UDF1[String, Integer] { " +
+      "override def call(s: String): Integer = s.length() }, IntegerType)`, " +
+      "if input types are all non primitive\n" +
+      s"3. set ${SQLConf.LEGACY_ALLOW_UNTYPED_SCALA_UDF.key} to true and " +
+      s"use this API with caution")
+  }
+
+  test("NO_UDF_INTERFACE_ERROR: java udf class does not implement any udf 
interface") {
+    val className = "org.apache.spark.sql.errors.MyCastToString"
+    val e = intercept[AnalysisException](
+      spark.udf.registerJava(
+        "myCast",
+        className,
+        StringType)
+    )
+
+    assert(e.errorClass === Some("NO_UDF_INTERFACE_ERROR"))
+    assert(e.message ===
+      s"UDF class $className doesn't implement any UDF interface")
+  }
+
+  test("MULTI_UDF_INTERFACE_ERROR: java udf implement multi UDF interface") {
+    val className = "org.apache.spark.sql.errors.MySum"
+    val e = intercept[AnalysisException](
+      spark.udf.registerJava(
+        "mySum",
+        className,
+        StringType)
+    )
+
+    assert(e.errorClass === Some("MULTI_UDF_INTERFACE_ERROR"))
+    assert(e.message ===
+      s"Not allowed to implement multiple UDF interfaces, UDF class 
$className")
+  }
+
+  test("UDF_WITH_TOO_MANY_TYPE_ARGUMENT_ERROR: java udf with too many type 
arguments") {

Review Comment:
   ```suggestion
     test("UNSUPPORTED_FEATURE: java udf with too many type arguments") {
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org

Reply via email to