HyukjinKwon opened a new pull request, #856:
URL: https://github.com/apache/spark-connect-rust/pull/856
### What changes were proposed in this pull request?
Four results of a DataFrame that differed from PySpark 4.3.0's, from a
review of the latest 100 commits against `pyspark-client` 4.3.0, each probed on
both clients against a Spark 4.3.0 server.
**1. The WKB of a spatial value is bytes, also with
`spark.sql.execution.pyspark.binaryAsBytes=false`.** That setting makes the
rows give their binary values as `bytearray`, the `wkb` of the Arrow struct of
a geometry or a geography included. `convert_spatial` of
`pyspark.sql.types._udt_converter` made the `Geometry` or `Geography` of that
`bytearray`, so its `getBytes()` was one, at the top level, in arrays and in
maps, of `collect` and `toLocalIterator`. PySpark's
`ArrowTableToRowsConversion` makes them of the bytes of the struct
(`conversion.py:1387-1419`), whatever the setting, which only its binary
converter reads (`conversion.py:1313-1322`). `convert_spatial` now passes
`bytes(value["wkb"])`.
**2. `toPandas` with
`spark.sql.execution.arrow.pyspark.selfDestruct.enabled` copies each column of
each batch first, as PySpark's `SparkConnectClient._execute_and_fetch` does
(`core.py:1994-2013`).**
- The copy (`pa.concat_arrays`) gives each column its own allocation, which
the conversion's `self_destruct` frees as it converts the column.
`DataFrame.toPandas` converted the table of the handoff as it was. The
`to_pandas` of `SparkSession.client` already copied, in its
`_execute_and_fetch`.
- So the errors and the results were not PySpark's:
- a year-month interval column raised the conversion's
`ArrowNotImplementedError` ("No known equivalent Pandas block for Arrow data of
type month_interval is known."), where PySpark raises the copy's `KeyError:
21`, also of an empty result, as PyArrow has no Python array of the type;
- a result without columns kept its rows, where PySpark's copy of a batch
without columns has none: `spark.range(3).select().toPandas()` is `(0, 0)` in
PySpark with the setting.
- The new `pyspark.sql.connect.client._self_destruct_table` makes PySpark's
copy of the chunks of the table, which are its batches, empty ones included.
The Rust `to_pandas` calls it when the setting, which it reads with the other
two, is `"true"`, and drops the table of the handoff.
**3. `DataFrame.executionInfo` is one object until another execution of the
DataFrame replaces it.**
- PySpark's property returns the `_execution_info` that the last execution
of the DataFrame set (`dataframe.py:2434-2436`). The drop-in made a new
`ExecutionInfo` at each access, so:
- `df.executionInfo is df.executionInfo` was `False`;
- an attribute set on it was gone at the next access;
- its `metrics`, a `CollectedMetrics` of its own, was another object each
time.
- The DataFrame now keeps the object that it made, with the information of
the core's execution that it is of. Another execution of the DataFrame records
other information in the core, of another operation ID, for which the next
access makes and keeps a new object. An action of a derived DataFrame, such as
`take`, leaves it, as in PySpark.
- The information is compared by its operation ID, its metrics and its
observed metrics, the latter by their encoding where they are not equal, as of
a NaN.
- The lock of the kept object is never held while Python code runs. The
object is reported to the garbage collector (`__traverse__`, `__clear__`), as
it may keep the DataFrame.
**4. `_to_table()` has the Arrow metadata that PySpark reads of the server's
stream.**
- PySpark's client reads the Arrow IPC stream of the server with PyArrow
(`core.py:1889`). The server writes the metadata of the schema and of every
field, nested ones too, an empty one where there is none, which PyArrow reads
as `{}`. It writes the keys of a field's metadata in its order: `geometry` or
`geography` before `srid`, `SPARK::metadata::json` before
`SPARK::time::precision`.
- The batches that arrow-rs decodes keep neither: the metadata was `None`,
and the keys of a field's metadata came in any order, from run to run, so
`repr(field.metadata)` and its iteration differed.
- The new `pyspark.sql.connect.client._table_as_received` casts the table to
the schema of that metadata, with the keys in byte order, which is the server's
order for every key that Spark writes. The cast shares the buffers and keeps
the chunks. A table without columns is made of its batches with the schema's
metadata only, as a cast, or PyArrow's `replace_schema_metadata`, would drop
its rows. `toArrow`, which casts to the Arrow schema of the schema, was already
PySpark's.
**Docs:**
- `dev/parity/known-differences.md`: the entry of the session time zones
that Python does not know now has the timestamp whose wall clock in the session
time zone is outside the years 1 to 9999 of Python's `datetime`, and in the
local time zone is not. PySpark's `collect`, `toLocalIterator` and `first`
raise `OverflowError` ("date value out of range"), and the drop-in returns the
instant in the local time zone, as its conversion reads no zone of the session.
Intentional, documented, not changed.
- `AGENTS.md` said that `crates/pyspark-rs/src/row.py` embeds PySpark's
`Row` class. It is a port, without its annotations and the `@overload`s of
`__new__`, with its imports in its methods, which the entry says now. The entry
of the value classes no longer says "the same way": `values.py` keeps PySpark's
annotations.
- `docs/architecture.md` describes the metadata of `_to_table` and the copy
of `toPandas`.
### Why are the changes needed?
So that the rows, the pandas frames, the execution information and the Arrow
tables of a DataFrame are PySpark's, as with `pyspark-client` 4.3.0: of the
same types, errors and objects.
### Does this PR introduce _any_ user-facing change?
Yes, as PySpark 4.3.0:
- With `spark.sql.execution.pyspark.binaryAsBytes=false`, `getBytes()` of a
collected `Geometry` or `Geography` is `bytes`, not `bytearray`.
- With `spark.sql.execution.arrow.pyspark.selfDestruct.enabled=true`:
- `toPandas` of a year-month interval column raises `KeyError: 21`, not
`ArrowNotImplementedError`;
- `toPandas` of a result without columns has no rows;
- the memory of `toPandas` is PySpark's, which copies the result once
before it converts it.
- `df.executionInfo is df.executionInfo` is `True` after an execution, and
the object keeps the attributes set on it until another execution of `df`.
- `_to_table()` gives `{}` for the metadata of the schema and of the fields
that have none, and the keys of the metadata of a field in the server's order.
### How was this patch tested?
- New tests that fail on master and pass here:
- `python/tests/binary_configuration_cases.py` (in the offline suite):
`test_spatial_values_have_bytes_wkb_as_pyspark`, of `collect`,
`toLocalIterator` and its prefetch, at the top level, in an array and in a map;
- `python/tests/test_arrow_handoff.py`:
- `test_to_pandas_copies_each_column_of_each_batch_with_self_destruct`:
the copies of each column of each batch, of an empty batch too, and a result
without columns of no rows, with the setting; no copy without it;
- `test_to_table_has_the_metadata_that_pyspark_reads_of_a_spark_server`:
`{}` of the schema and the fields, nested ones too, the keys in the server's
order, and the same of the types of the columns and of their chunks;
- `_dataframe` of the file serves the configuration of a test
(`settings`).
- `python/tests/test_dropin_offline.py`:
`test_execution_info_is_one_object_until_another_execution_as_pyspark`, with an
observed NaN, a derived action that leaves it, and a cycle through the kept
object that the collector frees;
- Rust unit tests of `same_execution` in
`crates/pyspark-rs/src/dataframe.rs`;
- `python/tests/test_dropin_e2e.py`, which also pass on `pyspark-client`
4.3.0:
-
`test_spatial_values_have_bytes_wkb_whatever_binary_as_bytes_as_pyspark`;
-
`test_to_pandas_with_self_destruct_copies_the_batches_first_as_pyspark`;
- `test_execution_info_is_one_object_until_another_execution_as_pyspark`;
- `test_to_table_has_the_metadata_that_the_server_sends_as_pyspark`.
- Probes of `pyspark-client` 4.3.0 and the drop-in against a Spark 4.3.0
server, which give the same output now:
- the spatial values of `collect` and `toLocalIterator`;
- `toPandas` with and without selfDestruct, of year-month intervals at the
top level, nested, in a struct and of an empty result, of a result without
columns and of several batches;
- the identity of `executionInfo` after each action;
- the metadata of `_to_table()` and `toArrow()`, of the schema, the
fields, the types of the columns and of their chunks, with a user's metadata
too, and the rows of `_to_table()` of results without columns;
- the type sweep of the review over `collect`, `first`, `take`, `tail`,
`toLocalIterator`, `toPandas` and `toArrow`, with selfDestruct and either
binaryAsBytes, and the user-defined types of either binaryAsBytes.
- The Rust gates:
- `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 -p apache-spark-connect
-p apache-spark-connect-core`;
- `cargo test -p pyspark-rs --lib` as `rust.yml` runs it: 15 passed, the 2
new ones included.
- The Python gates:
- ruff 0.14.0 `check` and `format --check`, `test_ruff_excludes.py` and
`scripts/audit_no_stubs.sh`;
- `scripts/tests`: 98 passed;
- `gen_type_stubs.py --pyspark-src <v4.3.0>/python --check` and
`gen_fn_dispatch.py --check`;
- `test_type_stubs_offline.py` with mypy: 7 passed, 2 skipped (pyright is
not installed here);
- the guards of SPARK-61851.
- The offline suite of `coverage.yml`: 6344 passed and 77 skipped. The
skips: 68 for "not a public constructor", 8 that need `SPARK_REMOTE`, and
`test_type_stubs_offline.py`, which needs mypy and ran above.
- `test_dropin_e2e.py` against a Spark 4.3.0 server: 323 passed. In the full
run, one case of
`test_foreach_batch_captures_the_readers_writers_and_groups_as_pyspark` failed
on the server's `Python worker failed to connect back` (a read timeout of its
worker on a loaded host); its 9 cases passed on the rerun.
### 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]