HyukjinKwon opened a new pull request, #388: URL: https://github.com/apache/spark-connect-rust/pull/388
### What changes were proposed in this pull request? [SPARK-61199](https://issues.apache.org/jira/browse/SPARK-61199): PySpark Connect's Column pickles its expression, and a sort Column's expression is a `SortOrder` of its child, its direction and its null ordering. So a pickled `F.col("x").desc()` is a sort order again wherever it unpickles, and `orderBy`, `Window.orderBy` and `repartitionByRange` sort by it. A Column of this client pickles as the bytes of its expression (SPARK-61167), which unpickle as an opaque expression. Now a sort order pickles as its child Column and the name of its sort method (`asc_nulls_first`, `asc_nulls_last`, `desc_nulls_first` or `desc_nulls_last`), as SPARK-61190 did for CASE WHEN: - `Column.__reduce__` (Rust) passes `(child, method)` of a sort order to `_reduce_column`. - `pyspark.sql.connect._column_reduce.column_of` calls that method on the unpickled child. On the client the child is a Column of this client. On a Python worker of the server it is PySpark Connect's Column, whose method gives back PySpark's `SortOrder`. ### Why are the changes needed? On a Python worker of the server, PySpark's `DataFrame.orderBy` wraps a Column that is not a `SortOrder` in `asc()`. So a `foreachBatch` function that captured a sort Column sent a sort order of a sort order: ``` captured = F.col("value").desc() def func(batch, batch_id): batch.orderBy(captured).limit(1).write.mode("append").saveAsTable("t") # SparkException: [INTERNAL_ERROR] Cannot evaluate expression: ... DESC NULLS LAST ``` On the client, `Window.orderBy` and `repartitionByRange` wrapped an unpickled sort Column in ASC NULLS FIRST in the same way: ``` >>> Window.orderBy(pickle.loads(pickle.dumps(F.col("x").desc()))) WindowSpec(OrderBy(x DESC NULLS LAST ASC NULLS FIRST)) # official: WindowSpec(OrderBy(x DESC NULLS LAST)) >>> df.select(F.row_number().over(that_window)).collect() # a partition of two or more rows SparkException: [INTERNAL_ERROR] Cannot evaluate expression: input[1, int, true] DESC NULLS LAST ``` ### Does this PR introduce _any_ user-facing change? Yes. A pickled, cloudpickled or deep-copied sort Column is a sort order: `orderBy`, `Window.orderBy` and `repartitionByRange` sort by it on the client, and so do the DataFrame methods on the Python worker of the server, for a sort Column that a `foreachBatch` function or a UDF captures. ### How was this patch tested? - `test_pickled_sort_columns_are_sort_orders_as_pyspark` (offline, mock server): for `asc`, `desc`, `asc_nulls_last`, `desc_nulls_first` and `F.desc`, each pickled, cloudpickled and deep-copied, the Column and its `Window.orderBy` have the reprs of the Column built without the pickling. `orderBy`, `repartitionByRange` and a window over it also send the same sort orders. A sort order of an expression, pickled twice, matches too. It fails without the change, where the `Window.orderBy` of the pickled Column renders another ASC NULLS FIRST. - `test_pickled_sort_columns_sort_on_the_server_as_pyspark` (live): `orderBy`, `row_number().over(Window.orderBy(...))` and `repartitionByRange` of a pickled `desc` and `asc_nulls_last` give the rows of the direct Column. A `foreachBatch` function that captures `F.col("value").desc()` and sorts by it on the server writes the largest value. It fails without the change, with the INTERNAL_ERROR above. - The same cases with official PySpark 4.3.0 against the same server, for `desc`, `asc_nulls_last`, `desc_nulls_first` and `asc`: the reprs, `orderBy`, windows and `repartitionByRange` give identical output. - The pickling tests of the offline suite (`-k pickl`, 67 passed) and the pickled `when()` live test. - The lib tests of both crates (618 passed), the build of all tests, also with `wasm-udf`, the offline Python suites (2457 passed, 8 skipped), and the live drop-in, Arrow and ML suites (215 passed) and live cases (11 passed) against a local Spark 4.3.0 server. Three live foreachBatch and progress-handler tests failed with `Python worker failed to connect back` while the machine was overloaded, and passed when run again. 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]
