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

   ### What changes were proposed in this pull request?
   
   A follow-up of #792 (SPARK-61881), #790 (SPARK-61880), #801 (SPARK-61912) 
and #794 (SPARK-61887), from round-3 reviews of the DataFrame API and of the 
map functions against pyspark-client 4.3.0. Each item was probed on both 
clients against a Spark 4.3.0 server, through a recording proxy.
   
   **1. A UDF's output type, and the schema of a map DataFrame and of `to()`, 
are read when the plan is built.**
   - The output type of a UDF and the cached schema of a map DataFrame were 
fixed at the call, while the type pickled with the function was read at the 
build.
   - PySpark builds both from the same object at each build 
(connect/expressions.py:752-766), and caches that object as the map DataFrame's 
schema (connect/dataframe.py:2291-2292, group.py:330-331). So after `rt = 
StructType([...]); c = udf(f, rt)("id"); rt.add("b", LongType())`:
     - PySpark's UDF returns the new field, where the drop-in raised 
`ARROW_TYPE_MISMATCH` or `STRUCT_ARRAY_LENGTH_MISMATCH`;
     - `mapInPandas` and `applyInPandas` dropped a field added after the call.
   - `df.to(schema)` snapshotted its schema the same way 
(connect/dataframe.py:2079-2086).
   - Now:
     - the output type and the command come from the same live object at each 
build, through the core's doc-hidden `CommandPickler::with_output_type`, for 
udf, pandas and Arrow UDFs, the map and group-map functions, cogroups, 
`applyInPandasWithState` and `transformWithState`;
     - a StructType given to a map function, and the schema of `to()`, are kept 
as their cached schemas and read at the build (`DataFrame::to_read_at_build`, 
and a doc-hidden `schema_reader` of `LogicalPlan::ToSchema`);
     - a UDF of a type that PySpark does not convert (`AtomicType()` with 
`useArrow=False`) is created at the call, and its requests raise PySpark's 
`UNSUPPORTED_OPERATION`.
   
   **2. Map and group-map UDFs bind their input columns by name.** A column 
named `*` or `s.*` was bound as a star, which the server refused with 
`INVALID_USAGE_OF_STAR_OR_REGEX`. PySpark binds `ColumnReference(name)` 
(connect/udf.py:198-206). This affects `mapInPandas`, `applyInPandas`, 
`mapInArrow`, `applyInArrow`, `transformWithStateInPandas` and 
`foreachPartition`.
   
   **3. transformWithState and transformWithStateInPandas.**
   - `initialState` is resolved as `cogroup` resolves its argument 
(connect/group.py:406, 458), so a value that is not grouped data raises 
PySpark's `AttributeError` of `_df`, or `SESSION_NOT_SAME`, not PyO3's 
`TypeError`.
   - The output type is checked (`NOT_EXPECTED_TYPE`) before the input's 
columns are read, as PySpark does, so no schema request is sent before the 
error.
   
   **4. `DataFrame.__getitem__` with `PYSPARK_VALIDATE_COLUMN_NAME_LEGACY=1`** 
sends PySpark's requests (connect/dataframe.py:1918-1930): a schema request, 
then `select(item).isLocal()` when `verify_col_name` fails. It returned the 
column with no request.
   
   **5. Signature defaults.** `unionByName`, `SparkSession.range`, 
`TableValuedFunction.range` and `StreamingQuery.explain` show PySpark's 
defaults, where they showed `Ellipsis`. CPython's text signatures of native 
methods take only literal defaults, so `persist`'s `StorageLevel`, and the 
`_NoValue` of `RuntimeConf.get` and of `replace`, still show `Ellipsis`; the 
calls take PySpark's defaults. This is documented.
   
   **6. `randomSplit`** takes any iterable of weights, as PySpark's 
(dataframe.py:1113-1156), and Decimal weights raise PySpark's `TypeError`.
   
   **7. Window.**
   - `Window` has PySpark's `_PRECEDING_THRESHOLD` and `_FOLLOWING_THRESHOLD`.
   - `Window()` makes an object of no state, as PySpark's plain class does.
   - The Connect `Window` has `_spec`, and `connect/window.py` has PySpark's 
`WindowFrame`.
   - `pyspark.sql.window.__all__` no longer lists `FrameBound`, which is still 
in the module. The stub generator and `window.pyi` follow.
   
   `docs/compatibility.md` and `dev/parity/known-differences.md` describe these 
and the remaining differences:
   - `.schema` after a copy, deep copy or unpickle of a DataFrame whose view's 
schema changed is the cached one, where PySpark gives the requested one;
   - the object defaults of signatures above;
   - `WindowSpec`'s `_partitionSpec`, `_orderSpec` and `_frame` are properties, 
not in `vars()`.
   
   ### Why are the changes needed?
   
   So that UDFs, map functions, `to()`, transformWithState, `__getitem__`, 
`randomSplit` and `Window` behave as PySpark's, and code that changes a type 
after a call, or names a column `*`, works as on PySpark.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes, as above. In the Rust API, a struct literal of `LogicalPlan::ToSchema` 
needs `schema_reader: None`; this is in the Rust migration notes.
   
   ### How was this patch tested?
   
   - **New tests in `test_dropin_offline.py`.**
     - Output types read at the build (13 cases).
     - Columns bound by name (8 cases).
     - transformWithState's initial state, and its order of checks.
     - `__getitem__` with the legacy flag.
     - The literal signature defaults.
     - `randomSplit` of iterables.
     - UDFs of types that PySpark does not convert.
     - A Window test in `test_dropin_window_tablearg_offline.py`, and core unit 
tests in `udf.rs` and `group.rs`.
     - `test_type_stubs_offline.py` no longer lists 
`pyspark.sql.connect.window.WindowFrame` as a name of the stubs only, as the 
module has it now.
   - **The reviewers' probes** give identical requests, plans and results on 
both clients, but for the documented differences.
   - **Rust.**
     - `cargo test -p apache-spark-connect -p apache-spark-connect-core 
--release`, and `--features wasm-udf`;
     - the live Rust e2e tests `e2e_coverage`, `e2e_dataframe`, 
`e2e_new_methods`, `e2e_ops_breadth` and `e2e_streaming`;
     - `cargo test -p pyspark-rs --lib`, linked as `rust.yml` links it.
   - **Python.**
     - The offline suite of `coverage.yml` on pandas 2 and 3.
     - Live `test_dropin_e2e.py` and `test_arrow_e2e.py`.
     - The type-stub drift check and test, with mypy and pyright.
   - **Lint.** `cargo fmt --all --check`, `cargo +1.100.0 clippy -p pyspark-rs 
-p apache-spark-connect --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