Skip to content

Commit 83faf56

Browse files
committed
Fix a bug when setting complete chunks
Closes #2849
1 parent 8ad4cd6 commit 83faf56

File tree

2 files changed

+40
-11
lines changed

2 files changed

+40
-11
lines changed

src/zarr/core/codec_pipeline.py

Lines changed: 14 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,20 @@ def _merge_chunk_array(
320309
for idx in range(chunk_spec.ndim)
321310
)
322311
chunk_value = chunk_value[item]
312+
if is_complete_chunk:
313+
# TODO: For the last chunk, we could have is_complete_chunk=True
314+
# that is smaller than the chunk_spec.shape but this throws
315+
# an error in the _decode_single
316+
return chunk_value
317+
if existing_chunk_array is None:
318+
chunk_array = chunk_spec.prototype.nd_buffer.create(
319+
shape=chunk_spec.shape,
320+
dtype=chunk_spec.dtype,
321+
order=chunk_spec.order,
322+
fill_value=fill_value_or_default(chunk_spec),
323+
)
324+
else:
325+
chunk_array = existing_chunk_array.copy() # make a writable copy
323326
chunk_array[chunk_selection] = chunk_value
324327
return chunk_array
325328

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)