HyukjinKwon opened a new pull request, #371: URL: https://github.com/apache/spark-connect-rust/pull/371
### What changes were proposed in this pull request? [SPARK-61191](https://issues.apache.org/jira/browse/SPARK-61191): Pickle `WindowSpec`, so UDFs and `foreachBatch` functions can capture windows, as PySpark does. `PyWindowSpec` gets a `__reduce__`. It pickles the partition Columns, the sort orders and the frame of the spec, and `pyspark.sql.connect._window_reduce.window_of` gives back an equal spec through `Window`: - a sort order is pickled as the Column of its child and the method of its direction and nulls (`desc_nulls_last`, ...), since an unpickled Column does not keep its sort order, and the method rebuilds it on the client and on the worker alike; - the frame is pickled as its ROWS flag and its start and end as the ints of `Window`, so the unbounded and current-row bounds, and `rowsBetween` vs `rangeBetween`, are kept. The `window_of` function is registered with cloudpickle to pickle by value, as the Column reducer is, so it runs on a Python worker of the server, where PySpark's `Window` builds the spec. Official PySpark's WindowSpec is a plain Python object that pickles the same way. ### Why are the changes needed? `pickle.dumps(Window.partitionBy("k").orderBy("x"))`, `cloudpickle.dumps(...)` and `copy.copy(...)` raised `TypeError: cannot pickle 'pyspark.sql.window.WindowSpec' object`. A `foreachBatch` or UDF function that captures a window, as `cloudpickle.dumps(lambda df: df.select(F.row_number().over(w)))`, therefore failed at serialization, where official PySpark runs it. ### Does this PR introduce _any_ user-facing change? Yes. `WindowSpec` pickles, deep-copies and shallow-copies, with the same repr and the same window in the plan, and UDFs and `foreachBatch` functions can capture window specs. ### How was this patch tested? - `test_window_specs_pickle_as_pyspark` (offline, mock server): for partition, order (`desc`, `asc_nulls_last`, `desc_nulls_first`, plain names), ROWS and RANGE frames with unbounded and current-row bounds, and an empty spec, pickle, cloudpickle and copy round trips have the same repr and send the same `F.sum(...).over(...)` expression as the spec. Fails without the fix. - `test_window_specs_captured_by_foreach_batch_functions_run_as_pyspark` (live): a `foreachBatch` function that captures a window and computes `row_number().over(window)` on the server's worker writes the expected rows. Fails without the fix. - The same specs give the same reprs and the same results (the `over` of each, and a cloudpickled closure over a window) with official PySpark 4.3.0. - The lib tests of both crates (599 passed), the build of all tests, also with `wasm-udf`, and the offline Python suites (2228 passed, 8 skipped), and the live tests of windows, foreachBatch, pickling and UDFs (16 passed) against a local Spark 4.3.0 server; rustfmt passes, and 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. -- 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]
