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

   ### What changes were proposed in this pull request?
   
   A follow-up of #792 (SPARK-61881), which pickles the functions of UDFs when 
the plan is built. To keep a copy from pickling, it made `copy.deepcopy` of a 
Column share the Column's expression, and with it the functions of its UDFs. 
Its docs said that "the functions of the UDFs of PySpark's deep copy are those 
of the Column". A round-2 review found that this is not so.
   
   PySpark's `copy.deepcopy` deep-copies `PythonUDF._func`. `copy.deepcopy` 
returns the same object for a plain function, a lambda or a closure, but it 
copies a callable instance, the `__self__` of a bound method, and a 
`functools.partial`. So, with `m = Mul(10)` and `col = udf(m)("id")`:
   
   | | PySpark 4.3.0 (now here too) | after #792 | before #792 |
   |---|---|---|---|
   | `dc = copy.deepcopy(col)`, then `m.k = 100`, then a select of `dc` | `10` 
| `100` | `10` |
   
   The same held for a bound method, a partial, a deep copy of 
`groupBy(udf_col)`, and a deep copy of `asTable()` given to a UDTF. Closures 
already matched.
   
   With this PR, `copy.deepcopy` of a Column, a DataFrame, grouped data or a 
table argument deep-copies the function of each UDF with the call's memo, as 
PySpark's does:
   - plain functions, lambdas and closures stay the same objects;
   - callable instances, bound methods and partials are copied, and the copies 
of one `copy.deepcopy` call share them, as `memo` makes them.
   
   **How.**
   - **The core** finds the picklers of a plan or an expression in the order of 
the build (`all_picklers`). With the functions' copies, it rebuilds only the 
plan or expression nodes above them, and shares the rest. So the shared nodes 
stay shared, the plan ids are kept, and Observations are mapped by their name.
     - `ExpressionNode::parts_mut` and `LogicalPlan::parts_mut` walk the nodes, 
without recursing.
     - `SubqueryExpression` records the plan ids of its session.
   - **The binding** deep-copies the shims' 
`functools.partial(_pickled_command, func, return_type)`, or the `(func, 
output_type)` of a map function. So the user's function is copied as PySpark 
copies `PythonUDF._func`.
   - A deep copy of a DataFrame now also builds its plan again at each request, 
so it warns of a window of no partitions at each one, as PySpark's does.
   
   `docs/compatibility.md` and `dev/parity/known-differences.md` are corrected.
   
   ### Why are the changes needed?
   
   So that a deep copy of a Column or a DataFrame of a UDF is independent of 
later changes to the UDF's callable object, as in PySpark. This regressed in 
#792.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes, as above.
   
   The difference that remains, documented as tracked, is a deep copy of a 
DataFrame into another session: the memo maps the session to another, or 
another session of the same id is active. It still carries the commands pickled 
at the copy, so a change after the copy is not seen.
   
   ### How was this patch tested?
   
   - **Probes of both clients.**
     - The reviewer's 4 probes match, and so do 37 cases of a Column, a 
DataFrame, grouped data and a table argument, each of a plain function, a 
lambda, a closure, an instance, a bound method and a partial. The cases include 
`deepcopy([c1, c2])`, a copy of a copy, a UDTF, `mapInPandas` and windows.
     - Edge cases match: the plan ids of `ddf.select(df["v"])`, Observations, a 
scalar subquery of a UDF, a self join, cogroup, and the errors of a locked 
instance.
   - **New tests.**
     - `test_deep_copies_copy_the_functions_of_udfs_as_pyspark`, in 
`test_dropin_offline.py` and in `test_dropin_e2e.py`. The live one passes on 
pyspark-client 4.3.0, and fails on master with `[100] == [10]`.
     - Rust unit tests in `udf.rs`, `dataframe.rs` and `table_arg.rs`.
   - **Rust.**
     - `cargo test -p apache-spark-connect -p apache-spark-connect-core 
--release` (1620 passed);
     - `cargo test -p apache-spark-connect --release --features wasm-udf` (1330 
passed).
   - **Python.**
     - The offline suite of `coverage.yml` (6107 passed), and 
`test_dropin_offline.py` on pandas 2 and 3.
     - Live `test_dropin_e2e.py` and `test_ml_subclasses_e2e.py` (308 passed).
   - **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`, `scripts/tests` and `check_vendored_upstream.sh`.
   
   ### 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