This is an automated email from the ASF dual-hosted git repository.
uros-b pushed a commit to branch branch-4.x
in repository https://gitbox.apache.org/repos/asf/spark.git
The following commit(s) were added to refs/heads/branch-4.x by this push:
new b9493ec28768 [SPARK-58169][SQL] Use java reflection for finding the
SparkSession companions
b9493ec28768 is described below
commit b9493ec28768585f764cb7d1be4d509c11143876
Author: Emil Ejbyfeldt <[email protected]>
AuthorDate: Sun Aug 23 13:38:34 2026 +0000
[SPARK-58169][SQL] Use java reflection for finding the SparkSession
companions
### What changes were proposed in this pull request?
Use java reflection over scala reflection for getting the companion object
instances from a class.
### Why are the changes needed?
Because of this issue scala/scala3#25896 scala reflection is broken if
using Spark from Scala 3.8.0 or later. By not depending on scala reflection we
remove this limitation.
### Does this PR introduce _any_ user-facing change?
No.
### How was this patch tested?
Existing tests ensure no regressions. That it makes the code more
compatible with Scala 3.8 was done by running a local build against the tests
in https://github.com/unum-io/tyda (a Scala 3 library that can use Spark as a
backend).
### Was this patch authored or co-authored using generative AI tooling?
No.
Closes #57302 from eejbyfeldt/SPARK-58169.
Authored-by: Emil Ejbyfeldt <[email protected]>
Signed-off-by: Uros Bojanic <[email protected]>
(cherry picked from commit 3c9ffb687997e90ebf6b8b7621a9012d1eeda1a6)
Signed-off-by: Uros Bojanic <[email protected]>
---
.../src/main/scala/org/apache/spark/util/SparkClassUtils.scala | 10 ++++++++++
sql/api/src/main/scala/org/apache/spark/sql/SparkSession.scala | 5 +----
2 files changed, 11 insertions(+), 4 deletions(-)
diff --git
a/common/utils/src/main/scala/org/apache/spark/util/SparkClassUtils.scala
b/common/utils/src/main/scala/org/apache/spark/util/SparkClassUtils.scala
index 3f22719240c0..8b50c3da5333 100644
--- a/common/utils/src/main/scala/org/apache/spark/util/SparkClassUtils.scala
+++ b/common/utils/src/main/scala/org/apache/spark/util/SparkClassUtils.scala
@@ -164,6 +164,16 @@ private[spark] trait SparkClassUtils {
currentClass = currentClass.getSuperclass
}
}
+
+ /**
+ * Returns the companion object for the given class.
+ */
+ def getCompanionObject(name: String): Any = {
+ val companionCls = classForName(name + "$")
+ // The companion object instance is held in a static MODULE$ field
+ val moduleField = companionCls.getField("MODULE$")
+ moduleField.get(null)
+ }
}
private[spark] object SparkClassUtils extends SparkClassUtils
diff --git a/sql/api/src/main/scala/org/apache/spark/sql/SparkSession.scala
b/sql/api/src/main/scala/org/apache/spark/sql/SparkSession.scala
index a17a5dcac9e8..d6444feffc0c 100644
--- a/sql/api/src/main/scala/org/apache/spark/sql/SparkSession.scala
+++ b/sql/api/src/main/scala/org/apache/spark/sql/SparkSession.scala
@@ -829,10 +829,7 @@ object SparkSession extends SparkSessionCompanion {
}
private[this] def lookupCompanion(name: String): SparkSessionCompanion = {
- val cls = SparkClassUtils.classForName(name)
- val mirror = scala.reflect.runtime.currentMirror
- val module = mirror.classSymbol(cls).companion.asModule
- mirror.reflectModule(module).instance.asInstanceOf[SparkSessionCompanion]
+
SparkClassUtils.getCompanionObject(name).asInstanceOf[SparkSessionCompanion]
}
/** @inheritdoc */
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]