HyukjinKwon opened a new pull request, #830:
URL: https://github.com/apache/spark-connect-rust/pull/830
### What changes were proposed in this pull request?
A follow-up of #782 (SPARK-61863), from a round-3 review of
`createDataFrame` from pandas against pyspark-client 4.3.0. Each item was
probed on both clients against a Spark 4.3.0 server, on pandas 2 and 3, and on
UTC and America/Los_Angeles hosts.
**1. Every pandas DataFrame takes PySpark's conversion, in the session time
zone.**
- A pandas DataFrame subclass, or a frame with a third-party ExtensionArray,
declined the Arrow path (`_pandas_input.py`), and took the drop-in's native row
path. That path converted a naive datetime with the host time zone, not the
session's. GeoPandas, pandera and BigQuery's `db_dtypes` frames are such frames.
- So such frames got timestamps shifted whenever the host and session time
zones differ (7 hours for a UTC host and a Los Angeles session), and nanosecond
values were truncated silently, where PySpark raises.
- A probe of 106 cases wrapped in a subclass differed from PySpark in 49 to
54 of them:
- dict columns became maps, where PySpark makes structs;
- ArrowDtype decimals lost their precision;
- object datetimes became `timestamp`, where PySpark gives `timestamp_ntz`;
- DST gaps converted silently;
- memoryview, VariantVal and list columns raised `TypeError`.
- PySpark sends every `isinstance(data, pd.DataFrame)` through
`create_arrow_table_from_pandas` with the session time zone
(connect/session.py:556, 634-639; pandas/types.py:758).
- Now every pandas DataFrame, of any class and any arrays, takes that
conversion. The guards that declined it (the frame's class, arrays from outside
pandas and numpy, hooks of labels, arrays and normalization methods) are
replaced by one check of the schema: `accepts_pandas_schema` refuses only
rebound type names and customized schema classes.
- The native row path is kept for frames without PyArrow, which PySpark does
not take. It localizes naive `datetime64` cells in the session time zone (or as
wall clocks for `timestamp_ntz`) through PySpark's own
`_check_series_convert_timestamps_internal`, one step of the conversion budget
per cell.
- The Row-column adaptation (`PandasRowColumns` and its helpers), which only
customized frames reached, is removed.
**2. The conversion of pandas strings yields to other threads and signals.**
`_pandas_string_ipc` (`pandas_input.rs`) looped over every cell with the GIL
held. It now takes a step of the conversion budget per value, as
`create_from_pandas` does:
| two string columns | longest pause of another thread: PySpark / master /
this PR |
|---|---|
| 1.2M rows | 77-86 ms / 138-176 ms / 15-24 ms |
| 6M rows | 318-434 ms / 641-645 ms / 110-146 ms |
**3. Smaller items.**
- The timestamp fast path emits PySpark's pandas 3 `FutureWarning`.
- `dev/parity/known-differences.md` no longer says that NumPy input is
unsupported (SPARK-61103 supports it).
- It describes the frames without PyArrow, and the session time zones that
Java takes and Python does not (`Z`, `PST`, `GMT+08:00`), for which PySpark's
`collect` of timestamps raises and the drop-in returns values.
- `docs/architecture.md`, `dev/design/architecture.md` and
`docs/row-conversion-benchmark.md` describe the paths as they are.
### Why are the changes needed?
So that a pandas DataFrame of any class converts as PySpark's does, and the
timestamps of subclasses are not shifted by the host time zone.
### Does this PR introduce _any_ user-facing change?
Yes:
- pandas DataFrame subclasses and frames of third-party arrays convert as
PySpark's, in the session time zone;
- the conversion of pandas strings yields every 1,024 values.
Remaining differences, documented:
- The `FutureWarning` is attributed to `_pandas_input.py`, and is shown at
each call.
- Frames without PyArrow convert on the row path, which PySpark refuses.
### How was this patch tested?
- **New tests.**
- `test_dropin_offline.py`:
- subclass and third-party-array frames in the session time zone, on UTC
and Los Angeles hosts;
- the row path's `datetime64` handling, for four schemas and two hosts,
and its DST-gap error;
- the no-PyArrow gate;
- an atomic schema that reads no rows.
- `local_data_conversion_cases.py`: a "pandas strings" case of the
interrupt and pause tests, which fails on master.
- Live `test_dropin_e2e.py`: subclass and third-party-array frames in the
session time zone.
- **`test_arrow_handoff.py`.** The cases that pinned the row path of
customized frames (`_PandasSubclass` and the hooks) now assert PySpark's
conversion; their tests were renamed accordingly. The tests of the removed
adaptation are deleted. An Arrow struct of another width still raises
`AXIS_LENGTH_MISMATCH` (SPARK-60902).
- **Probes of both clients** give no difference:
- the 106 cases wrapped in a subclass, on pandas 2 and 3 and on both hosts;
- 11 frames of third-party arrays, in four configurations;
- 30 cases of Row columns, decimals, error order and hooks;
- 30 cases of the row path without PyArrow;
- 387 further cases.
- **Performance.** The fast paths (int64, timestamps, float64, object and
Arrow strings, dates) at 1M rows, on pandas 2 and 3, show no regression.
- **Python.**
- The offline suite of `coverage.yml` on pandas 2 and 3.
- The pandas tests on a Los Angeles host.
- Live `test_arrow_e2e.py` and `test_dropin_e2e.py`.
- **Rust.** `cargo test -p pyspark-rs --lib`, linked as `rust.yml` links it.
- **Lint.** `cargo fmt --all --check`, `cargo +1.100.0 clippy -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]