[
https://issues.apache.org/jira/browse/SPARK-62231?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18149320#comment-18149320
]
Anirban Chand edited comment on SPARK-62231 at 11/22/26, 3:50 PM:
------------------------------------------------------------------
Hi @gurwls223,
Looking into `StructType.__init__` in `python/pyspark/sql/types.py`, noticed
that while `self.fields` and `self._needConversion` route through `self`
(respecting subclass attribute/property overrides), `self.names` and the
assertion currently read directly from the `fields` parameter:
```python
if not fields:
self.fields = []
self.names = []
else:
self.fields = fields
self.names = [f.name for f in fields]
assert all(isinstance(f, StructField) for f in fields)
self._needConversion = [f.needConversion() for f in self]
```
This causes two issues when subclassing:
1. **Desynchronized state:** When initialized with `fields=None`, a subclass
overriding `fields` property gets `self.names = []` while `self.fields` and
`self._needConversion` are populated.
2. **Split-brain state:** When initialized with arguments, `self.names`
reflects the argument rather than the subclass's `self.fields`.
#### Proposed Fix:
```diff
--- a/python/pyspark/sql/types.py
+++ b/python/pyspark/sql/types.py
@@ -1750,11 +1750,11 @@ class StructType(DataType):
def __init__(self, fields: Optional[List[StructField]] = None):
if not fields:
self.fields = []
- self.names = []
else:
self.fields = fields
- self.names = [f.name for f in fields]
- assert all(isinstance(f, StructField) for f in fields), (
+ assert all(isinstance(f, StructField) for f in self.fields), (
"fields should be a list of StructField"
)
+ self.names = [f.name for f in self.fields]
# Precalculated list of fields that need conversion with
fromInternal/toInternal functions
self._needConversion = [f.needConversion() for f in self]
```
#### Verified Properties:
* **Backward-compatible:** Zero impact on standard `StructType` usage.
* **Consistency:** `fields`, `names`, `_needConversion`, and
`_needSerializeAnyField` all route through subclass attributes.
* **Safe failure:** Subclasses returning non-`StructField` items immediately
raise `AttributeError` during `__init__`.
I would love to take this up and open a PR with unit test coverage if this
looks good to you!
was (Author: JIRAUSER313775):
Hi @gurwls223,
Looking into `StructType.__init__` in `python/pyspark/sql/types.py`, noticed
that while `self.fields` and `self._needConversion` route through `self`
(respecting subclass attribute/property overrides), `self.names` and the
validation assertion currently read directly from the `fields` argument:
```python
if not fields:
self.fields = []
self.names = []
else:
self.fields = fields
self.names = [f.name for f in fields] # reads parameter, not
self.fields
assert all(isinstance(f, StructField) for f in fields)
self._needConversion = [f.needConversion() for f in self] # reads self
self._needSerializeAnyField = any(self._needConversion)
```
To ensure `fields`, `names`, `_needConversion`, and `_needSerializeAnyField`
are consistently assigned and resolved through the subclass attributes:
```diff
--- a/python/pyspark/sql/types.py
+++ b/python/pyspark/sql/types.py
@@ -1753,8 +1753,8 @@ class StructType(DataType):
self.names = []
else:
self.fields = fields
- self.names = [f.name for f in fields]
- assert all(isinstance(f, StructField) for f in fields), (
+ self.names = [f.name for f in self.fields]
+ assert all(isinstance(f, StructField) for f in self.fields), (
"fields should be a list of StructField"
)
# Precalculated list of fields that need conversion with
fromInternal/toInternal functions
```
This ensures subclass attribute access triggers the expected PySpark error
during initialization.
I would love to take this up and prepare a PR with test coverage if this
approach looks good to you!
> Run the constructor of a StructType subclass as PySpark's __init__ does,
> assigning fields, names, _needConversion and _needSerializeAnyField through
> the subclass's attributes, and raise PySpark's errors
> ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
>
> Key: SPARK-62231
> URL: https://issues.apache.org/jira/browse/SPARK-62231
> Project: Spark
> Issue Type: Improvement
> Components: Connect
> Affects Versions: 5.0.0
> Reporter: Hyukjin Kwon
> Priority: Major
>
> PySpark's StructType.__init__ reads needConversion() of each element of
> fields through self, so a subclass whose fields property holds an object that
> is not a StructField fails to construct. The drop-in constructs it. Raise
> PySpark's error.
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]