Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions python/src/nanoarrow/iterator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Comment on lines +292 to +295

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe here we have to handle the zero length case first (before indexing into the entries)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the review :D. I added a quick return for the zero-length case at the top of the function.


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
Expand Down Expand Up @@ -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",
Expand Down
17 changes: 17 additions & 0 deletions python/tests/test_iterator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]]
Expand Down
Loading