@@ -296,6 +296,22 @@ def _merge_chunk_array(
296296 is_complete_chunk : bool ,
297297 drop_axes : tuple [int , ...],
298298 ) -> NDBuffer :
299+ if (
300+ is_complete_chunk
301+ and value .shape == chunk_spec .shape
302+ # Guard that this is not a partial chunk at the end with is_complete_chunk=True
303+ and value [out_selection ].shape == chunk_spec .shape
304+ ):
305+ return value
306+ if existing_chunk_array is None :
307+ chunk_array = chunk_spec .prototype .nd_buffer .create (
308+ shape = chunk_spec .shape ,
309+ dtype = chunk_spec .dtype .to_native_dtype (),
310+ order = chunk_spec .order ,
311+ fill_value = fill_value_or_default (chunk_spec ),
312+ )
313+ else :
314+ chunk_array = existing_chunk_array .copy () # make a writable copy
299315 if chunk_selection == () or is_scalar (
300316 value .as_ndarray_like (), chunk_spec .dtype .to_native_dtype ()
301317 ):
@@ -311,20 +327,6 @@ def _merge_chunk_array(
311327 for idx in range (chunk_spec .ndim )
312328 )
313329 chunk_value = chunk_value [item ]
314- if is_complete_chunk and chunk_value .shape == chunk_spec .shape :
315- # TODO: For the last chunk, we could have is_complete_chunk=True
316- # that is smaller than the chunk_spec.shape but this throws
317- # an error in the _decode_single
318- return chunk_value .copy () # make a writable copy
319- if existing_chunk_array is None :
320- chunk_array = chunk_spec .prototype .nd_buffer .create (
321- shape = chunk_spec .shape ,
322- dtype = chunk_spec .dtype .to_native_dtype (),
323- order = chunk_spec .order ,
324- fill_value = fill_value_or_default (chunk_spec ),
325- )
326- else :
327- chunk_array = existing_chunk_array .copy () # make a writable copy
328330 chunk_array [chunk_selection ] = chunk_value
329331 return chunk_array
330332
0 commit comments