samredai commented on a change in pull request #4081:
URL: https://github.com/apache/iceberg/pull/4081#discussion_r805118117



##########
File path: python/src/iceberg/io/pyarrow.py
##########
@@ -0,0 +1,215 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+"""FileIO implementation for reading and writing table files that uses 
pyarrow.fs
+
+This file contains a FileIO implementation that relies on the filesystem 
interface provided
+by PyArrow. It relies on PyArrow's `from_uri` method that infers the correct 
filesytem
+type to use. Theoretically, this allows the supported storage types to grow 
naturally
+with the pyarrow library.
+"""
+
+from typing import Union
+from urllib.parse import ParseResult, urlparse
+
+from pyarrow import NativeFile
+from pyarrow.fs import FileSystem, FileType
+
+from iceberg.io.base import FileIO, InputFile, InputStream, OutputFile, 
OutputStream
+
+
+class PyArrowInputFile(InputFile):
+    """An InputFile implementation that uses a pyarrow filesystem to generate 
pyarrow.lib.NativeFile instances for reading
+
+    Args:
+        location(str): A URI or a path to a local file
+
+    Attributes:
+        location(str): The URI or path to a local file for a PyArrowInputFile 
instance
+        parsed_location(urllib.parse.ParseResult): The parsed location with 
attributes `scheme`, `netloc`, `path`, `params`,
+          `query`, and `fragment`
+        exists(bool): Whether the file exists or not
+
+    Examples:
+        >>> from iceberg.io.pyarrow import PyArrowInputFile
+        >>> input_file = PyArrowInputFile("s3://foo/bar.txt")
+        >>> file_content = input_file.open().read()
+    """
+
+    def __init__(self, location: str):
+        parsed_location = urlparse(location)  # Create a ParseResult from the 
uri
+
+        if parsed_location.scheme and parsed_location.scheme not in (
+            "file",
+            "mock",
+            "s3fs",
+            "hdfs",
+            "viewfs",
+        ):  # Validate that a uri is provided with a scheme of `file`
+            raise ValueError("PyArrowInputFile location must have a scheme of 
`file`, `mock`, `s3fs`, `hdfs`, or `viewfs`")

Review comment:
       Good point about new schemes being supported in the future. We wouldn't 
have to come back here every time and update this check. The only thing is that 
it would be nice to fail early during an initialization of the wrapper class. 
Otherwise the user could do `input_file = 
PyArrowFile(location="foo://bar/baz.txt")` without any exception, and then 
later when running `input_file.open()` getting 
[here](https://github.com/apache/arrow/blob/master/cpp/src/arrow/filesystem/filesystem.cc#L722)
 and hitting `ArrowInvalid: Unrecognized filesystem type in URI: 
foo://bar/baz.txt`. I wonder if there's a light method available in pyarrow to 
just validate a URI, or maybe an array of supported schemes we could just 
import here and reference.




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