Repository navigation
[SPARK-61183][CONNECT][PYTHON] Give back the SparkSession object of a DataFrame in DataFrame.sparkSession, in the Python client as PySpark does - #378
Open
HyukjinKwon wants to merge 1 commit into
Conversation
… DataFrame in DataFrame.sparkSession, in the Python client as PySpark does In PySpark, the DataFrames of a session, its streams and its client give back the session object itself, so DataFrame.sparkSession is spark. The client built a new session object at each access, so the identity was lost, and so were the streams and the client cached on it. Session objects are now registered by session ID, as weak references, by one function that every session-making call and session-giving getter uses: getOrCreate, create, newSession, cloneSession, DataFrame.sparkSession, dataSource, addListener and registerFunction. A getter gives back the live object of its session, and makes and registers one only when none is alive, so the registry keeps no session alive. DataSourceRegistration also gets the sparkSession attribute that PySpark has. Co-authored-by: Isaac <no-reply@databricks.com>
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What changes were proposed in this pull request?
SPARK-61183: PySpark gives back the session object itself from
DataFrame.sparkSessionandDataFrame._session, sodf.sparkSession is sparkholds for the DataFrames of a session, and so dodf.sparkSession.streams is spark.streamsanddf.sparkSession.client is spark.client.weakref.ref). A getter that gives back a session takes the live object of its session from the registry, and makes and registers a new one only when none is alive. Sessions made apart may share a session ID (two of an explicitsession_id), so the registry keeps a list per ID and compares the core sessions with the newSparkSession::is_same_sessionof the Rust client (a session and its clones share their client). The getters that give back a session areDataFrame.sparkSessionandDataFrame._session, thestreamsandclientof a session,dataSource, the listener setup ofStreamingQueryManager.addListener, andCatalog.registerFunction. The session-making calls aregetOrCreate,create,newSessionandcloneSession.newSessionandcloneSessionnow return the registered object, not a new one.DataSourceRegistration.sparkSession: the session of the registration, which PySpark has and the client lacked.Why are the changes needed?
Each access made a new session object, so the identity of the session was lost, along with the
streamsand theclientthat are cached per object:Does this PR introduce any user-facing change?
Yes.
DataFrame.sparkSessionandDataFrame._sessionreturn the session object of the DataFrame, the same object assparkfor its session, and so do thestreamsandclientof it.newSessionandcloneSessionreturn the object of the new session, which is registered until it is dropped.DataSourceRegistration.sparkSessionis added.How was this patch tested?
test_dataframes_give_back_their_session_object_as_pysparkinpython/tests/test_dropin_offline.py. It checks the identity of the session forrange,select,groupBy().count(),na.drop,read,udf,udtfanddataSource, the identity ofstreamsandclient, that two sessions do not mix (their objects and streams differ), and that a droppednewSessionobject is freed (weakrefandgc), while the DataFrames ofsparkkeep giving backspark, and, in a process of its own, that two sessions made with one explicitsession_idare two objects, each that of its own DataFrames. It fails without this change, at the firstdf.sparkSessioncheck. The same body gives the same results with official PySpark 4.3.0 (run as a script withPYTHONPATHon the reference build).checkhelper that raises, not anassert: pytest reprs the operands of a failedassert, and reprs of DataFrames ask the server for their schema, which hung the baseline run.wasm-udf, the offline Python suites (2228 passed, 8 skipped), and the live drop-in and Arrow suites (210 passed) and live cases (10 passed) against a local Spark 4.3.0 server. Clippy reports no diagnostics in the changed lines.Was this patch authored or co-authored using generative AI tooling?
Generated-by: Claude Opus 5.5
This pull request and its description were written by Isaac.