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 06a46468 fix(python): Preserve negative decimal values during 
iteration (#1008)
06a46468 is described below

commit 06a4646813dc3a9687219299e6f09cf18942c4a9
Author: peterolkhov <[email protected]>
AuthorDate: Tue Nov 10 23:41:36 2026 -0500

    fix(python): Preserve negative decimal values during iteration (#1008)
    
    Negative decimal128 and decimal256 values become enormous positive
    values during Python conversion. The stored integer is signed, but
    `_decimal_iter()` currently decodes it as unsigned. This adds
    `signed=True` at that conversion boundary.
    
    The regression tests cover both widths, negative and positive values,
    nulls, zero, varied scales and full precision under modified decimal
    contexts.
    
    Tested with a rebuilt source wheel on macOS arm64/Python 3.14: 276 tests
    passed, 4 skipped; 118 doctests passed; stubtest passed for eight
    modules; applicable pre-commit hooks passed. The decimal regressions
    failed before the change. Other platforms and Python versions remain for
    CI.
    
    AI assistance: Codex generated the fix and tests and ran the local
    checks.
---
 python/src/nanoarrow/iterator.py |  2 +-
 python/tests/test_iterator.py    | 31 ++++++++++++++++++++++++++++++-
 2 files changed, 31 insertions(+), 2 deletions(-)

diff --git a/python/src/nanoarrow/iterator.py b/python/src/nanoarrow/iterator.py
index 85412496..a387c596 100644
--- a/python/src/nanoarrow/iterator.py
+++ b/python/src/nanoarrow/iterator.py
@@ -439,7 +439,7 @@ class PyIterator(ArrayViewBaseIterator):
             if item is None:
                 yield None
             else:
-                int_value = int.from_bytes(item, byteorder)
+                int_value = int.from_bytes(item, byteorder, signed=True)
                 yield Decimal(int_value).scaleb(scaleb, context)
 
     def _date_iter(self, offset, length):
diff --git a/python/tests/test_iterator.py b/python/tests/test_iterator.py
index cb16a000..20fe3bd4 100644
--- a/python/tests/test_iterator.py
+++ b/python/tests/test_iterator.py
@@ -357,7 +357,13 @@ def test_iterator_nullable_dictionary():
 def test_iterator_decimal():
     pa = pytest.importorskip("pyarrow")
 
-    items = [decimal.Decimal("12.3450"), None, decimal.Decimal("1234567.3456")]
+    items = [
+        decimal.Decimal("12.3450"),
+        None,
+        decimal.Decimal("1234567.3456"),
+        decimal.Decimal("-12.3450"),
+        decimal.Decimal("0.0000"),
+    ]
     array = pa.array(items, pa.decimal128(11, 4))
     assert list(iter_py(array)) == items
 
@@ -369,6 +375,29 @@ def test_iterator_decimal():
         assert list(iter_py(array)) == items
 
 
[email protected]("bitwidth,precision", [(128, 38), (256, 76)])
[email protected]("scale", [-2, 0, 2, None])
+def test_iterator_decimal_precision(bitwidth, precision, scale):
+    pa = pytest.importorskip("pyarrow")
+
+    if scale is None:
+        scale = precision
+    digits = (9,) * (precision + min(scale, 0))
+    items = [
+        decimal.Decimal((1, digits, -scale)),
+        decimal.Decimal((0, digits, -scale)),
+        decimal.Decimal((0, (0,), -scale)),
+        None,
+    ]
+    array = pa.array(items, getattr(pa, f"decimal{bitwidth}")(precision, 
scale))
+
+    for global_precision in [1, 28, 100]:
+        context = decimal.Context(prec=global_precision, 
rounding=decimal.ROUND_DOWN)
+        context.traps[decimal.Inexact] = True
+        with decimal.localcontext(context):
+            assert list(iter_py(array)) == items
+
+
 def test_iterator_date():
     pa = pytest.importorskip("pyarrow")
 

Reply via email to