Skip to content

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

Open
NIK-TIGER-BILL wants to merge 1 commit into
zarr-developers:mainfrom
NIK-TIGER-BILL:fix-3751-zero-dim-sharding
Open

fix: allow writing to 0-dimensional arrays with sharding#3966
NIK-TIGER-BILL wants to merge 1 commit into
zarr-developers:mainfrom
NIK-TIGER-BILL:fix-3751-zero-dim-sharding

Conversation

@NIK-TIGER-BILL
Copy link
Copy Markdown

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.

Signed-off-by: NIK-TIGER-BILL <nik.tiger.bill@github.com>
@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

❌ Patch coverage is 14.28571% with 6 lines in your changes missing coverage. Please review.
✅ Project coverage is 93.24%. Comparing base (c79425e) to head (521f425).

Files with missing lines Patch % Lines
src/zarr/codecs/sharding.py 14.28% 6 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3966      +/-   ##
==========================================
- Coverage   93.27%   93.24%   -0.04%     
==========================================
  Files          87       87              
  Lines       11739    11746       +7     
==========================================
+ Hits        10950    10952       +2     
- Misses        789      794       +5     
Files with missing lines Coverage Δ
src/zarr/codecs/sharding.py 87.50% <14.28%> (-1.60%) ⬇️

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

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

2 participants