HyukjinKwon opened a new pull request, #782:
URL: https://github.com/apache/spark-connect-rust/pull/782
### What changes were proposed in this pull request?
A follow-up of #762 (SPARK-61772) and #665 (SPARK-61837), from a review of
`createDataFrame` against PySpark 4.3.0.
**Other threads and Ctrl-C interrupt the native conversion of local data.**
#665 gave the conversion of results a Python-frame checkpoint and a signal
check every 1,024 steps. The conversion of local data had neither, so it held
the GIL for seconds: no other Python thread ran, and Ctrl-C waited until it
ended. PySpark's conversion is Python, which other threads and signals
interrupt. Now `interrupt.rs` has one `ConversionBudget`, which the result
conversion and every step of the conversion of local data share:
- the collection of the items and the sorting of dict keys;
- inference, of every value, nested ones and UDTs included;
- taking the rows apart, the pandas row path (per row and per cell) and the
integer fast path;
- the release of the rows.
The rows are assembled with the GIL released. The longest pause of a
CPU-busy Python thread, or the delay of a SIGINT, at 1.2M rows:
| data | PySpark | before | now |
|---|---|---|---|
| tuples and names | 44 ms | 2,270 to 2,623 ms | 6 ms |
| dicts, pause / SIGINT | 34 / 39 ms | 3,872 / 5,047 ms | 26 / 38 ms |
| Rows, pause / SIGINT | 20 / 11 ms | 3,512 / 3,263 ms | 15 / 19 ms |
| tuples and a DDL schema | 45 ms | 339 ms | 5 ms |
| integer tuples and a StructType | 29 ms | 63 ms | 5 ms |
| one array of 3M values, of a schema / inferred | 71 / 70 ms | 140 / 223 ms
| 5 / 5 ms |
The throughput is at most 2% lower than before, and the arrays convert about
5% faster.
**The errors of a struct schema of another width, as PySpark's.**
- A pyarrow table whose fields differ from a struct schema raised
`AXIS_LENGTH_MISMATCH` of a pre-check. It now raises PyArrow's `ValueError` of
the cast, or the `AssertionError` of the localization of nested or timestamp
columns, as PySpark's. For an atomic type or a list of names,
`AXIS_LENGTH_MISMATCH` comes after the cast, as PySpark's.
- A pandas frame of no columns under `StructType([])` is the table of its
index, as PySpark's `RecordBatch.from_pandas`. With a `RangeIndex` it gives
rows of no columns, as before. Another index is a column, so it raises
PySpark's `AXIS_LENGTH_MISMATCH` (0 and 1), where it gave rows.
- A pandas frame of another width than its struct still raises
`AXIS_LENGTH_MISMATCH`, the intentional difference of SPARK-60902, where
PySpark drops columns.
**The Rust API's cache threshold.** `create_dataframe` and
`create_dataframe_from_columns` ignored the server's
`spark.sql.session.localRelationCacheThreshold` below 64 KiB, unlike
`create_dataframe_from_arrow`, the Python binding and PySpark. They now read
the 13 settings in one request, as the others do, and upload at the threshold
at any size. They read them once they have converted the rows, as
`create_dataframe_from_arrow` does, so a row that does not convert fails with
no request. The 64 KiB constant is renamed `INLINE_IPC_BYTES`: it only chooses
the IPC writer of an inline relation. With the default threshold, the plans of
3, 2,000, 30,000 and 120,000 rows and columns are byte-identical to before.
**Stale comments** that said that the pandas helpers run before, or avoid, a
Config request are fixed. Since #762, `createDataFrame` reads its settings
once, before any conversion. `docs/architecture.md`,
`docs/row-conversion-benchmark.md` and `dev/parity/known-differences.md`
describe the new behavior.
### Why are the changes needed?
So that a `createDataFrame` of large local data does not stop the other
threads of the program for seconds, and can be cancelled with Ctrl-C, as on
PySpark. So that the errors of mismatched pyarrow tables and empty pandas
frames are PySpark's. And so that the Rust API honors the server's cache
threshold, as the other paths do.
### Does this PR introduce _any_ user-facing change?
Yes:
- other Python threads run, and Ctrl-C raises `KeyboardInterrupt`, during
the conversion of local data;
- a pyarrow table of other fields than its struct schema raises PyArrow's
error;
- a pandas frame of no columns and an index other than a `RangeIndex`, under
`StructType([])`, raises `AXIS_LENGTH_MISMATCH`;
- in the Rust API, `create_dataframe` and `create_dataframe_from_columns`
read the session's settings, and upload data at the server's cache threshold.
A remaining difference: the pandas row path of a DataFrame subclass still
pauses other threads for about 0.6 s at 1.2M rows, where PySpark pauses them
for 47 ms. That time is spent in NumPy's `tolist()`, which runs a garbage
collection inside one C call, not in a native loop of the drop-in.
### How was this patch tested?
- **Interrupts.** New `python/tests/local_data_conversion_cases.py`, run by
`test_dropin_offline.py`: 16 cases, one per part of the conversion. A second
thread tells that the conversion is in that part from the reference counts of
the data's objects, then checks that it runs there and that a SIGINT there
raises `KeyboardInterrupt`. `python/tests/_local_data_conversion_probe.py` is
the probe; `scripts/tests/test_local_data_conversion_probe.py` tests it. With
master's extension, all 16 cases fail. With this one, they passed 8 runs in a
row, and 3 runs under coverage.
- **Widths.**
- Updated `test_arrow_input_ddl_width_errors` and
`test_arrow_input_explicit_schema_width_errors`.
- In `test_arrow_handoff.py`, tests of `AXIS_LENGTH_MISMATCH` of pandas
frames of more, fewer and no columns, plain and subclassed, and of a frame of
no columns under a struct of no fields.
- A live test in `test_dropin_e2e.py` of the pyarrow and pandas cases.
- Probes of both clients against a Spark 4.3.0 server: the pyarrow and
pandas cases match PySpark, apart from SPARK-60902.
- **The Rust API.** New
`crates/spark-connect/tests/create_dataframe_settings.rs`, an offline mock
server: two of its three tests fail on master. A live test is added to
`e2e_dataframe.rs`. The ML golden replay of `tests/common/ml_golden.rs` now
answers the settings, as its datasets are made by `create_dataframe`.
- Before and after: the responsiveness and throughput probes at 300k and
1.2M rows, and the plans dumped at the default threshold.
- The offline suite of `coverage.yml` (`test_dropin_offline.py`: 2284
passed), and the 51 live `createDataFrame` tests of `test_dropin_e2e.py`. Rust:
- `cargo test -p apache-spark-connect --lib` (743 passed);
- the offline tests of both crates;
- live `e2e_dataframe`, `e2e_coverage` and `arrow_map_input`, and the
golden replays.
- `cargo fmt --all --check`, `cargo +1.100.0 clippy -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]