Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions python/pyspark/sql/tests/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -1579,6 +1579,13 @@ def test_row_without_field_sorting(self):
self.assertEqual(r, expected)
self.assertEqual(repr(r), "Row(b=1, a=2)")

def test_struct_field_from_json(self):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def test_struct_field_from_json(self):
def test_struct_field_from_json(self):
# SPARK-40820: fromJson with only name and type

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed!

# SPARK-40820: fromJson with only name and type
json = {"name": "c1", "type": "string"}
struct_field = StructField.fromJson(json)

self.assertEqual(repr(struct_field), "StructField('c1', StringType(), True)")


class TypesTests(TypesTestsMixin, ReusedSQLTestCase):
pass
Expand Down
4 changes: 2 additions & 2 deletions python/pyspark/sql/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -769,8 +769,8 @@ def fromJson(cls, json: Dict[str, Any]) -> "StructField":
return StructField(
json["name"],
_parse_datatype_json_value(json["type"]),
json["nullable"],
json["metadata"],
json.get("nullable", True),
json.get("metadata"),
)

def needConversion(self) -> bool:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,11 @@ object DataType {
("nullable", JBool(nullable)),
("type", dataType: JValue)) =>
StructField(name, parseDataType(dataType), nullable)
// Support reading schema when 'nullable' is missing.
case JSortedObject(
("name", JString(name)),
("type", dataType: JValue)) =>
StructField(name, parseDataType(dataType))
case other =>
throw new IllegalArgumentException(
s"Failed to convert the JSON string '${compact(render(other))}' to a field.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,29 @@ class DataTypeSuite extends SparkFunSuite {
assert(message.contains("Unrecognized token 'abcd'"))
}

// SPARK-40820: fromJson with only name and type
test("Deserialized and serialized schema without nullable or metadata in") {
val schema =
"""
|{
| "type": "struct",
| "fields": [
| {
| "name": "c1",
| "type": "string"
| }
| ]
|}
|""".stripMargin
val dt = DataType.fromJson(schema)

dt.simpleString equals "struct<c1:string>"
dt.json equals
"""
|{"type":"struct","fields":[{"name":"c1","type":"string","nullable":false,"metadata":{}}]}
|""".stripMargin
}

def checkDefaultSize(dataType: DataType, expectedDefaultSize: Int): Unit = {
test(s"Check the default size of $dataType") {
assert(dataType.defaultSize === expectedDefaultSize)
Expand Down