eduardo lazaro zevallos created SPARK-62423:
-----------------------------------------------

             Summary: [CONNECT][PYTHON] Session from builder.create() does not 
become active, so datetime literals use an older session's settings
                 Key: SPARK-62423
                 URL: https://issues.apache.org/jira/browse/SPARK-62423
             Project: Spark
          Issue Type: Bug
          Components: Connect
    Affects Versions: 4.1.0, 3.5.1
         Environment: PySpark Connect client 3.5.1 and 4.1.0 (Python
            Reporter: eduardo lazaro zevallos


In PySpark Connect, SparkSession.builder.create() calls 
_set_default_and_active_session(), which sets the thread-local active session 
only when none is set yet:
https://github.com/apache/spark/blob/v4.1.0/python/pyspark/sql/connect/session.py#L330

If an earlier session is still saved there, the new session never becomes 
active.

Some client-side code reads the active session instead of the session that owns 
the DataFrame. For example, LiteralExpression._infer_type calls 
is_timestamp_ntz_preferred() for every datetime literal (lit(datetime), 
Column.isin(datetimes), comparisons). That function reads 
spark.sql.timestampType from SparkSession.getActiveSession():
https://github.com/apache/spark/blob/master/python/pyspark/sql/utils.py#L229-L240

Result:
1. Datetime literals in queries on session B get their type from session A's 
settings.
2. If session A has ended on the server (session timeout, or released by 
another client) but its client is not closed, the config request goes to the 
dead session. Every query on session B that has a datetime literal then fails 
(we saw UNIMPLEMENTED). Other queries on session B work.

Steps to reproduce (Spark Connect server at sc://localhost:15002):

from datetime import datetime
from pyspark.sql import SparkSession, functions as F

a = SparkSession.builder.remote("sc://localhost:15002").create()
b = SparkSession.builder.remote("sc://localhost:15002").create()

print(SparkSession.getActiveSession() is a)   # True: b did not become active

b.conf.set("spark.sql.timestampType", "TIMESTAMP_NTZ")
print(b.range(1).select(F.lit(datetime(2020, 1, 1))).schema)
# Expected: TimestampNTZType (b's setting)
# Actual:   TimestampType (a's setting)

Workaround: call a.stop() before creating b. stop() clears the active session.

Possible fixes:
- Make create() set the new session as active, as classic getOrCreate does, or
- Make literal type inference use the DataFrame's own session instead of the 
global active session.



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to