Skip to content

Commit 3db2e22

Browse files
committed
implement config-sensitive write_empty_chunks in write_batch, and add a test
1 parent 25fc9ca commit 3db2e22

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

src/zarr/codecs/pipeline.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ async def _read_key(
361361
_read_key,
362362
config.get("async.concurrency"),
363363
)
364-
chunk_array_batch = await self.decode_batch(
364+
chunk_array_decoded = await self.decode_batch(
365365
[
366366
(chunk_bytes, chunk_spec)
367367
for chunk_bytes, (_, chunk_spec, _, _) in zip(
@@ -370,19 +370,20 @@ async def _read_key(
370370
],
371371
)
372372

373-
chunk_array_batch = [
373+
chunk_array_merged = [
374374
self._merge_chunk_array(
375375
chunk_array, value, out_selection, chunk_spec, chunk_selection, drop_axes
376376
)
377377
for chunk_array, (_, chunk_spec, chunk_selection, out_selection) in zip(
378-
chunk_array_batch, batch_info, strict=False
378+
chunk_array_decoded, batch_info, strict=False
379379
)
380380
]
381+
chunk_array_batch: list[NDBuffer | None] = []
381382
for chunk_array, (_, chunk_spec, _, _) in zip(
382-
chunk_array_batch, batch_info, strict=False
383+
chunk_array_merged, batch_info, strict=False
383384
):
384385
if chunk_array is None:
385-
chunk_array_batch.append(None)
386+
chunk_array_batch.append(None) # type: ignore[unreachable]
386387
else:
387388
if not write_empty_chunks and chunk_array.all_equal(chunk_spec.fill_value):
388389
chunk_array_batch.append(None)

tests/test_array.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from zarr.core.array import chunks_initialized
1212
from zarr.core.buffer.cpu import NDBuffer
1313
from zarr.core.common import JSON, MemoryOrder, ZarrFormat
14+
from zarr.core.config import config
1415
from zarr.core.group import AsyncGroup
1516
from zarr.core.indexing import ceildiv
1617
from zarr.core.sync import sync
@@ -436,3 +437,22 @@ def test_array_create_order(
436437
assert vals.flags.f_contiguous
437438
else:
438439
raise AssertionError
440+
441+
442+
@pytest.mark.parametrize("store", ["memory"], indirect=True)
443+
@pytest.mark.parametrize("write_empty_chunks", [True, False])
444+
@pytest.mark.parametrize("fill_value", [0, 5])
445+
def test_write_empty_chunks(
446+
zarr_format: ZarrFormat, store: MemoryStore, write_empty_chunks: bool, fill_value: int
447+
) -> None:
448+
arr = Array.create(
449+
store=store, shape=(1,), zarr_format=zarr_format, dtype="i4", fill_value=fill_value
450+
)
451+
452+
with config.set({"array.write_empty_chunks": write_empty_chunks}):
453+
arr[:] = fill_value
454+
455+
if not write_empty_chunks:
456+
assert arr.nchunks_initialized == 0
457+
else:
458+
assert arr.nchunks_initialized == arr.nchunks

0 commit comments

Comments
 (0)