Skip to content

Commit 8239fdc

Browse files
ericlAndrew Or
authored andcommitted
[SPARK-15520][SQL] SparkSession builder in python should also allow overriding confs of existing sessions
## What changes were proposed in this pull request? This fixes the python SparkSession builder to allow setting confs correctly. This was a leftover TODO from #13200. ## How was this patch tested? Python doc tests. cc andrewor14 Author: Eric Liang <[email protected]> Closes #13289 from ericl/spark-15520.
1 parent 01e7b9c commit 8239fdc

File tree

1 file changed

+24
-11
lines changed

1 file changed

+24
-11
lines changed

python/pyspark/sql/session.py

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -138,24 +138,37 @@ def getOrCreate(self):
138138
"""Gets an existing :class:`SparkSession` or, if there is no existing one, creates a
139139
new one based on the options set in this builder.
140140
141-
This method first checks whether there is a valid thread-local SparkSession,
142-
and if yes, return that one. It then checks whether there is a valid global
143-
default SparkSession, and if yes, return that one. If no valid global default
144-
SparkSession exists, the method creates a new SparkSession and assigns the
145-
newly created SparkSession as the global default.
141+
This method first checks whether there is a valid global default SparkSession, and if
142+
yes, return that one. If no valid global default SparkSession exists, the method
143+
creates a new SparkSession and assigns the newly created SparkSession as the global
144+
default.
145+
146+
>>> s1 = SparkSession.builder.config("k1", "v1").getOrCreate()
147+
>>> s1.conf.get("k1") == "v1"
148+
True
146149
147150
In case an existing SparkSession is returned, the config options specified
148151
in this builder will be applied to the existing SparkSession.
152+
153+
>>> s2 = SparkSession.builder.config("k2", "v2").getOrCreate()
154+
>>> s1.conf.get("k1") == s2.conf.get("k1")
155+
True
156+
>>> s1.conf.get("k2") == s2.conf.get("k2")
157+
True
149158
"""
150159
with self._lock:
151-
from pyspark.conf import SparkConf
152160
from pyspark.context import SparkContext
153-
from pyspark.sql.context import SQLContext
154-
sparkConf = SparkConf()
161+
from pyspark.conf import SparkConf
162+
session = SparkSession._instantiatedContext
163+
if session is None:
164+
sparkConf = SparkConf()
165+
for key, value in self._options.items():
166+
sparkConf.set(key, value)
167+
sc = SparkContext.getOrCreate(sparkConf)
168+
session = SparkSession(sc)
155169
for key, value in self._options.items():
156-
sparkConf.set(key, value)
157-
sparkContext = SparkContext.getOrCreate(sparkConf)
158-
return SQLContext.getOrCreate(sparkContext).sparkSession
170+
session.conf.set(key, value)
171+
return session
159172

160173
builder = Builder()
161174

0 commit comments

Comments
 (0)