Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SPARK-48414][PYTHON] Fix breaking change in python's fromJson #46737

Closed
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
32 changes: 32 additions & 0 deletions python/pyspark/sql/tests/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -817,6 +817,38 @@ def test_schema_with_collations_on_non_string_types(self):
PySparkTypeError, lambda: _parse_datatype_json_string(collations_in_nested_map_json)
)

def test_array_type_from_json(self):
arrayWithoutCollations = ArrayType(StringType(), True)
arrayWithCollations = ArrayType(StringType("UNICODE"), True)
array_json = {"type": "array", "elementType": "string", "containsNull": True}
collationsMap = {"element": "UNICODE"}

self.assertEqual(arrayWithoutCollations, ArrayType.fromJson(array_json))
self.assertEqual(
arrayWithCollations,
ArrayType.fromJson(array_json, fieldPath="", collationsMap=collationsMap),
)
self.assertEqual(
arrayWithCollations, ArrayType.fromJson(array_json, collationsMap=collationsMap)
)

def test_map_type_from_json(self):
mapWithoutCollations = MapType(StringType(), StringType(), True)
mapWithCollations = MapType(StringType("UNICODE"), StringType("UNICODE"), True)
map_json = {
"type": "map",
"keyType": "string",
"valueType": "string",
"valueContainsNull": True,
}
collationsMap = {"key": "UNICODE", "value": "UNICODE"}

self.assertEqual(mapWithoutCollations, MapType.fromJson(map_json))
self.assertEqual(
mapWithCollations, MapType.fromJson(map_json, fieldPath="", collationsMap=collationsMap)
)
self.assertEqual(mapWithCollations, MapType.fromJson(map_json, collationsMap=collationsMap))

def test_schema_with_bad_collations_provider(self):
from pyspark.sql.types import _parse_datatype_json_string, _COLLATIONS_METADATA_KEY

Expand Down
18 changes: 11 additions & 7 deletions python/pyspark/sql/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -771,11 +771,13 @@ def jsonValue(self) -> Dict[str, Any]:
def fromJson(
cls,
json: Dict[str, Any],
fieldPath: str,
collationsMap: Optional[Dict[str, str]],
fieldPath: str = "",
collationsMap: Optional[Dict[str, str]] = None,
Copy link
Member

Choose a reason for hiding this comment

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

Can you add a test please?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Extensive testing was added in #46280, this change is just so that the API for the fromJson method doesn't change which we missed in the previous PR

) -> "ArrayType":
elementType = _parse_datatype_json_value(
json["elementType"], fieldPath + ".element", collationsMap
json["elementType"],
"element" if fieldPath == "" else fieldPath + ".element",
collationsMap,
)
return ArrayType(elementType, json["containsNull"])

Expand Down Expand Up @@ -911,12 +913,14 @@ def jsonValue(self) -> Dict[str, Any]:
def fromJson(
cls,
json: Dict[str, Any],
fieldPath: str,
collationsMap: Optional[Dict[str, str]],
fieldPath: str = "",
collationsMap: Optional[Dict[str, str]] = None,
) -> "MapType":
keyType = _parse_datatype_json_value(json["keyType"], fieldPath + ".key", collationsMap)
keyType = _parse_datatype_json_value(
json["keyType"], "key" if fieldPath == "" else fieldPath + ".key", collationsMap
)
valueType = _parse_datatype_json_value(
json["valueType"], fieldPath + ".value", collationsMap
json["valueType"], "value" if fieldPath == "" else fieldPath + ".value", collationsMap
)
return MapType(
keyType,
Expand Down