Skip to content

Commit 1bd62eb

Browse files
committed
[spark] Fix memstream reflection picking wrong MemoryStream.apply overload
1 parent b7bc84c commit 1bd62eb

File tree

1 file changed

+12
-3
lines changed
  • paimon-spark/paimon-spark4-common/src/main/scala/org/apache/spark/sql/paimon/shims

1 file changed

+12
-3
lines changed

paimon-spark/paimon-spark4-common/src/main/scala/org/apache/spark/sql/paimon/shims/memstream.scala

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,20 @@ object memstream {
4848
object MemoryStream {
4949
def apply[A: Encoder](implicit sqlContext: SQLContext): MemoryStream[A] = {
5050
val companion = loadCompanion()
51+
// Spark 4.1 added a second 2-arg `apply(Encoder, SparkSession)` overload alongside the
52+
// existing `apply(Encoder, SQLContext)`, so filtering only on `parameterCount == 2` is
53+
// ambiguous — `Class#getMethods` ordering is JVM-dependent, and picking the wrong overload
54+
// produces `IllegalArgumentException: argument type mismatch` when we pass a `SQLContext`.
55+
// Pin the selection to the `(Encoder, SQLContext)` variant explicitly.
5156
val applyMethod = companion.getClass.getMethods
52-
.filter(m => m.getName == "apply" && m.getParameterCount == 2)
53-
.headOption
57+
.find {
58+
m =>
59+
m.getName == "apply" &&
60+
m.getParameterCount == 2 &&
61+
m.getParameterTypes()(1) == classOf[SQLContext]
62+
}
5463
.getOrElse(throw new NoSuchMethodError(
55-
"No 2-arg MemoryStream.apply(Encoder, SQLContext) found on " + companion.getClass))
64+
"No apply(Encoder, SQLContext) found on " + companion.getClass))
5665
val encoder = implicitly[Encoder[A]].asInstanceOf[AnyRef]
5766
val underlying = applyMethod.invoke(companion, encoder, sqlContext).asInstanceOf[AnyRef]
5867
new ReflectiveMemoryStream[A](underlying)

0 commit comments

Comments
 (0)