Skip to content

Commit 5f3a512

Browse files
brokkoli71normanrz
andauthored
sharding with empty inner chunk and index location start (#2336)
* test_sharding_with_empty_inner_chunk * only update non-empty chunks offset * format * Update test_sharding.py * format --------- Co-authored-by: Norman Rzepka <[email protected]>
1 parent ee112b9 commit 5f3a512

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
lines changed

Diff for: src/zarr/codecs/pipeline.py

-1
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,6 @@ async def decode_batch(
162162
) -> Iterable[NDBuffer | None]:
163163
chunk_bytes_batch: Iterable[Buffer | None]
164164
chunk_bytes_batch, chunk_specs = _unzip2(chunk_bytes_and_specs)
165-
166165
(
167166
aa_codecs_with_spec,
168167
ab_codec_with_spec,

Diff for: src/zarr/codecs/sharding.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,8 @@ async def finalize(
265265
) -> Buffer:
266266
index_bytes = await index_encoder(self.index)
267267
if index_location == ShardingCodecIndexLocation.start:
268-
self.index.offsets_and_lengths[..., 0] += len(index_bytes)
268+
empty_chunks_mask = self.index.offsets_and_lengths[..., 0] == MAX_UINT_64
269+
self.index.offsets_and_lengths[~empty_chunks_mask, 0] += len(index_bytes)
269270
index_bytes = await index_encoder(self.index) # encode again with corrected offsets
270271
out_buf = index_bytes + self.buf
271272
else:

Diff for: tests/test_codecs/test_sharding.py

+27
Original file line numberDiff line numberDiff line change
@@ -366,3 +366,30 @@ async def test_delete_empty_shards(store: Store) -> None:
366366
def test_pickle() -> None:
367367
codec = ShardingCodec(chunk_shape=(8, 8))
368368
assert pickle.loads(pickle.dumps(codec)) == codec
369+
370+
371+
@pytest.mark.parametrize("store", ["local", "memory"], indirect=["store"])
372+
@pytest.mark.parametrize(
373+
"index_location", [ShardingCodecIndexLocation.start, ShardingCodecIndexLocation.end]
374+
)
375+
async def test_sharding_with_empty_inner_chunk(
376+
store: Store, index_location: ShardingCodecIndexLocation
377+
) -> None:
378+
data = np.arange(0, 16 * 16, dtype="uint32").reshape((16, 16))
379+
fill_value = 1
380+
381+
path = f"sharding_with_empty_inner_chunk_{index_location}"
382+
spath = StorePath(store, path)
383+
a = await AsyncArray.create(
384+
spath,
385+
shape=(16, 16),
386+
chunk_shape=(8, 8),
387+
dtype="uint32",
388+
fill_value=fill_value,
389+
codecs=[ShardingCodec(chunk_shape=(4, 4), index_location=index_location)],
390+
)
391+
data[:4, :4] = fill_value
392+
await a.setitem(..., data)
393+
print("read data")
394+
data_read = await a.getitem(...)
395+
assert np.array_equal(data_read, data)

0 commit comments

Comments
 (0)