Skip to content

Core: Fall back to zstandard when stdlib compression.zstd is missing - #4498

Open
PCTEJA wants to merge 3 commits into
apache:mainfrom
PCTEJA:fix-zstd-fallback-4495
Open

PCTEJA wants to merge 3 commits into
apache:mainfrom
PCTEJA:fix-zstd-fallback-4495

Conversation

@PCTEJA

@PCTEJA PCTEJA commented Nov 26, 2026

Copy link
Copy Markdown

Rationale for this change

Refs #4495 (first follow-up item only; the cp315 wheel smoke test and the Python 3.15 CI items are not covered here).

PEP 784 makes compression.zstd optional: it is only built when libzstd is available. On a Python 3.14+ build without it, the import in pyiceberg/avro/codecs/zstandard_codec.py raises at module load, and because pyiceberg/avro/codecs/__init__.py imports ZStandardCodec eagerly, reading any manifest breaks. pyiceberg/table/puffin.py has the same flaw when the zstandard package is missing. With the python_version < '3.14' marker on the zstandard dependency, users also cannot recover through the dependency itself.

This adds a shared pyiceberg/utils/zstd.py module that tries compression.zstd first, then the zstandard package, and only raises ImportError on use when neither is available. The Avro codec and Puffin both go through it. The zstandard backend decompresses frame by frame and checks each frame is complete, so truncated or concatenated frames behave as before. The install hint now points at pip install zstandard, since there is no pyiceberg[zstandard] extra.

Are these changes tested?

Yes. New tests in tests/table/test_puffin.py cover the roundtrip, raising only on use when no backend is available, a fresh interpreter import with both zstandard and compression.zstd blocked (imports of pyiceberg.manifest, the codec and Puffin succeed), and truncated and concatenated frames.

tests/table/test_puffin.py, tests/avro/ and tests/utils/test_manifest.py: 243 passed, run twice with the same result. The new no-backend test fails on base.

Are there any user-facing changes?

No API changes. Manifest and Puffin reads no longer fail at import time on Python builds without stdlib compression.zstd; zstd-compressed data raises ImportError only when it is actually read and no backend is installed.

PEP 784 makes compression.zstd optional, so on Python 3.14+ builds
without libzstd the Avro codec import failed at module load and broke
reading any manifest, and Puffin imports failed the same way when the
zstandard package was missing. Move the backend selection into a shared
pyiceberg.utils.zstd module that tries the stdlib module first, then the
zstandard package, and only raises ImportError on use when neither is
available.

Refs apache#4495
mypy with no_implicit_reexport rejects importing ZStandardCodec through
the pyiceberg.avro.codecs package, which does not explicitly re-export
it. Import it from pyiceberg.avro.codecs.zstandard_codec instead.
On Python 3.14 the stdlib compression.zstd backend reports a truncated
frame as 'Compressed data ended before the end-of-stream marker was
reached', which the existing match terms do not cover, so the truncated
frame and concatenated frames tests failed on 3.14 only. Widen both
matches with end-of-stream.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant