Skip to content

Commit

Permalink
support metadata in python
Browse files Browse the repository at this point in the history
  • Loading branch information
mengxr committed Oct 9, 2014
1 parent 905bb89 commit 93518fb
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions python/pyspark/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,12 +305,15 @@ class StructField(DataType):
"""

def __init__(self, name, dataType, nullable):
def __init__(self, name, dataType, nullable, metadata={}):
"""Creates a StructField
:param name: the name of this field.
:param dataType: the data type of this field.
:param nullable: indicates whether values of this field
can be null.
:param metadata: metadata of this field, which is a map from string
to simple type that can be serialized to JSON
automatically
>>> (StructField("f1", StringType, True)
... == StructField("f1", StringType, True))
Expand All @@ -322,6 +325,7 @@ def __init__(self, name, dataType, nullable):
self.name = name
self.dataType = dataType
self.nullable = nullable
self.metadata = metadata

def __repr__(self):
return "StructField(%s,%s,%s)" % (self.name, self.dataType,
Expand All @@ -330,13 +334,15 @@ def __repr__(self):
def jsonValue(self):
return {"name": self.name,
"type": self.dataType.jsonValue(),
"nullable": self.nullable}
"nullable": self.nullable,
"metadata": self.metadata}

@classmethod
def fromJson(cls, json):
return StructField(json["name"],
_parse_datatype_json_value(json["type"]),
json["nullable"])
json["nullable"],
json["metadata"])


class StructType(DataType):
Expand Down Expand Up @@ -415,7 +421,8 @@ def _parse_datatype_json_string(json_string):
... StructField("simpleArray", simple_arraytype, True),
... StructField("simpleMap", simple_maptype, True),
... StructField("simpleStruct", simple_structtype, True),
... StructField("boolean", BooleanType(), False)])
... StructField("boolean", BooleanType(), False),
... StructField("withMeta", DoubleType(), False, {"name": "age"})])
>>> check_datatype(complex_structtype)
True
>>> # Complex ArrayType.
Expand Down

0 comments on commit 93518fb

Please sign in to comment.