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

   ### What changes were proposed in this pull request?
   
   A follow-up of #538 (SPARK-61483), from a review of it. Both changes are in 
`crates/pyspark-rs/src/infer.rs`, the conversion of the local rows of 
`createDataFrame` in the Python client.
   
   1. **The value of an array, as PyArrow reads it.** This is about an array 
column whose elements need no converter (an array of numbers, bools, dates, 
times or day-time intervals). PySpark passes such a value to PyArrow as it is. 
#538 added `is_arrow_list`, which took a value as PyArrow's list converter 
does, but then iterated it. PyArrow does not iterate a sequence: it reads it by 
its length and its items. The new `arrow_list` reads the value as PyArrow does:
      - A NumPy array that is not one-dimensional raises PyArrow's 
`ArrowInvalid` "Can only convert 1-dimensional array values of list<element: 
int32> to a variable-sized list", with the Arrow type of the column.
      - A list or a tuple is read as it is stored, also when it is of a 
subclass that overrides `__iter__` or `__getitem__`.
      - Another sequence (`PySequence_Check`), such as a range or a NumPy 
array, is read with `PySequence_Size`, then `PySequence_GetItem` of each index. 
Their errors are raised as PyArrow raises them: "object of type 'X' has no 
len()" for a `__getitem__` without a `__len__`, and the `IndexError` of the 
first index past the items for a `__len__` that is longer than them. A shorter 
`__len__` ends the list there.
      - A set and a `dict_values` are still iterated. Any other value is still 
"not a sequence".
   
      As before, these errors wait for the converters of the other columns, and 
the first one in the order of the columns, then of the rows, is raised.
   2. **An int by the `nb_int` slot, as PyArrow takes it.** `py_int`, which 
takes an int for an int, date, time or day-time interval value that PySpark 
passes to PyArrow, checked for an `__int__` attribute on the type. PyArrow's 
`PyObjectToPyInt` calls the type's `nb_int` slot. A NumPy `str_` or `bytes_` 
has no `nb_int`, but its type has the `__int__` of `numpy.generic`. So 
`np.str_("1")` was taken as the int 1, where PyArrow raises. `py_int` now reads 
the slot with `PyType_GetSlot`. Python 3.9 reads the slots of heap types only, 
so there the `__int__` of a static type stands for its slot, as before.
   
   ### Why are the changes needed?
   
   To match PySpark 4.3.0. Probes with the Connect client of official PySpark 
4.3.0 and with this client, against a local Spark 4.3.0 server.
   
   `GetOnly` has only a `__getitem__`. `LongLen` and `ShortLen` add a `__len__` 
that is 3 longer than the items, and one that is 1. `OwnIter` has a `__len__`, 
and an `__iter__` that gives 9s. The `list` and `tuple` subclasses also 
override `__iter__` (9s) and `__getitem__` (8):
   
   | Value in an `array<int>` column | PySpark 4.3.0 | Before |
   |---|---|---|
   | `GetOnly([1, 2])` | `TypeError: object of type 'GetOnly' has no len()` | 
`[1, 2]` |
   | `LongLen([1, 2])` | `IndexError: list index out of range` | `[1, 2]` |
   | `ShortLen([1, 2])` | `[1]` | `[1, 2]` |
   | `OwnIter([1, 2])` | `[1, 2]` | `[9, 9]` |
   | a `list` subclass of `[1, 2]` | `[1, 2]` | `[9, 9]` |
   | a `tuple` subclass of `(1, 2)` | `[1, 2]` | `[9, 9]` |
   | `np.array([[1, 2], [3, 4]])` | `ArrowInvalid: Can only convert 
1-dimensional array values of list<element: int32> to a variable-sized list` | 
`ArrowInvalid: Could not convert array([1, 2]) with type numpy.ndarray: tried 
to convert to int32` |
   | `np.zeros((0, 2))` | the same `ArrowInvalid` | `[]` |
   | `np.array(5)` | the same `ArrowInvalid` | `ArrowTypeError: ... was not a 
sequence or recognized null for conversion to list type` |
   | `np.array([[1, 2]])` in an `array<array<int>>` column | `ArrowInvalid` of 
`list<element: list<element: int32>>` | `[[1, 2]]` |
   | `GetOnly([1])`, then `["x"]` in the next row | `TypeError: object of type 
'GetOnly' has no len()` | `ArrowInvalid: Could not convert 'x' with type str: 
tried to convert to int32` |
   | `np.array(["1", "2"])` | `ArrowInvalid: Could not convert np.str_('1') 
with type numpy.str_: tried to convert to int32` | `[1, 2]` |
   
   | Value, column | PySpark 4.3.0 | Before |
   |---|---|---|
   | `np.str_("1")`, `int` | `ArrowInvalid: Could not convert np.str_('1') with 
type numpy.str_: tried to convert to int32` | `1` |
   | `np.bytes_(b"1")`, `bigint` | `ArrowInvalid: ... tried to convert to 
int64` | `1` |
   | `np.str_("1")`, `date` | `ArrowTypeError: object of type <class 
'numpy.str_'> cannot be converted to int` | `1970-01-02` |
   | `np.bytes_(b"1")`, `time` | `ArrowTypeError: object of type <class 
'numpy.bytes_'> cannot be converted to int` | sent to the server |
   
   After the change, this client gives PySpark's result in each of these cases. 
One difference remains, for a `__len__` that raises: PyArrow 26 loses that 
error, and PySpark raises `SystemError: <class 'ValueError'> returned a result 
with an exception set`. This client raises the error of `__len__`.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes, on unreleased changes. In the Python client's `createDataFrame` of 
local rows, an array value is read as PyArrow reads it, and a NumPy `str_` or 
`bytes_` in an int, date or time column raises PyArrow's error. The table above 
shows each case.
   
   ### How was this patch tested?
   
   - 
`test_an_array_of_no_converter_reads_a_sequence_by_its_length_and_items_as_pyarrow`
 in `python/tests/test_dropin_offline.py`, new. It covers the read values of 
the sequences above and their errors, the order of these errors against a 
converter's error and against an element's error, and the NumPy arrays that are 
not one-dimensional, for several element types and a nested array.
   - `test_values_of_local_rows_convert_and_raise_as_pyarrow`, which now has 
the NumPy `str_` and `bytes_` cases.
   - Both fail on an extension built from master and pass with the change. 
Every expected value and message was checked with official PySpark 4.3.0.
   - The offline drop-in and ML suites (`tests/test_dropin_offline.py 
tests/test_ml_offline.py`), `cargo build -p pyspark-rs --release`, `cargo fmt 
--all -- --check`, `ruff check` and `ruff format --check`, and 
`scripts/audit_no_stubs.sh`.
   
   ### 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