Skip to content

Commit

Permalink
Add shards to valid_encodings to enable sharded Zarr writing (#9948)
Browse files Browse the repository at this point in the history
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Deepak Cherian <[email protected]>
  • Loading branch information
3 people authored Jan 27, 2025
1 parent a848044 commit 2870710
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
2 changes: 2 additions & 0 deletions xarray/backends/zarr.py
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,7 @@ def extract_zarr_variable_encoding(
safe_to_drop = {"source", "original_shape", "preferred_chunks"}
valid_encodings = {
"chunks",
"shards",
"compressor", # TODO: delete when min zarr >=3
"compressors",
"filters",
Expand Down Expand Up @@ -825,6 +826,7 @@ def open_store_variable(self, name):
{
"compressors": zarr_array.compressors,
"filters": zarr_array.filters,
"shards": zarr_array.shards,
}
)
if self.zarr_group.metadata.zarr_format == 3:
Expand Down
18 changes: 18 additions & 0 deletions xarray/tests/test_backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -2496,6 +2496,24 @@ def test_chunk_encoding(self) -> None:
with self.roundtrip(data) as actual:
pass

def test_shard_encoding(self) -> None:
# These datasets have no dask chunks. All chunking/sharding specified in
# encoding
if has_zarr_v3 and zarr.config.config["default_zarr_format"] == 3:
data = create_test_data()
chunks = (1, 1)
shards = (5, 5)
data["var2"].encoding.update({"chunks": chunks})
data["var2"].encoding.update({"shards": shards})
with self.roundtrip(data) as actual:
assert shards == actual["var2"].encoding["shards"]

# expect an error with shards not divisible by chunks
data["var2"].encoding.update({"chunks": (2, 2)})
with pytest.raises(ValueError):
with self.roundtrip(data) as actual:
pass

@requires_dask
@pytest.mark.skipif(
ON_WINDOWS,
Expand Down

0 comments on commit 2870710

Please sign in to comment.