diff --git a/python/src/nanoarrow/iterator.py b/python/src/nanoarrow/iterator.py index 6b66e9eb6..0d577b776 100644 --- a/python/src/nanoarrow/iterator.py +++ b/python/src/nanoarrow/iterator.py @@ -281,6 +281,28 @@ def _list_iter(self, offset, length): for start, end in zip(starts, ends): yield list(islice(child_iter, end - start)) + def _map_iter(self, offset: int, length: int): + if length == 0: + return + + view = self._array_view + offset += view.offset + + offsets = memoryview(view.buffer(1))[offset : (offset + length + 1)] + starts = offsets[:-1] + ends = offsets[1:] + entries = self._children[0] + entries_iter = entries._struct_tuple_iter(starts[0], ends[-1] - starts[0]) + + if self._contains_nulls(): + validity = view.buffer(0).elements(offset, length) + for is_valid, start, end in zip(validity, starts, ends): + item = list(islice(entries_iter, end - start)) + yield item if is_valid else None + else: + for start, end in zip(starts, ends): + yield list(islice(entries_iter, end - start)) + def _fixed_size_list_iter(self, offset, length): view = self._array_view offset += view.offset @@ -625,6 +647,7 @@ def _get_tzinfo(tz_string, strategy=None): _types.STRUCT: "_struct_iter", _types.LIST: "_list_iter", _types.LARGE_LIST: "_list_iter", + _types.MAP: "_map_iter", _types.FIXED_SIZE_LIST: "_fixed_size_list_iter", _types.SPARSE_UNION: "_sparse_union_iter", _types.DENSE_UNION: "_dense_union_iter", diff --git a/python/tests/test_iterator.py b/python/tests/test_iterator.py index 8e62027b0..b07a585d2 100644 --- a/python/tests/test_iterator.py +++ b/python/tests/test_iterator.py @@ -278,6 +278,23 @@ def test_iterator_nullable_list(): assert (list(iter_py(nested_sliced))) == [[4, 5, 6], [7, 8, None], [0], None] +def test_iterator_map(): + pa = pytest.importorskip("pyarrow") + + array = pa.array([], pa.map_(pa.string(), pa.int64())) + assert list(iter_py(array)) == [] + + items = [[("a", 1), ("b", None)], [], None, [("c", 3)]] + array = pa.array(items, pa.map_(pa.string(), pa.int64())) + assert list(iter_py(array)) == items + assert list(iter_py(array[1:])) == items[1:] + + # No nulls takes the non-nullable path + items = [[("a", 1)], [], [("b", 2), ("c", 3)]] + array = pa.array(items, pa.map_(pa.string(), pa.int64())) + assert list(iter_py(array)) == items + + def test_iterator_fixed_size_list(): pa = pytest.importorskip("pyarrow") items = [[1, 2, 3], [4, 5, 6], [7, 8, None]]