HyukjinKwon opened a new pull request, #842:
URL: https://github.com/apache/spark-connect-rust/pull/842
### What changes were proposed in this pull request?
PySpark's `ChannelBuilder.session_id` (`connect/client/core.py:438-447`)
checks the `session_id` of the connection string with `uuid.UUID(session_id,
version=4)`. That does not check the version, and it takes more than the
canonical form.
- **What it takes.** Python's `uuid` first removes every `urn:` and then
every `uuid:`, strips braces from both ends and removes every `-`. What remains
must be 32 characters that `int(hex, 16)` reads, which `int` allows to have:
- whitespace;
- a sign;
- a `0x` prefix;
- single underscores between digits;
- non-ASCII decimal digits.
- **Its errors.** Its `ValueError` is `badly formed hexadecimal UUID
string`, or `int`'s `invalid literal for int() with base 16: '<repr>'`. PySpark
raises `INVALID_SESSION_UUID_ID` with that text as its `origin`.
- **What it sends.** The session ID is sent as given. The server refuses the
non-canonical forms as a handle (`INVALID_HANDLE.FORMAT`) at the first request.
The core (`ChannelBuilder::session_id`,
`crates/spark-connect-core/src/channel.rs`) checked the ID with the uuid
crate's `Uuid::parse_str`, with the origin `invalid UUID`. That path serves
`SparkSession.builder.remote(...).create()` of the drop-in and the Rust API's
builder. `spark.client`'s `ChannelBuilder` is Python's and was already right.
Checked on both clients against a Spark 4.3.0 server, with `u` a fresh UUID:
| `session_id=` | pyspark-client 4.3.0 | drop-in before |
|---|---|---|
| `u`, its upper case | the session works | the same |
| `uuid:u`, `urn:u`, `{32 hex digits}`, `1-2345678123442349234123456789abc`
| made; the first request raises the server's `INVALID_HANDLE.FORMAT` |
`INVALID_SESSION_UUID_ID`, `invalid UUID`, at `create()` |
| `urn:uuid:u`, `{u}` | made; the first request raises
`INVALID_HANDLE.FORMAT` | the same |
| `x` | `INVALID_SESSION_UUID_ID`, `badly formed hexadecimal UUID string` |
`INVALID_SESSION_UUID_ID`, `invalid UUID` |
The change, in `channel.rs`:
- `python_uuid_error` makes Python's checks and messages. Behind it:
- `python_hex_int` is `int(hex, 16)`. Its whitespace is Python's,
including the vertical tab and Unicode whitespace. It reads Unicode decimal
digits as their ASCII digits, as `_PyUnicode_TransformDecimalAndSpaceToASCII`
does.
- `python_str_repr` is Python's `repr` of a str, cut at 200 characters as
`int`'s `%.200R`. It escapes the characters of the categories that Python does
not print (`Cc`, `Cf`, `Co`, `Cn`, `Zl`, `Zp`, `Zs`, but the space).
- The Unicode classes come from the `regex` crate that the core already
depends on.
- `session_id` returns the ID as given, or `INVALID_SESSION_UUID_ID` with
that origin.
The same commit corrects what `docs/compatibility.md` and `proxy.rs` said of
a proxy port past 65535: grpcio does not fail to connect there. It takes the
port modulo 65536 and connects to that, so a proxy of port 99999 is reached at
34463. A probe through a proxy on an ephemeral port P, with
`https_proxy=http://127.0.0.1:<P+65536>`, tunnels through P on grpcio. The
client here still refuses such an address when the session is made, as before.
### Why are the changes needed?
So that a session ID that PySpark takes is taken, and a refused one raises
PySpark's message.
### Does this PR introduce _any_ user-facing change?
Yes. `SparkSession.builder.remote(...).create()`, in the drop-in and in the
Rust API, takes every session ID that Python's `uuid.UUID` reads, as given, and
the error of another one has the text of Python's `ValueError`. The server's
handling of the IDs that it refuses is unchanged.
### How was this patch tested?
- **Probes of both clients against a Spark 4.3.0 server:**
`p_session_uuid.py` gives the table above. After the change, the drop-in's rows
are PySpark's.
- **New tests**, each failing without the change:
- `session_id_is_checked_as_pythons_uuid` in `channel.rs`, of 28 taken
texts and 23 refused ones. The expected messages are those of Python 3.11's
`uuid.UUID`, including:
- whitespace, `0x`, `+` and underscores;
- fullwidth, Arabic-Indic and mathematical digits;
- quotes, controls, a lone ``, a private-use character, an unassigned
code point;
- a repr cut at 200 characters.
- `test_session_id_of_the_connection_string_is_checked_as_pythons_uuid` in
`test_dropin_offline.py`, on the mock server. It compares 17 session IDs with
Python's own `uuid.UUID`:
- the taken ones are sent as given;
- the refused ones raise the same origin.
- `cargo test -p apache-spark-connect-core --release --lib -- channel`: 27
passed.
- `cargo test -p pyspark-rs --lib`: 13 passed.
- The offline suite of `coverage.yml`: 6328 passed, 77 skipped.
- `test_dropin_e2e.py` against a Spark 4.3.0 server: 318 passed.
- **Lint and guards:**
- `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`;
- ruff 0.14.0, `scripts/audit_no_stubs.sh`, the stub and dispatch
generators' `--check`;
- `test_type_stubs_offline.py`;
- the test-list guards.
One difference remains, of the characters that Unicode assigned after its
version 14.0, which Python 3.11 has, and the newer tables of the `regex` crate
know. Such a decimal digit is read as a digit, where Python 3.11's `int`
refuses it. Another such character is shown as it is in the error, where
Python's `repr` escapes it as unassigned.
### 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]