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 a5bc7b44 fix(python): Iterate string and binary view children from
their offset (#1039)
a5bc7b44 is described below
commit a5bc7b44a95a68bcf74029f6125427ef22cc55a4
Author: Aaditya Jaiswar <[email protected]>
AuthorDate: Fri Nov 27 21:06:54 2026 +0530
fix(python): Iterate string and binary view children from their offset
(#1039)
AI assistance: Claude Code helped find this bug and write the fix and
test; I reviewed and ran everything myself.
`CArrayView._iter_bytes()` and `_iter_str()`, which `iter_py()` uses for
`BINARY_VIEW` and `STRING_VIEW` arrays, loop over `range(offset,
length)` instead of `range(offset, offset + length)`. Top-level
iteration passes `offset=0`, so it works, but when a parent iterates a
child slice that does not start at 0, view values are silently dropped:
```python
import nanoarrow as na
from nanoarrow.iterator import iter_py
child = na.c_array(["a", "b", "c", "d"], na.string_view())
# A list whose values start after the first child element
offsets = na.c_buffer([1, 3, 4], na.int32())
lst = na.c_array_from_buffers(na.list_(na.string_view()), 2, [None,
offsets], children=[child])
list(iter_py(lst))
# [['b', 'c'], []] (expected [['b', 'c'], ['d']])
# A struct with an offset
st = na.c_array_from_buffers(na.struct({"col": na.string_view()}), 2,
[None], children=[child], offset=2)
list(iter_py(st))
# [] (expected [{'col': 'c'}, {'col': 'd'}])
```
The same arrays with `na.string()` / `na.binary()` children iterate
correctly.
This changes both loops to `range(offset, offset + length)` and adds
`test_iterator_string_child_with_offset`, which checks a list child
starting after the first element and a struct child with an offset, for
string, string view, binary and binary view. The view cases fail without
the change and pass with it; `tests/test_iterator.py`,
`tests/test_c_array.py` and `tests/test_array.py` pass (built from
source on Linux).
---
python/src/nanoarrow/_array.pyx | 4 ++--
python/tests/test_iterator.py | 26 ++++++++++++++++++++++++++
2 files changed, 28 insertions(+), 2 deletions(-)
diff --git a/python/src/nanoarrow/_array.pyx b/python/src/nanoarrow/_array.pyx
index 239982d5..0cf41731 100644
--- a/python/src/nanoarrow/_array.pyx
+++ b/python/src/nanoarrow/_array.pyx
@@ -279,7 +279,7 @@ cdef class CArrayView:
def _iter_bytes(self, int64_t offset, int64_t length) -> bytes | None:
cdef ArrowBufferView item_view
- for i in range(offset, length):
+ for i in range(offset, offset + length):
if ArrowArrayViewIsNull(self._ptr, i):
yield None
else:
@@ -288,7 +288,7 @@ cdef class CArrayView:
def _iter_str(self, int64_t offset, int64_t length) -> str | None:
cdef ArrowStringView item_view
- for i in range(offset, length):
+ for i in range(offset, offset + length):
if ArrowArrayViewIsNull(self._ptr, i):
yield None
else:
diff --git a/python/tests/test_iterator.py b/python/tests/test_iterator.py
index 3ce2ab26..6e1e766e 100644
--- a/python/tests/test_iterator.py
+++ b/python/tests/test_iterator.py
@@ -108,6 +108,32 @@ def test_iterator_view_with_variadic_buffers():
assert list(iter_py(array[1:])) == [None, long_value.encode()]
[email protected](
+ "arrow_type, values",
+ [
+ (na.string(), ["a", "b", "c", "d"]),
+ (na.string_view(), ["a", "b", "c", "d"]),
+ (na.binary(), [b"a", b"b", b"c", b"d"]),
+ (na.binary_view(), [b"a", b"b", b"c", b"d"]),
+ ],
+)
+def test_iterator_string_child_with_offset(arrow_type, values):
+ child = na.c_array(values, arrow_type)
+
+ # A list whose values start after the first child element
+ offsets = na.c_buffer([1, 3, 4], na.int32())
+ array = na.c_array_from_buffers(
+ na.list_(arrow_type), 2, [None, offsets], children=[child]
+ )
+ assert list(iter_py(array)) == [values[1:3], values[3:]]
+
+ # A struct with an offset iterates its children from that offset
+ array = na.c_array_from_buffers(
+ na.struct({"col": arrow_type}), 2, [None], children=[child], offset=2
+ )
+ assert list(iter_py(array)) == [{"col": values[2]}, {"col": values[3]}]
+
+
@pytest.mark.parametrize(
"arrow_type", [na.binary(), na.large_binary(), na.binary_view()]
)