Github user ilganeli commented on a diff in the pull request:

    https://github.com/apache/spark/pull/6686#discussion_r32785670
  
    --- Diff: python/pyspark/sql/types.py ---
    @@ -368,8 +367,43 @@ def __init__(self, fields):
             >>> struct1 == struct2
             False
             """
    -        assert all(isinstance(f, DataType) for f in fields), "fields 
should be a list of DataType"
    -        self.fields = fields
    +        if not fields:
    +            self.fields = []
    +        else:
    +            self.fields = fields
    +            assert all(isinstance(f, StructField) for f in fields),\
    +                "fields should be a list of StructField"
    +
    +    def add(self, name_or_struct_field, data_type=None, nullable=True, 
metadata=None):
    +        """
    +        Construct a StructType by adding new elements to it to define the 
schema
    +
    +        >>> struct1 = StructType().add("f1", StringType(), True).add("f2", 
StringType(), True, None)
    +        >>> struct2 = StructType([StructField("f1", StringType(), True),\
    +         StructField("f2", StringType(), True, None)])
    +        >>> struct1 == struct2
    +        True
    +        >>> struct1 = (StructType().add(StructField("f1", StringType(), 
True))
    +        ...     .add(StructField("f2", StringType(), True, None)))
    +        >>> struct2 = StructType([StructField("f1", StringType(), True),
    +        ...     StructField("f2", StringType(), True, None)])
    +        >>> struct1 == struct2
    +        True
    +
    +        :param nameOrStructField: Either the name of the field or a 
StructField object
    +        :param data_type: If present, the DataType of the StructField to 
create
    +        :param nullable: Whether the field to add should be nullable 
(default True)
    +        :param metadata: Any additional metadata (default None)
    +        :return: a new updated StructType
    +        """
    +        if isinstance(name_or_struct_field, StructField):
    +            self.fields.append(name_or_struct_field)
    +            return self
    +        else:
    +            if isinstance(name_or_struct_field, str) and data_type is None:
    +                raise ValueError("Must specify DataType if passing name of 
struct_field to create.")
    +            self.fields.append(StructField(name_or_struct_field, 
data_type, nullable, metadata))
    --- End diff --
    
    @davies Is there an equivalent of the DataTypeParser in Python?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to