From 1434d66b8a42a40facc046906a06532f9652465e Mon Sep 17 00:00:00 2001 From: dor-bernstein <160585775+dor-bernstein@users.noreply.github.com> Date: Sun, 29 Nov 2026 18:26:18 +0200 Subject: [PATCH] Add s3.allow-delayed-open property for PyArrowFileIO pyarrow's S3 output stream starts a multipart upload when a file is opened unless S3FileSystem(allow_delayed_open=True) is set, so every object PyArrowFileIO writes (manifests, manifest lists, metadata.json) costs CreateMultipartUpload + UploadPart + CompleteMultipartUpload. With the flag on, objects under the 10 MiB part size go out as a single PutObject. pyarrow exposes the option from 21.0.0, so the property is guarded the same way as s3.ssl.ca-cert. Co-Authored-By: Claude Opus 5.5 --- mkdocs/docs/configuration.md | 1 + pyiceberg/io/__init__.py | 1 + pyiceberg/io/pyarrow.py | 13 +++++++++ tests/io/test_pyarrow.py | 55 +++++++++++++++++++++++++++++++++++- 4 files changed, 69 insertions(+), 1 deletion(-) diff --git a/mkdocs/docs/configuration.md b/mkdocs/docs/configuration.md index 3915ebd7ee..2af78ce09b 100644 --- a/mkdocs/docs/configuration.md +++ b/mkdocs/docs/configuration.md @@ -183,6 +183,7 @@ For the FileIO there are several configuration options available: | s3.server-side-encryption | aws:kms | Configure server-side encryption (e.g. `AES256` or `aws:kms`). Only supported by `FsspecFileIO`. | | s3.sse-kms-key-id | alias/my-key | Configure the SSE-KMS key id (or ARN) for multipart uploads. Only supported by `FsspecFileIO`. | | s3.ssl.ca-cert | /etc/ssl/certs/ca.pem | Path to a CA certificate bundle used to verify TLS connections to the S3 endpoint. Only supported by `PyArrowFileIO`, and requires pyarrow 21.0.0 or later. | +| s3.allow-delayed-open | True | Whether file-open methods may return before the object is actually opened. When enabled, writes of objects smaller than the multipart part size (10 MiB) are sent as a single `PutObject` request instead of a multipart upload (3 requests). Errors such as a missing bucket are then reported on write or close instead of on open. Only supported by `PyArrowFileIO`, and requires pyarrow 21.0.0 or later. | diff --git a/pyiceberg/io/__init__.py b/pyiceberg/io/__init__.py index 478bc1341b..45d9e3ce18 100644 --- a/pyiceberg/io/__init__.py +++ b/pyiceberg/io/__init__.py @@ -85,6 +85,7 @@ def _is_local_path(path: str) -> bool: S3_ROLE_SESSION_NAME = "s3.role-session-name" S3_FORCE_VIRTUAL_ADDRESSING = "s3.force-virtual-addressing" S3_RETRY_STRATEGY_IMPL = "s3.retry-strategy-impl" +S3_ALLOW_DELAYED_OPEN = "s3.allow-delayed-open" S3_SERVER_SIDE_ENCRYPTION = "s3.server-side-encryption" S3_SSE_KMS_KEY_ID = "s3.sse-kms-key-id" HDFS_HOST = "hdfs.host" diff --git a/pyiceberg/io/pyarrow.py b/pyiceberg/io/pyarrow.py index 8aff1f7e73..f5ddaee8f4 100644 --- a/pyiceberg/io/pyarrow.py +++ b/pyiceberg/io/pyarrow.py @@ -110,6 +110,7 @@ HDFS_PORT, HDFS_USER, S3_ACCESS_KEY_ID, + S3_ALLOW_DELAYED_OPEN, S3_ANONYMOUS, S3_CONNECT_TIMEOUT, S3_ENDPOINT, @@ -479,6 +480,16 @@ def _set_tls_ca_file_path(self, client_kwargs: dict[str, Any]) -> None: ) client_kwargs["tls_ca_file_path"] = tls_ca_file_path + def _set_allow_delayed_open(self, client_kwargs: dict[str, Any]) -> None: + if self.properties.get(S3_ALLOW_DELAYED_OPEN) is not None: + min_pyarrow_version_supporting_allow_delayed_open = (21, 0) + if tuple(map(int, pa.__version__.split(".")[:2])) < min_pyarrow_version_supporting_allow_delayed_open: + raise ImportError( + "pyarrow version >= 21.0.0 required for S3FileSystem allow_delayed_open support, " + f"but found version {pa.__version__}." + ) + client_kwargs["allow_delayed_open"] = property_as_bool(self.properties, S3_ALLOW_DELAYED_OPEN, False) + def _initialize_oss_fs(self) -> FileSystem: from pyarrow.fs import S3FileSystem @@ -512,6 +523,7 @@ def _initialize_oss_fs(self) -> FileSystem: client_kwargs["anonymous"] = strtobool(s3_anonymous) self._set_tls_ca_file_path(client_kwargs) + self._set_allow_delayed_open(client_kwargs) return S3FileSystem(**client_kwargs) @@ -570,6 +582,7 @@ def _initialize_s3_fs(self, netloc: str | None) -> FileSystem: client_kwargs["anonymous"] = strtobool(s3_anonymous) self._set_tls_ca_file_path(client_kwargs) + self._set_allow_delayed_open(client_kwargs) return S3FileSystem(**client_kwargs) diff --git a/tests/io/test_pyarrow.py b/tests/io/test_pyarrow.py index 92d533651c..5c1933fc20 100644 --- a/tests/io/test_pyarrow.py +++ b/tests/io/test_pyarrow.py @@ -81,7 +81,15 @@ Or, ) from pyiceberg.expressions.literals import literal -from pyiceberg.io import ADLS_ACCOUNT_NAME, S3_RETRY_STRATEGY_IMPL, S3_SSL_CA_CERT, InputStream, OutputStream, load_file_io +from pyiceberg.io import ( + ADLS_ACCOUNT_NAME, + S3_ALLOW_DELAYED_OPEN, + S3_RETRY_STRATEGY_IMPL, + S3_SSL_CA_CERT, + InputStream, + OutputStream, + load_file_io, +) from pyiceberg.io.pyarrow import ( ICEBERG_SCHEMA, PYARROW_PARQUET_FIELD_ID_KEY, @@ -469,6 +477,51 @@ def test_pyarrow_s3_ssl_ca_cert_requires_supported_pyarrow_version() -> None: PyArrowFileIO(properties=session_properties).new_input(location=f"s3://warehouse/{uuid.uuid4()}") +@pytest.mark.parametrize("value, expected", [("true", True), ("false", False)]) +def test_pyarrow_s3_allow_delayed_open(value: str, expected: bool) -> None: + session_properties: Properties = {S3_ALLOW_DELAYED_OPEN: value} + + with ( + patch("pyiceberg.io.pyarrow.pa.__version__", "21.0.0"), + patch("pyarrow.fs.S3FileSystem") as mock_s3fs, + patch("pyarrow.fs.resolve_s3_region") as mock_s3_region_resolver, + ): + mock_s3_region_resolver.side_effect = OSError("S3 bucket is not found") + PyArrowFileIO(properties=session_properties).new_input(location=f"s3://warehouse/{uuid.uuid4()}") + + assert mock_s3fs.call_args.kwargs["allow_delayed_open"] is expected + + +def test_pyarrow_s3_allow_delayed_open_not_set_by_default() -> None: + with patch("pyarrow.fs.S3FileSystem") as mock_s3fs, patch("pyarrow.fs.resolve_s3_region") as mock_s3_region_resolver: + mock_s3_region_resolver.side_effect = OSError("S3 bucket is not found") + PyArrowFileIO(properties={"s3.region": "us-east-1"}).new_input(location=f"s3://warehouse/{uuid.uuid4()}") + + assert "allow_delayed_open" not in mock_s3fs.call_args.kwargs + + +def test_pyarrow_oss_allow_delayed_open() -> None: + with ( + patch("pyiceberg.io.pyarrow.pa.__version__", "21.0.0"), + patch("pyarrow.fs.S3FileSystem") as mock_s3fs, + ): + PyArrowFileIO(properties={S3_ALLOW_DELAYED_OPEN: "true"}).new_input(location=f"oss://warehouse/{uuid.uuid4()}") + + assert mock_s3fs.call_args.kwargs["allow_delayed_open"] is True + + +def test_pyarrow_s3_allow_delayed_open_requires_supported_pyarrow_version() -> None: + with ( + patch("pyiceberg.io.pyarrow.pa.__version__", "20.0.0"), + patch("pyarrow.fs.S3FileSystem"), + patch("pyarrow.fs.resolve_s3_region") as mock_s3_region_resolver, + ): + mock_s3_region_resolver.side_effect = OSError("S3 bucket is not found") + + with pytest.raises(ImportError, match="pyarrow version >= 21.0.0 required for S3FileSystem allow_delayed_open"): + PyArrowFileIO(properties={S3_ALLOW_DELAYED_OPEN: "true"}).new_input(location=f"s3://warehouse/{uuid.uuid4()}") + + def test_pyarrow_s3_session_properties_with_anonymous() -> None: session_properties: Properties = { "s3.anonymous": "true",