Skip to content

Commit 1a537a2

Browse files
ericlgatorsmile
authored andcommitted
[SPARK-23809][SQL][BACKPORT] Active SparkSession should be set by getOrCreate
This backports #20927 to branch-2.3 ## What changes were proposed in this pull request? Currently, the active spark session is set inconsistently (e.g., in createDataFrame, prior to query execution). Many places in spark also incorrectly query active session when they should be calling activeSession.getOrElse(defaultSession) and so might get None even if a Spark session exists. The semantics here can be cleaned up if we also set the active session when the default session is set. Related: https://github.com/apache/spark/pull/20926/files ## How was this patch tested? Unit test, existing test. Note that if #20926 merges first we should also update the tests there. Author: Eric Liang <[email protected]> Closes #20971 from ericl/backport-23809.
1 parent ccc4a20 commit 1a537a2

File tree

4 files changed

+14
-1
lines changed

4 files changed

+14
-1
lines changed

sql/core/src/main/scala/org/apache/spark/sql/SparkSession.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -951,7 +951,8 @@ object SparkSession {
951951

952952
session = new SparkSession(sparkContext, None, None, extensions)
953953
options.foreach { case (k, v) => session.initialSessionOptions.put(k, v) }
954-
defaultSession.set(session)
954+
setDefaultSession(session)
955+
setActiveSession(session)
955956

956957
// Register a successfully instantiated context to the singleton. This should be at the
957958
// end of the class definition so that the singleton is updated only if there is no

sql/core/src/test/scala/org/apache/spark/sql/SparkSessionBuilderSuite.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,14 @@ class SparkSessionBuilderSuite extends SparkFunSuite with BeforeAndAfterEach {
5050
assert(SparkSession.builder().getOrCreate() == session)
5151
}
5252

53+
test("sets default and active session") {
54+
assert(SparkSession.getDefaultSession == None)
55+
assert(SparkSession.getActiveSession == None)
56+
val session = SparkSession.builder().master("local").getOrCreate()
57+
assert(SparkSession.getDefaultSession == Some(session))
58+
assert(SparkSession.getActiveSession == Some(session))
59+
}
60+
5361
test("config options are propagated to existing SparkSession") {
5462
val session1 = SparkSession.builder().master("local").config("spark-config1", "a").getOrCreate()
5563
assert(session1.conf.get("spark-config1") == "a")

sql/core/src/test/scala/org/apache/spark/sql/test/TestSQLContext.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ private[spark] class TestSparkSession(sc: SparkContext) extends SparkSession(sc)
3535
}
3636

3737
SparkSession.setDefaultSession(this)
38+
SparkSession.setActiveSession(this)
3839

3940
@transient
4041
override lazy val sessionState: SessionState = {

sql/hive/src/main/scala/org/apache/spark/sql/hive/test/TestHive.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,9 @@ private[hive] class TestHiveSparkSession(
179179
loadTestTables)
180180
}
181181

182+
SparkSession.setDefaultSession(this)
183+
SparkSession.setActiveSession(this)
184+
182185
{ // set the metastore temporary configuration
183186
val metastoreTempConf = HiveUtils.newTemporaryConfiguration(useInMemoryDerby = false) ++ Map(
184187
ConfVars.METASTORE_INTEGER_JDO_PUSHDOWN.varname -> "true",

0 commit comments

Comments
 (0)