Skip to content

Commit 93518fb

Browse files
committed
support metadata in python
1 parent 905bb89 commit 93518fb

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

python/pyspark/sql.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -305,12 +305,15 @@ class StructField(DataType):
305305
306306
"""
307307

308-
def __init__(self, name, dataType, nullable):
308+
def __init__(self, name, dataType, nullable, metadata={}):
309309
"""Creates a StructField
310310
:param name: the name of this field.
311311
:param dataType: the data type of this field.
312312
:param nullable: indicates whether values of this field
313313
can be null.
314+
:param metadata: metadata of this field, which is a map from string
315+
to simple type that can be serialized to JSON
316+
automatically
314317
315318
>>> (StructField("f1", StringType, True)
316319
... == StructField("f1", StringType, True))
@@ -322,6 +325,7 @@ def __init__(self, name, dataType, nullable):
322325
self.name = name
323326
self.dataType = dataType
324327
self.nullable = nullable
328+
self.metadata = metadata
325329

326330
def __repr__(self):
327331
return "StructField(%s,%s,%s)" % (self.name, self.dataType,
@@ -330,13 +334,15 @@ def __repr__(self):
330334
def jsonValue(self):
331335
return {"name": self.name,
332336
"type": self.dataType.jsonValue(),
333-
"nullable": self.nullable}
337+
"nullable": self.nullable,
338+
"metadata": self.metadata}
334339

335340
@classmethod
336341
def fromJson(cls, json):
337342
return StructField(json["name"],
338343
_parse_datatype_json_value(json["type"]),
339-
json["nullable"])
344+
json["nullable"],
345+
json["metadata"])
340346

341347

342348
class StructType(DataType):
@@ -415,7 +421,8 @@ def _parse_datatype_json_string(json_string):
415421
... StructField("simpleArray", simple_arraytype, True),
416422
... StructField("simpleMap", simple_maptype, True),
417423
... StructField("simpleStruct", simple_structtype, True),
418-
... StructField("boolean", BooleanType(), False)])
424+
... StructField("boolean", BooleanType(), False),
425+
... StructField("withMeta", DoubleType(), False, {"name": "age"})])
419426
>>> check_datatype(complex_structtype)
420427
True
421428
>>> # Complex ArrayType.

0 commit comments

Comments
 (0)