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

   ### What changes were proposed in this pull request?
   
   PySpark 4.3.0 pickles the function of a UDF each time it builds a plan that 
holds it: `PythonUDF.to_plan` (connect/expressions.py:752-766) and 
`PythonUDTF.to_plan` (plan.py:3073-3090) call `CloudPickleSerializer().dumps`. 
The map and group-map plans build their UDF's command from a `PythonUDF` too. 
The drop-in pickled the function when it was applied (`udf.py`, `udtf.py`, and 
`build_map_udf` of the map functions), so:
   
   | | PySpark 4.3.0 (now here too) | before |
   |---|---|---|
   | a global that the function reads, changed after the UDF is applied and 
before an action | the action runs with the new value, and so does each later 
action | the value at the time of the call |
   | a function that does not pickle | the call succeeds. Each request of the 
plan, from the first such as `df.schema`, raises `PicklingError`, or 
`UDTF_SERIALIZATION_ERROR` for a UDTF, and sends nothing | the call raised |
   
   With this PR, these are pickled at each plan build, in PySpark's order of 
the build:
   - `udf`, and pandas and Arrow UDFs of every eval type;
   - UDTFs, whether called, of a table argument or in a lateral join;
   - `mapInPandas`, `mapInArrow`, `applyInPandas`, `applyInArrow`, the 
cogroup's `applyInPandas` and `applyInArrow`, `applyInPandasWithState`, 
`transformWithStateInPandas` and `transformWithState`.
   
   `spark.udf.register` and `spark.udtf.register` are commands that PySpark 
sends at the call, so they still pickle there, as PySpark's do.
   
   **How.**
   - **The core** (`spark-connect`): `PythonUDFPayload` and `CommonInlineUdtf` 
get a doc-hidden `CommandPickler`, a Python object and the function that 
pickles it, which the binding implements. The Rust API's payloads of bytes are 
unchanged.
   - **The two builds.** A request whose plan holds picklers builds it twice:
     1. The first build records them, in PySpark's order, up to the first 
expression that fails to build.
     2. They are pickled in turn on the caller's stack, outside the recursion 
of the build, so the Python code of a pickling runs where PySpark's does. The 
first failure is the request's error, and nothing is sent.
     3. Otherwise, the second build takes the pickled commands.
   
     The second build does not warn again of a window of no partitions (#786, 
SPARK-61875), so a request warns once, as PySpark's does.
   - **The order of the build.** A UDTF's arguments are built before its 
handler, and transformWithState's initial state and grouping before its 
function, as PySpark builds them.
   - **The binding.** `pyfunc_make_udf` and `pyfunc_make_udtf` take the command 
as bytes or as a function that pickles it at each build. The map functions 
defer their pickling.
   - **Copies and garbage collection.** `copy.copy` and `copy.deepcopy` of a 
Column share its expression, so a copy does not pickle, as PySpark's copy does 
not. A Column and a DataFrame report the functions that they alone hold to the 
garbage collector, so a reference cycle through a function is collected. 
Without that, this change would leak such cycles.
   - **The shims.** `udf.py`'s and `udtf.py`'s pickling moves into functions 
called at the build. A UDTF's error names `func.__name__`, as PySpark's 
`PythonUDTF` does.
   
   `docs/compatibility.md`, `dev/parity/known-differences.md` and 
`dev/design/architecture.md` describe this.
   
   ### Why are the changes needed?
   
   So that UDFs read their functions' globals when PySpark's do. This matters 
to code that sets a global, a broadcast-like value or a model, before an 
action, and to libraries that apply a UDF before its function is ready. Also, 
an unpicklable function fails where PySpark's fails, at the first request, not 
at the call.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes, as above. In the Rust API, the new doc-hidden `pickler` field breaks 
code that builds `PythonUDFPayload` or `CommonInlineUdtf` with a struct 
literal; that code sets it to `None`. `PythonUDFPayload::new` and 
`TableValuedFunction::udtf` are unchanged.
   
   Documented differences that remain:
   - **Pickled Columns and DataFrames, and deep copies of DataFrames**, carry 
the commands that their functions pickled to then, as protobufs, where 
PySpark's re-pickle the functions at each build. This shows only for a function 
pickled by reference whose globals change afterwards.
   - **`pickle.dumps` of a Column of a lambda UDF** pickles, where PySpark's 
raises, as before.
   - **`mergeInto` clauses** are pickled when the clause is added, as PySpark's 
are, and again at `merge`.
   - **Shared functions and cycles.** A reference cycle through a function that 
a Column and a DataFrame both hold is not collected.
   - **A failing pickle before a window.** Where a function that does not 
pickle comes before a window of no partitions in the build, the drop-in warns 
of the window before it raises.
   
   **Cost**, per plan build without the network:
   
   | plan | master | now | PySpark |
   |---|---|---|---|
   | 1 UDF | 2.5 µs | 32.2 µs | 59.2 µs |
   | 5 UDFs | 6.7 µs | 138.8 µs | 240.9 µs |
   | mapInPandas | 1.9 µs | 37.1 µs | 66.3 µs |
   | 50 `withColumn` over 1 UDF | 270.6 µs | 367.7 µs | 1931 µs |
   
   ### How was this patch tested?
   
   - **Probes of both clients, through a recording proxy:**
     - late globals: 22 of 22 operations match in requests, command digests and 
results. They cover each kind of UDF, the UDTFs, every map and group-map API, a 
second action, and a UDF used twice in one select;
     - unpicklable functions: 55 of 55 match;
     - the order of the errors of the build: 18 of 18;
     - the window warnings of plans of UDFs, before and after the UDF, at 
`schema`, `collect` and `count`.
   - **Request bytes.** The plans of `explain`, `schema` and `collect` of 23 
shapes of UDFs, map functions, UDTFs and transformWithState are byte-identical 
to master's: 65 of 65.
   - **New tests**, of which 22 fail on master:
     - Rust: the payload's pickler, the holders of picklers, the order of a 
request's picklings, and the order of a UDTF's and a group map's builds;
     - `test_dropin_offline.py`:
       - 
`test_the_functions_of_udfs_are_pickled_when_their_plans_are_built_as_pyspark` 
(19 cases);
       - 
`test_udf_functions_are_pickled_in_the_order_of_the_build_of_each_request_as_pyspark`;
       - `test_cycles_through_the_functions_of_udfs_are_collected_as_pyspark`;
       - `test_windows_of_no_partitions_warn_once_a_request_of_udfs_as_pyspark`;
       - rewritten to PySpark's semantics: 
`test_udtf_handlers_are_pickled_when_plans_are_built_as_pyspark` and 
`test_unpicklable_functions_raise_picklingerror_as_pyspark`;
     - live: 
`test_dropin_e2e.py::test_udf_functions_are_pickled_when_their_plans_are_built_as_pyspark`,
 which also passes on the official client.
   - **Python.**
     - The offline suite of `coverage.yml`.
     - `test_dropin_offline.py` and `test_dropin_udf_args_offline.py` on pandas 
2 and 3.
     - Live `test_dropin_e2e.py`.
   - **Rust.** `cargo test -p apache-spark-connect --release --lib`, 
`coverage_plan_map_udtf` and `coverage_expression_extra`, live `e2e_coverage` 
and `e2e_dataframe`, and `--no-run` with and without `wasm-udf`.
   - **Lint.** `cargo fmt --all --check`, `cargo +1.100.0 clippy -p 
apache-spark-connect -p pyspark-rs --all-targets -- -D warnings`, ruff 0.14.0, 
`audit_no_stubs.sh` and `scripts/tests`.
   
   ### 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