Skip to content

Commit 9c29a0d

Browse files
dylanpulverd-v-b
andauthored
fix: shape the value buffer for coordinate selections in sharded writes (#4284)
* fix: reshape the value buffer for coordinate selections in sharded writes * docs: add changelog entry * fix: reshape the value buffer in the sync partial-encode twin too _encode_partial_sync derives its indexer the same way as _encode_partial_single and so hits the same coordinate-selection shape mismatch under FusedCodecPipeline. The regression test is parametrized over both pipelines. --------- Co-authored-by: Dylan Pulver <dylanpulver@users.noreply.github.com> Co-authored-by: Davis Bennett <davis.v.bennett@gmail.com>
1 parent 45ead4f commit 9c29a0d

3 files changed

Lines changed: 70 additions & 12 deletions

File tree

‎changes/4284.bugfix.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixed a `ValueError` when setting an orthogonal selection on a sharded array where more than one dimension is indexed by an array. The sharding codec re-derives an indexer from the chunk selection it is handed, which turns such a selection into a coordinate selection addressing the value buffer flat, so the write failed on a shape mismatch. Both partial-encode paths are fixed, so the write works under either codec pipeline.

‎src/zarr/codecs/sharding.py‎

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -796,13 +796,18 @@ def _encode_partial_sync(
796796
chunk_spec = self._get_chunk_spec(shard_spec)
797797
inner_transform = self._get_inner_chunk_transform(shard_spec)
798798

799-
indexer = list(
800-
get_indexer(
801-
selection,
802-
shape=shard_shape,
803-
chunk_grid=ChunkGrid.from_sizes(shard_shape, self.chunk_shape),
804-
)
799+
shard_indexer = get_indexer(
800+
selection,
801+
shape=shard_shape,
802+
chunk_grid=ChunkGrid.from_sizes(shard_shape, self.chunk_shape),
805803
)
804+
# A coordinate indexer flattens the selection, so its projections address
805+
# `value` as 1-D while the caller shaped it like `sel_shape`. Mirrors the
806+
# reshape `_encode_partial_single` applies on the async path.
807+
sel_shape = getattr(shard_indexer, "sel_shape", None)
808+
if sel_shape is not None and value.shape == sel_shape:
809+
value = value.reshape(shard_indexer.shape)
810+
indexer = list(shard_indexer)
806811

807812
is_complete = self._is_complete_shard_write(indexer, chunks_per_shard)
808813

@@ -1359,13 +1364,18 @@ async def _encode_partial_single(
13591364
chunks_per_shard = self._get_chunks_per_shard(shard_spec)
13601365
chunk_spec = self._get_chunk_spec(shard_spec)
13611366

1362-
indexer = list(
1363-
get_indexer(
1364-
selection,
1365-
shape=shard_shape,
1366-
chunk_grid=ChunkGrid.from_sizes(shard_shape, chunk_shape),
1367-
)
1367+
shard_indexer = get_indexer(
1368+
selection,
1369+
shape=shard_shape,
1370+
chunk_grid=ChunkGrid.from_sizes(shard_shape, chunk_shape),
13681371
)
1372+
# A coordinate indexer flattens the selection, so its projections address
1373+
# `shard_array` as 1-D while the caller shaped it like `sel_shape`. This
1374+
# mirrors the reshape `_decode_partial_single` applies on the way out.
1375+
sel_shape = getattr(shard_indexer, "sel_shape", None)
1376+
if sel_shape is not None and shard_array.shape == sel_shape:
1377+
shard_array = shard_array.reshape(shard_indexer.shape)
1378+
indexer = list(shard_indexer)
13691379

13701380
if self._is_complete_shard_write(indexer, chunks_per_shard):
13711381
shard_dict = dict.fromkeys(lexicographic_order_coords(chunks_per_shard))

‎tests/test_codecs/test_sharding.py‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1262,3 +1262,50 @@ def test_shard_reader_to_dict_vectorized(chunks_per_shard: tuple[int, ...]) -> N
12621262
assert buf.to_bytes() == present[coords]
12631263
else:
12641264
assert buf is None
1265+
1266+
1267+
@pytest.mark.parametrize(
1268+
"pipeline_path",
1269+
[
1270+
"zarr.core.codec_pipeline.FusedCodecPipeline",
1271+
"zarr.core.codec_pipeline.BatchedCodecPipeline",
1272+
],
1273+
)
1274+
@pytest.mark.parametrize("nested", [False, True], ids=["single", "nested"])
1275+
def test_sharding_orthogonal_set_multiple_array_dims(nested: bool, pipeline_path: str) -> None:
1276+
"""Orthogonal set with more than one array-indexed dimension.
1277+
1278+
``OrthogonalIndexer`` converts such a chunk selection to an ``np.ix_`` pair
1279+
of broadcastable arrays before handing it to the codec pipeline. The
1280+
sharding codec re-derives an indexer from that selection and gets a
1281+
``CoordinateIndexer``, whose projections address the value buffer flat.
1282+
Regression test for the resulting shape mismatch on write.
1283+
1284+
Parametrized over both pipelines because the partial-encode path is
1285+
written twice -- ``_encode_partial_single`` for ``BatchedCodecPipeline``
1286+
and ``_encode_partial_sync`` for ``FusedCodecPipeline`` -- and each
1287+
derives its own indexer.
1288+
"""
1289+
inner = ShardingCodec(chunk_shape=(1, 1), codecs=(BytesCodec(),))
1290+
serializer = ShardingCodec(chunk_shape=(2, 2), codecs=((inner,) if nested else (BytesCodec(),)))
1291+
base = np.arange(16, dtype="int32").reshape(4, 4)
1292+
selection = (np.array([3, 1, 2]), np.array([0, 2]))
1293+
value = np.arange(6, dtype="int32").reshape(3, 2) + 100
1294+
1295+
with zarr.config.set({"codec_pipeline.path": pipeline_path}):
1296+
a = zarr.create_array(
1297+
MemoryStore(),
1298+
shape=base.shape,
1299+
chunks=(2, 4),
1300+
dtype=base.dtype,
1301+
serializer=serializer,
1302+
compressors=None,
1303+
fill_value=0,
1304+
)
1305+
a[:] = base
1306+
a.oindex[selection] = value
1307+
1308+
expected = base.copy()
1309+
expected[np.ix_(*selection)] = value
1310+
assert np.array_equal(a[:], expected)
1311+
assert np.array_equal(a.oindex[selection], value)

0 commit comments

Comments
 (0)