This is an automated email from the ASF dual-hosted git repository.
paleolimbot pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow-nanoarrow.git
The following commit(s) were added to refs/heads/main by this push:
new 057e253a fix(python): Handle empty list chunks during iteration (#1021)
057e253a is described below
commit 057e253a5b56233f8f681b51de292ae7a5e97bea
Author: peterolkhov <[email protected]>
AuthorDate: Mon Nov 16 15:10:20 2026 -0500
fix(python): Handle empty list chunks during iteration (#1021)
AI assistance: Codex generated the guard and regression tests and ran
the local checks below.
`iter_py()` raises `IndexError` on an empty LIST or LARGE_LIST chunk
because `_list_iter()` indexes empty start/end slices. A zero-row record
batch with a list column also aborts direct stream iteration before
later rows are returned.
Return immediately for a zero-length list slice, matching `_map_iter()`.
The regression tests cover both list offset widths, empty arrays and
slices, and a real `RecordBatchReader` with empty batches before,
between and after populated batches.
Validation on macOS arm64 / Python 3.13.14 using a rebuilt and
reinstalled Meson/Cython wheel:
- New regression cases fail on the unchanged implementation and pass
with the guard.
- Python suite: 288 passed, 4 skipped.
- Package doctests: 118 passed; compiled-module stubtests: all 8 passed.
- Black 22.3.0, Flake8 7.1.1 and isort 5.12.0 pass for both changed
files.
---
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 a387c596..ad616a22 100644
--- a/python/src/nanoarrow/iterator.py
+++ b/python/src/nanoarrow/iterator.py
@@ -263,6 +263,9 @@ class PyIterator(ArrayViewBaseIterator):
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 20fe3bd4..41565d9c 100644
--- a/python/tests/test_iterator.py
+++ b/python/tests/test_iterator.py
@@ -252,6 +252,41 @@ def test_iterator_list():
]
[email protected]("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))) == []
+
+
[email protected]("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]