Skip to content

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

Merged
Fokko merged 6 commits into
apache:mainfrom
PCTEJA:fix-zstd-fallback-4495
Nov 30, 2026
Merged

Fokko merged 6 commits into
apache:mainfrom
PCTEJA:fix-zstd-fallback-4495

Conversation

@PCTEJA

@PCTEJA PCTEJA commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

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. On Python 3.11 to 3.13 that changes two behaviours, both for the better: truncated input now raises instead of silently returning empty or partial output, as the old stream_reader path did, and Puffin blobs whose frames carry no content size in the header now decompress, where ZstdDecompressor().decompress() used to fail on them. The install hint now points at pip install zstandard, since there is no pyiceberg[zstandard] extra.

The zstandard floor is now >=0.19.0 on Python < 3.14: the fallback reads ZstdDecompressionObj.eof and unused_data (added in 0.18.0), and 0.19.0 fixed an assertion and memory leak in the C backend when decompressobj().decompress() is called more than once.

Are these changes tested?

Yes. The codec tests live in tests/avro/test_file.py: the roundtrip, exact ZstdError assertions for truncated, empty and concatenated frames, and raising only on use when no backend is available. tests/table/test_puffin.py keeps the Puffin reads and the Puffin no-backend assertion, plus two in-process tests that block backends in sys.modules and reload the module: one with both zstandard and compression.zstd blocked (the module and the codec still import, and reads raise only on use), and one that blocks only compression.zstd while zstandard is installed and roundtrips through the fallback (skipped where zstandard is not installed, as on the 3.14 CI).

tests/table/test_puffin.py, tests/avro/ and tests/utils/test_manifest.py: 244 passed, run twice with the same result. The new empty input test fails on the previous head.

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. On Python < 3.14, installing PyIceberg now pulls zstandard>=0.19.0.

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.
@PCTEJA

PCTEJA commented Nov 27, 2026

Copy link
Copy Markdown
Contributor Author

@Fokko, this PR is fully green now, including Python 3.14. Could you please review it?

@Fokko Fokko left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @PCTEJA for picking this up! This covers the first item of #4495 nicely: importing no longer fails without compression.zstd, the fallback order matches the proposal, and the Avro codec and Puffin now go through one shared module.

A few comments inline. The zstandard version floor needs fixing before we can merge. Two more things:

  • test_imports_without_zstd_backend blocks both backends, so it covers the raise-on-use stub but not the case from #4495 itself: compression.zstd missing while zstandard is installed. A subprocess test that only blocks compression/compression.zstd and does a roundtrip would cover it. It would need to skip when zstandard isn't installed, as on 3.14 CI.
  • The description says truncated and concatenated frames behave as before, but this changes behavior on 3.11–3.13. The old stream_reader path silently returned empty or partial output for truncated input instead of raising. Puffin's ZstdDecompressor().decompress() also failed on frames without a content size in the header. Both are improvements, but could you mention them in the description?

Comment thread pyiceberg/utils/_zstd.py Resolved
Comment thread pyiceberg/utils/_zstd.py Resolved
Comment thread pyiceberg/utils/zstd.py Resolved
Comment thread pyiceberg/table/puffin.py Resolved
Comment thread tests/table/test_puffin.py Resolved
Comment thread tests/table/test_puffin.py Resolved
Comment thread tests/table/test_puffin.py Resolved
The fallback relies on ZstdDecompressionObj.eof and unused_data, so zstandard 0.19.0 is the oldest safe release on Python < 3.14. Empty input now raises the same ZstdError as the stdlib, the module exposes the active backend's ZstdError, and Puffin calls the shared module directly.
…lback

The codec tests belong with the other codec tests in tests/avro/test_file.py, with exact ZstdError assertions. A new subprocess test blocks only compression.zstd while zstandard is installed and roundtrips through the fallback.
@PCTEJA

PCTEJA commented Nov 30, 2026

Copy link
Copy Markdown
Contributor Author

@Fokko pushed the fixes, can u pls have another look at it?

Thank you very much for your time

@Fokko Fokko left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @PCTEJA for the quick turnaround, this addresses everything from the last round. A few smaller things inline: moving the module to pyiceberg/utils/_zstd.py so it doesn't become public API, an in-process alternative to the subprocess tests, and some unrelated uv.lock churn. After that I think this is good to go.

Comment thread pyiceberg/utils/_zstd.py Resolved
Comment thread tests/table/test_puffin.py Resolved
Comment thread tests/table/test_puffin.py Resolved
Comment thread tests/avro/test_file.py Resolved
Comment thread uv.lock Resolved
Rename pyiceberg/utils/zstd.py to pyiceberg/utils/_zstd.py so compress, decompress and ZstdError do not become public API. Replace the subprocess backend tests with patch.dict and importlib.reload tests, and revert the unrelated beautifulsoup4 churn in uv.lock so only the zstandard specifier changes.
@PCTEJA

PCTEJA commented Nov 30, 2026

Copy link
Copy Markdown
Contributor Author

@Fokko I finished the latest change requests.

Pls review if they are GTG?

@Fokko

Fokko commented Nov 30, 2026

Copy link
Copy Markdown
Contributor

Thanks again @PCTEJA for the quick follow-up 👍

@Fokko Fokko left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good @PCTEJA ! Thanks for sticking with me on the reviews, appreciate it 👍

@Fokko
Fokko enabled auto-merge November 30, 2026 13:58
@PCTEJA

PCTEJA commented Nov 30, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for your time @Fokko , I am continuing to work on #4505

@Fokko
Fokko added this pull request to the merge queue Nov 30, 2026
Merged via the queue into apache:main with commit 2d61a81 Nov 30, 2026
24 of 26 checks passed
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.

2 participants