Skip to content

Commit 3ec687e

Browse files
authored
fix: resolve chunks=-1 to chunk size 1 on zero-length axes (#4307)
* fix: resolve chunks=-1 to chunk size 1 on zero-length axes The -1 chunk sentinel resolved to chunk size 0 on zero-length axes, which broke every downstream sharding path differently: a ValueError with shards=None, a ZeroDivisionError with shards="auto", and an infinite loop with shards="auto" plus array.target_shard_size_bytes. Clamp it to 1, matching the auto-chunking clamp in _guess_regular_chunks. Also guard _guess_num_chunks_per_axis_shard against zero-size chunk shapes directly (same non-terminating-loop cause as the 0-d case fixed in #4305), and fix the arithmetic in its docstring example. Follow-up to #4305 / issue #4304. Assisted-by: ClaudeCode:claude-fable-5-1 * docs: add changelog fragment Assisted-by: ClaudeCode:claude-fable-5-1
1 parent 5e00e8c commit 3ec687e

3 files changed

Lines changed: 52 additions & 6 deletions

File tree

‎changes/4307.bugfix.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixed `chunks=-1` on a zero-length axis resolving to an invalid chunk size of 0, which caused a `ValueError`, `ZeroDivisionError`, or infinite loop depending on the sharding configuration. Such axes now get chunk size 1, matching `chunks="auto"`.

‎src/zarr/core/chunk_grids.py‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -734,7 +734,9 @@ def normalize_chunks_1d(
734734
if isinstance(chunks, numbers.Integral):
735735
chunk_size = int(chunks)
736736
if chunk_size == -1:
737-
return np.array([span], dtype=np.int64)
737+
# A zero-length span still gets chunk size 1 (chunk sizes must be positive),
738+
# matching the auto-chunking clamp in _guess_regular_chunks.
739+
return np.array([max(span, 1)], dtype=np.int64)
738740
if chunk_size <= 0:
739741
raise ValueError(f"Chunk size must be positive, got {chunk_size}")
740742
if span == 0:
@@ -844,7 +846,10 @@ def _guess_num_chunks_per_axis_shard(
844846
845847
For example, for a (2,2,2) chunk size and item size 4, maximum bytes of 256 would return 2.
846848
In other words the shard would be a (2,2,2) grid of (2,2,2) chunks
847-
i.e., prod(chunk_shape) * (returned_val * len(chunk_shape)) * item_size = 256 bytes.
849+
i.e., prod(chunk_shape) * (returned_val ** len(chunk_shape)) * item_size = 256 bytes.
850+
851+
Degenerate chunk shapes — a 0-dimensional shape, or one containing a zero-length
852+
axis — return 1, as the search loop's stopping conditions can never be met.
848853
849854
Parameters
850855
----------
@@ -865,7 +870,9 @@ def _guess_num_chunks_per_axis_shard(
865870
if max_bytes < bytes_per_chunk:
866871
return 1
867872
num_axes = len(chunk_shape)
868-
if num_axes == 0:
873+
# For a 0-dimensional chunk shape or one with a zero-length axis, both loop
874+
# conditions below are constant, so the loop would never terminate.
875+
if num_axes == 0 or bytes_per_chunk == 0:
869876
return 1
870877
chunks_per_shard = 1
871878
# First check for byte size, second check to make sure we don't go bigger than the array shape

‎tests/test_chunk_grids.py‎

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,8 @@ def test_normalize_chunks_nd_errors(case: ExpectFail[tuple[Any, tuple[int, ...]]
267267
Expect(input=([10, 20, 30, 40], 100), output=[10, 20, 30, 40], id="explicit-list"),
268268
# -1 sentinel branch: one chunk covering the full span.
269269
Expect(input=(-1, 100), output=[100], id="full-span-sentinel"),
270+
# -1 on a zero-length span clamps to chunk size 1 (chunk sizes must be positive).
271+
Expect(input=(-1, 0), output=[1], id="full-span-sentinel-empty"),
270272
],
271273
ids=lambda c: c.id,
272274
)
@@ -282,11 +284,24 @@ def test_normalize_chunks_1d_returns_int64_array(
282284
assert result.tolist() == case.output
283285

284286

285-
def test_guess_num_chunks_per_axis_shard_0d() -> None:
286-
"""Regression test for https://github.com/zarr-developers/zarr-python/issues/4304."""
287+
@pytest.mark.parametrize(
288+
("chunk_shape", "array_shape"),
289+
[((), ()), ((0,), (0,)), ((0, 0), (0, 0))],
290+
ids=["0d", "zero-1d", "zero-2d"],
291+
)
292+
def test_guess_num_chunks_per_axis_shard_degenerate(
293+
chunk_shape: tuple[int, ...], array_shape: tuple[int, ...]
294+
) -> None:
295+
"""Degenerate chunk shapes must return 1 instead of hanging the search loop.
296+
297+
Regression test for https://github.com/zarr-developers/zarr-python/issues/4304.
298+
"""
287299
assert (
288300
_guess_num_chunks_per_axis_shard(
289-
chunk_shape=(), item_size=8, max_bytes=128 * 1024 * 1024, array_shape=()
301+
chunk_shape=chunk_shape,
302+
item_size=8,
303+
max_bytes=128 * 1024 * 1024,
304+
array_shape=array_shape,
290305
)
291306
== 1
292307
)
@@ -303,3 +318,26 @@ def test_create_0d_array_auto_shards_with_target_shard_size() -> None:
303318
):
304319
arr = zarr.create_array(store={}, shape=(), dtype="int64", shards="auto")
305320
assert arr.shards == ()
321+
322+
323+
@pytest.mark.parametrize(
324+
"target_shard_size_bytes",
325+
[None, 128 * 1024 * 1024],
326+
ids=["no-budget", "budget"],
327+
)
328+
def test_create_zero_length_array_full_span_chunks_auto_shards(
329+
target_shard_size_bytes: int | None,
330+
) -> None:
331+
"""`chunks=-1` on a zero-length axis with shards="auto" must neither hang nor raise.
332+
333+
The -1 sentinel used to resolve to chunk size 0 on zero-length axes, which broke
334+
every sharding code path: a ZeroDivisionError without a shard size budget, and an
335+
infinite loop with one (https://github.com/zarr-developers/zarr-python/issues/4304).
336+
"""
337+
with (
338+
zarr.config.set({"array.target_shard_size_bytes": target_shard_size_bytes}),
339+
pytest.warns(ZarrUserWarning, match="Automatic shard shape inference is experimental"),
340+
):
341+
arr = zarr.create_array(store={}, shape=(0,), dtype="int64", chunks=-1, shards="auto")
342+
assert arr.chunks == (1,)
343+
assert arr.shards == (1,)

0 commit comments

Comments
 (0)