Skip to content

align_chunks not working for datasets #10516

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

Open
wants to merge 19 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
49c9ea4
The align_chunks parameter was not being sent on the to_zarr method o…
josephnowak Jul 8, 2025
fa00c95
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 8, 2025
6d3ff30
Add a note on the whats-new.rst about the error of the align_chunks f…
josephnowak Jul 8, 2025
daf1295
Merge remote-tracking branch 'origin/fix/align-chunks' into fix/align…
josephnowak Jul 8, 2025
62e3ddb
Fix a ValueError on the test_dataset_to_zarr_align_chunks_true
josephnowak Jul 8, 2025
a2789f6
Fix the case when enc_chunks are bigger than the dask chunks
josephnowak Jul 9, 2025
60c6c75
Linter
josephnowak Jul 9, 2025
2d5fd41
Merge branch 'main' into fix/align-chunks
josephnowak Jul 10, 2025
f0d60a6
Fix small reintroduced issue when the region is None
josephnowak Jul 10, 2025
467e8d2
Merge remote-tracking branch 'origin/fix/align-chunks' into fix/align…
josephnowak Jul 10, 2025
b471a8c
Fix mypy issues
josephnowak Jul 10, 2025
9ac9872
Merge branch 'main' into fix/align-chunks
josephnowak Jul 11, 2025
328161a
Update whats-new.rst
josephnowak Jul 11, 2025
67e9193
Merge branch 'main' into fix/align-chunks
josephnowak Jul 13, 2025
8e9c284
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 13, 2025
0c620cf
Merge branch 'main' into fix/align-chunks
josephnowak Jul 18, 2025
1ecacdd
Merge branch 'main' into fix/align-chunks
josephnowak Jul 26, 2025
4ebb345
Merge branch 'main' into fix/align-chunks
josephnowak Aug 19, 2025
a8c0172
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 19, 2025
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
3 changes: 3 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ Bug fixes
(:issue:`10637`).
By `Stephan Hoyer <https://github.com/shoyer>`_.

- Fix the ``align_chunks`` parameter on the :py:meth:`~xarray.Dataset.to_zarr` method, it was not being
passed to the underlying :py:meth:`~xarray.backends.api` method (:issue:`10501`, :pull:`10516`).

Documentation
~~~~~~~~~~~~~

Expand Down
24 changes: 17 additions & 7 deletions xarray/backends/chunks.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,13 @@ def grid_rechunk(
if not nd_var_chunks:
return v

# This is useful for the scenarios where the enc_chunks are bigger than the
# variable chunks, which happens when the user specifies the enc_chunks manually.
enc_chunks = tuple(
min(enc_chunk, sum(var_chunk))
for enc_chunk, var_chunk in zip(enc_chunks, nd_var_chunks, strict=True)
)

nd_grid_chunks = tuple(
build_grid_chunks(
sum(var_chunks),
Expand Down Expand Up @@ -191,9 +198,9 @@ def validate_grid_chunks_alignment(
base_error = (
"Specified Zarr chunks encoding['chunks']={enc_chunks!r} for "
"variable named {name!r} would overlap multiple Dask chunks. "
"Check the chunk at position {var_chunk_pos}, which has a size of "
"{var_chunk_size} on dimension {dim_i}. It is unaligned with "
"backend chunks of size {chunk_size} in region {region}. "
"Please check the Dask chunks at position {var_chunk_pos} and "
"{var_chunk_pos_next}, on axis {axis}, they are overlapped "
"on the same Zarr chunk in the region {region}. "
"Writing this array in parallel with Dask could lead to corrupted data. "
"To resolve this issue, consider one of the following options: "
"- Rechunk the array using `chunk()`. "
Expand All @@ -202,7 +209,7 @@ def validate_grid_chunks_alignment(
"- Enable automatic chunks alignment with `align_chunks=True`."
)

for dim_i, chunk_size, var_chunks, interval, size in zip(
for axis, chunk_size, var_chunks, interval, size in zip(
range(len(enc_chunks)),
enc_chunks,
nd_var_chunks,
Expand All @@ -215,9 +222,10 @@ def validate_grid_chunks_alignment(
raise ValueError(
base_error.format(
var_chunk_pos=i + 1,
var_chunk_pos_next=i + 2,
var_chunk_size=chunk,
axis=axis,
name=name,
dim_i=dim_i,
chunk_size=chunk_size,
region=interval,
enc_chunks=enc_chunks,
Expand All @@ -237,9 +245,10 @@ def validate_grid_chunks_alignment(
raise ValueError(
base_error.format(
var_chunk_pos=0,
var_chunk_pos_next=0,
var_chunk_size=var_chunks[0],
axis=axis,
name=name,
dim_i=dim_i,
chunk_size=chunk_size,
region=interval,
enc_chunks=enc_chunks,
Expand All @@ -251,9 +260,10 @@ def validate_grid_chunks_alignment(

error_on_last_chunk = base_error.format(
var_chunk_pos=len(var_chunks) - 1,
var_chunk_pos_next=len(var_chunks) - 1,
var_chunk_size=var_chunks[-1],
axis=axis,
name=name,
dim_i=dim_i,
chunk_size=chunk_size,
region=interval,
enc_chunks=enc_chunks,
Expand Down
1 change: 1 addition & 0 deletions xarray/core/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -2377,6 +2377,7 @@ def to_zarr(
append_dim=append_dim,
region=region,
safe_chunks=safe_chunks,
align_chunks=align_chunks,
zarr_version=zarr_version,
zarr_format=zarr_format,
write_empty_chunks=write_empty_chunks,
Expand Down
48 changes: 48 additions & 0 deletions xarray/tests/test_backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -7586,6 +7586,54 @@ def test_zarr_safe_chunk_region(self, mode: Literal["r+", "a"]):
chunk = chunk.chunk()
self.save(store, chunk.chunk(), region=region)

@requires_dask
def test_dataset_to_zarr_align_chunks_true(self, tmp_store) -> None:
# This test is a replica of the one in `test_dataarray_to_zarr_align_chunks_true`
# but for datasets
with self.create_zarr_target() as store:
ds = (
DataArray(
np.arange(4).reshape((2, 2)),
dims=["a", "b"],
coords={
"a": np.arange(2),
"b": np.arange(2),
},
)
.chunk(a=(1, 1), b=(1, 1))
.to_dataset(name="foo")
)

self.save(
store,
ds,
align_chunks=True,
encoding={"foo": {"chunks": (3, 3)}},
mode="w",
)
assert_identical(ds, xr.open_zarr(store))

ds = (
DataArray(
np.arange(4, 8).reshape((2, 2)),
dims=["a", "b"],
coords={
"a": np.arange(2),
"b": np.arange(2),
},
)
.chunk(a=(1, 1), b=(1, 1))
.to_dataset(name="foo")
)

self.save(
store,
ds,
align_chunks=True,
region="auto",
)
assert_identical(ds, xr.open_zarr(store))


@requires_h5netcdf
@requires_fsspec
Expand Down
Loading