-
Notifications
You must be signed in to change notification settings - Fork 174
/
__init__.py
379 lines (301 loc) · 12.7 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
# 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.
"""Base FileIO classes for implementing reading and writing table files.
The FileIO abstraction includes a subset of full filesystem implementations. Specifically,
Iceberg needs to read or write a file at a given location (as a seekable stream), as well
as check if a file exists. An implementation of the FileIO abstract base class is responsible
for returning an InputFile instance, an OutputFile instance, and deleting a file given
its location.
"""
from __future__ import annotations
import importlib
import logging
import os
import warnings
from abc import ABC, abstractmethod
from io import SEEK_SET
from types import TracebackType
from typing import (
Dict,
List,
Optional,
Protocol,
Tuple,
Type,
Union,
runtime_checkable,
)
from urllib.parse import urlparse
from pyiceberg.typedef import EMPTY_DICT, Properties
logger = logging.getLogger(__name__)
ADLFS_CONNECTION_STRING = "adlfs.connection-string"
ADLFS_ACCOUNT_NAME = "adlfs.account-name"
ADLFS_ACCOUNT_KEY = "adlfs.account-key"
ADLFS_SAS_TOKEN = "adlfs.sas-token"
ADLFS_TENANT_ID = "adlfs.tenant-id"
ADLFS_CLIENT_ID = "adlfs.client-id"
ADLFS_ClIENT_SECRET = "adlfs.client-secret"
ADLFS_PREFIX = "adlfs"
AWS_REGION = "client.region"
AWS_ACCESS_KEY_ID = "client.access-key-id"
AWS_SECRET_ACCESS_KEY = "client.secret-access-key"
AWS_SESSION_TOKEN = "client.session-token"
S3_ENDPOINT = "s3.endpoint"
S3_ACCESS_KEY_ID = "s3.access-key-id"
S3_SECRET_ACCESS_KEY = "s3.secret-access-key"
S3_SESSION_TOKEN = "s3.session-token"
S3_REGION = "s3.region"
S3_PROXY_URI = "s3.proxy-uri"
S3_CONNECT_TIMEOUT = "s3.connect-timeout"
S3_SIGNER_URI = "s3.signer.uri"
S3_SIGNER_ENDPOINT = "s3.signer.endpoint"
S3_SIGNER_ENDPOINT_DEFAULT = "v1/aws/s3/sign"
HDFS_HOST = "hdfs.host"
HDFS_PORT = "hdfs.port"
HDFS_USER = "hdfs.user"
HDFS_KERB_TICKET = "hdfs.kerberos_ticket"
ADLS_CONNECTION_STRING = "adls.connection-string"
ADLS_ACCOUNT_NAME = "adls.account-name"
ADLS_ACCOUNT_KEY = "adls.account-key"
ADLS_SAS_TOKEN = "adls.sas-token"
ADLS_TENANT_ID = "adls.tenant-id"
ADLS_CLIENT_ID = "adls.client-id"
ADLS_ClIENT_SECRET = "adls.client-secret"
GCS_TOKEN = "gcs.oauth2.token"
GCS_TOKEN_EXPIRES_AT_MS = "gcs.oauth2.token-expires-at"
GCS_PROJECT_ID = "gcs.project-id"
GCS_ACCESS = "gcs.access"
GCS_CONSISTENCY = "gcs.consistency"
GCS_CACHE_TIMEOUT = "gcs.cache-timeout"
GCS_REQUESTER_PAYS = "gcs.requester-pays"
GCS_SESSION_KWARGS = "gcs.session-kwargs"
GCS_ENDPOINT = "gcs.endpoint"
GCS_DEFAULT_LOCATION = "gcs.default-bucket-location"
GCS_VERSION_AWARE = "gcs.version-aware"
PYARROW_USE_LARGE_TYPES_ON_READ = "pyarrow.use-large-types-on-read"
@runtime_checkable
class InputStream(Protocol):
"""A protocol for the file-like object returned by InputFile.open(...).
This outlines the minimally required methods for a seekable input stream returned from an InputFile
implementation's `open(...)` method. These methods are a subset of IOBase/RawIOBase.
"""
@abstractmethod
def read(self, size: int = 0) -> bytes: ...
@abstractmethod
def seek(self, offset: int, whence: int = SEEK_SET) -> int: ...
@abstractmethod
def tell(self) -> int: ...
@abstractmethod
def close(self) -> None: ...
def __enter__(self) -> InputStream:
"""Provide setup when opening an InputStream using a 'with' statement."""
@abstractmethod
def __exit__(
self, exctype: Optional[Type[BaseException]], excinst: Optional[BaseException], exctb: Optional[TracebackType]
) -> None:
"""Perform cleanup when exiting the scope of a 'with' statement."""
@runtime_checkable
class OutputStream(Protocol): # pragma: no cover
"""A protocol for the file-like object returned by OutputFile.create(...).
This outlines the minimally required methods for a writable output stream returned from an OutputFile
implementation's `create(...)` method. These methods are a subset of IOBase/RawIOBase.
"""
@abstractmethod
def write(self, b: bytes) -> int: ...
@abstractmethod
def close(self) -> None: ...
@abstractmethod
def __enter__(self) -> OutputStream:
"""Provide setup when opening an OutputStream using a 'with' statement."""
@abstractmethod
def __exit__(
self, exctype: Optional[Type[BaseException]], excinst: Optional[BaseException], exctb: Optional[TracebackType]
) -> None:
"""Perform cleanup when exiting the scope of a 'with' statement."""
class InputFile(ABC):
"""A base class for InputFile implementations.
Args:
location (str): A URI or a path to a local file.
Attributes:
location (str): The URI or path to a local file for an InputFile instance.
exists (bool): Whether the file exists or not.
"""
def __init__(self, location: str):
self._location = location
@abstractmethod
def __len__(self) -> int:
"""Return the total length of the file, in bytes."""
@property
def location(self) -> str:
"""The fully-qualified location of the input file."""
return self._location
@abstractmethod
def exists(self) -> bool:
"""Check whether the location exists.
Raises:
PermissionError: If the file at self.location cannot be accessed due to a permission error.
"""
@abstractmethod
def open(self, seekable: bool = True) -> InputStream:
"""Return an object that matches the InputStream protocol.
Args:
seekable: If the stream should support seek, or if it is consumed sequential.
Returns:
InputStream: An object that matches the InputStream protocol.
Raises:
PermissionError: If the file at self.location cannot be accessed due to a permission error.
FileNotFoundError: If the file at self.location does not exist.
"""
class OutputFile(ABC):
"""A base class for OutputFile implementations.
Args:
location (str): A URI or a path to a local file.
Attributes:
location (str): The URI or path to a local file for an OutputFile instance.
exists (bool): Whether the file exists or not.
"""
def __init__(self, location: str):
self._location = location
@abstractmethod
def __len__(self) -> int:
"""Return the total length of the file, in bytes."""
@property
def location(self) -> str:
"""The fully-qualified location of the output file."""
return self._location
@abstractmethod
def exists(self) -> bool:
"""Check whether the location exists.
Raises:
PermissionError: If the file at self.location cannot be accessed due to a permission error.
"""
@abstractmethod
def to_input_file(self) -> InputFile:
"""Return an InputFile for the location of this output file."""
@abstractmethod
def create(self, overwrite: bool = False) -> OutputStream:
"""Return an object that matches the OutputStream protocol.
Args:
overwrite (bool): If the file already exists at `self.location`
and `overwrite` is False a FileExistsError should be raised.
Returns:
OutputStream: An object that matches the OutputStream protocol.
Raises:
PermissionError: If the file at self.location cannot be accessed due to a permission error.
FileExistsError: If the file at self.location already exists and `overwrite=False`.
"""
class FileIO(ABC):
"""A base class for FileIO implementations."""
properties: Properties
def __init__(self, properties: Properties = EMPTY_DICT):
self.properties = properties
@abstractmethod
def new_input(self, location: str) -> InputFile:
"""Get an InputFile instance to read bytes from the file at the given location.
Args:
location (str): A URI or a path to a local file.
"""
@abstractmethod
def new_output(self, location: str) -> OutputFile:
"""Get an OutputFile instance to write bytes to the file at the given location.
Args:
location (str): A URI or a path to a local file.
"""
@abstractmethod
def delete(self, location: Union[str, InputFile, OutputFile]) -> None:
"""Delete the file at the given path.
Args:
location (Union[str, InputFile, OutputFile]): A URI or a path to a local file--if an InputFile instance or
an OutputFile instance is provided, the location attribute for that instance is used as the URI to delete.
Raises:
PermissionError: If the file at location cannot be accessed due to a permission error.
FileNotFoundError: When the file at the provided location does not exist.
"""
LOCATION = "location"
WAREHOUSE = "warehouse"
ARROW_FILE_IO = "pyiceberg.io.pyarrow.PyArrowFileIO"
FSSPEC_FILE_IO = "pyiceberg.io.fsspec.FsspecFileIO"
# Mappings from the Java FileIO impl to a Python one. The list is ordered by preference.
# If an implementation isn't installed, it will fall back to the next one.
SCHEMA_TO_FILE_IO: Dict[str, List[str]] = {
"s3": [ARROW_FILE_IO, FSSPEC_FILE_IO],
"s3a": [ARROW_FILE_IO, FSSPEC_FILE_IO],
"s3n": [ARROW_FILE_IO, FSSPEC_FILE_IO],
"gs": [ARROW_FILE_IO],
"file": [ARROW_FILE_IO, FSSPEC_FILE_IO],
"hdfs": [ARROW_FILE_IO],
"viewfs": [ARROW_FILE_IO],
"abfs": [FSSPEC_FILE_IO],
"abfss": [FSSPEC_FILE_IO],
}
def _import_file_io(io_impl: str, properties: Properties) -> Optional[FileIO]:
try:
path_parts = io_impl.split(".")
if len(path_parts) < 2:
raise ValueError(f"py-io-impl should be full path (module.CustomFileIO), got: {io_impl}")
module_name, class_name = ".".join(path_parts[:-1]), path_parts[-1]
module = importlib.import_module(module_name)
class_ = getattr(module, class_name)
return class_(properties)
except ModuleNotFoundError:
logger.warning("Could not initialize FileIO: %s", io_impl)
return None
PY_IO_IMPL = "py-io-impl"
def _infer_file_io_from_scheme(path: str, properties: Properties) -> Optional[FileIO]:
parsed_url = urlparse(path)
if parsed_url.scheme:
if file_ios := SCHEMA_TO_FILE_IO.get(parsed_url.scheme):
for file_io_path in file_ios:
if file_io := _import_file_io(file_io_path, properties):
return file_io
else:
warnings.warn(f"No preferred file implementation for scheme: {parsed_url.scheme}")
return None
def load_file_io(properties: Properties = EMPTY_DICT, location: Optional[str] = None) -> FileIO:
# First look for the py-io-impl property to directly load the class
if io_impl := properties.get(PY_IO_IMPL):
if file_io := _import_file_io(io_impl, properties):
logger.info("Loaded FileIO: %s", io_impl)
return file_io
else:
raise ValueError(f"Could not initialize FileIO: {io_impl}")
# Check the table location
if location:
if file_io := _infer_file_io_from_scheme(location, properties):
return file_io
# Look at the schema of the warehouse
if warehouse_location := properties.get(WAREHOUSE):
if file_io := _infer_file_io_from_scheme(warehouse_location, properties):
return file_io
try:
# Default to PyArrow
logger.info("Defaulting to PyArrow FileIO")
from pyiceberg.io.pyarrow import PyArrowFileIO
return PyArrowFileIO(properties)
except ModuleNotFoundError as e:
raise ModuleNotFoundError(
'Could not load a FileIO, please consider installing one: pip3 install "pyiceberg[pyarrow]", for more options refer to the docs.'
) from e
def _parse_location(location: str) -> Tuple[str, str, str]:
"""Return the path without the scheme."""
uri = urlparse(location)
if not uri.scheme:
return "file", uri.netloc, os.path.abspath(location)
elif uri.scheme in ("hdfs", "viewfs"):
return uri.scheme, uri.netloc, uri.path
else:
return uri.scheme, uri.netloc, f"{uri.netloc}{uri.path}"