Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion .github/workflows/codspeed.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ jobs:
repo: casey/just
version: 1.58.0
- name: Run the benchmarks
uses: CodSpeedHQ/action@af75f77f17a054769abefaa42fe8dc2975469b5f # v5.6.3
uses: CodSpeedHQ/action@b1f28b5eccf7cb5bfe6a2a89cb3053eb67f1150c # v5.8.0
env:
ZARR_BENCHMARK_CLEAR_CACHE: '1'
with:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/zizmor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@ jobs:
persist-credentials: false

- name: Run zizmor
uses: zizmorcore/zizmor-action@45d7276c9ca790aabd705e31ca99c6185c3e76dd # v0.8.1
uses: zizmorcore/zizmor-action@1b4550e929643b8b26a62303c6bd64814271a4f1 # v0.9.2
2 changes: 1 addition & 1 deletion Justfile
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ coverage-serve *args:

# Run slow Hypothesis tests and write coverage.xml
hypothesis *args:
hatch run {{ quote(hatch_env) }}:coverage run --source=src -m pytest -nauto --run-slow-hypothesis tests/test_properties.py tests/test_store/test_stateful* tests/test_array_stateful.py "$@"
hatch run {{ quote(hatch_env) }}:coverage run --source=src -m pytest -nauto --run-slow-hypothesis tests/test_properties.py tests/test_store/test_stateful* tests/test_array_stateful.py tests/test_zarr_metadata_conformance.py "$@"
hatch run {{ quote(hatch_env) }}:coverage xml

# Validate executable documentation code blocks
Expand Down
1 change: 1 addition & 0 deletions changes/4633.bugfix.1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Creating a sharded array no longer fails when the codecs inside the shard cannot encode the data type's default value, for example a `scale_offset` filter that maps only part of the data type's range. The inner codecs are now checked against the array's fill value, which must still be encodable.
1 change: 1 addition & 0 deletions changes/4633.bugfix.2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Creating or opening an array no longer rejects a codec placed after a data-type-changing codec such as `cast_value` because its configuration would be invalid for the array's original data type. Each codec is now checked against the data type it receives.
23 changes: 15 additions & 8 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -111,41 +111,47 @@ homepage = "https://github.com/zarr-developers/zarr-python"
# pins deliberately, e.g. via dependabot or `uv lock --upgrade`.
[dependency-groups]
test = [
"coverage==7.17.3",
"coverage==7.17.5",
"pytest==9.2.0",
"pytest-asyncio==1.5.0",
"pytest-cov==7.1.0",
"numpydoc==1.11.0",
"hypothesis==6.170.15",
"hypothesis==6.170.17",
"pytest-reportlog==1.0.0",
"pytest-xdist==3.8.0",
"pytest-benchmark==5.3.1",
"pytest-codspeed==5.0.4",
"uv==0.13.9",
"uv==0.13.10",
]
# tests/test_zarr_metadata_conformance.py draws array documents from zarr-metadata's
# strategies. Its own group, because zarr-metadata needs a newer typing-extensions than
# the `min_deps` env pins; the test skips where it is not installed.
conformance = [
"zarr-metadata[testing]==0.9.0",
]
remote-tests = [
{include-group = "test"},
"fsspec>=2023.10.0",
"obstore>=0.5.1",
"botocore",
"s3fs>=2023.10.0",
"moto[s3,server]==5.2.5",
"moto[s3,server]==5.2.6",
"requests==2.34.2",
]
release = [
"towncrier==26.9.0",
]
docs = [
# Doc building
"mkdocs-material[imaging]==9.8.1",
"mkdocs-material[imaging]==9.8.2",
"mkdocs==1.6.1",
"mkdocstrings==1.0.8",
"mkdocstrings-python==2.0.11",
"mike==2.2.0",
"mkdocs-redirects==1.2.3",
"markdown-exec[ansi]==1.13.0",
"markdown-exec[ansi]==1.14.0",
"griffe-inherited-docstrings==1.1.4",
"ruff==0.17.5",
"ruff==0.17.6",
# Changelog generation
{include-group = "release"},
# Optional dependencies to run examples
Expand All @@ -156,6 +162,7 @@ docs = [
]
dev = [
{include-group = "test"},
{include-group = "conformance"},
{include-group = "remote-tests"},
{include-group = "docs"},
"universal-pathlib",
Expand Down Expand Up @@ -188,7 +195,7 @@ hooks.vcs.version-file = "src/zarr/_version.py"
dependency-groups = ["dev"]

[tool.hatch.envs.test]
dependency-groups = ["test"]
dependency-groups = ["test", "conformance"]

[tool.hatch.envs.test.env-vars]

Expand Down
75 changes: 40 additions & 35 deletions src/zarr/codecs/sharding.py
Original file line number Diff line number Diff line change
Expand Up @@ -588,27 +588,47 @@ def to_dict(self) -> dict[str, JSON]:
}

def evolve_from_array_spec(self, array_spec: ArraySpec) -> Self:
"""Thread the spec through the inner chain.
"""Evolve and validate the inner chain against the chunks it encodes.

Each codec is evolved against the spec produced by the previous one.
Evolving every codec against the same unthreaded spec is the bug shape that
strips `BytesCodec.endian` behind a dtype-changing codec β€” and this
method runs on the real array-creation path, baking the damaged chain
into the evolved instance before the transform builders ever run.
Each inner codec is evolved against, validated against, and resolved through
the spec the codecs before it leave, as array metadata does for its own codecs,
with a regular grid of one inner chunk as its chunk grid. A sharding codec among
them is thereby checked against the chunk it splits.

The spec is threaded for two reasons. A dtype-changing codec changes what the
codecs after it see: evolving every codec against the unthreaded spec strips
`BytesCodec.endian` behind one. And the fill value the inner chain must encode
is the array's fill value as the codecs before this one leave it, which is what
an unwritten inner chunk holds. Probing the chain with any other value, such as
the data type's default scalar, rejects chains that never see that value.

Parameters
----------
array_spec
The base spec to be evolved as we thread it.
The spec of the shards this codec encodes.

Returns
-------
This codec with the evolved code chain.
"""
from zarr.core.chunk_utils import evolve_codecs
This codec with the evolved codec chain.

shard_spec = self._get_chunk_spec(array_spec)
evolved_codecs = evolve_codecs(self.codecs, shard_spec)
Raises
------
ValueError
If an inner codec is incompatible with the chunk it encodes, or cannot
encode the fill value it receives.
"""
evolved: list[Codec] = []
spec = self._get_chunk_spec(array_spec)
for codec in self.codecs:
evolved_codec = codec.evolve_from_array_spec(spec)
evolved_codec.validate(
shape=spec.shape,
dtype=spec.dtype,
chunk_grid=RegularChunkGridMetadata(chunk_shape=spec.shape),
)
evolved.append(evolved_codec)
spec = evolved_codec.resolve_metadata(spec)
evolved_codecs = tuple(evolved)
if evolved_codecs != self.codecs:
return replace(self, codecs=evolved_codecs)
return self
Expand All @@ -635,35 +655,20 @@ def validate(
f"Sharding is only compatible with regular and rectilinear chunk grids, "
f"got {type(chunk_grid)}"
)
for i, (edges, inner) in enumerate(zip(edges_per_dim, self.chunk_shape, strict=False)):
# A codec before this one can change the number of dimensions, while `chunk_grid`
# is still the array's own. Its edges then say nothing about the shards.
if len(edges_per_dim) != len(self.chunk_shape):
raise ValueError(
f"The shard's `chunk_shape` has rank {len(self.chunk_shape)}, but the "
f"array's chunk grid has rank {len(edges_per_dim)}."
)
for i, (edges, inner) in enumerate(zip(edges_per_dim, self.chunk_shape, strict=True)):
for edge in set(edges):
if edge % inner != 0:
raise ValueError(
f"Chunk edge length {edge} in dimension {i} is not "
f"divisible by the shard's inner chunk size {inner}."
)
self._validate_inner_codecs(dtype)

def _validate_inner_codecs(self, dtype: ZDType[TBaseDType, TBaseScalar]) -> None:
"""Validate the codecs that encode each inner chunk, as array metadata validates
its codecs: each against the chunk the codecs before it leave it, a regular
grid of one inner chunk. A sharding codec among them is thereby checked against
the chunk it splits, which `validate` of the outer codec alone does not see.
"""
spec = ArraySpec(
shape=self.chunk_shape,
dtype=dtype,
fill_value=dtype.default_scalar(),
config=ArrayConfig.from_dict({}),
prototype=default_buffer_prototype(),
)
for codec in self.codecs:
codec.validate(
shape=spec.shape,
dtype=spec.dtype,
chunk_grid=RegularChunkGridMetadata(chunk_shape=spec.shape),
)
spec = codec.resolve_metadata(spec)

def _get_inner_chunk_transform(self, shard_spec: ArraySpec) -> Any:
"""The synchronous transform for the inner codec chain.
Expand Down
10 changes: 6 additions & 4 deletions src/zarr/core/metadata/v3.py
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,9 @@ def __init__(
# Per-codec validate() must run before resolve_metadata(), since the
# latter may rely on invariants the former checks (e.g. cast_value
# rejects complex source dtypes that would otherwise crash _do_cast).
# Codecs validate against the chunk grid, so its rank must be checked first.
if len(shape_parsed) != chunk_grid_parsed.ndim:
raise ValueError("`chunk_grid` and `shape` need to have the same number of dimensions.")
evolved: list[Codec] = []
spec = array_spec
for c in codecs_parsed_partial:
Expand All @@ -594,8 +597,6 @@ def __init__(
self._validate_metadata()

def _validate_metadata(self) -> None:
if len(self.shape) != self.chunk_grid.ndim:
raise ValueError("`chunk_grid` and `shape` need to have the same number of dimensions.")
if isinstance(self.chunk_grid, RectilinearChunkGridMetadata):
validate_rectilinear_edges(self.chunk_grid.chunk_shapes, self.shape)
if self.dimension_names is not None and len(self.shape) != len(self.dimension_names):
Expand All @@ -604,8 +605,9 @@ def _validate_metadata(self) -> None:
)
if self.fill_value is None:
raise ValueError("`fill_value` is required.")
for codec in self.codecs:
codec.validate(shape=self.shape, dtype=self.data_type, chunk_grid=self.chunk_grid)
# The codecs were validated in `__init__`, each against the spec the codecs
# before it leave. Validating them again here against the array's own data type
# would reject a codec that only ever sees the output of a dtype-changing codec.

@property
def ndim(self) -> int:
Expand Down
23 changes: 22 additions & 1 deletion tests/test_codecs/test_cast_value.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import zarr
from tests.conftest import Expect, ExpectFail
from zarr.codecs import BytesCodec, TransposeCodec
from zarr.codecs import BytesCodec, ScaleOffset, TransposeCodec
from zarr.codecs.cast_value import CAST_VALUE_RS_MIN_VERSION, CastValue
from zarr.storage import MemoryStore

Expand Down Expand Up @@ -610,3 +610,24 @@ def make_array(filters: list[Any]) -> Any:

array[:] = data
assert_array_equal(array[:], data)


@requires_cast_value_rs
@pytest.mark.parametrize("shards", [None, (8,)], ids=["unsharded", "sharded"])
def test_codec_validated_against_the_dtype_it_sees(shards: tuple[int, ...] | None) -> None:
"""A codec behind `cast_value` is validated against the cast's target data type,
not the array's: a `scale_offset` scale of 0.5 is representable in float32, which
the codec sees, though not in the array's int16."""
store = MemoryStore()
data = np.arange(8, dtype="int16")
arr = zarr.create_array(
store,
shape=data.shape,
dtype=data.dtype,
chunks=(4,),
shards=shards,
filters=[CastValue(data_type="float32"), ScaleOffset(scale=0.5)],
compressors=None,
)
arr[:] = data
assert_array_equal(zarr.open_array(store, mode="r")[:], data)
105 changes: 104 additions & 1 deletion tests/test_codecs/test_sharding.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@
BytesCodec,
Crc32cCodec,
GzipCodec,
ScaleOffset,
ShardingCodec,
TransposeCodec,
)
from zarr.codecs.numcodecs import CRC32
from zarr.codecs.numcodecs import CRC32, PackBits
from zarr.codecs.sharding import (
INDEX_LOCATION,
MAX_UINT_64,
Expand Down Expand Up @@ -1522,3 +1523,105 @@ def test_open_rejects_stored_nested_sharding_with_indivisible_inner_chunk_shape(
doc["codecs"][0]["configuration"]["codecs"][0]["configuration"]["chunk_shape"] = (4, 7)
with pytest.raises(ValueError, match="not divisible"):
ArrayV3Metadata.from_dict(doc)


# Each layout pairs codecs with a fill value the inner chain can encode, as the codecs
# before it leave that value, and one it cannot. The accepted fill value is chosen so
# that neither the data type's default scalar (0) nor, where a filter precedes the
# sharding codec, the array's fill value as given would pass the inner chain.
_INNER_CHAIN_FILL_LAYOUTS: dict[str, tuple[dict[str, Any], int, int]] = {
# `scale_offset` inside the shard: the inner chain encodes [100, 65535].
"inside-shard": (
{"chunks": (4,), "shards": (16,), "filters": [ScaleOffset(offset=100)]},
100,
0,
),
# `scale_offset` before the sharding codec doubles the fill value, so the inner
# chain sees 120 for an array fill value of 60, and 80 for 40. Its own
# `scale_offset` encodes [100, 65535].
"behind-filter": (
{
"chunks": (16,),
"filters": [ScaleOffset(scale=2)],
"serializer": ShardingCodec(
chunk_shape=(4,), codecs=[ScaleOffset(offset=100), BytesCodec()]
),
},
60,
40,
),
# The fill value reaches a sharding codec nested in another one.
"nested": (
{
"chunks": (16,),
"serializer": ShardingCodec(
chunk_shape=(8,),
codecs=[
ShardingCodec(chunk_shape=(4,), codecs=[ScaleOffset(offset=100), BytesCodec()])
],
),
},
100,
0,
),
}


@pytest.mark.filterwarnings(
"ignore:Combining a `sharding_indexed` codec:zarr.errors.ZarrUserWarning"
)
@pytest.mark.parametrize("layout", _INNER_CHAIN_FILL_LAYOUTS)
def test_sharding_inner_chain_probed_with_array_fill_value(layout: str) -> None:
"""A sharding codec's inner chain is validated against the array's fill value, as
the codecs before it leave it, not against the data type's default scalar
(https://github.com/zarr-developers/zarr-python/issues/4511). The array is created,
reads its fill value before any write, and round-trips a partial write."""
codecs, fill_value, _ = _INNER_CHAIN_FILL_LAYOUTS[layout]
store = MemoryStore()
arr = zarr.create_array(
store, shape=(16,), dtype="uint16", fill_value=fill_value, compressors=None, **codecs
)
expected = np.full(16, fill_value, dtype="uint16")
np.testing.assert_array_equal(arr[:], expected)
written = np.arange(110, 114, dtype="uint16")
arr[4:8] = written
expected[4:8] = written
np.testing.assert_array_equal(zarr.open_array(store, mode="r")[:], expected)


@pytest.mark.filterwarnings(
"ignore:Combining a `sharding_indexed` codec:zarr.errors.ZarrUserWarning"
)
@pytest.mark.parametrize("layout", _INNER_CHAIN_FILL_LAYOUTS)
def test_sharding_rejects_fill_value_inner_chain_cannot_encode(layout: str) -> None:
"""An array whose fill value the inner chain cannot encode is rejected: an
unwritten inner chunk holds the fill value, so the array could not be read before
its first write."""
codecs, _, fill_value = _INNER_CHAIN_FILL_LAYOUTS[layout]
with pytest.raises(ValueError, match="outside the range"):
zarr.create_array(
MemoryStore(),
shape=(16,),
dtype="uint16",
fill_value=fill_value,
compressors=None,
**codecs,
)


@pytest.mark.filterwarnings("ignore:Numcodecs codecs are not in the Zarr version 3 specification")
def test_sharding_rejects_filter_that_changes_the_number_of_dimensions() -> None:
"""A sharding codec behind a filter that flattens each chunk is rejected: the array's
chunk grid, which the inner chunk shape is checked against, says nothing about the
flattened chunk. Accepting it lost the end of each chunk whose flattened length the
inner chunk size did not divide."""
with pytest.raises(ValueError, match="has rank 1, but the array's chunk grid has rank 2"):
zarr.create_array(
MemoryStore(),
shape=(8, 8),
chunks=(8, 8),
dtype="bool",
filters=[PackBits()],
serializer=ShardingCodec(chunk_shape=(8,)),
compressors=None,
)
Loading
Loading