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

   ### What changes were proposed in this pull request?
   
   A follow-up from a review of the last 20 commits (#550 and others). The 
Python drop-in raised the errors of protobuf's **pure-Python** backend where 
PySpark sets a value in a proto field. A default install of `pyspark-client` 
uses protobuf's **upb** backend. The pure-Python backend is used only when 
`PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python` is set, which the environment 
where the earlier changes were calibrated exports. Under upb (protobuf 6.33), 
official PySpark 4.3.0 does the following, and the drop-in now does the same:
   
   - **A str that UTF-8 cannot encode** (a lone surrogate) raises the 
`UnicodeEncodeError` of its encoding, `'utf-8' codec can't encode character 
'\ud800' in position 1: surrogates not allowed`. This was `ValueError: '...' 
isn't a valid unicode string and can't be encoded in UTF-8.` in literals, 
options, fills, the runtime conf, table names and sql. `not_utf8_string` is 
removed, and PyO3's error of the extraction is raised as it is.
   - **Bytes in a string field** are decoded as UTF-8 
(`PyUnicode_FromEncodedObject`, as upb), and bytes that are not UTF-8 raise the 
`UnicodeDecodeError` of their decoding. This applies where PySpark sets the 
value as it is:
     - the key and the value of `spark.conf.set`, and the key of `unset` and 
`isModifiable`;
     - the keys of `SparkSession.builder.config`, also of `map=`;
     - the option keys of the writers (`df.write`, `writeTo`, `writeStream`);
     - the text of `spark.sql`;
     - the names of `_joinAsOf`'s `on` and of `replace`'s subset;
     - the values of `createTable`'s options.
   - **Any other object in a string field** raises `TypeError: bad argument 
type for built-in operation`. The drop-in raised `Cannot set 
spark.connect.KeyValue.value to ...`, `... has type ..., but expected one of: 
...`, or PyO3's cast error.
   - **A bool in an int field**, `dropna(thresh=True)` and `sampleBy(..., 
seed=True)`, is sent as 1 or 0, with a `DeprecationWarning` that the default 
filters hide. The drop-in raised a `TypeError`.
   - **`StreamingQuery.explain(extended)`** reads `extended` as a C long 
(`PyLong_AsLong`), and a NumPy bool by its truth. Another object raises the 
`TypeError` or `OverflowError` of that conversion, and a NumPy array raises 
`[1] has type ndarray, but expected one of: bool`. The conversion now runs in 
the call, as PyO3 would prefix an argument's `TypeError` with its name.
   
   Siblings found while probing the same paths, also as PySpark:
   - `DataFrameReader.option` and `DataStreamReader.option` keep the key in a 
dict, and their `DataSource` plan asserts that a key is a str. A key that is 
not a str, bytes too, raises a bare `AssertionError`.
   - The `option` of a writer or of `DataStreamReader` returns at once for a 
value of None, before it looks at the key (B-4 of the review). 
`DataFrameReader.option` hashes the key even then.
   - A key kept in a dict raises the `TypeError` of an unhashable key 
(`unhashable type: 'bytearray'`).
   - An as-of column of `_joinAsOf` that is not a Column or a str raises 
PySpark's `ColumnReference` `AssertionError`, and the as-of columns are 
converted before `on`.
   - `spark.conf.set(None, v)` and `builder.config(map={None: v})` send the 
empty key, as the constructor of `KeyValue` leaves a field of None unset.
   - `spark.sql` checks `args`, then formats the query object as it is, and 
only then sets it in the plan. So `spark.sql(b"... {x}", x=1)` raises the 
formatter's `expected str, got bytes`.
   
   ### Why are the changes needed?
   
   To match PySpark 4.3.0 as installed by default. Three probe scripts of about 
130 cases each ran against official PySpark under upb, with `env -u 
PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION`, and against this branch, on the same 
Spark 4.3.0 server. Their outputs are now identical. The only remaining 
difference is the module path in the repr of the session and stream writer 
objects, which this PR does not touch.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes. In the Python client:
   - These paths raise the error class and message of PySpark as installed by 
default.
   - Bytes are accepted where PySpark accepts them.
   - A bool `thresh` or `seed` is sent as an int.
   - A None option value no longer checks its key.
   
   ### How was this patch tested?
   
   - Updated the tests that pinned the pure-Python backend's texts, in 
`python/tests/test_dropin_offline.py`, to upb's exact messages and classes:
     - the conf;
     - the two surrogate tests of options, keys, conf, names and sql;
     - literals and sql args;
     - the seeds of sampleBy;
     - explain;
     - createTable.
   - Three new tests:
     - `test_dropna_sends_a_bool_thresh_as_an_int_as_pyspark`;
     - 
`test_protobuf_string_fields_take_bytes_and_refuse_other_objects_as_pyspark`, 
which checks what is sent for bytes and the errors of other objects;
     - `test_option_keys_are_checked_as_the_dicts_and_the_plans_of_pyspark`.
   - `python/tests/_mock_connect_server.py` records a `KeyValue` with no key as 
the empty key.
   - 11 of the 12 changed or new tests fail on the drop-in before this change. 
The 12th only lost its bool cases to the new dropna test.
   - `cargo build -p pyspark-rs --release`, the offline drop-in suites 
(`test_dropin_offline.py`, `test_arrow_handoff.py`), `cargo fmt --all --check`, 
`ruff check`, `scripts/audit_no_stubs.sh`, and clippy, with no new warnings in 
the changed code.
   
   ### 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