viirya opened a new pull request, #158: URL: https://github.com/apache/spark-connect-rust/pull/158
### What changes were proposed in this pull request? JIRA: [SPARK-60920](https://issues.apache.org/jira/browse/SPARK-60920) The drop-in's `DataFrameReader`, `DataStreamReader`, `DataFrameWriter`, `DataFrameWriterV2`, `DataStreamWriter` and `MergeIntoWriter` (`crates/pyspark-rs/src/{readwriter,streaming,dataframe}.rs`) now change themselves in place, as PySpark's do. - Each wrapper holds its core builder directly, not in an `Option` that every setter emptied. A setter replaces the builder in place and returns the same Python object (`PyRefMut<Self>`). - Terminal methods (`load`, `table`, `save`, `saveAsTable`, `insertInto`, `start`, `toTable`, `create`, `merge`, ...) build from a clone, so the object can be used again afterwards. - As in PySpark, methods that take inline settings keep them on the object: the format methods (`json`, `csv`, `parquet`, ..., `jdbc`), and the extra arguments of `load`, `save`, `saveAsTable`, `insertInto`, `start` and `toTable`. A call that fails argument validation leaves the object unchanged. - `DataFrameWriter.jdbc` now applies `url` and `dbtable` after `properties`, so they win over same-named properties, as in PySpark. - A merge clause (`whenMatched()`, `whenNotMatched()`, `whenNotMatchedBySource()`) holds its parent writer, as in PySpark Connect. Each action appends to that writer and returns it, and a clause can be used more than once. `withSchemaEvolution()` returns `self`. - The core builders get `#[derive(Clone)]`; their consuming Rust API does not change. The two core readers also get read-only `get_format` / `get_schema` / `get_options`. On the Python side these back PySpark Connect's private `_format` / `_schema` / `_options` attributes. ### Why are the changes needed? Each setter took the inner builder and returned a new wrapper, which left the original Python object empty. So the usual PySpark pattern ```python r = spark.read r.option("a", "b") r.format("jdbc").load() ``` failed with `RuntimeError: DataFrameReader already consumed`. The vendored pandas-on-Spark `read_sql_table` uses this pattern: it calls `reader.option(...)`, `reader.schema(...)` and `reader.options(...)` and ignores the return values. The same error came up when reusing a reader or writer after a read or write, which PySpark allows, and after a format method failed argument validation, because the builder was taken before the arguments were checked. A merge clause object could also be used only once. ### Does this PR introduce _any_ user-facing change? Yes. Reader, writer and merge-writer setters now return the same object and change it in place, and these objects can be reused after a read, write or merge, matching PySpark. Code that only chained calls behaves as before. ### How was this patch tested? New offline tests in `python/tests/test_dropin_offline.py`: - calling setters and ignoring their return values, then loading (the `read_sql_table` call sequence) - the vendored `read_sql_table` itself, run up to the read, with `default_session` pointed at an offline session - reuse of a reader after successful reads, and after reads that fail argument validation, checking the format, schema and options left on it - the in-place setters on `DataStreamReader`, `DataFrameWriter`, `DataFrameWriterV2` and `DataStreamWriter` - merge clause actions returning the same writer, and a clause used more than once This touches the same code in `readwriter.rs` / `streaming.rs` as #151 (reader schema handling). Whichever merges second will need a rebase. ### Was this patch authored or co-authored using generative AI tooling? Generated-by: Claude Code (Anthropic) 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]
