Skip to content

sharding with empty inner chunk and index location start #2336

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/zarr/codecs/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,6 @@ async def decode_batch(
) -> Iterable[NDBuffer | None]:
chunk_bytes_batch: Iterable[Buffer | None]
chunk_bytes_batch, chunk_specs = _unzip2(chunk_bytes_and_specs)

(
aa_codecs_with_spec,
ab_codec_with_spec,
Expand Down
3 changes: 2 additions & 1 deletion src/zarr/codecs/sharding.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,8 @@ async def finalize(
) -> Buffer:
index_bytes = await index_encoder(self.index)
if index_location == ShardingCodecIndexLocation.start:
self.index.offsets_and_lengths[..., 0] += len(index_bytes)
empty_chunks_mask = self.index.offsets_and_lengths[..., 0] == MAX_UINT_64
self.index.offsets_and_lengths[~empty_chunks_mask, 0] += len(index_bytes)
index_bytes = await index_encoder(self.index) # encode again with corrected offsets
out_buf = index_bytes + self.buf
else:
Expand Down
27 changes: 27 additions & 0 deletions tests/test_codecs/test_sharding.py
Original file line number Diff line number Diff line change
Expand Up @@ -366,3 +366,30 @@ async def test_delete_empty_shards(store: Store) -> None:
def test_pickle() -> None:
codec = ShardingCodec(chunk_shape=(8, 8))
assert pickle.loads(pickle.dumps(codec)) == codec


@pytest.mark.parametrize("store", ["local", "memory"], indirect=["store"])
@pytest.mark.parametrize(
"index_location", [ShardingCodecIndexLocation.start, ShardingCodecIndexLocation.end]
)
async def test_sharding_with_empty_inner_chunk(
store: Store, index_location: ShardingCodecIndexLocation
) -> None:
data = np.arange(0, 16 * 16, dtype="uint32").reshape((16, 16))
fill_value = 1

path = f"sharding_with_empty_inner_chunk_{index_location}"
spath = StorePath(store, path)
a = await AsyncArray.create(
spath,
shape=(16, 16),
chunk_shape=(8, 8),
dtype="uint32",
fill_value=fill_value,
codecs=[ShardingCodec(chunk_shape=(4, 4), index_location=index_location)],
)
data[:4, :4] = fill_value
await a.setitem(..., data)
print("read data")
data_read = await a.getitem(...)
assert np.array_equal(data_read, data)