Skip to content

Commit 2870710

Browse files
jacobbiekerpre-commit-ci[bot]dcherian
authored
Add shards to valid_encodings to enable sharded Zarr writing (#9948)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Deepak Cherian <[email protected]>
1 parent a848044 commit 2870710

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

xarray/backends/zarr.py

+2
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,7 @@ def extract_zarr_variable_encoding(
444444
safe_to_drop = {"source", "original_shape", "preferred_chunks"}
445445
valid_encodings = {
446446
"chunks",
447+
"shards",
447448
"compressor", # TODO: delete when min zarr >=3
448449
"compressors",
449450
"filters",
@@ -825,6 +826,7 @@ def open_store_variable(self, name):
825826
{
826827
"compressors": zarr_array.compressors,
827828
"filters": zarr_array.filters,
829+
"shards": zarr_array.shards,
828830
}
829831
)
830832
if self.zarr_group.metadata.zarr_format == 3:

xarray/tests/test_backends.py

+18
Original file line numberDiff line numberDiff line change
@@ -2496,6 +2496,24 @@ def test_chunk_encoding(self) -> None:
24962496
with self.roundtrip(data) as actual:
24972497
pass
24982498

2499+
def test_shard_encoding(self) -> None:
2500+
# These datasets have no dask chunks. All chunking/sharding specified in
2501+
# encoding
2502+
if has_zarr_v3 and zarr.config.config["default_zarr_format"] == 3:
2503+
data = create_test_data()
2504+
chunks = (1, 1)
2505+
shards = (5, 5)
2506+
data["var2"].encoding.update({"chunks": chunks})
2507+
data["var2"].encoding.update({"shards": shards})
2508+
with self.roundtrip(data) as actual:
2509+
assert shards == actual["var2"].encoding["shards"]
2510+
2511+
# expect an error with shards not divisible by chunks
2512+
data["var2"].encoding.update({"chunks": (2, 2)})
2513+
with pytest.raises(ValueError):
2514+
with self.roundtrip(data) as actual:
2515+
pass
2516+
24992517
@requires_dask
25002518
@pytest.mark.skipif(
25012519
ON_WINDOWS,

0 commit comments

Comments
 (0)