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

   ### What changes were proposed in this pull request?
   
   [SPARK-61221](https://issues.apache.org/jira/browse/SPARK-61221): raise 
`ValueError` for a non-nullable null type in `createDataFrame` of local rows, 
as PySpark does, instead of a panic or a raw Arrow error.
   
   - A non-empty `createDataFrame` of local rows now raises `ValueError: A null 
type field may not be non-nullable` when the schema has a null type that is not 
nullable, at any depth: a struct field, an array element 
(`ArrayType(NullType(), False)`), a map value (`MapType(..., NullType(), 
False)`) or a map key. PySpark raises it when its Arrow schema conversion 
builds the field with `pa.field(name, pa.null(), nullable=False)`, after the 
values are converted and before any data is sent, so the check runs after the 
value conversion, as in PySpark. Empty data (a zero-length list, an empty 
pandas or Arrow table) is not converted by PySpark and is accepted, and so it 
is here.
   - The Rust core builds list arrays with `ListArray::try_new`, so a 
non-nullable element that has nulls is an error (`list build: ...`) instead of 
a panic from `unwrap()` on `ListArray::new`.
   
   Before this change, the list of a non-nullable NullType element with a 
`None` panicked the client (a `PanicException`, which `except Exception` does 
not catch). A map value or a struct field of such a type raised 
`PySparkRuntimeError` with the raw Arrow text. An empty or `None` value was 
accepted silently, including `createDataFrame(spark.sql("SELECT 
array()").collect(), schema)`.
   
   Checked against official PySpark 4.3.0 with the same probes (rows, pandas, 
Arrow tables, NumPy arrays, with and without data): the exception class, the 
message and the cases that raise now match. The pandas and Arrow paths raise 
from PyArrow, as before, and the NumPy path raises the same server error as 
before.
   
   ### Why are the changes needed?
   
   A non-nullable NullType with data is not a valid Arrow field, so the client 
must reject it as PySpark does. Repro:
   
   ```python
   spark.createDataFrame([Row(a=[None])], StructType([StructField("a", 
ArrayType(NullType(), False))]))
   ```
   
   On master this panics the Python process (`pyo3_runtime.PanicException: 
called Result::unwrap() on an Err value: InvalidArgumentError("Non-nullable 
field of ListArray \"element\" cannot contain nulls")`). Official PySpark 4.3.0 
raises `ValueError: A null type field may not be non-nullable`. The same schema 
with `Row(s=Row(x=None))` (a non-nullable NullType struct field) or `{"k": 
None}` (a map value) raised `PySparkRuntimeError: struct build: Invalid 
argument error: Found unmasked nulls ...` on master.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes. `createDataFrame` of non-empty local rows with a non-nullable NullType 
raises `ValueError` ("A null type field may not be non-nullable") instead of 
panicking or raising `PySparkRuntimeError`, and an empty or `None` value no 
longer passes silently for it. Nullable NullType schemas and empty data are 
unchanged.
   
   ### How was this patch tested?
   
   - `test_non_nullable_null_type_is_rejected_before_the_rpc_as_pyspark` 
(offline, mock Connect server): nine rejected schemas (struct field, array 
element, array of array, array of struct, map value, map key, struct in a map 
value) raise `ValueError` with the message, and send no Execute RPC; accepted 
schemas (empty data, nullable element, nullable field) are not rejected. The 
test fails on the code before this change (the check was disabled to confirm).
   - `a_non_nullable_element_with_nulls_is_an_error_not_a_panic` (Rust, 
`apache-spark-connect` lib): a list with a null element of a non-nullable 
NullType element returns an error naming `list build`, and an empty list is 
accepted. The test fails on the code before this change (the panic from 
`ListArray::new`).
   - The same probe matrix (28 cases over rows, pandas, Arrow tables, NumPy 
arrays, and the server `SELECT array()` schema) gives identical results on 
official PySpark 4.3.0 and on the drop-in, for class, message and success.
   - The lib tests of both crates (659 passed), the build of all tests, also 
with `wasm-udf`, the offline Python suites (2704 passed, 8 skipped), and the 
live drop-in and Arrow suites (224 passed) and live cases (12 passed) against a 
local Spark 4.3.0 server. Clippy reports no diagnostics in the changed lines.
   
   ### 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