Skip to content

Commit

Permalink
[SPARK-35244][SQL] Invoke should throw the original exception
Browse files Browse the repository at this point in the history
### What changes were proposed in this pull request?

This PR updates the interpreted code path of invoke expressions, to unwrap the `InvocationTargetException`

### Why are the changes needed?

Make interpreted and codegen path consistent for invoke expressions.

### Does this PR introduce _any_ user-facing change?

no

### How was this patch tested?

new UT

Closes #32370 from cloud-fan/minor.

Authored-by: Wenchen Fan <[email protected]>
Signed-off-by: hyukjinkwon <[email protected]>
  • Loading branch information
cloud-fan committed Apr 28, 2021
1 parent cb1d802 commit e58055b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,12 @@ trait InvokeLike extends Expression with NonSQLExpression {
// return null if one of arguments is null
null
} else {
val ret = method.invoke(obj, args: _*)
val ret = try {
method.invoke(obj, args: _*)
} catch {
// Re-throw the original exception.
case e: java.lang.reflect.InvocationTargetException => throw e.getCause
}
val boxedClass = ScalaReflection.typeBoxedJavaMapping.get(dataType)
if (boxedClass.isDefined) {
boxedClass.get.cast(ret)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,16 @@ class ObjectExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper {
checkExceptionInExpression[RuntimeException](
serializer4, EmptyRow, "Cannot use null as map key!")
}

test("SPARK-35244: invoke should throw the original exception") {
val strClsType = ObjectType(classOf[String])
checkExceptionInExpression[StringIndexOutOfBoundsException](
Invoke(Literal("a", strClsType), "substring", strClsType, Seq(Literal(3))), "")

val mathCls = classOf[Math]
checkExceptionInExpression[ArithmeticException](
StaticInvoke(mathCls, IntegerType, "addExact", Seq(Literal(Int.MaxValue), Literal(1))), "")
}
}

class TestBean extends Serializable {
Expand Down

0 comments on commit e58055b

Please sign in to comment.