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

   ### What changes were proposed in this pull request?
   
   [SPARK-61160](https://issues.apache.org/jira/browse/SPARK-61160): Close the 
local iterator of `toLocalIterator` as a generator, so `contextlib.closing` and 
`close()` end the execution in the Python client as PySpark does.
   
   `DataFrame.toLocalIterator()` returns a `LocalRowIterator` that implements 
only `__iter__` and `__next__`. PySpark returns a generator. The change adds 
`close()` to `LocalRowIterator`:
   
   - It takes the underlying row stream, so the server execution is released, 
as dropping the iterator already did. A query that has not started sends 
nothing.
   - It marks the iterator finished, so `next()` then raises `StopIteration`, 
as a closed generator does.
   - Closing it again does nothing.
   
   `send()` and `throw()` are not added. PySpark's generator does not implement 
`send()` with a value after its first row either (the delegated list iterator 
raises `AttributeError`), so they are left out.
   
   Official PySpark 4.3.0 gives `None` for `close()` and `StopIteration` for 
the `next()` after it, the same as this change.
   
   ### Why are the changes needed?
   
   ```python
   from contextlib import closing
   with closing(spark.range(1000, numPartitions=10).toLocalIterator()) as rows:
       next(rows)
   # AttributeError: 'LocalRowIterator' object has no attribute 'close'
   ```
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes. `close()` exists on the iterator of `toLocalIterator`, so 
`contextlib.closing` and `close()` work and end the iteration early, as in 
PySpark.
   
   ### How was this patch tested?
   
   - Offline `test_local_iterator_close_ends_it_before_it_runs_as_pyspark`: 
closing an iterator before its first row, twice and with `contextlib.closing`, 
ends it, and no query is sent.
   - Live `test_local_iterator_closes_as_a_generator_as_pyspark`: 
`contextlib.closing` after one row ends the iteration, `next()` after the close 
gives nothing, and a second close does nothing. The same results with official 
PySpark 4.3.0.
   - The lib tests of both crates (590 passed), the build of all tests, also 
with `wasm-udf`, and the offline Python suites (2202 passed, 8 skipped), and 
the live tests of toLocalIterator, collect and toPandas (16 passed) against a 
local Spark 4.3.0 server; rustfmt passes, and clippy reports no diagnostics in 
the changed lines.
   
   ### 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