(self)
| 2922 | |
| 2923 | @requires_dask |
| 2924 | def test_shard_encoding_with_dask(self) -> None: |
| 2925 | # Test that dask chunks must align with shard boundaries. |
| 2926 | # See https://github.com/pydata/xarray/issues/10831 |
| 2927 | if not (has_zarr_v3 and zarr.config.config["default_zarr_format"] == 3): |
| 2928 | pytest.skip("sharding requires zarr v3 format") |
| 2929 | |
| 2930 | ds = xr.DataArray(np.arange(12), dims="x", name="var1").to_dataset() |
| 2931 | |
| 2932 | # Case 1: Dask chunks equal to shards should work |
| 2933 | # (zarr chunk=3, shard=6, dask chunk=6) |
| 2934 | ds1 = ds.chunk({"x": 6}) |
| 2935 | ds1["var1"].encoding = {"chunks": (3,), "shards": (6,)} |
| 2936 | with self.roundtrip(ds1) as actual: |
| 2937 | assert_identical(ds, actual) |
| 2938 | |
| 2939 | # Case 2: Dask chunks that are multiples of shards should work |
| 2940 | # (zarr chunk=1, shard=3, dask chunk=6) |
| 2941 | ds2 = ds.chunk({"x": 6}) |
| 2942 | ds2["var1"].encoding = {"chunks": (1,), "shards": (3,)} |
| 2943 | with self.roundtrip(ds2) as actual: |
| 2944 | assert_identical(ds, actual) |
| 2945 | |
| 2946 | # Case 3: Dask chunks smaller than shards should fail |
| 2947 | # (zarr chunk=2, shard=4, dask chunk=3) - dask chunk doesn't align with shard |
| 2948 | ds3 = ds.chunk({"x": 3}) |
| 2949 | ds3["var1"].encoding = {"chunks": (2,), "shards": (4,)} |
| 2950 | with pytest.raises(ValueError, match=r"would overlap"): |
| 2951 | with self.roundtrip(ds3) as actual: |
| 2952 | pass |
| 2953 | |
| 2954 | # Case 4: Can bypass with safe_chunks=False (but data may be corrupted) |
| 2955 | with self.roundtrip(ds3, save_kwargs={"safe_chunks": False}) as actual: |
| 2956 | pass |
| 2957 | |
| 2958 | @requires_dask |
| 2959 | @pytest.mark.skipif( |
nothing calls this directly
no test coverage detected