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

   ### What changes were proposed in this pull request?
   
   [SPARK-61413](https://issues.apache.org/jira/browse/SPARK-61413): make 
`toLocalIterator` of the Python client return a generator, as PySpark does.
   
   `DataFrame.toLocalIterator` of the Python client returns a generator, as 
PySpark's generator function does, instead of the `LocalRowIterator` object. 
The generator is a small Python function in `pyspark/sql/connect/dataframe.py` 
that `yield from`s the Rust row iterator, which stays lazy: the query is sent 
with the first `next()`. A `finally` closes the row iterator, so a close, a 
throw, an error and the end of the generator each end the execution at once.
   
   The error of a progress handler's done call for an iteration dropped before 
its end goes to `sys.unraisablehook` with the object `types.GeneratorType` 
(`crates/pyspark-rs/src/progress.rs`), the type of what `toLocalIterator` 
returns, as PySpark reports its generator; it was the `LocalRowIterator` class.
   
   ### Why are the changes needed?
   
   In PySpark, `isinstance(df.toLocalIterator(), types.GeneratorType)` and 
`inspect.isgenerator` are `True`, and the result has `send()`, `throw()`, 
`gi_frame` and `gi_running`. With the Rust object they were `False`, and 
`throw()`, `send()` and the others raised `AttributeError`. Since SPARK-61160 
`close()` already ended the execution as a generator's does; the generator 
keeps that, and also ends the execution on `throw()`, which the row iterator, 
having no `throw`, would leave open until the traceback is freed.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes, the Python client: `toLocalIterator()` returns a generator with the 
name `toLocalIterator`, so code that checks for a generator or uses its methods 
works as in PySpark. The rows, the error types and the lazy start are 
unchanged; a traceback through the generator has one more frame, and a `next()` 
while another thread runs it raises `ValueError`, as PySpark's generator does.
   
   ### How was this patch tested?
   
   - `test_to_local_iterator_is_a_generator_as_pyspark` (offline, fails without 
the fix): the type, the name and the state, and no request sent before the 
first row.
   - `test_to_local_iterator_ending_releases_the_execution_as_pyspark` 
(offline): after one row, a `close()` and a `throw()` (with the exception kept) 
each send ReleaseExecute at once. The `throw()` case fails without the 
`finally`.
   - `test_local_iterator_is_a_generator_as_pyspark` (live): the rows, the 
state after the last row, `throw()` and `close()` in the middle.
   - 
`test_a_dropped_local_iterator_hands_the_done_error_to_unraisablehook_with_an_object`
 (offline, existing) now sees the generator type, the type of the dropped 
iterator.
   - `cargo test -p apache-spark-connect --release --lib` (599 passed) and `-p 
apache-spark-connect-core`, `--no-run` with and without `wasm-udf`, `cargo fmt 
--all --check`, `ruff check`, `cargo clippy` (nothing on the changed lines).
   - The offline Python suites (3117 passed, 8 skipped) and the live ones (282 
passed, 1 skipped) against an OSS Spark 4.3.0 Connect server.
   
   ### Was this patch authored or co-authored using generative AI tooling?
   
   Generated-by: Claude Opus 5.5 and Claude Sonnet 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