HyukjinKwon opened a new pull request, #356: URL: https://github.com/apache/spark-connect-rust/pull/356
### What changes were proposed in this pull request? [SPARK-61167](https://issues.apache.org/jira/browse/SPARK-61167): PySpark Connect's Columns are Python objects of their expressions, which pickle and deepcopy, also with cloudpickle. So a `foreachBatch` function, which runs on a Python worker of the server, can capture a Column defined outside of it, and the worker unpickles it as PySpark Connect's. The client's Column now pickles as the bytes of its `spark.connect.Expression` and its rendering, of `pyspark.sql.connect._column_reduce.column_of`: - **On the client** it gives back a Column of the new `ExpressionNode::Sent` of the Rust core. That node sends the expression as it was sent, and renders as the Column did. The expressions over it send and render as those over the original. - **On a worker without the Rust client** it gives back PySpark Connect's Column, of an `Expression` of that proto. Everything that it calls there is of PySpark. - `pyspark.sql.connect.column` registers the module of the function with cloudpickle to pickle by value. So cloudpickle sends the function with a Column, while `pickle` pickles it by reference on the client, as PySpark Connect's Columns pickle with `pickle` too. A Column so unpickled is of an expression as it was sent. The methods that build on the kind of an expression take it as an expression of its own, on the client as on PySpark's worker, which do not know the kind of a sent expression either. Such methods are chaining `when` on an unpickled `when`, or sorting by an unpickled `desc()`, rather than calling `desc()` again. ### Why are the changes needed? The client's Column, of the Rust extension, could not be pickled: ``` PySparkPicklingError: [STREAMING_CONNECT_SERIALIZATION_ERROR] Cannot serialize the function `foreachBatch`. ... ``` `pickle.dumps(column)` and `copy.deepcopy(column)` raised `TypeError`. Apache Spark's `test_streaming_foreach_batch_external_column` of `test_parity_foreach_batch.py` failed on this client. ### Does this PR introduce _any_ user-facing change? Yes. Columns pickle and deepcopy, and `foreachBatch` functions that capture them run, as with PySpark 4.3.0. For the Rust API, `ExpressionNode::Sent` and `SentExpression` are new. ### How was this patch tested? - A new Rust unit test checks that a sent expression sends and renders as it was, also under a function. - A new offline test pickles, cloudpickles and deep-copies six Columns, of a reference, an alias, a literal, `when`, a field and `desc()`. It checks their reprs and plans, and those of an expression over them. It passes with official PySpark 4.3.0, and fails without this change. - A new live test runs a `foreachBatch` function that captures a Column and writes a table of it, as Apache Spark's test does. It passes with official PySpark 4.3.0, and fails without this change. - Apache Spark's `test_parity_foreach_batch.py` passes on this client against a local server, all 23 tests. - The lib tests of both crates (598 passed), the build of all tests, also with `wasm-udf`, the offline Python suites (2220 passed, 8 skipped), and the live drop-in and Arrow suites (204 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. -- 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]
