Repository navigation
Core: Fall back to zstandard when stdlib compression.zstd is missing - #4498
Conversation
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.
|
@Fokko, this PR is fully green now, including Python 3.14. Could you please review it? |
Fokko
left a comment
There was a problem hiding this comment.
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_backendblocks both backends, so it covers the raise-on-use stub but not the case from #4495 itself:compression.zstdmissing whilezstandardis installed. A subprocess test that only blockscompression/compression.zstdand does a roundtrip would cover it. It would need to skip whenzstandardisn'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_readerpath silently returned empty or partial output for truncated input instead of raising. Puffin'sZstdDecompressor().decompress()also failed on frames without a content size in the header. Both are improvements, but could you mention them in the description?
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.
|
@Fokko pushed the fixes, can u pls have another look at it? Thank you very much for your time |
Fokko
left a comment
There was a problem hiding this comment.
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.
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.
|
@Fokko I finished the latest change requests. Pls review if they are GTG? |
|
Thanks again @PCTEJA for the quick follow-up 👍 |
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.zstdoptional: it is only built when libzstd is available. On a Python 3.14+ build without it, the import inpyiceberg/avro/codecs/zstandard_codec.pyraises at module load, and becausepyiceberg/avro/codecs/__init__.pyimportsZStandardCodeceagerly, reading any manifest breaks.pyiceberg/table/puffin.pyhas the same flaw when thezstandardpackage is missing. With thepython_version < '3.14'marker on thezstandarddependency, users also cannot recover through the dependency itself.This adds a shared
pyiceberg/utils/_zstd.pymodule that triescompression.zstdfirst, then thezstandardpackage, and only raisesImportErroron use when neither is available. The Avro codec and Puffin both go through it. Thezstandardbackend 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 oldstream_readerpath did, and Puffin blobs whose frames carry no content size in the header now decompress, whereZstdDecompressor().decompress()used to fail on them. The install hint now points atpip install zstandard, since there is nopyiceberg[zstandard]extra.The
zstandardfloor is now>=0.19.0on Python < 3.14: the fallback readsZstdDecompressionObj.eofandunused_data(added in 0.18.0), and 0.19.0 fixed an assertion and memory leak in the C backend whendecompressobj().decompress()is called more than once.Are these changes tested?
Yes. The codec tests live in
tests/avro/test_file.py: the roundtrip, exactZstdErrorassertions for truncated, empty and concatenated frames, and raising only on use when no backend is available.tests/table/test_puffin.pykeeps the Puffin reads and the Puffin no-backend assertion, plus two in-process tests that block backends insys.modulesand reload the module: one with bothzstandardandcompression.zstdblocked (the module and the codec still import, and reads raise only on use), and one that blocks onlycompression.zstdwhilezstandardis installed and roundtrips through the fallback (skipped wherezstandardis not installed, as on the 3.14 CI).tests/table/test_puffin.py,tests/avro/andtests/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 raisesImportErroronly when it is actually read and no backend is installed. On Python < 3.14, installing PyIceberg now pullszstandard>=0.19.0.