Skip to content
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

Fix reading partial shards #2397

Merged
merged 2 commits into from
Oct 18, 2024
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
4 changes: 2 additions & 2 deletions src/zarr/codecs/sharding.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ def get_chunk_slice(self, chunk_coords: ChunkCoords) -> tuple[int, int] | None:
if (chunk_start, chunk_len) == (MAX_UINT_64, MAX_UINT_64):
return None
else:
return (int(chunk_start), int(chunk_start) + int(chunk_len))
return (int(chunk_start), int(chunk_len))

def set_chunk_slice(self, chunk_coords: ChunkCoords, chunk_slice: slice | None) -> None:
localized_chunk = self._localize_chunk(chunk_coords)
Expand Down Expand Up @@ -203,7 +203,7 @@ def create_empty(
def __getitem__(self, chunk_coords: ChunkCoords) -> Buffer:
chunk_byte_slice = self.index.get_chunk_slice(chunk_coords)
if chunk_byte_slice:
return self.buf[chunk_byte_slice[0] : chunk_byte_slice[1]]
return self.buf[chunk_byte_slice[0] : (chunk_byte_slice[0] + chunk_byte_slice[1])]
raise KeyError

def __len__(self) -> int:
Expand Down
36 changes: 36 additions & 0 deletions tests/test_codecs/test_sharding.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,42 @@ def test_sharding_partial(
assert np.array_equal(data, read_data)


@pytest.mark.parametrize("index_location", ["start", "end"])
@pytest.mark.parametrize("store", ["local", "memory", "zip"], indirect=["store"])
@pytest.mark.parametrize(
"array_fixture",
[
ArrayRequest(shape=(128,) * 3, dtype="uint16", order="F"),
],
indirect=["array_fixture"],
)
def test_sharding_partial_readwrite(
store: Store, array_fixture: npt.NDArray[Any], index_location: ShardingCodecIndexLocation
) -> None:
data = array_fixture
spath = StorePath(store)
a = Array.create(
spath,
shape=data.shape,
chunk_shape=data.shape,
dtype=data.dtype,
fill_value=0,
codecs=[
ShardingCodec(
chunk_shape=(1, data.shape[1], data.shape[2]),
codecs=[BytesCodec()],
index_location=index_location,
)
],
)

a[:] = data

for x in range(data.shape[0]):
read_data = a[x, :, :]
assert np.array_equal(data[x], read_data)


@pytest.mark.parametrize(
"array_fixture",
[
Expand Down