Skip to content

fix: allow writing to 0-dimensional arrays with sharding#3966

Merged
d-v-b merged 3 commits into
zarr-developers:mainfrom
NIK-TIGER-BILL:fix-3751-zero-dim-sharding
May 14, 2026
Merged

fix: allow writing to 0-dimensional arrays with sharding#3966
d-v-b merged 3 commits into
zarr-developers:mainfrom
NIK-TIGER-BILL:fix-3751-zero-dim-sharding

Conversation

@NIK-TIGER-BILL
Copy link
Copy Markdown
Contributor

Closes #3751

This PR fixes a bug where writing to a 0-dimensional array with sharding enabled would crash with an IndexError in the sharding codec's vectorized chunk slice lookup.

The root cause was that get_chunk_slices_vectorized in zarr/codecs/sharding.py assumed offsets_and_lengths was at least 2-dimensional, but for 0-dimensional arrays it has shape (2,) instead of (1, 2). This caused offsets_and_lengths[:, 0] to fail with "too many indices for array".

Fix: Add an early return in get_chunk_slices_vectorized to handle the 0-dimensional case by reshaping offsets_and_lengths to (1, 2) before slicing.

Test: Added test_sharding_zero_dimensional in tests/test_codecs/test_sharding.py as a regression test.

@github-actions github-actions Bot added the needs release notes Automatically applied to PRs which haven't added release notes label May 11, 2026
@codecov
Copy link
Copy Markdown

codecov Bot commented May 11, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.29%. Comparing base (eac9c86) to head (bb315a8).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3966      +/-   ##
==========================================
+ Coverage   93.28%   93.29%   +0.01%     
==========================================
  Files          87       87              
  Lines       11745    11752       +7     
==========================================
+ Hits        10956    10964       +8     
+ Misses        789      788       -1     
Files with missing lines Coverage Δ
src/zarr/codecs/sharding.py 89.32% <100.00%> (+0.23%) ⬆️

... and 1 file with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Copy Markdown
Contributor

@chuckwondo chuckwondo left a comment

Choose a reason for hiding this comment

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

Thanks @NIK-TIGER-BILL!

Would you mind also adding a test that covers your patch that handles the 0D case? The test you added shows that the original case no longer errors, but that case no longer enters the function that you patched, so your patch itself remains untested, hence the failed codecov run.

@NIK-TIGER-BILL
Copy link
Copy Markdown
Contributor Author

@chuckwondo Good catch — the integration test bypassed the patched function. I just pushed a direct unit test that exercises _ShardIndex.get_chunk_slices_vectorized with a 0-D offsets_and_lengths array, covering both the written and unwritten chunk cases. This should satisfy codecov for the new branch. Let me know if you would like anything else adjusted!

@chuckwondo
Copy link
Copy Markdown
Contributor

Thanks @NIK-TIGER-BILL. Would you mind updating your branch and addressing the ruff pre-commit error? (See https://results.pre-commit.ci/run/github/48049137/1778641889.s58oPP4KQbGdn7pJdtUPAg)

@maxrjones and/or @d-v-b, please review.

NIK-TIGER-BILL added 3 commits May 13, 2026 23:00
Signed-off-by: NIK-TIGER-BILL <nik.tiger.bill@github.com>
Signed-off-by: NIK-TIGER-BILL <nik.tiger.bill@github.com>
Signed-off-by: NIK-TIGER-BILL <nik.tiger.bill@github.com>
@NIK-TIGER-BILL NIK-TIGER-BILL force-pushed the fix-3751-zero-dim-sharding branch from 411cf37 to bb315a8 Compare May 13, 2026 23:01
@NIK-TIGER-BILL
Copy link
Copy Markdown
Contributor Author

@chuckwondo Done — rebased on latest upstream/main and fixed the ruff unused-variable error (prefixed ends_e with an underscore). CI should be green now. Let me know if anything else is needed!

@NIK-TIGER-BILL
Copy link
Copy Markdown
Contributor Author

@chuckwondo I believe I've addressed all feedback — rebased on latest upstream/main and fixed the ruff unused-variable issue (prefixed with ). Could you please take another look when you have a moment? Thanks!

@chuckwondo
Copy link
Copy Markdown
Contributor

@d-v-b, this is a very small PR. Looks good to me, but would you mind reviewing as well?

Copy link
Copy Markdown
Contributor

@chuckwondo chuckwondo left a comment

Choose a reason for hiding this comment

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

Thanks @NIK-TIGER-BILL!

@d-v-b
Copy link
Copy Markdown
Contributor

d-v-b commented May 14, 2026

this looks good! there's some baseline code smell in these classes but this fix is appropriately targeted. I'll open an issue with my follow-up recommendations.

@d-v-b d-v-b merged commit fcb3cd3 into zarr-developers:main May 14, 2026
30 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs release notes Automatically applied to PRs which haven't added release notes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cannot write a 0 dimensional array with sharding

3 participants