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

Add shards to valid_encodings to enable sharded Zarr writing #9948

Merged
merged 10 commits into from
Jan 27, 2025
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
Loading