Skip to content

fix(sharding): reject variable-size codecs in index_codecs - #4397

Merged
d-v-b merged 2 commits into
zarr-developers:mainfrom
d-v-b:fix/sharding-index-codecs-fixed-size
Sep 24, 2026
Merged

d-v-b merged 2 commits into
zarr-developers:mainfrom
d-v-b:fix/sharding-index-codecs-fixed-size

Conversation

@d-v-b

@d-v-b d-v-b commented Sep 24, 2026 •

Copy link
Copy Markdown
Contributor

Found this old bug unresolved, so I'm resolving it here. AI PR etc etc

🤖 AI text below 🤖

Closes #2030.

The spec says codecs with a variable-size encoding (such as compressors) MUST NOT be used as sharding index_codecs. The shard index is found by its byte size, so that size has to be known before the index is read. ShardingCodec accepted such chains anyway, and the first write then failed with a NotImplementedError that carried no message, raised from compute_encoded_size.

Changes

  • ShardingCodec.__init__ now checks is_fixed_size on every index codec. If any codec is not fixed-size, it raises a ValueError that names the offending codecs. Because the check is in __init__, it also covers from_dict, so opening an existing array with such an index chain fails with the same message.
  • The numcodecs checksum wrappers (zarr.codecs.numcodecs.CRC32, CRC32C, Adler32, Fletcher32, JenkinsLookup3) now set is_fixed_size = True, so they stay valid as index codecs. Their compute_encoded_size was already fixed (input + 4).
  • The comment on Codec.is_fixed_size now says the flag also governs whether a codec can be used as an index codec.

Behaviour change

is_fixed_size defaults to False, so a third-party codec that produces a fixed-size encoding but never sets the flag is now rejected in index_codecs. One test helper in test_sharding_unit.py was in exactly that position and now sets the flag. In-tree fixed-size codecs (bytes, crc32c, transpose, the numcodecs checksums) already declare it.

Tests

  • test_sharding_fixed_size_index_codecs_roundtrip: writes and reads back data through several valid index chains, including transpose and a numcodecs checksum.
  • test_sharding_codec_rejects_variable_size_index_codecs: gzip or blosc in the index chain raises the new error.

I ran the full test suite locally: 10784 passed.

🤖 Generated with Claude Code

The spec forbids variable-size codecs (e.g. compressors) in a sharding
codec's `index_codecs`. ShardingCodec accepted them, and the first write
then failed with a bare NotImplementedError from compute_encoded_size.
Check `is_fixed_size` on every index codec at construction time, and mark
the numcodecs checksum wrappers as fixed-size so they stay valid there.

Closes zarr-developers#2030

Assisted-by: ClaudeCode:claude-opus-5-5
Assisted-by: ClaudeCode:claude-opus-5-5
@d-v-b
d-v-b marked this pull request as ready for review September 24, 2026 09:53
@codecov

codecov Bot commented Sep 24, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 94.26%. Comparing base (aa2ba28) to head (3f5ee3d).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #4397   +/-   ##
=======================================
  Coverage   94.26%   94.26%           
=======================================
  Files          92       92           
  Lines       12982    12988    +6     
=======================================
+ Hits        12237    12243    +6     
  Misses        745      745           
Files with missing lines Coverage Δ
src/zarr/abc/codec.py 98.79% <ø> (ø)
src/zarr/codecs/numcodecs/_codecs.py 95.48% <100.00%> (+0.02%) ⬆️
src/zarr/codecs/sharding.py 95.53% <100.00%> (+0.03%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@d-v-b
d-v-b merged commit edba2ee into zarr-developers:main Sep 24, 2026
39 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.

[v3] ShardingCodec allows user to violate the index_codecs invariant.

1 participant