Skip to content

(fix): structured dtype fill value consolidated metadata #3015

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

Merged
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions changes/2998.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix structured `dtype` fill value serialization for consolidated metadata
9 changes: 8 additions & 1 deletion src/zarr/core/group.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import asyncio
import base64
import itertools
import json
import logging
Expand Down Expand Up @@ -358,7 +359,13 @@
d[f"{k}/{ZATTRS_JSON}"] = _replace_special_floats(attrs)
if "shape" in v:
# it's an array
d[f"{k}/{ZARRAY_JSON}"] = _replace_special_floats(v)
if isinstance(v.get("fill_value", None), np.void):
v["fill_value"] = base64.standard_b64encode(

Check warning on line 363 in src/zarr/core/group.py

View check run for this annotation

Codecov / codecov/patch

src/zarr/core/group.py#L362-L363

Added lines #L362 - L363 were not covered by tests
cast(bytes, v["fill_value"])
).decode("ascii")
else:
v = _replace_special_floats(v)
d[f"{k}/{ZARRAY_JSON}"] = v

Check warning on line 368 in src/zarr/core/group.py

View check run for this annotation

Codecov / codecov/patch

src/zarr/core/group.py#L367-L368

Added lines #L367 - L368 were not covered by tests
else:
d[f"{k}/{ZGROUP_JSON}"] = {
"zarr_format": self.zarr_format,
Expand Down
13 changes: 13 additions & 0 deletions tests/test_metadata/test_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,3 +316,16 @@ def test_zstd_checksum() -> None:
arr.metadata.to_buffer_dict(default_buffer_prototype())[".zarray"].to_bytes()
)
assert "checksum" not in metadata["compressor"]


def test_structured_dtype_fill_value_serialization(tmp_path):
group_path = tmp_path / "test.zarr"
root_group = zarr.open_group(group_path, mode="w", zarr_format=2)
root_group.create_array(
name="structured_headers",
shape=(100, 100),
chunks=(100, 100),
dtype=np.dtype([("foo", "i4"), ("bar", "i4")]),
)

zarr.consolidate_metadata(root_group.store, zarr_format=2)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this test is weak (it only tests that consolidate metadata doesn't error). Do you think it makes sense to make this test a big stronger, e.g. by checking that the fill value was actually encoded the way we think it should have been?

Also I think we need a check to ensure that if the dtype is void and the fill value is None, then there's no base64 encoding (I know from the implementation in this PR that the test will pass, but it's good to have the test in any case)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this test is weak (it only tests that consolidate metadata doesn't error). Do you think it makes sense to make this test a big stronger, e.g. by checking that the fill value was actually encoded the way we think it should have been?

Oh wow, I totally intended to do that (hence the name of the test).

Also I think we need a check to ensure that if the dtype is void and the fill value is None, then there's no base64 encoding

Great suggestion

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! tried both!