From ee6bb2dd6bf3d266ee9cf09954c23f4cb5a84f18 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edgar=20Ram=C3=ADrez=20Mondrag=C3=B3n?= Date: Mon, 26 Oct 2026 11:42:07 -0600 Subject: [PATCH 1/2] fix(python): Support iterating map arrays as Python objects MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Edgar Ramírez Mondragón --- python/src/nanoarrow/iterator.py | 21 +++++++++++++++++++++ python/tests/test_iterator.py | 13 +++++++++++++ 2 files changed, 34 insertions(+) diff --git a/python/src/nanoarrow/iterator.py b/python/src/nanoarrow/iterator.py index 6b66e9eb6..1ee36e01c 100644 --- a/python/src/nanoarrow/iterator.py +++ b/python/src/nanoarrow/iterator.py @@ -281,6 +281,26 @@ 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): + # A map is a list of (key, value) structs, converted to a list of tuples + 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 +645,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..a042ead44 100644 --- a/python/tests/test_iterator.py +++ b/python/tests/test_iterator.py @@ -278,6 +278,19 @@ 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") + 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]] From ac5da3fa9285bf4a5be8438968d9f347a4b828cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edgar=20Ram=C3=ADrez=20Mondrag=C3=B3n?= Date: Wed, 28 Oct 2026 23:41:52 -0600 Subject: [PATCH 2/2] Handle zero-length maps first MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Edgar Ramírez Mondragón --- python/src/nanoarrow/iterator.py | 4 +++- python/tests/test_iterator.py | 4 ++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/python/src/nanoarrow/iterator.py b/python/src/nanoarrow/iterator.py index 1ee36e01c..0d577b776 100644 --- a/python/src/nanoarrow/iterator.py +++ b/python/src/nanoarrow/iterator.py @@ -282,7 +282,9 @@ def _list_iter(self, offset, length): yield list(islice(child_iter, end - start)) def _map_iter(self, offset: int, length: int): - # A map is a list of (key, value) structs, converted to a list of tuples + if length == 0: + return + view = self._array_view offset += view.offset diff --git a/python/tests/test_iterator.py b/python/tests/test_iterator.py index a042ead44..b07a585d2 100644 --- a/python/tests/test_iterator.py +++ b/python/tests/test_iterator.py @@ -280,6 +280,10 @@ def test_iterator_nullable_list(): 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