Skip to content

Commit 5b65d8a

Browse files
viiryadongjoon-hyun
authored andcommitted
[SPARK-35347][SQL] Use MethodUtils for looking up methods in Invoke and StaticInvoke
### What changes were proposed in this pull request? This patch proposes to use `MethodUtils` for looking up methods `Invoke` and `StaticInvoke` expressions. ### Why are the changes needed? Currently we wrote our logic in `Invoke` and `StaticInvoke` expressions for looking up methods. It is tricky to consider all the cases and there is already existing utility package for this purpose. We should reuse the utility package. ### Does this PR introduce _any_ user-facing change? No, internal change only. ### How was this patch tested? Existing tests. Closes #32474 from viirya/invoke-util. Authored-by: Liang-Chi Hsieh <viirya@gmail.com> Signed-off-by: Dongjoon Hyun <dongjoon@apache.org>
1 parent e31bef1 commit 5b65d8a

File tree

1 file changed

+7
-24
lines changed
  • sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/objects

1 file changed

+7
-24
lines changed

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/objects/objects.scala

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ import scala.collection.mutable.{Builder, WrappedArray}
2424
import scala.reflect.ClassTag
2525
import scala.util.{Properties, Try}
2626

27+
import org.apache.commons.lang3.reflect.MethodUtils
28+
2729
import org.apache.spark.{SparkConf, SparkEnv}
2830
import org.apache.spark.serializer._
2931
import org.apache.spark.sql.Row
@@ -147,30 +149,11 @@ trait InvokeLike extends Expression with NonSQLExpression {
147149
}
148150

149151
final def findMethod(cls: Class[_], functionName: String, argClasses: Seq[Class[_]]): Method = {
150-
// Looking with function name + argument classes first.
151-
try {
152-
cls.getMethod(functionName, argClasses: _*)
153-
} catch {
154-
case _: NoSuchMethodException =>
155-
// For some cases, e.g. arg class is Object, `getMethod` cannot find the method.
156-
// We look at function name + argument length
157-
val m = cls.getMethods.filter { m =>
158-
m.getName == functionName && m.getParameterCount == arguments.length
159-
}
160-
if (m.isEmpty) {
161-
sys.error(s"Couldn't find $functionName on $cls")
162-
} else if (m.length > 1) {
163-
// More than one matched method signature. Exclude synthetic one, e.g. generic one.
164-
val realMethods = m.filter(!_.isSynthetic)
165-
if (realMethods.length > 1) {
166-
// Ambiguous case, we don't know which method to choose, just fail it.
167-
sys.error(s"Found ${realMethods.length} $functionName on $cls")
168-
} else {
169-
realMethods.head
170-
}
171-
} else {
172-
m.head
173-
}
152+
val method = MethodUtils.getMatchingAccessibleMethod(cls, functionName, argClasses: _*)
153+
if (method == null) {
154+
sys.error(s"Couldn't find $functionName on $cls")
155+
} else {
156+
method
174157
}
175158
}
176159
}

0 commit comments

Comments
 (0)