From 2417a9d5c5e9226db75130bca89807b35a17b70b Mon Sep 17 00:00:00 2001 From: peterolkhov Date: Mon, 16 Nov 2026 14:01:55 -0500 Subject: [PATCH] fix(python): Handle empty list chunks during iteration --- python/src/nanoarrow/iterator.py | 3 +++ python/tests/test_iterator.py | 35 ++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/python/src/nanoarrow/iterator.py b/python/src/nanoarrow/iterator.py index a387c5966..ad616a225 100644 --- a/python/src/nanoarrow/iterator.py +++ b/python/src/nanoarrow/iterator.py @@ -263,6 +263,9 @@ def _struct_iter(self, offset, length): yield None if item is None else {key: val for key, val in zip(names, item)} def _list_iter(self, offset, length): + 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 20fe3bd41..41565d9cc 100644 --- a/python/tests/test_iterator.py +++ b/python/tests/test_iterator.py @@ -252,6 +252,41 @@ def test_iterator_list(): ] +@pytest.mark.parametrize("list_type", ["list_", "large_list"]) +def test_iterator_empty_list(list_type): + pa = pytest.importorskip("pyarrow") + arrow_type = getattr(pa, list_type)(pa.int64()) + + array = pa.array([], arrow_type) + assert list(iter_py(array)) == [] + + array = pa.array([[7], [], None], arrow_type) + assert list(iter_py(array.slice(1, 0))) == [] + assert list(iter_py(array.slice(3, 0))) == [] + + +@pytest.mark.parametrize("list_type", ["list_", "large_list"]) +def test_iterator_list_stream_empty_batches(list_type): + pa = pytest.importorskip("pyarrow") + arrow_type = getattr(pa, list_type)(pa.int64()) + empty = pa.record_batch([pa.array([], arrow_type)], names=["values"]) + populated = pa.record_batch( + [pa.array([[7], [], None], arrow_type)], names=["values"] + ) + reader = pa.RecordBatchReader.from_batches( + empty.schema, [empty, populated, empty, populated, empty] + ) + + assert list(iter_py(reader)) == [ + {"values": [7]}, + {"values": []}, + {"values": None}, + {"values": [7]}, + {"values": []}, + {"values": None}, + ] + + def test_iterator_nullable_list(): pa = pytest.importorskip("pyarrow") items = [[1, 2, 3], [4, 5, 6], [7, 8, None], [0], None]