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

   ### What changes were proposed in this pull request?
   
   A follow-up of SPARK-62189 (#919) and of the first row of 
`dev/parity/known-differences.md` (SPARK-60902).
   
   PySpark's conversion of a pandas DataFrame under a struct zips the frame's 
columns with the struct's fields (`zip(data.items(), spark_types)`). It 
converts those columns, renames the table to the field names, and casts it. As 
a result:
   - columns past the fields are dropped, and not converted;
   - under a struct of no fields, the rows are dropped too, as a table of no 
columns has none;
   - with fewer columns than fields, the zipped columns are converted, and then 
PyArrow's rename raises `ArrowInvalid` (`tried to rename a table of 1 columns 
but only 2 names were provided`).
   
   The drop-in raised `AXIS_LENGTH_MISMATCH` for both, as an intended 
difference.
   
   So:
   - **`create_from_pandas_arrow` converts every frame with 
`prepare_pandas_table`.** That function zips the columns with the fields as 
PySpark's conversion does, with its errors, and no width check comes first.
   - **The conversion of a struct of an element that is not a `StructField` 
(`convert_of_unlowered_schema`) does the same.** Its first step deduplicates 
the field names.
   - **The row path converts with PySpark's conversion where PyArrow is.** The 
row path takes a struct of a Python subclass that reads its fields of its own, 
such as one with its own `__iter__`, which the fast path declines 
(`accepts_pandas_schema`). With PyArrow, it now converts such a frame of rows 
with `prepare_pandas_table`, as the Arrow path does, and takes the table, the 
errors and the columns (`create_from_pandas_table`). It then checks the number 
of columns against `len(schema.fields)`, as PySpark does, and applies the 
struct's names with `toDF`. The relation's schema stays that of the row path, 
of the core types of the fields. So:
     - a frame of no columns whose index is not a RangeIndex is the columns of 
its index, as `pa.RecordBatch.from_pandas` makes them (a naive DatetimeIndex is 
of UTC wall clocks), and under a struct of no fields it raises 
`AXIS_LENGTH_MISMATCH`;
     - a bad value in a column zipped with a field raises PySpark's conversion 
error, at any width;
     - no label is read, so a label whose `str()` raises converts.
   - **The row path without PyArrow, and of patched native classes.** Without 
PyArrow, the row path drops the columns past the fields, and the rows under a 
struct of no fields. It raises `AXIS_LENGTH_MISMATCH` for fewer columns, as 
before. It also still converts the frame's rows when a type name that the 
conversion resolves is rebound, or when the native `StructType` or 
`StructField` is patched (`converts_row_path_frames`): the conversion would 
read the patch, and the relation's schema would not.
   - **Docs:**
     - The KD row of the width is removed.
     - The no-PyArrow row says what that path does, with the index of a frame 
of no columns, and which frames convert with PySpark's conversion.
     - The compat text of the width gives the rules of each path, with the 
RangeIndex case, and links the KD row.
     - The compat text on a struct of an element that is not a `StructField` 
now says that its pandas DataFrames zip their columns too.
   
   Checked on both clients against a Spark 4.3.0 server:
   
   | case | pyspark-client 4.3.0 | before (e2 46b2dc93) | drop-in now |
   |---|---|---|---|
   | int64 2 columns, 1 field | struct<a:bigint> rows=[Row(a=1)] columns=['a'] 
| raises PySparkValueError: [AXIS_LENGTH_MISMATCH] Length mismatch: Expected 
axis has 1 elements, | struct<a:bigint> rows=[Row(a=1)] columns=['a'] |
   | int64 3 columns, 1 field | struct<x:bigint> rows=[Row(x=1)] columns=['x'] 
| raises PySparkValueError: [AXIS_LENGTH_MISMATCH] Length mismatch: Expected 
axis has 1 elements, | struct<x:bigint> rows=[Row(x=1)] columns=['x'] |
   | int64 3 columns, 2 fields | struct<x:bigint,y:bigint> rows=[Row(x=1, y=2)] 
columns=['x', 'y'] | raises PySparkValueError: [AXIS_LENGTH_MISMATCH] Length 
mismatch: Expected axis has 2 elements, | struct<x:bigint,y:bigint> 
rows=[Row(x=1, y=2)] columns=['x', 'y'] |
   | int64 1 column, 2 fields | raises ArrowInvalid: tried to rename a table of 
1 columns but only 2 names were provided | raises PySparkValueError: 
[AXIS_LENGTH_MISMATCH] Length mismatch: Expected axis has 2 elements, | raises 
ArrowInvalid: tried to rename a table of 1 columns but only 2 names were 
provided |
   | int64 1 column, 3 fields | raises ArrowInvalid: tried to rename a table of 
1 columns but only 3 names were provided | raises PySparkValueError: 
[AXIS_LENGTH_MISMATCH] Length mismatch: Expected axis has 3 elements, | raises 
ArrowInvalid: tried to rename a table of 1 columns but only 3 names were 
provided |
   | int64 2 columns, 0 fields | struct<> rows=[] columns=[] | raises 
PySparkValueError: [AXIS_LENGTH_MISMATCH] Length mismatch: Expected axis has 0 
elements, | struct<> rows=[] columns=[] |
   | 0 columns 1 row, 1 field | struct<a:bigint> rows=[Row(a=0)] columns=['a'] 
| raises PySparkValueError: [AXIS_LENGTH_MISMATCH] Length mismatch: Expected 
axis has 1 elements, | struct<a:bigint> rows=[Row(a=0)] columns=['a'] |
   | 1000 rows int64 2 columns, 1 field | struct<a:bigint> rows=[Row(a=0), 
Row(a=1)] columns=['a'] | raises PySparkValueError: [AXIS_LENGTH_MISMATCH] 
Length mismatch: Expected axis has 1 elements, | struct<a:bigint> 
rows=[Row(a=0), Row(a=1)] columns=['a'] |
   | Int64 nullable 2 columns, 1 field | struct<a:bigint> rows=[Row(a=1), 
Row(a=None)] columns=['a'] | raises PySparkValueError: [AXIS_LENGTH_MISMATCH] 
Length mismatch: Expected axis has 1 elements, | struct<a:bigint> 
rows=[Row(a=1), Row(a=None)] columns=['a'] |
   | float64 2 columns, 1 field | struct<a:double> rows=[Row(a=1.5)] 
columns=['a'] | raises PySparkValueError: [AXIS_LENGTH_MISMATCH] Length 
mismatch: Expected axis has 1 elements, | struct<a:double> rows=[Row(a=1.5)] 
columns=['a'] |
   | string 2 columns, 1 field | struct<a:string> rows=[Row(a='x')] 
columns=['a'] | raises PySparkValueError: [AXIS_LENGTH_MISMATCH] Length 
mismatch: Expected axis has 1 elements, | struct<a:string> rows=[Row(a='x')] 
columns=['a'] |
   | timestamp 2 columns, 1 field | struct<a:timestamp> 
rows=[Row(a=datetime.datetime(2020, 1, 1, 0, 0))] columns=['a'] | raises 
PySparkValueError: [AXIS_LENGTH_MISMATCH] Length mismatch: Expected axis has 1 
elements, | struct<a:timestamp> rows=[Row(a=datetime.datetime(2020, 1, 1, 0, 
0))] columns=['a'] |
   | date 2 columns, 1 field | struct<a:date> rows=[Row(a=datetime.date(2020, 
1, 1))] columns=['a'] | raises PySparkValueError: [AXIS_LENGTH_MISMATCH] Length 
mismatch: Expected axis has 1 elements, | struct<a:date> 
rows=[Row(a=datetime.date(2020, 1, 1))] columns=['a'] |
   | extra column unconvertible | struct<a:bigint> rows=[Row(a=1)] 
columns=['a'] | raises PySparkValueError: [AXIS_LENGTH_MISMATCH] Length 
mismatch: Expected axis has 1 elements, | struct<a:bigint> rows=[Row(a=1)] 
columns=['a'] |
   | extra column of mixed types | struct<a:bigint> rows=[Row(a=1)] 
columns=['a'] | raises PySparkValueError: [AXIS_LENGTH_MISMATCH] Length 
mismatch: Expected axis has 1 elements, | struct<a:bigint> rows=[Row(a=1)] 
columns=['a'] |
   | narrower, first column bad for its type | raises PySparkValueError: 
Exception thrown when converting pandas.Series (object) with name 'a' | raises 
PySparkValueError: [AXIS_LENGTH_MISMATCH] Length mismatch: Expected axis has 2 
elements, | raises PySparkValueError: Exception thrown when converting 
pandas.Series (object) with name 'a' |
   | narrower, first column fine, int to string | raises PySparkTypeError: 
Exception thrown when converting pandas.Series (int64) with name 'a' t | raises 
PySparkValueError: [AXIS_LENGTH_MISMATCH] Length mismatch: Expected axis has 2 
elements, | raises PySparkTypeError: Exception thrown when converting 
pandas.Series (int64) with name 'a' t |
   | wider, duplicate field names | struct<x:bigint,x:bigint> rows=[Row(x=1, 
x=2)] columns=['x', 'x'] | raises PySparkValueError: [AXIS_LENGTH_MISMATCH] 
Length mismatch: Expected axis has 2 elements, | struct<x:bigint,x:bigint> 
rows=[Row(x=1, x=2)] columns=['x', 'x'] |
   | wider, duplicate column labels | struct<x:bigint,y:bigint> rows=[Row(x=1, 
y=2)] columns=['x', 'y'] | raises PySparkValueError: [AXIS_LENGTH_MISMATCH] 
Length mismatch: Expected axis has 2 elements, | struct<x:bigint,y:bigint> 
rows=[Row(x=1, y=2)] columns=['x', 'y'] |
   | wider, non-str labels | struct<x:bigint> rows=[Row(x=1)] columns=['x'] | 
raises PySparkValueError: [AXIS_LENGTH_MISMATCH] Length mismatch: Expected axis 
has 1 elements, | struct<x:bigint> rows=[Row(x=1)] columns=['x'] |
   | wider, nested struct field | struct<a:struct<x:bigint>> 
rows=[Row(a=Row(x=1))] columns=['a'] | raises PySparkValueError: 
[AXIS_LENGTH_MISMATCH] Length mismatch: Expected axis has 1 elements, | 
struct<a:struct<x:bigint>> rows=[Row(a=Row(x=1))] columns=['a'] |
   | wider, int to int field overflow in dropped column | struct<a:int> 
rows=[Row(a=1)] columns=['a'] | raises PySparkValueError: 
[AXIS_LENGTH_MISMATCH] Length mismatch: Expected axis has 1 elements, | 
struct<a:int> rows=[Row(a=1)] columns=['a'] |
   | wider, nulls in non-nullable dropped column | struct<a:bigint> 
rows=[Row(a=1)] columns=['a'] | raises PySparkValueError: 
[AXIS_LENGTH_MISMATCH] Length mismatch: Expected axis has 1 elements, | 
struct<a:bigint> rows=[Row(a=1)] columns=['a'] |
   | narrower, non-nullable fields | raises ArrowInvalid: tried to rename a 
table of 1 columns but only 2 names were provided | raises PySparkValueError: 
[AXIS_LENGTH_MISMATCH] Length mismatch: Expected axis has 2 elements, | raises 
ArrowInvalid: tried to rename a table of 1 columns but only 2 names were 
provided |
   | numpy int32 wider | struct<a:int> rows=[Row(a=1)] columns=['a'] | raises 
PySparkValueError: [AXIS_LENGTH_MISMATCH] Length mismatch: Expected axis has 1 
elements, | struct<a:int> rows=[Row(a=1)] columns=['a'] |
   | customized struct, 2 columns, 1 field | raises ParseException:  | raises 
PySparkValueError: [AXIS_LENGTH_MISMATCH] Length mismatch: Expected axis has 1 
elements, | struct<a:bigint> rows=[Row(a=1)] columns=['a'] |
   | customized struct, 1 column, 2 fields | raises ArrowInvalid: tried to 
rename a table of 1 columns but only 2 names were provided | raises 
PySparkValueError: [AXIS_LENGTH_MISMATCH] Length mismatch: Expected axis has 2 
elements, | raises ArrowInvalid: tried to rename a table of 1 columns but only 
2 names were provided |
   | customized struct, 1 bad column, 2 fields | raises PySparkValueError: 
Exception thrown when converting pandas.Series (object) with name 'a' | raises 
PySparkValueError: [AXIS_LENGTH_MISMATCH] Length mismatch: Expected axis has 2 
elements, | raises PySparkValueError: Exception thrown when converting 
pandas.Series (object) with name 'a' |
   | duck element, 3 columns, 2 fields | struct<a:bigint,d:bigint> 
rows=[Row(a=1, d=2)] columns=['a', 'd'] | raises PySparkValueError: 
[AXIS_LENGTH_MISMATCH] Length mismatch: Expected axis has 2 elements, | 
struct<a:bigint,d:bigint> rows=[Row(a=1, d=2)] columns=['a', 'd'] |
   | duck element, 1 column, 2 fields | raises ArrowInvalid: tried to rename a 
table of 1 columns but only 2 names were provided | raises PySparkValueError: 
[AXIS_LENGTH_MISMATCH] Length mismatch: Expected axis has 2 elements, | raises 
ArrowInvalid: tried to rename a table of 1 columns but only 2 names were 
provided |
   | DDL string, 2 columns, 1 field | struct<a:bigint> rows=[Row(a=1)] 
columns=['a'] | raises PySparkValueError: [AXIS_LENGTH_MISMATCH] Length 
mismatch: Expected axis has 1 elements, | struct<a:bigint> rows=[Row(a=1)] 
columns=['a'] |
   | DDL string, 1 column, 2 fields | raises ArrowInvalid: tried to rename a 
table of 1 columns but only 2 names were provided | raises PySparkValueError: 
[AXIS_LENGTH_MISMATCH] Length mismatch: Expected axis has 2 elements, | raises 
ArrowInvalid: tried to rename a table of 1 columns but only 2 names were 
provided |
   
   31 of 32 cases match PySpark now; 0 with before (e2 46b2dc93)
   
   The one case that differs, "customized struct, 2 columns, 1 field", is not a 
difference of width. It uses `Iterated`, a `StructType` subclass with its own 
`__iter__`. PySpark zips the frame, as the drop-in now does, and then sends the 
struct's `json()` as the relation's schema. That JSON's `type` is the 
subclass's `typeName()`, `iter` (`Iterated` without its last four letters), 
which the server fails to parse (`ParseException`, `PARSE_SYNTAX_ERROR`). The 
drop-in sends `struct`, so the server gives the rows. With one column and two 
fields, both clients raise PyArrow's rename error first.
   
   The second round of review found differences on the row path of a struct of 
a class of its own (`StructIter`, with its own `__iter__`), which PySpark's 
conversion now closes:
   
   | case | pyspark-client 4.3.0 | first round (345335c6) | now |
   |---|---|---|---|
   | StructIter([a long]), pd.DataFrame(index=[0]) | struct<a:bigint> 
rows=[Row(a=0)] columns=['a'] | raises PySparkValueError: 
[AXIS_LENGTH_MISMATCH] Length mismatch: Expected axis has 1 elements, new 
values have 0 elements. | struct<a:bigint> rows=[Row(a=0)] columns=['a'] |
   | StructIter([]), pd.DataFrame(index=[0]) | raises PySparkValueError: 
[AXIS_LENGTH_MISMATCH] Length mismatch: Expected axis has 0 elements, new 
values have 1 elements. | struct<> rows=[Row()] columns=[] | raises 
PySparkValueError: [AXIS_LENGTH_MISMATCH] Length mismatch: Expected axis has 0 
elements, new values have 1 elements. |
   | StructIter([t timestamp]), a DatetimeIndex frame, session time zone LA | 
struct<s:string> rows=[Row(s='2019-12-31 16:00:00')] columns=['s'] | raises 
PySparkValueError: [AXIS_LENGTH_MISMATCH] ... | struct<s:string> 
rows=[Row(s='2019-12-31 16:00:00')] columns=['s'] |
   | StructIter([a long]), a bad value in a kept column, 2 columns | raises 
PySparkValueError: Exception thrown when converting pandas.Series (object) with 
name 'a' to Arrow Array (int64). ... | raises PySparkRuntimeError: Type 
mismatch in row data | raises PySparkValueError: Exception thrown when 
converting pandas.Series (object) with name 'a' to Arrow Array (int64). ... |
   | StructIter([a long]), a bad value in a kept column, 1 column | the same 
PySparkValueError | raises PySparkRuntimeError: Type mismatch in row data | the 
same PySparkValueError |
   | StructIter([a long]), a label whose `str()` raises, 1 and 2 columns | 
struct<a:bigint> rows=[Row(a=1)] columns=['a'] | raises RuntimeError: str of a 
label | struct<a:bigint> rows=[Row(a=1)] columns=['a'] |
   
   The review's probes, run again on both clients: p1_shapes 48/48, p2_types 
53/53 (also under Asia/Seoul), p3_schemas 59/59 (55/59 in the first round), 
p5_misc 26/26, p7_labels 10/10 and its extra cases 3/3, p8_plan 4/4, 
p9_assigned 6/6, p10_index_ts 3/3 (2/3). p6_iterated stays 3/8, of the 
`typeName` JSON of the subclass, which `docs/compatibility.md` documents. The 
no-PyArrow probe gives what the first round gave.
   
   **The third round** (a regression and the docs that the delta review found):
   - **N2, a regression of round 2:** a StructType subclass whose `names` is a 
property and setter, which the native `__init__` does not assign through, has 
no `names` as Python reads them. PySpark's conversion of the row path then 
raised `IndexError`, where master and round 1 gave the frame's rows. The row 
path now takes PySpark's conversion only when the struct's `names` and 
`fields`, as Python reads them, are as many (`names_as_many_as_fields`); 
otherwise it converts the frame's rows as before. A test covers it. The 
constructor itself is SPARK-62231.
   - **N4:** the doc comments of `create_from_pandas` and 
`accepts_pandas_schema` say which frames take PySpark's conversion and which 
the rows.
   - **N1 and N3:** the KD row of the row path states the differences exactly:
     - a field subclass whose `dataType` reads another type gives a table that 
follows the override, and a relation schema that does not (SPARK-62236);
     - under patched native classes or rebound type names, PySpark's conversion 
reads the patch and mostly raises, where the drop-in converts by the native 
types (SPARK-62237).
   - **Since SPARK-62235 (gc-shared):** the relation of the row path releases 
the GIL with `interruptible_without_parts`, as the other relations of local 
data do.
   
   ### Why are the changes needed?
   
   So that `createDataFrame` of a pandas DataFrame wider or narrower than its 
struct gives PySpark's result: the extra columns are dropped, and a narrower 
frame raises PyArrow's error.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes. A pandas DataFrame with more columns than the fields of its struct 
schema now drops the columns past the fields, where it raised 
`AXIS_LENGTH_MISMATCH`. One with fewer columns raises PySpark's errors: the 
conversion errors of the zipped columns, then PyArrow's `ArrowInvalid` of the 
rename.
   
   ### How was this patch tested?
   
   - **New offline test** in `python/tests/test_dropin_offline.py`: 
`test_create_dataframe_of_a_pandas_frame_of_another_width_zips_it_as_pyspark`. 
The second round adds:
     - the index of a frame of no columns, of an int index and a DatetimeIndex, 
on both paths, and its `AXIS_LENGTH_MISMATCH` under a struct of no fields;
     - the conversion errors of a kept column, and a label whose `str()` 
raises, on the row path;
     - a struct of no fields over a frame of two columns, on the row path with 
and without PyArrow, which fails with the review's mutant that keeps the rows;
     - a struct of an element that is not a `StructField` in the place of a 
field, over three and one columns, which fails with the review's mutant of 
`convert_of_unlowered_schema`.
   - **`python/tests/test_arrow_handoff.py`**: 
`test_pandas_explicit_struct_of_another_width_zips_the_columns_as_pyspark` and 
`test_pandas_explicit_struct_of_more_fields_raises_the_rename_of_pyspark`, 
which asserted the old `AXIS_LENGTH_MISMATCH` before. The latter now collects 
the frame of a struct of no fields and asserts that its table has no rows. The 
tests of patched native classes and rebound type names pass as before (the 
whole file: 1,234 passed).
   - **`test_dropin_e2e.py`**: 
`test_create_dataframe_of_a_struct_of_another_width` asserts official PySpark 
4.3.0's results now.
   - **Probes of both clients** against a Spark 4.3.0 server: the table above.
   - **The Rust and Python gates** (`gates.sh build rust py`):
     - `cargo fmt --all --check`;
     - `cargo +1.100.0 clippy -p apache-spark-connect-core -p 
apache-spark-connect -p pyspark-rs --all-targets -- -D warnings`;
     - `RUSTDOCFLAGS="-D warnings" cargo doc --no-deps`;
     - `cargo test -p pyspark-rs --lib`: 18 passed;
     - ruff 0.14.0, `test_ruff_excludes.py`, `scripts/audit_no_stubs.sh`, the 
stub and dispatch generators, the test-list guards and the license checks;
     - `test_type_stubs_offline.py`: 15 passed, 4 skipped;
     - the offline suite of `coverage.yml`: 7,055 passed, 114 skipped.
   - **`scripts/check_vendored_upstream.sh`**: every vendored file matches 
Apache Spark v4.3.0.
   - **`test_dropin_e2e.py`** against a Spark 4.3.0 server: 352 passed.
   
   ### 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