Skip to content

Commit fcc24bb

Browse files
d-v-bclaude
andcommitted
fix(metadata): give the recipe for recreating an array read from a stored chunk size of 0
The warning names the call, zarr.from_array(array.store, name=array.path, data=array, chunks=..., overwrite=True, write_data=False), which keeps the data type, fill value, attributes and codecs, and a test pins the recipe on both formats. Assisted-by: ClaudeCode:claude-fable-5-1 Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
1 parent 5dccfb8 commit fcc24bb

3 files changed

Lines changed: 52 additions & 4 deletions

File tree

‎changes/4334.bugfix.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ A chunk edge length is now always at least 1, while an array extent may be 0. Ev
22

33
`FixedDimension(size=0, ...)` now raises a `ValueError`. `ArrayV2Metadata(chunks=(0,))` is still accepted and written as given; an array built from such metadata (with `create_hierarchy`, for example) reads the chunk size as a stored chunk size of 0 is read (below). `create_hierarchy` now builds every array and group before it deletes or stores anything, so a node that cannot be built fails with the store untouched. The regular chunk grid metadata class reads a `bool` chunk edge length as the `int` it equals, and rejects a string or a mapping as a chunk shape as a whole. `RectilinearChunkGridMetadata`, which is experimental, now reads a `bool` edge length as the `int` it equals and rejects a NumPy integer or a float edge length such as `4.0` with a `TypeError`; it used to keep them as given, so that it could not store a NumPy integer, could not read back a `bool`, and stored a float as a JSON float, which the Zarr specification does not allow.
44

5-
Stored metadata with a regular chunk size of 0 or JSON `false` (Zarr format 2 `chunks`, Zarr format 3 `regular` `chunk_shape`) is now read as chunk size 1 (the inner chunk size for sharded arrays, which previously failed to open), however long the axis is: that is the chunk size `chunks=-1` gives on a zero-length axis, and it does not depend on how far the axis has grown since. zarr-python wrote such sizes for arrays created with a zero-length axis until 3.4, and, from 2.18.7 to 3.2.1, for an explicit chunk size of 0 or `False` on an axis of any length, as in `chunks=(0,)` (those arrays could store no data). On a zero-length axis this opens silently, as before, and appending to it stores chunks of size 1. On an axis of positive length it opens with a `ZarrUserWarning`, which says that the array holds only its fill value, so that recreating it with the wanted chunk shape loses nothing, and how to keep it by storing valid metadata instead: `array.update_attributes({})`, then `zarr.consolidate_metadata` if the metadata is consolidated. JSON `true`, as zarr-python 3.0 and 3.2 wrote for a chunk size of `True`, is read as 1, silently, in the chunk grid (regular, and the explicit edges and run-length encoded sizes of a rectilinear one) and in the inner chunk shape of every sharding codec, nested or not. A stored rectilinear chunk grid whose edge lengths are integral JSON floats (`[[4.0, 2]]`), as zarr-python 3.2 wrote for float edges, is read with those edges as integers, silently; a float anywhere no release wrote one (a regular chunk shape, the inner chunk shape of a sharding codec, a run-length repeat count, an edge below 1) is rejected.
5+
Stored metadata with a regular chunk size of 0 or JSON `false` (Zarr format 2 `chunks`, Zarr format 3 `regular` `chunk_shape`) is now read as chunk size 1 (the inner chunk size for sharded arrays, which previously failed to open), however long the axis is: that is the chunk size `chunks=-1` gives on a zero-length axis, and it does not depend on how far the axis has grown since. zarr-python wrote such sizes for arrays created with a zero-length axis until 3.4, and, from 2.18.7 to 3.2.1, for an explicit chunk size of 0 or `False` on an axis of any length, as in `chunks=(0,)` (those arrays could store no data). On a zero-length axis this opens silently, as before, and appending to it stores chunks of size 1. On an axis of positive length it opens with a `ZarrUserWarning`, which says that the array holds only its fill value, so that recreating it with the wanted chunk shape loses nothing (`zarr.from_array(array.store, name=array.path, data=array, chunks=..., overwrite=True, write_data=False)` keeps its data type, fill value, attributes and codecs), and how to keep it by storing valid metadata instead: `array.update_attributes({})`, then `zarr.consolidate_metadata` if the metadata is consolidated. JSON `true`, as zarr-python 3.0 and 3.2 wrote for a chunk size of `True`, is read as 1, silently, in the chunk grid (regular, and the explicit edges and run-length encoded sizes of a rectilinear one) and in the inner chunk shape of every sharding codec, nested or not. A stored rectilinear chunk grid whose edge lengths are integral JSON floats (`[[4.0, 2]]`), as zarr-python 3.2 wrote for float edges, is read with those edges as integers, silently; a float anywhere no release wrote one (a regular chunk shape, the inner chunk shape of a sharding codec, a run-length repeat count, an edge below 1) is rejected.
66

77
Writing data to an array whose stored metadata holds a regular chunk size of 0 or `false` (other than an empty selection) first stores the upgrade of the metadata the store then holds, unless another writer has stored valid metadata since, so that other readers find the chunks written (a `true` chunk size or an integral float edge is read as the value it equals, so a write stores no metadata for it); in a `ZipStore` this adds a second entry for the metadata document, as every metadata update does. If the metadata the store then holds lays out chunks differently (another writer stored a different chunk size since, say), the write raises a `ValueError` asking to reopen the array, and stores nothing. Apart from the array's own metadata writes (`update_attributes`, `resize`), which store the upgrade as before, this is the only operation that stores it: a group's consolidated metadata keeps such an array's metadata as it was stored (`zarr.consolidate_metadata` copies it as the array's own document holds it) until the array stores its upgrade through the group's handle, and changing a group reads and writes no metadata of its members.

‎src/zarr/core/metadata/upgrades.py‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,12 @@ class Reading(NamedTuple):
5252

5353
RECREATE_HINT: Final = (
5454
"The array holds only its fill value, so recreate it with the chunk shape you want; "
55-
"nothing is lost. To keep it instead, store valid metadata: open the array writable and "
56-
"call `array.update_attributes({})`; if a group holds consolidated metadata for the "
57-
"array, then also call `zarr.consolidate_metadata` on that group."
55+
"nothing is lost: `zarr.from_array(array.store, name=array.path, data=array, "
56+
"chunks=<chunk shape>, overwrite=True, write_data=False)` keeps its data type, fill "
57+
"value, attributes and codecs. To keep the array as it is instead, store valid metadata "
58+
"with `array.update_attributes({})`. Either way, open the array writable first, and if a "
59+
"group holds consolidated metadata for the array, then also call "
60+
"`zarr.consolidate_metadata` on that group."
5861
)
5962
"""How to act on a document whose array can hold no data: a stored chunk size of 0 is read
6063
as the smallest chunk size, which is a poor chunk shape for the data the array grows into,

‎tests/test_metadata/test_upgrades.py‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -742,6 +742,51 @@ def test_stale_handle_write_keeps_valid_document_as_written(
742742
np.testing.assert_array_equal(_open_strictly(path)[...], [9, 0, 0])
743743

744744

745+
@pytest.mark.parametrize("zarr_format", [2, 3])
746+
def test_recreate_hint_recipe(tmp_path: Path, zarr_format: Literal[2, 3]) -> None:
747+
"""The recipe the warning gives for an array read from a stored chunk size of 0
748+
recreates it with the chunk shape asked for, keeping its data type, fill value,
749+
attributes and codecs, and the array then reopens without a warning."""
750+
path = tmp_path / "legacy.zarr"
751+
zarr.create_array(
752+
store=path,
753+
shape=(3,),
754+
chunks=(3,),
755+
dtype="int16",
756+
fill_value=7,
757+
attributes={"units": "m"},
758+
zarr_format=zarr_format,
759+
)
760+
_rewrite_doc(
761+
path,
762+
zarr_format,
763+
lambda doc: (
764+
doc.update(chunks=[0])
765+
if zarr_format == 2
766+
else doc["chunk_grid"]["configuration"].update(chunk_shape=[0])
767+
),
768+
)
769+
recipe = (
770+
"`zarr.from_array(array.store, name=array.path, data=array, chunks=<chunk shape>, "
771+
"overwrite=True, write_data=False)`"
772+
)
773+
with pytest.warns(ZarrUserWarning, match=re.escape(recipe)):
774+
array = zarr.open_array(store=path, mode="r+")
775+
776+
zarr.from_array(
777+
array.store, name=array.path, data=array, chunks=(2,), overwrite=True, write_data=False
778+
)
779+
780+
reopened = _open_strictly(path)
781+
assert reopened.chunks == (2,)
782+
assert reopened.dtype == array.dtype
783+
assert reopened.fill_value == 7
784+
assert reopened.attrs.asdict() == {"units": "m"}
785+
assert reopened.compressors == array.compressors
786+
assert reopened.filters == array.filters
787+
np.testing.assert_array_equal(reopened[...], [7, 7, 7])
788+
789+
745790
def _store_zero(doc: dict[str, Any]) -> None:
746791
_stored_chunks(doc)[0] = 0
747792

0 commit comments

Comments
 (0)