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

   ### What changes were proposed in this pull request?
   
   [SPARK-61290](https://issues.apache.org/jira/browse/SPARK-61290): raise 
`CANNOT_INFER_ARRAY_ELEMENT_TYPE` for a list without an element type in the 
Column methods of the Python client, as PySpark does.
   
   - `crates/pyspark-rs/src/functions.rs`: a new `to_value_column` converts a 
value that a Column method takes. A list becomes a typed array literal, as 
PySpark's `LiteralExpression._from_value` makes it: the element type is that of 
the first element, an empty list or one that starts with None raises 
`CANNOT_INFER_ARRAY_ELEMENT_TYPE`, and each element is converted to that type 
(a None element is a typed null, a nested list is an array of the element type, 
and a value of another type raises a bare `AssertionError`, as PySpark's 
asserts do). `to_column` is unchanged, so `lit`, the functions and the other 
callers keep building `array(...)`.
   - `crates/pyspark-rs/src/column.rs`: `when`, `otherwise`, `getItem`, 
`__getitem__` and the elements of `isin` use `to_value_column`. These are the 
Column methods that PySpark converts their values through `LiteralExpression`.
   
   The comparison and arithmetic operators, `between`, `contains`, `startswith` 
and `endswith` are unchanged: PySpark does not convert a list in those methods 
(it raises `AttributeError`), so they do not reach the typed literal.
   
   ### Why are the changes needed?
   
   With this change `F.col("id") > 0` style code that passes `[]` or `[None, 
2]` to `when`, `otherwise` or `getItem` fails on the Python client as it does 
on PySpark 4.3.0, instead of sending an `array()` of the elements. A list of a 
valid type sends the same literal as PySpark: for `[1, None, 3]`, `array<int>` 
with the null typed as `int`. `F.when(F.lit(True), [])` still returns an 
`array()`, as PySpark's functions do.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes, in the Python client only. `Column.otherwise([])`, 
`Column.otherwise([None, 2])`, `Column.when(cond, [])`, `Column.getItem([])` 
and `Column[[]]` now raise `PySparkTypeError` with 
`CANNOT_INFER_ARRAY_ELEMENT_TYPE`, as PySpark does. Previously they were 
accepted and sent as `array(...)`, for example `Row(o=[])` and `Row(o=[None, 
2])`.
   
   ### How was this patch tested?
   
   - Offline, `python/tests/test_dropin_offline.py`: 
`test_column_methods_cannot_infer_the_element_type_of_a_list_as_pyspark` 
(parametrized over `[]`, `[None, 2]` and `[[], [1]]`, across when/otherwise, 
getItem, `[]` and isin), 
`test_column_methods_send_a_list_as_the_typed_array_literal_of_pyspark` (the 
sent literal for valid lists, including nested lists, a long element type, a 
bool among ints, and the AssertionError cases), and 
`test_functions_lit_of_a_list_stays_an_array_as_pyspark` (`F.lit([])` and 
`F.when(F.lit(True), [])` remain `array()`). With the fix reverted, the first 
two fail; the third passes both ways.
   - Live, `python/tests/test_dropin_e2e.py`: 
`test_column_list_values_are_typed_array_literals_on_the_server_as_pyspark` 
against a Spark 4.3.0 Connect server: `otherwise([])` and `otherwise([None, 
2])` raise `CANNOT_INFER_ARRAY_ELEMENT_TYPE`, `otherwise([1, 2])` gives the 
rows `[Row(o=[1, 2]), Row(o=[1])]` as PySpark does, and `F.when(F.lit(True), 
[])` gives `[Row(w=[])]`.
   - The literal sent for 23 lists was compared with PySpark 4.3.0's 
`LiteralExpression` protos (`getItem` argument), and all match, including the 
error cases.
   - `cargo test -p apache-spark-connect --release --lib` (528 passed), 
`--no-run` and `--features wasm-udf --no-run`, and `cargo fmt --all --check` 
pass. Clippy reports no diagnostics on the changed lines.
   - Full offline suites (`test_dropin_offline.py`, `test_ml_offline.py`, 
`test_arrow_handoff.py`): 2932 passed, 8 skipped. Live suites 
(`test_arrow_e2e.py`, `test_dropin_e2e.py`, `test_ml_e2e.py`): 237 passed, 1 
skipped. Two clippy-only edits after these runs were re-tested with the 
affected offline and live tests.
   
   ### Was this patch authored or co-authored using generative AI tooling?
   
   Generated-by: Claude Opus 5.5 and Claude Haiku 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