HyukjinKwon opened a new pull request, #651:
URL: https://github.com/apache/spark-connect-rust/pull/651

   ### What changes were proposed in this pull request?
   
   A foreachBatch function runs on the server's Python worker, which has 
PySpark 4.3.0 and no drop-in. A function that captured one of the following 
objects failed there:
   - a reader or a stream reader;
   - a writer, a V2 writer or a stream writer;
   - grouped data or cogrouped ops;
   - a StreamingQuery.
   
   Their `__reduce__` named the drop-in's `_of_state` staticmethods, which 
cloudpickle pickles by reference and the worker's PySpark does not have. This 
PR changes their reduce, and fixes the other findings of a post-merge review of 
#632 (SPARK-61627):
   
   - Each reduce now gives a reconstructor from a new module, 
`pyspark.sql.connect._state_reduce`, as `_reduce_dataframe` does. The module 
registers itself with cloudpickle to pickle by value. On the drop-in, the 
reconstructor gives back the drop-in's object through `_of_state`. On a worker 
without `pyspark._pyspark`, it builds PySpark Connect's object of the same 
state:
     - the readers and writers by replaying PySpark's public builder calls, 
giving the kept arguments of #629 to PySpark as the user gave them;
     - grouped data, cogrouped ops and StreamingQuery through PySpark's 
constructors, which take the same state.
   - A shallow copy of a reader or a stream reader shares its options, as 
PySpark's copy shares `_options`. Before, the copy kept a snapshot, so an 
option that the original unset came back, and the copy's load wrote it back 
into the shared options.
   - `SparkSession.Builder` copies and pickles, as PySpark's does.
   - `GroupedData._pivot_values` is readable, as in PySpark.
   - A plain `pickle.dumps` of a session, a DataFrame or one of these objects 
fails with PySpark's message, which names 
`SparkSession.__reduce__.<locals>.creator`.
   
   ### Why are the changes needed?
   
   For parity with PySpark 4.3.0. Before:
   
   ```python
   reader = spark.read.format("json")
   
   def func(batch_df, batch_id):
       reader.load(path).count()
   
   df.writeStream.foreachBatch(func).start()
   ```
   
   failed when the query started, with 
`StreamingPythonRunnerInitializationException: AttributeError: type object 
'DataFrameReader' has no attribute '_of_state'`. The same happened for a 
captured writer, V2 writer, GroupedData, pivot, cogroup, stream reader, stream 
writer and StreamingQuery. After, they all run as in PySpark.
   
   Before, after `c = copy.copy(r); r.option("header", None)`, both `r` and `c` 
loaded with `header=true`. After, neither does, as in PySpark.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes, in the Python client:
   - A foreachBatch function can capture these objects.
   - A copied reader shares its options.
   - The Builder copies and pickles.
   - A plain `pickle.dumps` of a reader, writer, grouped data or session fails 
with PySpark's error, as PySpark's does. Before, some of them pickled on the 
drop-in.
   
   Still different from PySpark, and left for later:
   - A pickle round trip of an observed DataFrame with its Observation does not 
keep the link between them.
   - `copy.copy` of a Builder does not share its options.
   - A reader's `_options` drops a key that is set to None, while PySpark keeps 
it as None. The behavior is the same.
   
   ### How was this patch tested?
   
   - New tests, each checked to fail without the fix:
     - e2e against Spark 4.3.0: 
`test_foreach_batch_captures_the_readers_writers_and_groups_as_pyspark`, 9 
cases: reader, writer, V2 writer, groupBy, pivot, cogroup, stream reader, 
stream writer and StreamingQuery;
     - e2e: `test_a_shallow_copy_of_a_reader_shares_an_unset_option_as_pyspark`;
     - offline: the worker reconstructors, the plain-pickle message, the 
Builder's copy and pickle, and `_pivot_values`.
   - Also passing, with `TZ=UTC`:
     - CI's offline Python set: 3712 passed;
     - `test_dropin_e2e.py`: 290 passed;
     - the Rust lib tests and the `--no-run` builds;
     - `cargo fmt`, the stub audit and ruff.
   
   ### 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.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


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

Reply via email to