dejankrak-db commented on code in PR #60533:
URL: https://github.com/apache/spark/pull/60533#discussion_r4580733509
##########
sql/connect/common/src/main/protobuf/spark/connect/types.proto:
##########
@@ -81,6 +81,17 @@ message DataType {
// even though their physical value is identical, mirroring timestamp vs
timestamp_ntz.
TimestampNTZNanos timestamp_ntz_nanos = 29;
TimestampLTZNanos timestamp_ltz_nanos = 30;
+
+ // A reference to a file, with metadata describing the file.
+ File file = 31;
+ }
+
+ // The mode of a File type.
+ enum FileTypeMode {
+ FILE_TYPE_MODE_UNSPECIFIED = 0;
+ FILE_TYPE_MODE_UNKNOWN = 1;
+ reserved 2;
Review Comment:
Added a comment on the reservation in ddf91563dd0: the value is held for
another file mode, so that the values of the other modes stay stable.
##########
sql/connect/common/src/main/protobuf/spark/connect/types.proto:
##########
@@ -233,6 +244,13 @@ message DataType {
uint32 type_variation_reference = 1;
}
+ message File {
+ // (Required) The mode of the file. A mode other than
FILE_TYPE_MODE_EXTERNAL is read as
+ // FILE_TYPE_MODE_UNKNOWN.
Review Comment:
Agreed, done in ddf91563dd0: dropped `(Required)` and documented that an
unset or unrecognized value is read as `FILE_TYPE_MODE_UNKNOWN`.
##########
sql/connect/common/src/main/scala/org/apache/spark/sql/connect/common/DataTypeProtoConverter.scala:
##########
@@ -92,6 +93,11 @@ object DataTypeProtoConverter {
}
}
+ private def toCatalystFileType(t: proto.DataType.File): FileType = t.getMode
match {
+ case proto.DataType.FileTypeMode.FILE_TYPE_MODE_EXTERNAL =>
FileType.external
+ case _ => FileType()
Review Comment:
The coercion is deliberate. A mode this server does not know, for example
one a newer client adds, cannot be represented here. Reading it as `FileType()`
keeps a schema that holds it readable, as an unset mode is. The FILE checks
then treat it as an unknown file, so a write that would store it in a table
fails with `FILE_TYPE_UNKNOWN_WRITE` rather than storing the wrong mode.
Throwing `InvalidPlanInput` would instead fail any plan whose schema holds such
a column, including a read.
I added tests for the reserved value 2 and an unrecognized value (42), in
both converters (ddf91563dd0), so the behavior is pinned.
##########
python/pyspark/sql/tests/test_types.py:
##########
@@ -3749,6 +3770,99 @@ 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):
Review Comment:
Done in ddf91563dd0: the test checks `CANNOT_PARSE_DATATYPE` and its `msg`.
##########
python/pyspark/sql/tests/test_types.py:
##########
@@ -3749,6 +3770,99 @@ 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):
+ _parse_datatype_json_value(json_value)
+
+ # 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())),
+ StructField("e", StructType([StructField("x",
FileType.external())])),
+ ]
+ )
+ fields = schema.jsonValue()["fields"]
+ self.assertEqual("file", fields[0]["type"])
+ self.assertEqual("file unknown", fields[1]["type"])
+ self.assertEqual("file", fields[2]["type"]["elementType"])
+ self.assertEqual("file", fields[3]["type"]["valueType"])
+ self.assertEqual("file", fields[4]["type"]["fields"][0]["type"])
+ self.assertEqual(schema, _parse_datatype_json_string(schema.json()))
+ self.assertEqual(
+ StructField("f", FileType.external()),
+ StructField.fromJson({"name": "f", "type": "file"}),
Review Comment:
Good catch, added in ddf91563dd0: a plain `"file"` in a top-level array and
in a top-level map both parse to `FileType()`.
--
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]