HyukjinKwon opened a new pull request, #932:
URL: https://github.com/apache/spark-connect-rust/pull/932
### What changes were proposed in this pull request?
This is the eighth sub-task of SPARK-62207, which closes the rows "The
`__dict__` of the objects of a session" and "Subclasses" of
`dev/parity/known-differences.md`. This PR covers the `SparkSession.Builder` of
`pyspark.sql.connect.session`, which `SparkSession.builder` makes at each
access, and which `pyspark.sql.SparkSession.builder`, PySpark's classic
builder, makes its sessions with.
In PySpark the builder is a plain Python class. `__init__` sets `_options =
{}`, `_channel_builder = None` and `_hook_factories = []`:
- `config` sets `self._options[key] = to_str(value)`, or that of each item
of `map`, while it holds `self._lock`; `remote` and `appName` are
`self.config("spark.remote", location)` and `self.config("spark.app.name",
name)`;
- `channelBuilder` sets `_channel_builder`, and `_registerHook` appends to
`_hook_factories`;
- `create` reads `_channel_builder` and `_options` (both or neither raise
`SESSION_NEED_CONN_STR_OR_BUILDER`, and a `spark.remote` of None fails an
assertion), makes the session of the hooks of `_hook_factories`, and calls
`self._apply_options(session)`;
- `getOrCreate` returns the active or the default session, or
`self.create()`, and calls `self._apply_options(session)`;
- `_apply_options` sends the options but `spark.remote` and `spark.master`
in one silent `ConfigRequest.Set` (`session.conf._set_all(configs=opts,
silent=True)`), after the `spark.sql.` options of the last
`PYSPARK_REMOTE_INIT_CONF_<i>` of the environment.
In the drop-in, the builder held its options in a native, locked structure
shared by its copies, with a Python object that reported them to the garbage
collector. As a result `vars()` of a builder was empty, an assignment of
`_options` had no effect, the class could not be subclassed (`TypeError: type
'Builder' is not an acceptable base type`), a subclass's `config`, `create` or
`_apply_options` would not have been called, and a key of None of `config` was
dropped.
The object-state test file of SPARK-62208 compares the builder with goldens
captured from PySpark 4.3.0:
| | Passed | Strict xfails |
|---|---|---|
| Before, master ab03fcff | 164 | 259 |
| After, this PR | 180 | 245 |
The 14 generic cases that this PR fixes are the builder's: its `vars`,
`attributes`, `copies` (copy, deepcopy, pickle and cloudpickle, of a new
builder and of one of its setters), `construct` and `uninit` cases all pass,
and they are deleted from the group "the session and its builder" of
`_DIFFERS_BY_CHANGE`, which keeps the session's for SPARK-62216. The other 2
passing tests are new (below).
**The changes** (`crates/pyspark-rs/src/session.rs`):
- The native object is empty. `__init__` sets PySpark's three attributes,
and raises CPython's `TypeError` of any argument, as PySpark's `__init__(self)`
does.
- `config` sets `self._options[k] = to_str(v)` while it holds the lock of
the builder: of each item of `map.items()`, of any mapping, or else of `key`
and `value`, of any key, None included, as PySpark's. The drop-in's `conf`
argument (a `SparkConf`) sets each of its `getAll()` pairs so.
- `remote` and `appName` call `self.config(...)`, of a subclass too.
`remote` keeps CPython's errors of its arguments, and `sc://localhost` of none.
- `channelBuilder` sets `_channel_builder`, and `_registerHook` appends to
`_hook_factories`, while they hold the lock.
- `create` reads `_channel_builder` and `_options` at the call, with
PySpark's errors, and makes the session of the URL of
`to_str(self._options.get("spark.remote"))` or of the channel builder, and of
the hooks of `_hook_factories`. A str that UTF-8 cannot encode raises the
`UnicodeEncodeError` of the encoding of its endpoint, as PySpark's channel
raises it, where the drop-in raised `SESSION_NEED_CONN_STR_OR_BUILDER`. It then
calls `self._apply_options(session)`.
- `getOrCreate` calls `self.create()` and `self._apply_options(session)`, of
a subclass too.
- `_apply_options` is a method of the builder, as PySpark's: it reads the
init confs of the environment and then `_options`, while it holds the lock, and
sets them on the session in one silent request, of the pairs that PySpark's
`KeyValue(key=k, value=v)` makes: a bool as `"true"` or `"false"`, an int as
its `str()`, a key or a value of None unset, and the first key or value that
protobuf refuses failing the request before it is sent, of protobuf's error
(`TypeError`, `UnicodeEncodeError`, `UnicodeDecodeError`).
- `__new__` takes any arguments, and the signature is PySpark's `()`.
`__getstate__` returns the `__dict__`, and `__reduce_ex__` is that of a plain
object: a shallow copy shares the dict of the options and the list of the hook
factories, a deep copy and a pickle copy them, and a pickle is PySpark's, of
`pyspark.sql.connect.session` `SparkSession.Builder` and the dict, which a
worker's PySpark loads as it is.
- Removed: `BuilderOptions`, the `PySharedObjects` reporter of the builder,
`__copy__`, `__reduce__`, `__traverse__`, `__clear__`, and `builder_reduce` of
`_state_reduce.py`. The builder holds its options, its channel builder and its
hook factories in its `__dict__`, which the garbage collector traverses, as
PySpark's.
- **Docs:**
- `dev/parity/known-differences.md`: the `__dict__` row lists the builder
as PySpark's, and the Subclasses row lists `SparkSession.Builder` among the
subclassable classes.
- `docs/compatibility.md`: the same, a paragraph on the state of the
builder, and the garbage-collection and copy paragraphs no longer count the
builder among the objects whose state is native.
Nothing of the builder remains different: it builds no plan, and its
requests are those of the session that it makes.
### Why are the changes needed?
In PySpark, code that reads or writes the state of a builder works because
the state is in its `__dict__`: `vars(SparkSession.builder.remote(url))`, an
assignment of `_options`, a subclass whose `create` or `_apply_options` is its
own, copies and pickles. Until now, none of this worked in the drop-in.
### Does this PR introduce _any_ user-facing change?
Yes. `SparkSession.Builder` keeps its state in its `__dict__`, as PySpark's
does:
- `vars(SparkSession.builder.remote("sc://h"))` is `{'_options':
{'spark.remote': 'sc://h'}, '_channel_builder': None, '_hook_factories': []}`;
- an assignment of the state, or a subclass's method, changes what `create`
and `getOrCreate` do;
- the class can be subclassed;
- `create` and `getOrCreate` send the init confs of the environment that
PySpark's send, and a key of None of `config`;
- copies and pickles carry the `__dict__`.
### How was this patch tested?
- **`tests/test_dropin_object_state_offline.py`:** the builder's cases are
deleted from `_DIFFERS_BY_CHANGE`: 180 passed and 245 xfailed on this branch,
and 425 passed with PySpark 4.3.0. Two new tests cover what this PR adds, and
pass with both clients:
- a builder creates its session of the state that it holds then (assigned
options, init confs of the environment, a subclass's `config`, `create` and
`_apply_options`, a channel builder and a `spark.remote` together, a
`spark.remote` of None);
- the options are sent as PySpark's `_set_all` sends them, and the first
that protobuf refuses fails the request before it is sent, of its error.
- **`tests/test_dropin_offline.py`:** three assertions that read the old
reduce of the builder now read its `_options` and `_hook_factories`, as
PySpark's.
- **`tests/test_dropin_e2e.py`:** a new live test,
`test_a_builder_creates_its_session_of_its_dict_as_pysparks`. Against a Spark
4.3.0 server, a subclass's builder of an assigned option and a hook factory
makes a session of that option and that hook, of its own `_apply_options`. 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`;
- 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]