Skip to content

Commit 966bab4

Browse files
authored
sharding with empty inner chunk and index location start (#16)
* test_sharding_with_empty_inner_chunk * only update non empty chunks offset
1 parent 23cf523 commit 966bab4

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

tests/test_v3.py

+29
Original file line numberDiff line numberDiff line change
@@ -1082,3 +1082,32 @@ def test_update_attributes_group(store: Store):
10821082

10831083
g = Group.open(store / "update_attributes_group")
10841084
assert g.metadata.attributes["hello"] == "zarrita"
1085+
1086+
1087+
@pytest.mark.parametrize(
1088+
"index_location", [ShardingCodecIndexLocation.start, ShardingCodecIndexLocation.end]
1089+
)
1090+
def test_sharding_with_empty_inner_chunk(store: Store, index_location):
1091+
data = np.arange(0, 16 * 16, dtype="uint16").reshape((16, 16))
1092+
fill_value = 1
1093+
data[:4, :4] = fill_value
1094+
1095+
a = Array.create(
1096+
store / "sharding_with_empty_inner_chunk",
1097+
shape=(16, 16),
1098+
chunk_shape=(8, 8),
1099+
dtype=data.dtype,
1100+
fill_value=fill_value,
1101+
codecs=[
1102+
codecs.sharding_codec(
1103+
chunk_shape=(4, 4),
1104+
codecs=[
1105+
codecs.bytes_codec(),
1106+
codecs.blosc_codec(typesize=data.dtype.itemsize),
1107+
],
1108+
index_location=index_location,
1109+
)
1110+
],
1111+
)
1112+
a[:] = data
1113+
assert np.array_equal(a[:], data)

zarrita/sharding.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,8 @@ async def finalize(
191191
) -> BytesLike:
192192
index_bytes = await index_encoder(self.index)
193193
if index_location == ShardingCodecIndexLocation.start:
194-
self.index.offsets_and_lengths[..., 0] += len(index_bytes)
194+
empty_chunks_mask = self.index.offsets_and_lengths[..., 0] == MAX_UINT_64
195+
self.index.offsets_and_lengths[~empty_chunks_mask, 0] += len(index_bytes)
195196
index_bytes = await index_encoder(
196197
self.index
197198
) # encode again with corrected offsets

0 commit comments

Comments
 (0)