dtenedor commented on code in PR #60533:
URL: https://github.com/apache/spark/pull/60533#discussion_r4580554597


##########
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:
   `(Required)` reads oddly next to "a mode other than EXTERNAL is read as 
UNKNOWN": an unset mode is accepted and both converters (and the tests) rely on 
that. Elsewhere in this file `(Required)` means an unset value is an error 
(e.g. `Unparsed.data_type_string`). Suggest dropping `(Required)` and saying 
something like `// Unset or unrecognized values are read as 
FILE_TYPE_MODE_UNKNOWN.`



##########
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:
   nit: the reason for the reservation lives only in the PR description. A 
one-line comment here (what value 2 is held for) would save the next reader 
from wondering whether a value was removed.



##########
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:
   Question on intent: `case _` also swallows `UNRECOGNIZED` (a client sending 
a mode this server does not know about, e.g. the reserved value 2 once it is 
defined) and silently retypes it as `FileType()`. Since the mode is part of 
type identity, that is a silent change of type rather than an error, which is a 
different choice from the JSON reader (`"file managed"` raises 
`CANNOT_PARSE_DATATYPE`). If the forward-compatible coercion is deliberate, 
fine, but it might be worth matching UNSPECIFIED/UNKNOWN explicitly and 
throwing `InvalidPlanInput` for anything else. Either way, a test for 
`setModeValue(2)` / an unrecognized value here and in 
`test_connect_plan.test_file_datatype_conversion` would pin the documented 
behavior (only the unset case is covered today).



##########
sql/connect/server/src/test/scala/org/apache/spark/sql/connect/planner/LiteralExpressionProtoConverterSuite.scala:
##########
@@ -90,6 +90,27 @@ class LiteralExpressionProtoConverterSuite extends 
AnyFunSuite { // scalastyle:i
     }
   }
 
+  test("FILE DataType proto round-trip preserves the mode") {

Review Comment:
   nit: the neighbouring tests carry the JIRA id in the name (`SPARK-59276: 
...`); `SPARK-61751: ...` here for consistency.



##########
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:
   nit: `check_error` with `errorClass="CANNOT_PARSE_DATATYPE"` would pin which 
error these fall into, rather than just the exception type.



##########
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:
   The positive side of `inStructField` is covered well here, but the negative 
side is not: a top-level array or map whose element is the plain `"file"` 
should parse to `FileType()` (unknown), and nothing asserts that. Something like
   
   ```python
   self.assertEqual(
       ArrayType(FileType()),
       _parse_datatype_json_value({"type": "array", "elementType": "file", 
"containsNull": True}),
   )
   ```
   
   would catch an implementation that always passed `inStructField=True` 
through `ArrayType.fromJson` / `MapType.fromJson`.



-- 
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]

Reply via email to