Github user ilganeli commented on a diff in the pull request:
https://github.com/apache/spark/pull/6686#discussion_r32376192
--- Diff: python/pyspark/sql/types.py ---
@@ -367,9 +378,54 @@ def __init__(self, fields):
>>> struct1 == struct2
False
"""
- assert all(isinstance(f, DataType) for f in fields), "fields
should be a list of DataType"
+ if not fields:
+ fields = []
+ assert all(isinstance(f, StructField) for f in fields),\
+ "fields should be a list of StructField"
self.fields = fields
+ def add_field(self, data_type):
+ """
+ Construct a StructType by adding new elements to it to define the
schema
+ >>> struct1 = StructType().add_field(StructField("f1",
StringType(), True))\
+ .add_field(StructField("f2",
StringType(), True, None))
+ >>> struct2 = StructType([StructField("f1", StringType(), True),\
+ StructField("f2", StringType(), True, None)])
+ >>> struct1 == struct2
+ True
+ >>> struct1 = StructType().add_field(StructField("f1",
StringType(), True))\
+ .add_field(StructField("f2",
StringType(), True, None))
+ >>> struct2 = StructType([StructField("f1", StringType(), True)])
+ >>> struct1 == struct2
+ False
+
+ :param data_type: A StructField object to be added to the
StructType
+ :return: a new updated StructType
+ """
+ assert isinstance(data_type, StructField)
+ self.fields.append(data_type)
+ return self
+
+ def add(self, name, data_type, nullable=True, metadata=None):
--- End diff --
I wanted to support adding a StructField since the constructor accepts an
array of StructFields. I thought it would be more intuitive for backwards
compatibility.
---
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]