HyukjinKwon opened a new pull request, #927:
URL: https://github.com/apache/spark-connect-rust/pull/927
### What changes were proposed in this pull request?
A follow-up of SPARK-62189 (#919), stacked on SPARK-62224 (the bound of the
walks of the type objects, which it uses). It covers the uses of such a struct
that the KD row "A StructType whose fields hold an element that is not a
StructField" kept after #919.
PySpark's `StructType` methods read each element of `fields` as they go, and
take the element as it is:
- an element that lacks the attribute a use reads (a str appended in place)
raises that `AttributeError`;
- an element that has it (a duck-typed field) is used.
An assignment of `fields` takes any elements, pickles and copies keep them,
and the types made of such a struct keep it. The drop-in lowered the list into
the core's struct type at each use, and raised `TypeError: StructType fields
must be StructField objects`.
So:
- **`json()` and `jsonValue()` write the `jsonValue` of each element**,
through `ForeignFields`, as `createDataFrame` of no rows does. This applies to
such a struct and to a `StructField`, `ArrayType` or `MapType` that holds one.
`json_value_of_native` is used at the top, so a Python subclass's own method
that calls the native one does not loop.
- **The other methods read the elements as PySpark does:**
- `simpleString()` joins each element's `simpleString()`, as PySpark's
`",".join` does;
- `treeString()` calls each element's `_build_formatted_string`, through a
port of PySpark's `treeString` (`_foreign_tree_string` of the shim);
- a lookup by name compares each element's `name`, and a slice is
`StructType(...)` of the elements;
- `add()` appends the field and its name, then reads each element's
`needConversion()`;
- `toNullable()` reads each element's `name`, `dataType` and `metadata`,
in PySpark's order.
- **An assignment of `fields` takes any elements**, and such a struct
pickles and copies its `__dict__`, as PySpark's does. For the uses that do not
read an element, the stand-in of the element is a field of the null type. So a
`StructField`, an `ArrayType` and a `MapType` of such a struct are made and
keep it.
- **The plans read the object as PySpark's `pyspark_types_to_proto_types`
does, when the request is built.** `py_to_proto_data_type` uses
`foreign_proto_type`, which reads each element's `name`, `dataType`, `nullable`
and `metadata`. This covers:
- `DataFrame.to`, whose schema is the object;
- a cast, whose repr shows the object's `simpleString()` and raises its
error, and whose requests raise the type's error;
- a UDF, and a map or group function, whose payload holds the stand-in
until the build.
- **Docs:** the KD row of such a struct is removed, and
`docs/compatibility.md` describes its uses.
Checked on both clients against a Spark 4.3.0 server. Each use of a struct
with a str, a duck-typed field or another object appended to its `fields`:
| use | pyspark-client 4.3.0 | e2 (46b2dc93) | now |
|---|---|---|---|
| str | json(): raises AttributeError: 'str' object has no attribute
'jsonValue' | json(): raises TypeError: StructType fields must be StructField
objects | json(): raises AttributeError: 'str' object has no attribute
'jsonValue' |
| str | jsonValue(): raises AttributeError: 'str' object has no attribute
'jsonValue' | jsonValue(): raises TypeError: StructType fields must be
StructField objects | jsonValue(): raises AttributeError: 'str' object has no
attribute 'jsonValue' |
| str | simpleString(): raises AttributeError: 'str' object has no attribute
'simpleString' | simpleString(): raises TypeError: StructType fields must be
StructField objects | simpleString(): raises AttributeError: 'str' object has
no attribute 'simpleString' |
| str | treeString(): raises AttributeError: 'str' object has no attribute
'_build_formatted_ | treeString(): raises TypeError: StructType fields must be
StructField objects | treeString(): raises AttributeError: 'str' object has no
attribute '_build_formatted_ |
| str | repr: StructType([StructField('a', LongType(), True), x]) | repr:
StructType([StructField('a', LongType(), True), x]) | repr:
StructType([StructField('a', LongType(), True), x]) |
| str | str: "StructType([StructField('a', LongType(), True), x])" | str:
"StructType([StructField('a', LongType(), True), x])" | str:
"StructType([StructField('a', LongType(), True), x])" |
| str | ['a']: StructField('a', LongType(), True) | ['a']: StructField('a',
LongType(), True) | ['a']: StructField('a', LongType(), True) |
| str | ['x']: raises AttributeError: 'str' object has no attribute 'name' |
['x']: raises TypeError: StructType fields must be StructField objects | ['x']:
raises AttributeError: 'str' object has no attribute 'name' |
| str | [1]: 'x' | [1]: 'x' | [1]: 'x' |
| str | [0:2]: raises AttributeError: 'str' object has no attribute 'name' |
[0:2]: raises TypeError: StructType fields must be StructField objects | [0:2]:
raises AttributeError: 'str' object has no attribute 'name' |
| str | [-1]: 'x' | [-1]: 'x' | [-1]: 'x' |
| str | fieldNames(): ['a'] | fieldNames(): ['a'] | fieldNames(): ['a'] |
| str | len: 2 | len: 2 | len: 2 |
| str | 'x' in: True | 'x' in: True | 'x' in: True |
| str | list(): [StructField('a', LongType(), True), 'x'] | list():
[StructField('a', LongType(), True), 'x'] | list(): [StructField('a',
LongType(), True), 'x'] |
| str | add: "raises AttributeError: 'str' object has no attribute
'needConversion'; fields=[ | add: "raises TypeError: StructType fields must be
StructField objects; fields=[Struct | add: "raises AttributeError: 'str' object
has no attribute 'needConversion'; fields=[ |
| str | needConversion(): True | needConversion(): True | needConversion():
True |
| str | toInternal: (1, 2) | toInternal: (1, 2) | toInternal: (1, 2) |
| str | fromInternal: Row(a=1) | fromInternal: Row(a=1) | fromInternal:
Row(a=1) |
| str | toNullable(): raises AttributeError: 'str' object has no attribute
'name' | toNullable(): raises AttributeError: 'str' object has no attribute
'dataType' | toNullable(): raises AttributeError: 'str' object has no attribute
'name' |
| str | typeName(): 'struct' | typeName(): 'struct' | typeName(): 'struct' |
| str | ==: True | ==: True | ==: True |
| str | == plain: False | == plain: False | == plain: False |
| str | hash equal: True | hash equal: True | hash equal: True |
| str | pickle roundtrip: StructType([StructField('a', LongType(), True),
x]) | pickle roundtrip: raises TypeError: StructType fields must be StructField
objects | pickle roundtrip: StructType([StructField('a', LongType(), True), x])
|
| str | pickle roundtrip fields: [StructField('a', LongType(), True), 'x'] |
pickle roundtrip fields: raises TypeError: StructType fields must be
StructField obje | pickle roundtrip fields: [StructField('a', LongType(),
True), 'x'] |
| str | copy: [StructField('a', LongType(), True), 'x'] | copy: raises
TypeError: StructType fields must be StructField objects | copy:
[StructField('a', LongType(), True), 'x'] |
| str | deepcopy: [StructField('a', LongType(), True), 'x'] | deepcopy:
raises TypeError: StructType fields must be StructField objects | deepcopy:
[StructField('a', LongType(), True), 'x'] |
| str | copy shares fields: True | copy shares fields: raises TypeError:
StructType fields must be StructField objects | copy shares fields: True |
| str | StructField: StructField('s', StructType([StructField('a',
LongType(), True), x]), Tr | StructField: raises TypeError: StructType fields
must be StructField objects | StructField: StructField('s',
StructType([StructField('a', LongType(), True), x]), Tr |
| str | ArrayType: ArrayType(StructType([StructField('a', LongType(), True),
x]), True) | ArrayType: raises TypeError: StructType fields must be StructField
objects | ArrayType: ArrayType(StructType([StructField('a', LongType(), True),
x]), True) |
| str | MapType value: MapType(StringType(), StructType([StructField('a',
LongType(), True), | MapType value: raises TypeError: StructType fields must
be StructField objects | MapType value: MapType(StringType(),
StructType([StructField('a', LongType(), True), |
| str | MapType key: MapType(StructType([StructField('a', LongType(), True),
x]), StringType( | MapType key: raises TypeError: StructType fields must be
StructField objects | MapType key: MapType(StructType([StructField('a',
LongType(), True), x]), StringType( |
| str | StructType of a field of it: StructType([StructField('s',
StructType([StructField('a' | StructType of a field of it: raises TypeError:
StructType fields must be StructField | StructType of a field of it:
StructType([StructField('s', StructType([StructField('a' |
| str | StructType(fields): raises AttributeError: 'str' object has no
attribute 'name' | StructType(fields): raises AttributeError: 'str' object has
no attribute 'name' | StructType(fields): raises AttributeError: 'str' object
has no attribute 'name' |
| str | to().schema: StructType([StructField('a', LongType(), True), x]) |
to().schema: raises TypeError: StructType fields must be StructField objects |
to().schema: StructType([StructField('a', LongType(), True), x]) |
| str | to().collect(): raises AttributeError: 'str' object has no attribute
'name' | to().collect(): raises TypeError: StructType fields must be
StructField objects | to().collect(): raises AttributeError: 'str' object has
no attribute 'name' |
| str | cast: raises AttributeError: 'str' object has no attribute
'simpleString' | cast: raises TypeError: StructType fields must be StructField
objects | cast: raises AttributeError: 'str' object has no attribute
'simpleString' |
| str | cast collect: raises AttributeError: 'str' object has no attribute
'name' | cast collect: raises TypeError: StructType fields must be StructField
objects | cast collect: raises AttributeError: 'str' object has no attribute
'name' |
| str | from_json: raises AttributeError: 'str' object has no attribute
'jsonValue' | from_json: raises TypeError: StructType fields must be
StructField objects | from_json: raises AttributeError: 'str' object has no
attribute 'jsonValue' |
| str | read.schema: raises AttributeError: 'str' object has no attribute
'jsonValue' | read.schema: raises TypeError: StructType fields must be
StructField objects | read.schema: raises AttributeError: 'str' object has no
attribute 'jsonValue' |
| str | readStream.schema: raises AttributeError: 'str' object has no
attribute 'jsonValue' | readStream.schema: raises TypeError: StructType fields
must be StructField objects | readStream.schema: raises AttributeError: 'str'
object has no attribute 'jsonValue' |
| None | json(): raises AttributeError: 'NoneType' object has no attribute
'jsonValue' | json(): raises TypeError: StructType fields must be StructField
objects | json(): raises AttributeError: 'NoneType' object has no attribute
'jsonValue' |
| None | jsonValue(): raises AttributeError: 'NoneType' object has no
attribute 'jsonValue' | jsonValue(): raises TypeError: StructType fields must
be StructField objects | jsonValue(): raises AttributeError: 'NoneType' object
has no attribute 'jsonValue' |
| None | simpleString(): raises AttributeError: 'NoneType' object has no
attribute 'simpleStri | simpleString(): raises TypeError: StructType fields
must be StructField objects | simpleString(): raises AttributeError: 'NoneType'
object has no attribute 'simpleStri |
| None | treeString(): raises AttributeError: 'NoneType' object has no
attribute '_build_forma | treeString(): raises TypeError: StructType fields
must be StructField objects | treeString(): raises AttributeError: 'NoneType'
object has no attribute '_build_forma |
| None | repr: StructType([StructField('a', LongType(), True), None]) |
repr: StructType([StructField('a', LongType(), True), None]) | repr:
StructType([StructField('a', LongType(), True), None]) |
| None | str: "StructType([StructField('a', LongType(), True), None])" |
str: "StructType([StructField('a', LongType(), True), None])" | str:
"StructType([StructField('a', LongType(), True), None])" |
| None | ['a']: StructField('a', LongType(), True) | ['a']: StructField('a',
LongType(), True) | ['a']: StructField('a', LongType(), True) |
| None | ['x']: raises AttributeError: 'NoneType' object has no attribute
'name' | ['x']: raises TypeError: StructType fields must be StructField objects
| ['x']: raises AttributeError: 'NoneType' object has no attribute 'name' |
| None | [1]: None | [1]: None | [1]: None |
| None | [0:2]: raises AttributeError: 'NoneType' object has no attribute
'name' | [0:2]: raises TypeError: StructType fields must be StructField objects
| [0:2]: raises AttributeError: 'NoneType' object has no attribute 'name' |
| None | [-1]: None | [-1]: None | [-1]: None |
| None | fieldNames(): ['a'] | fieldNames(): ['a'] | fieldNames(): ['a'] |
| None | len: 2 | len: 2 | len: 2 |
| None | 'x' in: False | 'x' in: False | 'x' in: False |
| None | list(): [StructField('a', LongType(), True), None] | list():
[StructField('a', LongType(), True), None] | list(): [StructField('a',
LongType(), True), None] |
| None | add: "raises AttributeError: 'NoneType' object has no attribute
'needConversion'; fie | add: "raises TypeError: StructType fields must be
StructField objects; fields=[Struct | add: "raises AttributeError: 'NoneType'
object has no attribute 'needConversion'; fie |
| None | needConversion(): True | needConversion(): True | needConversion():
True |
| None | toInternal: (1, 2) | toInternal: (1, 2) | toInternal: (1, 2) |
| None | fromInternal: Row(a=1) | fromInternal: Row(a=1) | fromInternal:
Row(a=1) |
| None | toNullable(): raises AttributeError: 'NoneType' object has no
attribute 'name' | toNullable(): raises AttributeError: 'NoneType' object has
no attribute 'dataType' | toNullable(): raises AttributeError: 'NoneType'
object has no attribute 'name' |
| None | typeName(): 'struct' | typeName(): 'struct' | typeName(): 'struct' |
| None | ==: True | ==: True | ==: True |
| None | == plain: False | == plain: False | == plain: False |
| None | hash equal: True | hash equal: True | hash equal: True |
| None | pickle roundtrip: StructType([StructField('a', LongType(), True),
None]) | pickle roundtrip: raises TypeError: StructType fields must be
StructField objects | pickle roundtrip: StructType([StructField('a',
LongType(), True), None]) |
| None | pickle roundtrip fields: [StructField('a', LongType(), True), None]
| pickle roundtrip fields: raises TypeError: StructType fields must be
StructField obje | pickle roundtrip fields: [StructField('a', LongType(),
True), None] |
| None | copy: [StructField('a', LongType(), True), None] | copy: raises
TypeError: StructType fields must be StructField objects | copy:
[StructField('a', LongType(), True), None] |
| None | deepcopy: [StructField('a', LongType(), True), None] | deepcopy:
raises TypeError: StructType fields must be StructField objects | deepcopy:
[StructField('a', LongType(), True), None] |
| None | copy shares fields: True | copy shares fields: raises TypeError:
StructType fields must be StructField objects | copy shares fields: True |
| None | StructField: StructField('s', StructType([StructField('a',
LongType(), True), None]), | StructField: raises TypeError: StructType fields
must be StructField objects | StructField: StructField('s',
StructType([StructField('a', LongType(), True), None]), |
| None | ArrayType: ArrayType(StructType([StructField('a', LongType(),
True), None]), True) | ArrayType: raises TypeError: StructType fields must be
StructField objects | ArrayType: ArrayType(StructType([StructField('a',
LongType(), True), None]), True) |
| None | MapType value: MapType(StringType(), StructType([StructField('a',
LongType(), True), | MapType value: raises TypeError: StructType fields must
be StructField objects | MapType value: MapType(StringType(),
StructType([StructField('a', LongType(), True), |
| None | MapType key: MapType(StructType([StructField('a', LongType(),
True), None]), StringTy | MapType key: raises TypeError: StructType fields must
be StructField objects | MapType key: MapType(StructType([StructField('a',
LongType(), True), None]), StringTy |
| None | StructType of a field of it: StructType([StructField('s',
StructType([StructField('a' | StructType of a field of it: raises TypeError:
StructType fields must be StructField | StructType of a field of it:
StructType([StructField('s', StructType([StructField('a' |
| None | StructType(fields): raises AttributeError: 'NoneType' object has no
attribute 'name' | StructType(fields): raises AttributeError: 'NoneType' object
has no attribute 'name' | StructType(fields): raises AttributeError: 'NoneType'
object has no attribute 'name' |
| None | to().schema: StructType([StructField('a', LongType(), True), None])
| to().schema: raises TypeError: StructType fields must be StructField objects
| to().schema: StructType([StructField('a', LongType(), True), None]) |
| None | to().collect(): raises AttributeError: 'NoneType' object has no
attribute 'name' | to().collect(): raises TypeError: StructType fields must be
StructField objects | to().collect(): raises AttributeError: 'NoneType' object
has no attribute 'name' |
| None | cast: raises AttributeError: 'NoneType' object has no attribute
'simpleString' | cast: raises TypeError: StructType fields must be StructField
objects | cast: raises AttributeError: 'NoneType' object has no attribute
'simpleString' |
| None | cast collect: raises AttributeError: 'NoneType' object has no
attribute 'name' | cast collect: raises TypeError: StructType fields must be
StructField objects | cast collect: raises AttributeError: 'NoneType' object
has no attribute 'name' |
| None | from_json: raises AttributeError: 'NoneType' object has no
attribute 'jsonValue' | from_json: raises TypeError: StructType fields must be
StructField objects | from_json: raises AttributeError: 'NoneType' object has
no attribute 'jsonValue' |
| None | read.schema: raises AttributeError: 'NoneType' object has no
attribute 'jsonValue' | read.schema: raises TypeError: StructType fields must
be StructField objects | read.schema: raises AttributeError: 'NoneType' object
has no attribute 'jsonValue' |
| None | readStream.schema: raises AttributeError: 'NoneType' object has no
attribute 'jsonVal | readStream.schema: raises TypeError: StructType fields
must be StructField objects | readStream.schema: raises AttributeError:
'NoneType' object has no attribute 'jsonVal |
| int | json(): raises AttributeError: 'int' object has no attribute
'jsonValue' | json(): raises TypeError: StructType fields must be StructField
objects | json(): raises AttributeError: 'int' object has no attribute
'jsonValue' |
| int | jsonValue(): raises AttributeError: 'int' object has no attribute
'jsonValue' | jsonValue(): raises TypeError: StructType fields must be
StructField objects | jsonValue(): raises AttributeError: 'int' object has no
attribute 'jsonValue' |
| int | simpleString(): raises AttributeError: 'int' object has no attribute
'simpleString' | simpleString(): raises TypeError: StructType fields must be
StructField objects | simpleString(): raises AttributeError: 'int' object has
no attribute 'simpleString' |
| int | treeString(): raises AttributeError: 'int' object has no attribute
'_build_formatted_ | treeString(): raises TypeError: StructType fields must be
StructField objects | treeString(): raises AttributeError: 'int' object has no
attribute '_build_formatted_ |
| int | repr: StructType([StructField('a', LongType(), True), 5]) | repr:
StructType([StructField('a', LongType(), True), 5]) | repr:
StructType([StructField('a', LongType(), True), 5]) |
| int | str: "StructType([StructField('a', LongType(), True), 5])" | str:
"StructType([StructField('a', LongType(), True), 5])" | str:
"StructType([StructField('a', LongType(), True), 5])" |
| int | ['a']: StructField('a', LongType(), True) | ['a']: StructField('a',
LongType(), True) | ['a']: StructField('a', LongType(), True) |
| int | ['x']: raises AttributeError: 'int' object has no attribute 'name' |
['x']: raises TypeError: StructType fields must be StructField objects | ['x']:
raises AttributeError: 'int' object has no attribute 'name' |
| int | [1]: 5 | [1]: 5 | [1]: 5 |
| int | [0:2]: raises AttributeError: 'int' object has no attribute 'name' |
[0:2]: raises TypeError: StructType fields must be StructField objects | [0:2]:
raises AttributeError: 'int' object has no attribute 'name' |
| int | [-1]: 5 | [-1]: 5 | [-1]: 5 |
| int | fieldNames(): ['a'] | fieldNames(): ['a'] | fieldNames(): ['a'] |
| int | len: 2 | len: 2 | len: 2 |
| int | 'x' in: False | 'x' in: False | 'x' in: False |
| int | list(): [StructField('a', LongType(), True), 5] | list():
[StructField('a', LongType(), True), 5] | list(): [StructField('a', LongType(),
True), 5] |
| int | add: "raises AttributeError: 'int' object has no attribute
'needConversion'; fields=[ | add: "raises TypeError: StructType fields must be
StructField objects; fields=[Struct | add: "raises AttributeError: 'int' object
has no attribute 'needConversion'; fields=[ |
| int | needConversion(): True | needConversion(): True | needConversion():
True |
| int | toInternal: (1, 2) | toInternal: (1, 2) | toInternal: (1, 2) |
| int | fromInternal: Row(a=1) | fromInternal: Row(a=1) | fromInternal:
Row(a=1) |
| int | toNullable(): raises AttributeError: 'int' object has no attribute
'name' | toNullable(): raises AttributeError: 'int' object has no attribute
'dataType' | toNullable(): raises AttributeError: 'int' object has no attribute
'name' |
| int | typeName(): 'struct' | typeName(): 'struct' | typeName(): 'struct' |
| int | ==: True | ==: True | ==: True |
| int | == plain: False | == plain: False | == plain: False |
| int | hash equal: True | hash equal: True | hash equal: True |
| int | pickle roundtrip: StructType([StructField('a', LongType(), True),
5]) | pickle roundtrip: raises TypeError: StructType fields must be StructField
objects | pickle roundtrip: StructType([StructField('a', LongType(), True), 5])
|
| int | pickle roundtrip fields: [StructField('a', LongType(), True), 5] |
pickle roundtrip fields: raises TypeError: StructType fields must be
StructField obje | pickle roundtrip fields: [StructField('a', LongType(),
True), 5] |
| int | copy: [StructField('a', LongType(), True), 5] | copy: raises
TypeError: StructType fields must be StructField objects | copy:
[StructField('a', LongType(), True), 5] |
| int | deepcopy: [StructField('a', LongType(), True), 5] | deepcopy: raises
TypeError: StructType fields must be StructField objects | deepcopy:
[StructField('a', LongType(), True), 5] |
| int | copy shares fields: True | copy shares fields: raises TypeError:
StructType fields must be StructField objects | copy shares fields: True |
| int | StructField: StructField('s', StructType([StructField('a',
LongType(), True), 5]), Tr | StructField: raises TypeError: StructType fields
must be StructField objects | StructField: StructField('s',
StructType([StructField('a', LongType(), True), 5]), Tr |
| int | ArrayType: ArrayType(StructType([StructField('a', LongType(), True),
5]), True) | ArrayType: raises TypeError: StructType fields must be StructField
objects | ArrayType: ArrayType(StructType([StructField('a', LongType(), True),
5]), True) |
| int | MapType value: MapType(StringType(), StructType([StructField('a',
LongType(), True), | MapType value: raises TypeError: StructType fields must
be StructField objects | MapType value: MapType(StringType(),
StructType([StructField('a', LongType(), True), |
| int | MapType key: MapType(StructType([StructField('a', LongType(), True),
5]), StringType( | MapType key: raises TypeError: StructType fields must be
StructField objects | MapType key: MapType(StructType([StructField('a',
LongType(), True), 5]), StringType( |
| int | StructType of a field of it: StructType([StructField('s',
StructType([StructField('a' | StructType of a field of it: raises TypeError:
StructType fields must be StructField | StructType of a field of it:
StructType([StructField('s', StructType([StructField('a' |
| int | StructType(fields): raises AttributeError: 'int' object has no
attribute 'name' | StructType(fields): raises AttributeError: 'int' object has
no attribute 'name' | StructType(fields): raises AttributeError: 'int' object
has no attribute 'name' |
| int | to().schema: StructType([StructField('a', LongType(), True), 5]) |
to().schema: raises TypeError: StructType fields must be StructField objects |
to().schema: StructType([StructField('a', LongType(), True), 5]) |
| int | to().collect(): raises AttributeError: 'int' object has no attribute
'name' | to().collect(): raises TypeError: StructType fields must be
StructField objects | to().collect(): raises AttributeError: 'int' object has
no attribute 'name' |
| int | cast: raises AttributeError: 'int' object has no attribute
'simpleString' | cast: raises TypeError: StructType fields must be StructField
objects | cast: raises AttributeError: 'int' object has no attribute
'simpleString' |
| int | cast collect: raises AttributeError: 'int' object has no attribute
'name' | cast collect: raises TypeError: StructType fields must be StructField
objects | cast collect: raises AttributeError: 'int' object has no attribute
'name' |
| int | from_json: raises AttributeError: 'int' object has no attribute
'jsonValue' | from_json: raises TypeError: StructType fields must be
StructField objects | from_json: raises AttributeError: 'int' object has no
attribute 'jsonValue' |
| int | read.schema: raises AttributeError: 'int' object has no attribute
'jsonValue' | read.schema: raises TypeError: StructType fields must be
StructField objects | read.schema: raises AttributeError: 'int' object has no
attribute 'jsonValue' |
| int | readStream.schema: raises AttributeError: 'int' object has no
attribute 'jsonValue' | readStream.schema: raises TypeError: StructType fields
must be StructField objects | readStream.schema: raises AttributeError: 'int'
object has no attribute 'jsonValue' |
| duck | json():
'{"fields":[{"metadata":{},"name":"a","nullable":true,"type":"long"},{"metada |
json(): raises TypeError: StructType fields must be StructField objects |
json():
'{"fields":[{"metadata":{},"name":"a","nullable":true,"type":"long"},{"metada |
| duck | jsonValue(): {'type': 'struct', 'fields': [{'name': 'a', 'type':
'long', 'nullable': | jsonValue(): raises TypeError: StructType fields must be
StructField objects | jsonValue(): {'type': 'struct', 'fields': [{'name': 'a',
'type': 'long', 'nullable': |
| duck | simpleString(): 'struct<a:bigint,d:bigint>' | simpleString():
raises TypeError: StructType fields must be StructField objects |
simpleString(): 'struct<a:bigint,d:bigint>' |
| duck | treeString(): raises AttributeError: 'Duck' object has no attribute
'_build_formatted | treeString(): raises TypeError: StructType fields must be
StructField objects | treeString(): raises AttributeError: 'Duck' object has no
attribute '_build_formatted |
| duck | repr: StructType([StructField('a', LongType(), True), Duck()]) |
repr: StructType([StructField('a', LongType(), True), Duck()]) | repr:
StructType([StructField('a', LongType(), True), Duck()]) |
| duck | str: "StructType([StructField('a', LongType(), True), Duck()])" |
str: "StructType([StructField('a', LongType(), True), Duck()])" | str:
"StructType([StructField('a', LongType(), True), Duck()])" |
| duck | ['a']: StructField('a', LongType(), True) | ['a']: StructField('a',
LongType(), True) | ['a']: StructField('a', LongType(), True) |
| duck | ['x']: raises PySparkKeyError: [KEY_NOT_EXISTS] Key `x` does not
exist. | ['x']: raises TypeError: StructType fields must be StructField objects
| ['x']: raises PySparkKeyError: [KEY_NOT_EXISTS] Key `x` does not exist. |
| duck | [1]: Duck() | [1]: Duck() | [1]: Duck() |
| duck | [0:2]: raises AssertionError: fields should be a list of
StructField | [0:2]: raises TypeError: StructType fields must be StructField
objects | [0:2]: raises AssertionError: fields should be a list of StructField |
| duck | [-1]: Duck() | [-1]: Duck() | [-1]: Duck() |
| duck | fieldNames(): ['a'] | fieldNames(): ['a'] | fieldNames(): ['a'] |
| duck | len: 2 | len: 2 | len: 2 |
| duck | 'x' in: False | 'x' in: False | 'x' in: False |
| duck | list(): [StructField('a', LongType(), True), Duck()] | list():
[StructField('a', LongType(), True), Duck()] | list(): [StructField('a',
LongType(), True), Duck()] |
| duck | add: "returned; fields=[StructField('a', LongType(), True), Duck(),
StructField('b', | add: "raises TypeError: StructType fields must be
StructField objects; fields=[Struct | add: "returned; fields=[StructField('a',
LongType(), True), Duck(), StructField('b', |
| duck | needConversion(): True | needConversion(): True | needConversion():
True |
| duck | toInternal: (1, 2) | toInternal: (1, 2) | toInternal: (1, 2) |
| duck | fromInternal: Row(a=1) | fromInternal: Row(a=1) | fromInternal:
Row(a=1) |
| duck | toNullable(): StructType([StructField('a', LongType(), True),
StructField('d', LongTy | toNullable(): StructType([StructField('a',
LongType(), True), StructField('d', LongTy | toNullable():
StructType([StructField('a', LongType(), True), StructField('d', LongTy |
| duck | typeName(): 'struct' | typeName(): 'struct' | typeName(): 'struct' |
| duck | ==: False | ==: False | ==: False |
| duck | == plain: False | == plain: False | == plain: False |
| duck | hash equal: True | hash equal: True | hash equal: True |
| duck | pickle roundtrip: StructType([StructField('a', LongType(), True),
Duck()]) | pickle roundtrip: raises TypeError: StructType fields must be
StructField objects | pickle roundtrip: StructType([StructField('a',
LongType(), True), Duck()]) |
| duck | pickle roundtrip fields: [StructField('a', LongType(), True),
Duck()] | pickle roundtrip fields: raises TypeError: StructType fields must be
StructField obje | pickle roundtrip fields: [StructField('a', LongType(),
True), Duck()] |
| duck | copy: [StructField('a', LongType(), True), Duck()] | copy: raises
TypeError: StructType fields must be StructField objects | copy:
[StructField('a', LongType(), True), Duck()] |
| duck | deepcopy: [StructField('a', LongType(), True), Duck()] | deepcopy:
raises TypeError: StructType fields must be StructField objects | deepcopy:
[StructField('a', LongType(), True), Duck()] |
| duck | copy shares fields: True | copy shares fields: raises TypeError:
StructType fields must be StructField objects | copy shares fields: True |
| duck | StructField: StructField('s', StructType([StructField('a',
LongType(), True), Duck()] | StructField: raises TypeError: StructType fields
must be StructField objects | StructField: StructField('s',
StructType([StructField('a', LongType(), True), Duck()] |
| duck | ArrayType: ArrayType(StructType([StructField('a', LongType(),
True), Duck()]), True) | ArrayType: raises TypeError: StructType fields must be
StructField objects | ArrayType: ArrayType(StructType([StructField('a',
LongType(), True), Duck()]), True) |
| duck | MapType value: MapType(StringType(), StructType([StructField('a',
LongType(), True), | MapType value: raises TypeError: StructType fields must
be StructField objects | MapType value: MapType(StringType(),
StructType([StructField('a', LongType(), True), |
| duck | MapType key: MapType(StructType([StructField('a', LongType(),
True), Duck()]), String | MapType key: raises TypeError: StructType fields must
be StructField objects | MapType key: MapType(StructType([StructField('a',
LongType(), True), Duck()]), String |
| duck | StructType of a field of it: StructType([StructField('s',
StructType([StructField('a' | StructType of a field of it: raises TypeError:
StructType fields must be StructField | StructType of a field of it:
StructType([StructField('s', StructType([StructField('a' |
| duck | StructType(fields): raises AssertionError: fields should be a list
of StructField | StructType(fields): raises AssertionError: fields should be a
list of StructField | StructType(fields): raises AssertionError: fields should
be a list of StructField |
| duck | to().schema: StructType([StructField('a', LongType(), True),
Duck()]) | to().schema: raises TypeError: StructType fields must be StructField
objects | to().schema: StructType([StructField('a', LongType(), True), Duck()])
|
| duck | to().collect(): [Row(a=None, d=None)] | to().collect(): raises
TypeError: StructType fields must be StructField objects | to().collect():
[Row(a=None, d=None)] |
| duck | cast: Column<'CAST(NULL AS STRUCT<A:BIGINT,D:BIGINT>)'> | cast:
raises TypeError: StructType fields must be StructField objects | cast:
Column<'CAST(NULL AS STRUCT<A:BIGINT,D:BIGINT>)'> |
| duck | cast collect: [Row(NULL=None)] | cast collect: raises TypeError:
StructType fields must be StructField objects | cast collect: [Row(NULL=None)] |
| duck | from_json: Column<'from_json({},
{"fields":[{"metadata":{},"name":"a","nullable":true | from_json: raises
TypeError: StructType fields must be StructField objects | from_json:
Column<'from_json({}, {"fields":[{"metadata":{},"name":"a","nullable":true |
| duck | read.schema: 'DataFrameReader' | read.schema: raises TypeError:
StructType fields must be StructField objects | read.schema: 'DataFrameReader' |
| duck | readStream.schema: 'DataStreamReader' | readStream.schema: raises
TypeError: StructType fields must be StructField objects | readStream.schema:
'DataStreamReader' |
168 of 168 match PySpark now; 69 with e2.
More probes, at each level of a nested struct, of a struct as an array
element and as a map key or value, and of further uses:
| probe | cases | e2 (46b2dc93) | now |
|---|---|---|---|
| nested (each use at each level of the type) | 146 | 24 | 146 |
| more (assignments, pickles, copies, constructors) | 20 | 10 | 20 |
| extra (`catalog.createTable`, `createExternalTable`,
`applyInPandasWithState`, `udf.register`, equality, a new struct) | 6 | 1 | 6 |
**The second round of review** closed its findings, as separate commits:
- **F1, the recursion:** a struct whose fields hold the struct itself
recursed until the memory of the stacks ran out. The foreign walks now take
SPARK-62224's bound (`type_level`), so json(), jsonValue(), the JSON of an
array and a field of it, from_json and createDataFrame raise RecursionError,
and the proto raises the element's AttributeError, as PySpark's do.
- **F2, transformWithState and UDTs:**
- `transformWithState` and `transformWithStateInPandas` stand the output
schema as the function's output type does, so the plan sends the build-time
read;
- the foreign walks look into the `sqlType()` of a `UserDefinedType`
(`udt_of`).
- **F3, plain siblings:** the foreign proto path checks a plain sibling
field's type, so `AtomicType()`, `NumericType()` and a `StructField` as a type
raise PySpark's `UNSUPPORTED_OPERATION` on the client.
- **F4, borrows:** `__getitem__`, `simpleString` and `add` read a clone of
the fields list with a Python iterator, and `add` updates the struct after the
borrow, so an element that edits the struct as it is read sees it as PySpark's
does.
- **F5, speed:** the lowering runs first. The walk of the foreign fields
runs only for a type that may hold one: its lowering raised TypeError, or it
holds a struct, field, array or map of a Python subclass
(`lower_noting_subclasses`).
- **F7:** the cast's errors map as `PlanArgs::raised` does (a
`PySparkValueError` of `UNSUPPORTED_OPERATION`).
- **F9:** the structs, arrays and maps below a type that does not lower
stand without another lowering of each (`STANDING_IN`), so making a foreign
struct a level at a time is quadratic, as an ordinary one is. At 100 levels it
took 2.7 s and takes 0.05 s now.
- **F6 and F7's timing:** a cast and `DataFrame.to` read the struct at the
call, also for plain structs. This is documented exactly in a KD row until
SPARK-62228 reads it at render and build. **F8:** the list metadata of an
element is documented in a KD row until SPARK-62230.
- **F10, F11:** the docs and comments say which plans read the struct when.
The test is split into six tests, plus the recursion test in its own process.
Ordinary types against master (bdf82090), in microseconds, as the minimum of
4 interleaved rounds of the review's `p_bench.py`, with SPARK-62224 below this
PR:
| path | master | now | now/master |
|---|---|---|---|
| cast, 1,000 fields | 469.3 | 441.2 | 0.94 |
| UDF column, 1,000 fields | 363.7 | 309.4 | 0.85 |
| treeString, 1,000 fields | 691.6 | 627.9 | 0.91 |
| `pyspark_types_to_proto_types`, 1,000 fields | 1,190.7 | 1,170.9 | 0.98 |
| simpleString, 1,000 fields | 305.4 | 299.5 | 0.98 |
| json, 1,000 fields | 3,969.8 | 3,791.3 | 0.96 |
| cast, 8 fields | 13.4 | 13.9 | 1.04 |
| simpleString, 8 fields | 2.8 | 2.9 | 1.04 |
| cast, 40 nested levels | 371.3 | 113.9 | 0.31 |
Round 1 was 1.2 to 2x slower on these paths: the review measured 1.68x for
the 1,000-field cast and 1.50x for treeString. Before F5, round 2's first
commit was 1.91x for that cast and 2.13x for the UDF column.
The review's probes, run again on both clients (`cmp3.py`):
- the common uses: 58/58 for each of the str, None, int, duck, duck_intname
and duck_strnull elements;
- the KeyError element: 55/58. The 3 left are the cast's error timing that
the new KD row states.
- shapes: 27/27 for udt, sf_sub, sf_sub_foreign, deep10, deep10_duck,
arr_in_fields and struct_in_fields_duck. udt_duck is 23/27; its 4 differ only
in the cloudpickled `serializedClass` bytes of a `__main__` class.
- transformWithState: 6/6 for the str and duck elements.
- siblings: 5/5 for each of the 4 kinds.
- p_reentry, p_reentry_sub and p_stale_memo: identical to PySpark's output.
- st_sub_fields differs, because PySpark's constructor itself raises; that
is SPARK-62231.
### Why are the changes needed?
So that every use of such a struct raises PySpark's error, or gives
PySpark's result, where it raised the drop-in's `TypeError`.
### Does this PR introduce _any_ user-facing change?
Yes. The uses of a `StructType` whose `fields` hold an element that is not a
`StructField` read that element as PySpark's do, so they give PySpark's error
or result. Before, they raised `TypeError: StructType fields must be
StructField objects`.
### How was this patch tested?
- **New offline tests** in `python/tests/test_dropin_offline.py`:
- `test_the_methods_of_a_struct_of_a_foreign_element_read_it_as_pyspark`
-
`test_pickles_copies_and_the_types_of_a_struct_of_a_foreign_element_keep_it`
- `test_the_plans_of_a_struct_of_a_foreign_element_read_it_as_pyspark`
- `test_the_proto_of_a_struct_of_a_foreign_element_raises_pysparks_errors`
- `test_a_struct_of_a_foreign_element_is_read_without_a_borrow_as_pyspark`
-
`test_transform_with_state_and_a_udt_of_a_foreign_struct_read_it_as_pyspark`
-
`test_a_struct_whose_fields_hold_the_struct_itself_raises_recursion_error_as_pyspark`,
in a process of its own under a 4 GiB cap.
Together they cover each use above with a str, a duck-typed element,
nested structs and the pickles of module-level elements, and the round-2 cases.
- **Probes of both clients** against a Spark 4.3.0 server: the tables above.
- **The Rust and Python gates** (`gates.sh build rust py`):
- `cargo fmt --all --check`;
- `cargo +1.100.0 clippy -p apache-spark-connect-core -p
apache-spark-connect -p pyspark-rs --all-targets -- -D warnings`;
- `RUSTDOCFLAGS="-D warnings" cargo doc --no-deps`;
- `cargo test -p pyspark-rs --lib`: 18 passed;
- ruff 0.14.0, `test_ruff_excludes.py`, `scripts/audit_no_stubs.sh`, the
stub and dispatch generators, the test-list guards and the license checks;
- `test_type_stubs_offline.py`: 15 passed, 4 skipped;
- the offline suite of `coverage.yml`: 7,103 passed, 121 skipped.
- **`scripts/check_vendored_upstream.sh`**: every vendored file matches
Apache Spark v4.3.0.
- **`test_dropin_e2e.py`** against a Spark 4.3.0 server: 352 passed.
### 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]