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

   ### What changes were proposed in this pull request?
   
   This is the first sub-task of SPARK-62207. The sub-tasks close two rows of 
`dev/parity/known-differences.md`, "The `__dict__` of the objects of a session" 
and "Subclasses". This PR covers the four objects of a session that PySpark 
makes from a single argument: the `Catalog`, the `TableValuedFunction`, the 
`DataSourceRegistration` and the `RuntimeConf` of `pyspark.sql.connect`.
   
   In PySpark these are plain Python classes. `__init__` stores its argument, 
whatever it is, as an instance attribute: `_sparkSession` of a catalog and of 
the table-valued functions, `sparkSession` of a registration, and `_client` of 
a configuration. The methods read that attribute each time they run. In the 
drop-in, these classes held a native session instead. As a result:
   - `vars()` was empty;
   - the constructors of the catalog and the registration required a session;
   - `TableValuedFunction` and `RuntimeConf` could be neither constructed nor 
subclassed;
   - an assignment of the attribute had no effect on the requests.
   
   A new offline test file compares the objects of a session and of a DataFrame 
with goldens captured from PySpark 4.3.0. It checks their `vars()`, the 
attributes that their methods read, their copies and pickles, their 
constructors, objects whose `__init__` never ran, and objects made of a foreign 
argument (an `int`, or a proxy of a session). Of its 419 generic cases:
   
   | | Same as PySpark | Differ |
   |---|---|---|
   | Before, master bdf82090 | 131 | 288 |
   | After, this PR | 160 | 259 |
   
   All 29 cases that this PR fixes are of the four classes. Their `vars`, 
`attributes`, `copies`, `construct`, `uninit` and `foreign` cases all pass. The 
remaining 259 belong to the later sub-tasks: streams, grouped data, readers, 
writers, the builder, the session, Column, and the DataFrame. They are listed 
as strict xfails, grouped by the sub-task that fixes them. Each sub-task 
deletes its own group, and a case that passes while it is still listed fails 
the run.
   
   **The changes:**
   - **A new module, `crates/pyspark-rs/src/state.rs`, holds the helpers that 
the later sub-tasks share:**
     - `attr` is `getattr` inside CPython's recursion check. A `__getattr__` 
that reads the same attribute again then ends in a `RecursionError`, as in 
PySpark, instead of overflowing the native stack.
     - `assign` sets attributes through `setattr`, so that a descriptor or a 
`__setattr__` of a subclass sees them.
     - `getstate` and `reduce_ex` give the `__getstate__` and `__reduce_ex__` 
of a plain object. From protocol 2 that is `object.__reduce_ex__`; below it, 
the same `copyreg.__newobj__` form.
     - `session_of_value` resolves the session of the object that a method 
reads, in the same order as PySpark. A drop-in session is used as is. For any 
other object, the method reads its `client` (or `_client`, where PySpark reads 
that) and uses the session of that client if it is a drop-in client. Otherwise 
it raises a `TypeError`.
     - `overrides` tells whether a client's class overrides a method of 
`pyspark.sql.connect.client.core.SparkConnectClient`.
   - **`Catalog` and `TableValuedFunction`:**
     - The native object is empty.
     - `__init__(sparkSession)` sets `_sparkSession`, and each method resolves 
the session from it at the call.
     - The methods that PySpark builds from others call those methods on `self`:
       - `registerFunction` calls `self._sparkSession.udf.register`;
       - `createExternalTable` calls `self.createTable`;
       - `listViews` calls `self.currentDatabase()`;
       - `range` of the table-valued functions calls `self._sparkSession.range`.
   - **`DataSourceRegistration`:** `register` reads 
`self.sparkSession._client`. It registers natively for a drop-in client that 
does not override `register_data_source`, and otherwise calls 
`client.register_data_source(dataSource)`, as PySpark does.
   - **`RuntimeConf`:** `__init__(client)` sets `_client`. Each method uses the 
native transport for a drop-in client that does not override `config`. 
Otherwise it calls `client.config(operation)` and reads the pairs and the 
warnings of the result, as PySpark does. The `ConfigRequest.Operation` of each 
method is built as PySpark builds it, by a new private module, 
`pyspark.sql.connect._conf_operations`. Its errors are PySpark's too: 
`VALUE_NOT_ALLOWED` of `isModifiable`, and the `AssertionError` of `getAll` for 
a value of None.
   - **Shared by all four:**
     - `__new__` takes any arguments, and the signature is PySpark's.
     - `__getstate__` returns the `__dict__` (None when it is empty), and 
`__reduce_ex__` is that of a plain object.
     - The previous `__copy__` and `__reduce__` of the four, the `__setstate__` 
of the catalog and the registration, and the registration's getter, 
`__traverse__` and `__clear__`, are removed. The `__dict__` and the default 
slots take their place.
     - All four can be subclassed. An object whose `__init__` never ran raises 
the `AttributeError` of the first attribute that PySpark's method reads.
   - **`SparkSession`:**
     - `conf`, `tvf` and `dataSource` make a new object at each access, as 
PySpark's do: `RuntimeConf(self.client)`, `TableValuedFunction(self)` and 
`DataSourceRegistration(self)`.
     - `catalog` is made once, as `Catalog(self)`.
     - The catalog holds the session object as its `_sparkSession`, so the 
session and its catalog form a reference cycle, as in PySpark. The garbage 
collector frees it, since the session's `__traverse__` already visits the 
catalog.
     - So a dropped session whose catalog was read stays open until a 
collection. If the program exits before one, the exit sends the session's 
`clean_cache`, as PySpark's does. This is decision 1 of the design, applied to 
the catalog now; the cycle of `streams` comes with SPARK-62209.
   - **Docs:**
     - `dev/parity/known-differences.md`:
       - The `__dict__` row lists the four classes as PySpark's.
       - The Subclasses row lists `TableValuedFunction` and `RuntimeConf` among 
the subclassable classes, and drops them from the native list.
       - The exit row describes the catalog cycle.
     - `docs/compatibility.md` gets the same changes.
   
   **What remains** of these four classes is inherent: the native transport 
makes the requests. A catalog and the table-valued functions make their 
requests through the native transport of the session they hold. For a proxy of 
a session, they use the session of the drop-in client of its `client`; 
PySpark's would call the methods of that client, such as `to_table`. As a 
result:
   - the methods that a subclass of the drop-in's client overrides are not 
called;
   - a client of another class raises a `TypeError`;
   - the DataFrames they make belong to that session, since a DataFrame holds 
its session natively (until SPARK-62218).
   
   `RuntimeConf` and the registration have no such residue: they call the 
client's methods, as PySpark does. Calling the client's methods for every 
request would mean a second implementation of the requests, PySpark's Python 
client running over the results of those methods. The native transport makes 
them in Rust, with its retries, reattachment, progress, interrupts and decoding 
of the results.
   
   ### Why are the changes needed?
   
   In PySpark, code that reads or writes the state of these objects works 
because the state is in their `__dict__`: `vars()`, an assignment of 
`_sparkSession`, a subclass with a property or a `__getattr__`, a proxy of a 
session, copies and pickles. So do `RuntimeConf(client)`, 
`TableValuedFunction(spark)` and subclasses of them. Until now, none of this 
worked the same way in the drop-in.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes. The `Catalog`, `TableValuedFunction`, `DataSourceRegistration` and 
`RuntimeConf` of a session keep their state in their `__dict__`, as PySpark's 
do:
   - `vars(spark.catalog)` is `{'_sparkSession': spark}`;
   - an assignment of the attribute, or a subclass's property or `__getattr__`, 
changes what the methods use;
   - the four classes can be constructed from PySpark's arguments, and 
subclassed;
   - copies and pickles carry the `__dict__`.
   
   Once `spark.catalog` has been read, a dropped session is freed by the next 
garbage collection instead of at once, as PySpark's is.
   
   ### How was this patch tested?
   
   - **`tests/test_dropin_object_state_offline.py`** (new, added to the offline 
suite of `coverage.yml`), with its goldens (`_object_state_goldens.py`), its 
cases (`_object_state_cases.py`) and the script that captures the goldens from 
PySpark 4.3.0 (`capture_object_state_goldens.py`):
     - 164 passed and 259 xfailed on this branch;
     - 423 passed with PySpark 4.3.0.
   
     Besides the generic cases, four tests cover what this PR adds:
     - a `RuntimeConf` of a client of another class calls its `config` with 
PySpark's operations, results, warnings and errors;
     - a registration of such a client calls its `register_data_source`;
     - the `range` of the table-valued functions is the `range` of their 
session;
     - a catalog sends its requests through the session it holds, and through 
the client of a proxy.
   
     These four pass with both clients.
   - **`tests/test_exit_cleanup_offline.py`:** a new test,
     
`test_i_a_dropped_new_session_whose_catalog_was_read_is_open_until_a_collection`.
 With the drop-in and with PySpark, a dropped session whose catalog was read 
still sends `clean_cache` at exit, plus `release_session` with 
`SPARK_CONNECT_RELEASE_SESSION_ON_EXIT`, and nothing once `gc.collect()` has 
run. 29 passed.
   - **`tests/test_dropin_offline.py`:** PySpark's pickles of a catalog and a 
registration now load with their `__dict__` as PySpark's.
   - **`tests/test_dropin_e2e.py`:** a new live test,
     
`test_the_catalog_tvf_registration_and_conf_make_the_requests_of_their_dict_as_pysparks`.
 Against a Spark 4.3.0 server, it covers:
     - `vars()` of the four;
     - a catalog and table-valued functions of a proxy of the session;
     - an assignment of `spark.catalog._sparkSession`;
     - a `RuntimeConf` of a recording client, whose `config` receives PySpark's 
operations.
   
     It passes with both clients.
   - **The 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`;
     - ruff 0.14.0, the no-stub audit, `gen_type_stubs.py --check`, 
`gen_fn_dispatch.py --check`, the license checks, the guards and 
`test_type_stubs_offline.py`;
     - `check_vendored_upstream.sh`;
     - the offline suite of `coverage.yml`;
     - `test_dropin_e2e.py` against a Spark 4.3.0 server.
   
   ### 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