[ 
https://issues.apache.org/jira/browse/SPARK-62231?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18149320#comment-18149320
 ] 

Anirban Chand commented on SPARK-62231:
---------------------------------------

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]

Reply via email to