This is an automated email from the ASF dual-hosted git repository.
HyukjinKwon pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/spark-connect-rust.git
The following commit(s) were added to refs/heads/master by this push:
new 864611fa [SPARK-61522][CONNECT][PYTHON] Name a Python class as PyArrow
does in the not-a-sequence error of createDataFrame, in the Python client
864611fa is described below
commit 864611faef94114085b3c361ca08437b3ae9c6af
Author: Hyukjin Kwon <[email protected]>
AuthorDate: Thu Nov 12 18:36:19 2026 +0900
[SPARK-61522][CONNECT][PYTHON] Name a Python class as PyArrow does in the
not-a-sequence error of createDataFrame, in the Python client
### What changes were proposed in this pull request?
The `createDataFrame` errors of the Python client that name the type of a
value now name it as PyArrow does. PyArrow formats the type's `tp_name`: the
bare name of a class defined in Python (a heap type), and the module-qualified
name of a builtin or C type. `arrow_type_name`
(`crates/pyspark-rs/src/infer.rs`) now returns the bare `__name__` for a heap
type and keeps the module-qualified name for a static type. The same helper
produces both the "was not a sequence or recognized null f [...]
The comment of `test_freed_instances_release_their_dict_as_pyspark` no
longer claims that `vars()` of a `Column` is PySpark's. Ours holds only the
attributes that were set; PySpark's also holds `_expr`.
### Why are the changes needed?
```python
class DictSub(dict): pass
spark.createDataFrame([(DictSub({1: 2}),)], "a array<int>")
# before: ArrowTypeError: Could not convert {1: 2} with type
__main__.DictSub: was not a sequence ...
# after (and PySpark 4.3.0 with pyarrow 26.0.1): ... with type DictSub: was
not a sequence ...
spark.createDataFrame([(collections.Counter(),)], "a array<int>")
# before: ... with type collections.Counter ... after (and PySpark): ...
with type Counter ...
```
Builtin and C types (`dict`, `collections.OrderedDict`) were already named
as PyArrow names them, and are unchanged. This is a follow-up of SPARK-61483
(#538).
### Does this PR introduce _any_ user-facing change?
Yes, in error messages only. The type name in these two `createDataFrame`
errors is the bare class name for a class defined in Python, as in PySpark.
### How was this patch tested?
- A new offline test,
`test_pyarrow_names_a_type_by_its_tp_name_in_its_errors`, covers a dict
subclass, a `Counter`, a frozenset subclass, `OrderedDict` and `dict`, for both
messages. Its three heap-type cases fail without the fix and pass with it. The
messages were checked against PySpark 4.3.0.
- The offline suite (1907 passed), the e2e suites against a Spark 4.3.0
server (`test_arrow_e2e.py`, `test_dropin_e2e.py`, `test_ml_e2e.py`: 303
passed), the Rust library tests, the `--no-run` builds (with and without
`wasm-udf`), `cargo fmt --check`, `scripts/audit_no_stubs.sh` and the ruff
gates.
### 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.
Closes #555 from HyukjinKwon/SPARK-61522-arrow-type-name.
Authored-by: Hyukjin Kwon <[email protected]>
Signed-off-by: Hyukjin Kwon <[email protected]>
---
crates/pyspark-rs/src/infer.rs | 12 ++++++++++--
python/tests/test_dropin_offline.py | 36 +++++++++++++++++++++++++++++++++++-
2 files changed, 45 insertions(+), 3 deletions(-)
diff --git a/crates/pyspark-rs/src/infer.rs b/crates/pyspark-rs/src/infer.rs
index 5923f979..1b2b0bbd 100644
--- a/crates/pyspark-rs/src/infer.rs
+++ b/crates/pyspark-rs/src/infer.rs
@@ -939,10 +939,18 @@ fn unsupported(obj: &Bound<'_, PyAny>) -> PyResult<PyErr>
{
)))
}
-/// The name of the type of `obj` as PyArrow writes it in its errors: with its
module, unless
-/// it is a builtin type, such as `int` or `numpy.int64`.
+/// The name of the type of `obj` as PyArrow writes it in its errors, which is
the `tp_name` of
+/// the type: the bare name of a heap type, a class defined in Python, such as
`Counter`; else
+/// the name with its module, unless it is a builtin type, such as `int` or
`numpy.int64`.
fn arrow_type_name(obj: &Bound<'_, PyAny>) -> PyResult<String> {
let ty = obj.get_type();
+ // SAFETY: `ty` is a live type object, and PyType_HasFeature only reads
its flags.
+ let heap_type = unsafe {
+ pyo3::ffi::PyType_HasFeature(ty.as_type_ptr(),
pyo3::ffi::Py_TPFLAGS_HEAPTYPE) != 0
+ };
+ if heap_type {
+ return Ok(ty.name()?.to_string());
+ }
let (module, name) = (ty.module()?.to_string(),
ty.qualname()?.to_string());
Ok(if module == "builtins" {
name
diff --git a/python/tests/test_dropin_offline.py
b/python/tests/test_dropin_offline.py
index 041dc9a5..52027cb2 100644
--- a/python/tests/test_dropin_offline.py
+++ b/python/tests/test_dropin_offline.py
@@ -7,6 +7,7 @@ that require a live server live in test_dropin_e2e.py and are
gated on SPARK_REM
"""
import array
+import collections
import contextlib
import datetime
import decimal
@@ -7690,7 +7691,8 @@ def
test_variant_and_spatial_values_are_sent_and_collected_by_their_attributes_a
def test_freed_instances_release_their_dict_as_pyspark(mock_connect):
# As PySpark's classes, plain Python classes, whose instance __dict__
CPython releases with
- # the instance: an attribute is set, read, listed by vars() and dir() and
deleted; one that
+ # the instance: an attribute is set, read, listed by dir() and deleted,
and vars() of a Column
+ # here holds the attributes set and nothing else (PySpark's also holds
_expr); one that
# shadows a method, as monkeypatching sets it, is called in its place
until it is deleted;
# and a freed instance releases its dict, which keeps its items while it
is referenced. Of
# a class, of one that extends it in Rust (ArrayType) and of a Python
subclass (a UDT), whose
@@ -15814,6 +15816,38 @@ def
test_an_array_of_no_converter_takes_the_values_that_pyarrow_takes_as_pyspark
assert table.column(0).to_pylist() == [[0, 1], [1, 2], [2], [3]]
+class _DictSub(dict):
+ pass
+
+
+class _FrozenSub(frozenset):
+ pass
+
+
[email protected]("value, type_name", [
+ (_DictSub({1: 2}), "_DictSub"),
+ (collections.Counter(), "Counter"),
+ (_FrozenSub([1]), "_FrozenSub"),
+ (collections.OrderedDict(), "collections.OrderedDict"),
+ ({}, "dict"),
+])
+def test_pyarrow_names_a_type_by_its_tp_name_in_its_errors(mock_connect,
value, type_name):
+ # As PyArrow's errors, which name the type of a value by its tp_name: the
bare name of a class
+ # defined in Python, and the module with the name of a builtin or C type,
as OrderedDict.
+ import pyarrow as pa
+
+ spark, _ = mock_connect
+ with pytest.raises(pa.ArrowTypeError) as e:
+ spark.createDataFrame([(value,)], "a array<int>")
+ assert str(e.value) == (
+ f"Could not convert {value!r} with type {type_name}: was not a
sequence or recognized "
+ "null for conversion to list type"
+ )
+ with pytest.raises(pa.ArrowTypeError,
+ match=f"^Expected bytes, got a '{re.escape(type_name)}'
object$"):
+ spark.createDataFrame([(T.VariantVal(b"\x0c\x01", value),)], "v
variant")
+
+
def
test_an_array_of_no_converter_reads_a_sequence_by_its_length_and_items_as_pyarrow(
mock_connect,
):
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]