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

   ### What changes were proposed in this pull request?
   
   A follow-up of #762 (SPARK-61772), which made `createDataFrame` read its 
settings in one request at PySpark's point. A review found other conversions 
whose requests or checks come in another order than PySpark 4.3.0's, or that 
give other values. With this PR, each of the 85 operations of the review's 
probes sends the same requests to a Spark 4.3.0 server as official PySpark, and 
gives the same result; 44 of them differed. The probes compared the requests 
through a recording proxy.
   
   | conversion | PySpark 4.3.0 (now here too) | before |
   |---|---|---|
   | `toArrow()` | `to_arrow_schema(self.schema, ...)` first, of the cached 
schema (an AnalyzePlan), then Execute, then the cast | Execute only, then the 
check of the result's schema. So duplicated nested field names and interval 
types ran the query before the error, and a later `df.columns` sent an 
AnalyzePlan |
   | `toPandas()`, `toLocalIterator()` | build the plan (`to_proto`), then read 
the settings, then send | read the settings first, so a plan that cannot be 
built sent Config requests; the first plan's compression lookup came after them 
|
   | `toLocalIterator()` of a year-month interval and a server error | the 
server's error first, as the conversion runs per record batch | 
`NOT_IMPLEMENTED` first |
   | `toLocalIterator()` of a calendar interval | `pyarrow.MonthDayNano`, 
nested too | a str `"14 months 25 days ..."` |
   | `mapInPandas`, `mapInArrow`, `applyInPandas`, `applyInArrow` | parse the 
DDL first | analyze the input first, so a bad DDL on a bad input raised 
`UNRESOLVED_COLUMN` instead of `UNSUPPORTED_DATATYPE` |
   | `cogroup(...).applyInPandas` | parses only; the function binds no columns 
| an AnalyzePlan of each side, and their columns as the UDF's arguments |
   | `applyInPandasWithState` | sends both schemas as given | parsed the state 
schema |
   | `spark.udf.register("f", fn, "long")` | two DDL parses | one |
   
   Also, `toLocalIterator()` releases the GIL only to fetch a batch, instead of 
for every row. With one CPU-busy Python thread, 300k rows took 15 to 38 s, 2.62 
s on PySpark, and now 0.75 s. Ctrl+C takes 8 to 15 ms. The 1,024-step 
checkpoint of #665 is unchanged.
   
   How:
   - **`spark-connect`.**
     - `DataFrame::prepare_execution()` (doc-hidden) builds the request and 
does the first plan's compression lookup, as PySpark's `to_proto` does. The 
`PreparedExecution` it returns sends itself, by collecting or by a local 
iterator.
     - `CoGroupedData::input_columns` is removed: PySpark's cogroup function 
binds no columns. This is a change of the Rust API, noted in 
`docs/compatibility.md`.
     - `apply_in_pandas_with_state_with_schemas` sends both schema strings as 
given.
     - `Value::calendar_interval_parts` gives the months, days and nanoseconds; 
the Rust API still collects a calendar interval as its text.
   - **`spark-connect-core`**: `read_plan_compression()`, the lookup of the 
first plan.
   - **The binding.**
     - `toArrow` converts the cached schema first.
     - `_to_table()` is PySpark's private method of the raw table and the 
server's schema.
     - The calendar-interval paths of a result's schema become 
`pyarrow.MonthDayNano`.
     - `udf.register` passes the return type as given, as PySpark's does.
   - `LogicalPlan` gets `#[allow(clippy::large_enum_variant)]`. Its schema 
fields are strings now, so `GroupMap` is the largest variant by more than 
clippy's threshold. Boxing its fields would change the public fields of that 
variant, which an integration test builds; each plan node is shared in an `Arc`.
   
   `docs/types.md`, `docs/architecture.md`, `dev/design/architecture.md` and 
`docs/compatibility.md` are updated.
   
   ### Why are the changes needed?
   
   So that these conversions send PySpark's requests and raise PySpark's errors 
in PySpark's order, as #762 did for `createDataFrame`, and so that their values 
match.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes, as above:
   - the order of requests and errors of these conversions;
   - `toLocalIterator()` yields `pyarrow.MonthDayNano` for a calendar interval;
   - iterating no longer stalls behind other Python threads.
   
   In the Rust API, `CoGroupedData::input_columns` is removed.
   
   Remaining differences:
   - `toArrow()` of a result with no batch returns the empty table of the 
schema, where PySpark raises `AssertionError`; a Spark server always sends a 
batch.
   - The drop-in creates the resource profile of `mapInPandas(..., profile=)` 
at the call, where PySpark creates it when the plan is built.
   
   ### How was this patch tested?
   
   - New and updated tests in `test_dropin_offline.py`:
     - toArrow's order;
     - the map family's parse order;
     - applyInPandasWithState's schemas;
     - udf.register's two parses;
     - the plan built before the settings for toPandas and toLocalIterator;
     - the year-month error at the first record batch;
     - MonthDayNano;
     - the GIL held within a fetched batch;
     - the SIGINT test extended to toPandas and toLocalIterator.
   
     The mock server now sends a 0-row record batch for an empty result, as a 
server does. The new Rust-side tests fail with master's extension.
   - New live tests in `test_dropin_e2e.py` and 
`crates/spark-connect/tests/e2e_dataframe.rs`.
   - Test fixtures that used `toArrow()` as a trigger and answered only 
ExecutePlan now answer the schema too, or go through `_to_table()`.
   - The review's probes, against official PySpark 4.3.0 on the same server.
   - Rust:
     - `cargo test -p apache-spark-connect --release --lib` (744 passed) and 
`-p apache-spark-connect-core --lib` (234 passed);
     - 9 live `--test` files against Spark 4.3.0;
     - `--no-run` with and without `wasm-udf`.
   - Python: the offline suite of `coverage.yml`, and `test_dropin_e2e.py` and 
`test_arrow_e2e.py` live (310 passed).
   - Lint: `cargo fmt --all --check`, `cargo +1.100.0 clippy -D warnings`, ruff 
0.14.0 and `audit_no_stubs.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