Skip to content

Commit c076237

Browse files
committed
Fix a bug when setting complete chunks
Closes zarr-developers#2849
1 parent 8ad4cd6 commit c076237

2 files changed

Lines changed: 39 additions & 11 deletions

File tree

‎src/zarr/core/codec_pipeline.py‎

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -296,17 +296,6 @@ def _merge_chunk_array(
296296
is_complete_chunk: bool,
297297
drop_axes: tuple[int, ...],
298298
) -> NDBuffer:
299-
if is_complete_chunk and value.shape == chunk_spec.shape:
300-
return value
301-
if existing_chunk_array is None:
302-
chunk_array = chunk_spec.prototype.nd_buffer.create(
303-
shape=chunk_spec.shape,
304-
dtype=chunk_spec.dtype,
305-
order=chunk_spec.order,
306-
fill_value=fill_value_or_default(chunk_spec),
307-
)
308-
else:
309-
chunk_array = existing_chunk_array.copy() # make a writable copy
310299
if chunk_selection == () or is_scalar(value.as_ndarray_like(), chunk_spec.dtype):
311300
chunk_value = value
312301
else:
@@ -320,6 +309,19 @@ def _merge_chunk_array(
320309
for idx in range(chunk_spec.ndim)
321310
)
322311
chunk_value = chunk_value[item]
312+
if is_complete_chunk and chunk_value.shape == chunk_spec.shape:
313+
# TODO: For the last chunk, we could gave is_complete_chunk=True
314+
# that is smaller than the chunk_spec.shape
315+
return chunk_value
316+
if existing_chunk_array is None:
317+
chunk_array = chunk_spec.prototype.nd_buffer.create(
318+
shape=chunk_spec.shape,
319+
dtype=chunk_spec.dtype,
320+
order=chunk_spec.order,
321+
fill_value=fill_value_or_default(chunk_spec),
322+
)
323+
else:
324+
chunk_array = existing_chunk_array.copy() # make a writable copy
323325
chunk_array[chunk_selection] = chunk_value
324326
return chunk_array
325327

‎tests/test_array.py‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -829,6 +829,32 @@ def test_append_bad_shape(store: MemoryStore, zarr_format: ZarrFormat) -> None:
829829
z.append(b)
830830

831831

832+
def test_append_shape_not_equals_chunk_shape() -> None:
833+
# regression test for GH2849
834+
g = zarr.open("foo.zarr", zarr_format=3, mode="w")
835+
g["bar"] = np.arange(3)
836+
837+
size = 4
838+
data = [0, 1, 2, 3]
839+
g["bar"].append(data)
840+
assert (g["bar"][-size:] == data).all(), (g["bar"][-size:], data)
841+
842+
size = 3
843+
data = [-1, -2, -3]
844+
g["bar"].append(data)
845+
assert (g["bar"][-size:] == data).all(), (g["bar"][-size:], data)
846+
847+
size = 2
848+
data = [-10, -11]
849+
g["bar"].append(data)
850+
assert (g["bar"][-size:] == data).all(), (g["bar"][-size:], data)
851+
852+
size = 3
853+
data = [-4, -5, -6]
854+
g["bar"].append(data)
855+
assert (g["bar"][-size:] == data).all(), (g["bar"][-size:], data)
856+
857+
832858
@pytest.mark.parametrize("store", ["memory"], indirect=True)
833859
@pytest.mark.parametrize("write_empty_chunks", [True, False])
834860
@pytest.mark.parametrize("fill_value", [0, 5])

0 commit comments

Comments
 (0)