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

   ### What changes were proposed in this pull request?
   
   PySpark 4.3.0's `createDataFrame` reads 
`spark.sql.execution.arrow.useLargeVarTypes`. When it is `"true"`, PySpark 
builds its Arrow table with large types:
   - the strings and binaries of the schema are `large_string` and 
`large_binary`, also in arrays, maps, structs and the SQL type of a 
user-defined type (`LocalDataToArrowConversion.convert`, 
`conversion.py:994-1005`; `to_arrow_type`, `pandas/types.py:137-139`);
   - the binaries of a variant, a geometry and a geography stay `binary`.
   
   PySpark then compares the table's pyarrow `nbytes` with 
`spark.sql.session.localRelationCacheThreshold` to choose between an inline and 
a cached relation (`connect/session.py:782`). Its chunks add up the `nbytes` of 
their columns (`plan.py:534-538`). pyarrow's `nbytes` is Arrow C++'s 
`ReferencedBufferSize`, which counts `len` offsets of a string, binary, list or 
map array, not the `len + 1` of the buffer.
   
   The drop-in and the Rust API read the setting, but two paths ignored it:
   - **Local rows**, and the Rust API's columns: the core's Arrow conversion 
always made `Utf8` and `Binary` (`datatype_to_arrow`, 
`crates/spark-connect/src/session.rs`).
   - **The binding's pandas fast path**: the IPC of a pandas frame's object 
strings (`_pandas_string_ipc`, `crates/pyspark-rs/src/pandas_input.rs`) is 
written as `Utf8`.
   
   Every path measured the size that it compared with the threshold with 
arrow-rs's `get_slice_memory_size`, which counts `len + 1` offsets: 4 bytes 
more per string column than PySpark (`batch_data_size`, 
`crates/spark-connect/src/arrow_input.rs`).
   
   Checked on both clients against a Spark 4.3.0 server. Under a 
`localRelationSizeLimit` of 1000 bytes, an uploaded relation fails with 
`LOCAL_RELATION_SIZE_LIMIT_EXCEEDED` and an inline one works:
   
   | Data, schema `s string` | Settings | pyspark-client 4.3.0 | drop-in before 
|
   |---|---|---|---|
   | 10000 rows `("abcd",)` | `useLargeVarTypes=true`, threshold 100000 | 
uploaded (120000 bytes of large strings) | inline (80004 bytes of strings) |
   | a pandas frame of 10000 object strings | the same | uploaded | inline |
   | 1000 rows `("abcd",)` | threshold 8001 | inline (8000 bytes) | uploaded 
(8004 bytes) |
   | a pandas frame of 10000 object strings | threshold 80001 | inline (80000 
bytes) | uploaded (80004 bytes) |
   
   The paths that were already right are unchanged: pandas frames through 
pyarrow, pyarrow tables, NumPy arrays, and frames without a schema.
   
   The change, in the core:
   - **`batch_data_size` measures as pyarrow's `nbytes`** (`referenced_size`, 
`arrow_input.rs`):
     - the bytes that cover the bits of a slice's validity bitmap and booleans;
     - fixed-width values;
     - `len` offsets of a string, binary, list or map array, with the range of 
the values or of the child that they reference;
     - the children of a struct.
   
     It is used by the threshold of every path and by the chunks of an upload. 
Another layout counts as before.
   - **Local rows and columns take the large types.** `datatype_to_arrow_of` 
gives the Arrow type of a Spark type with PySpark's large types. 
`local_relation`, which rows and columns share, casts the columns of strings 
and binaries to them under the setting, once it has read the settings, before 
it measures the relation. The cast keeps the values buffers and writes 64-bit 
offsets.
   - **The binding's IPC takes them too.** Under the setting, 
`create_dataframe_from_inline_arrow_ipc` decodes the IPC of a schema with 
strings or binaries, casts it the same way, and sends it as the other paths 
send their batches, inline or uploaded by their size. Without the setting, the 
IPC is sent as it is.
   
   `docs/architecture.md` says so. No code of the binding changes.
   
   ### Why are the changes needed?
   
   So that, under `useLargeVarTypes`, `createDataFrame` sends the Arrow types 
that PySpark sends, and so that a relation of the same data is inline or 
uploaded as in PySpark at every threshold.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes.
   - Under `spark.sql.execution.arrow.useLargeVarTypes=true`, `createDataFrame` 
of local rows, the Rust API's `create_dataframe` and 
`create_dataframe_from_columns`, and pandas object-string columns send 
`large_string` and `large_binary`, as PySpark does.
   - A relation whose Arrow size is within a few bytes per string, binary, list 
or map column of `localRelationCacheThreshold` may now be inline where it was 
uploaded. It is then inline where PySpark's is.
   
   ### How was this patch tested?
   
   - **Probes of both clients against a Spark 4.3.0 server:** 
`p_large_threshold.py` and `p_cache_measure.py` give PySpark's choices after 
the change.
   - **New tests**, each failing without the change:
     - `data_sizes_are_the_nbytes_of_pyarrow` in `arrow_input.rs`: 26 arrays, 
sliced ones too, of the sizes that pyarrow 26 gives.
     - In `tests/create_dataframe_settings.rs`, on a loopback service:
       - 
`the_cache_threshold_is_compared_with_the_nbytes_of_pyarrow_as_pyspark`;
       - 
`rows_and_columns_are_of_the_large_types_of_pyspark_under_use_large_var_types`: 
nested strings, binaries, maps and structs, a variant's binaries kept, and 
columns too;
       - 
`the_ipc_of_the_binding_is_of_the_large_types_and_its_nbytes_under_use_large_var_types`.
     - `test_local_rows_and_pandas_strings_take_the_large_types_of_pyspark` and 
`test_the_cache_threshold_is_compared_with_the_nbytes_of_pyarrow` in 
`test_arrow_handoff.py`, on the mock server.
     - `test_local_relations_are_of_large_types_and_measured_as_pyspark` in 
`test_dropin_e2e.py`. Its body also passes on pyspark-client 4.3.0.
   - **Two tests corrected:** `test_string_columns_preserve_every_ipc_cell` and 
`test_string_large_inline_input_keeps_row_transport_and_schema` asserted 
`pa.string()` under `useLargeVarTypes=true`. PySpark sends `pa.large_string()`.
   - **cargo, without `--release`, as the coverage job builds:**
     - `cargo test -p apache-spark-connect --lib`: 792 passed;
     - `--test create_dataframe_settings`: 6 passed;
     - the live `--test arrow_map_input` (2), `--test e2e_dataframe` (23) and 
`--test e2e_coverage` (25), all passed.
   - `cargo test -p pyspark-rs --lib`: 13 passed.
   - The offline suite of `coverage.yml`: 6340 passed, 77 skipped.
   - `test_dropin_e2e.py` against a Spark 4.3.0 server: 320 passed.
   - **Lint and guards:**
     - `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` of the lint job's 
crates and features;
     - ruff 0.14.0, `scripts/audit_no_stubs.sh`, the stub and dispatch 
generators' `--check`, `test_type_stubs_offline.py`;
     - the test-list guards.
   
   One difference remains, outside this change: the `actualSize` of 
`LOCAL_RELATION_SIZE_LIMIT_EXCEEDED` is the size of the IPC that arrow-rs 
writes, such as 122783 bytes, where pyarrow's writer gives 121895 for the same 
table.
   
   ### 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