diff --git a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/objects/objects.scala b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/objects/objects.scala index 76ba52397bd93..b93e2e6ce83ae 100644 --- a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/objects/objects.scala +++ b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/objects/objects.scala @@ -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) diff --git a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/ObjectExpressionsSuite.scala b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/ObjectExpressionsSuite.scala index bc2b93e5390da..6e71c95d344c3 100644 --- a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/ObjectExpressionsSuite.scala +++ b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/ObjectExpressionsSuite.scala @@ -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 {