Skip to content

Commit

Permalink
[SPARK-24982][SQL] UDAF resolution should not throw AssertionError
Browse files Browse the repository at this point in the history
## What changes were proposed in this pull request?
When user calls anUDAF with the wrong number of arguments, Spark previously throws an AssertionError, which is not supposed to be a user-facing exception.  This patch updates it to throw AnalysisException instead, so it is consistent with a regular UDF.

## How was this patch tested?
Updated test case udaf.sql.

Author: Reynold Xin <[email protected]>

Closes apache#21938 from rxin/SPARK-24982.
  • Loading branch information
rxin authored and gatorsmile committed Aug 1, 2018
1 parent 1f7e22c commit 1efffb7
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import org.apache.spark.sql.AnalysisException
import org.apache.spark.sql.catalyst._
import org.apache.spark.sql.catalyst.analysis._
import org.apache.spark.sql.catalyst.analysis.FunctionRegistry.FunctionBuilder
import org.apache.spark.sql.catalyst.expressions.{Expression, ExpressionInfo}
import org.apache.spark.sql.catalyst.expressions.{Expression, ExpressionInfo, ImplicitCastInputTypes}
import org.apache.spark.sql.catalyst.parser.{CatalystSqlParser, ParserInterface}
import org.apache.spark.sql.catalyst.plans.logical.{LogicalPlan, SubqueryAlias, View}
import org.apache.spark.sql.catalyst.util.StringUtils
Expand Down Expand Up @@ -1124,13 +1124,22 @@ class SessionCatalog(
name: String,
clazz: Class[_],
input: Seq[Expression]): Expression = {
// Unfortunately we need to use reflection here because UserDefinedAggregateFunction
// and ScalaUDAF are defined in sql/core module.
val clsForUDAF =
Utils.classForName("org.apache.spark.sql.expressions.UserDefinedAggregateFunction")
if (clsForUDAF.isAssignableFrom(clazz)) {
val cls = Utils.classForName("org.apache.spark.sql.execution.aggregate.ScalaUDAF")
cls.getConstructor(classOf[Seq[Expression]], clsForUDAF, classOf[Int], classOf[Int])
val e = cls.getConstructor(classOf[Seq[Expression]], clsForUDAF, classOf[Int], classOf[Int])
.newInstance(input, clazz.newInstance().asInstanceOf[Object], Int.box(1), Int.box(1))
.asInstanceOf[Expression]
.asInstanceOf[ImplicitCastInputTypes]

// Check input argument size
if (e.inputTypes.size != input.size) {
throw new AnalysisException(s"Invalid number of arguments for function $name. " +
s"Expected: ${e.inputTypes.size}; Found: ${input.size}")
}
e
} else {
throw new AnalysisException(s"No handler for UDAF '${clazz.getCanonicalName}'. " +
s"Use sparkSession.udf.register(...) instead.")
Expand Down
4 changes: 2 additions & 2 deletions sql/core/src/test/resources/sql-tests/results/udaf.sql.out
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ SELECT default.myDoubleAvg(int_col1, 3) as my_avg from t1
-- !query 3 schema
struct<>
-- !query 3 output
java.lang.AssertionError
assertion failed: Incorrect number of children
org.apache.spark.sql.AnalysisException
Invalid number of arguments for function default.myDoubleAvg. Expected: 1; Found: 2; line 1 pos 7


-- !query 4
Expand Down

0 comments on commit 1efffb7

Please sign in to comment.