dejankrak-db commented on code in PR #60533:
URL: https://github.com/apache/spark/pull/60533#discussion_r4588546100
##########
python/pyspark/sql/types.py:
##########
@@ -1494,7 +1503,8 @@ def _dataTypeJsonValue(
string_metadata: Dict[str, str],
char_varchar_metadata: Dict[str, str],
) -> Union[str, Dict[str, Any]]:
- if not string_metadata and not char_varchar_metadata:
+ has_file = _has_type(self.dataType, FileType)
Review Comment:
Done in 27f4d3ee2c1: the check now looks only at the field's own type and
its array element and map key and value types, the positions the writer
rewrites, and an external FILE has to be there. Otherwise the method returns
what the base branch returns, so schemas without one serialize as before.
##########
python/pyspark/sql/tests/test_types.py:
##########
@@ -3749,6 +3770,122 @@ def test_varchar_type(self):
self.assertEqual(v1, v3)
self.assertFalse(v1 is v3)
+ def test_file_type(self):
+ external, unknown = FileType.external(), FileType()
+ self.assertEqual(FileTypeMode.UNKNOWN, unknown.mode)
+ self.assertEqual(FileTypeMode.EXTERNAL, external.mode)
+ self.assertEqual(external, FileType(FileTypeMode.EXTERNAL))
+ # The mode is part of the type identity.
+ self.assertNotEqual(external, unknown)
+ self.assertNotEqual(hash(external), hash(unknown))
+
+ self.assertEqual("file external", external.simpleString())
+ self.assertEqual("file", unknown.simpleString())
+ self.assertEqual("file", FileType.typeName())
+ self.assertEqual("FileType(FileTypeMode.EXTERNAL)", repr(external))
+ self.assertEqual("FileType()", repr(unknown))
+ self.assertEqual("file external",
DataType._get_jvm_type_name(external))
+ self.assertEqual('"file external"', external.json())
+ self.assertEqual('"file unknown"', unknown.json())
+
+ with self.assertRaises(PySparkTypeError) as pe:
+ FileType("external") # type: ignore[arg-type]
+ self.check_error(
+ exception=pe.exception,
+ errorClass="NOT_EXPECTED_TYPE",
+ messageParameters={
+ "arg_name": "mode",
+ "expected_type": "FileTypeMode",
+ "arg_type": "str",
+ },
+ )
+
+ def test_file_type_json(self):
+ from pyspark.sql.types import _parse_datatype_json_string,
_parse_datatype_json_value
+
+ # Outside a struct field, the JSON carries the mode, and a plain
"file" is unknown.
+ for json_value, expected in [
+ ("file external", FileType.external()),
+ ("file unknown", FileType()),
+ ("file", FileType()),
+ ]:
+ with self.subTest(json_value=json_value):
+ self.assertEqual(expected,
_parse_datatype_json_value(json_value))
+ for dt in [FileType(), FileType.external(),
ArrayType(FileType.external())]:
+ with self.subTest(dt=dt):
+ self.assertEqual(dt, _parse_datatype_json_string(dt.json()))
+ for json_value in ["file managed", "file external unknown", "files"]:
+ with self.subTest(json_value=json_value):
+ with self.assertRaises(PySparkValueError) as pe:
+ _parse_datatype_json_value(json_value)
+ self.check_error(
+ exception=pe.exception,
+ errorClass="CANNOT_PARSE_DATATYPE",
+ messageParameters={"msg": json_value},
+ )
+ # Outside a struct field, a plain "file" in an array or a map is
unknown too.
+ self.assertEqual(
+ ArrayType(FileType()),
+ _parse_datatype_json_value(
+ {"type": "array", "elementType": "file", "containsNull": True}
+ ),
+ )
+ self.assertEqual(
+ MapType(StringType(), FileType()),
+ _parse_datatype_json_value(
+ {
+ "type": "map",
+ "keyType": "string",
+ "valueType": "file",
+ "valueContainsNull": True,
+ }
+ ),
+ )
+
+ # In a struct field, and in an array or map of one, an external file
is the plain "file"
+ # and a plain "file" is read as external. An unknown file keeps its
mode.
+ schema = StructType(
+ [
+ StructField("a", FileType.external()),
+ StructField("b", FileType()),
+ StructField("c", ArrayType(FileType.external())),
+ StructField("d", MapType(StringType(), FileType.external())),
Review Comment:
Done in 27f4d3ee2c1: a `MapType(FileType.external(), IntegerType())` field
is now in the schemas of `test_file_type_json`, which checks that the key is
written as `"file"` and reads back as external, and of
`test_file_type_json_matches_jvm`.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]