You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by "paleolimbot (via GitHub)" <gi...@apache.org> on 2024/04/11 20:33:34 UTC

[PR] feat(python): Support Decimal types in convert to Python [arrow-nanoarrow]

paleolimbot opened a new pull request, #425:
URL: https://github.com/apache/arrow-nanoarrow/pull/425

   I experimented with a few different methods here...I think this one has a good balance of speed and not messing with the global precision.
   
   ```python
   import pyarrow as pa
   import decimal
   from nanoarrow.iterator import iter_py
   
   items = [decimal.Decimal("12.3450"), None, decimal.Decimal("1234567.3456")]
   array = pa.array(items, pa.decimal128(11, 4))
   list(iter_py(array))
   #> [Decimal('12.3450'), None, Decimal('1234567.3456')]
   ```
   
   This seems to be vaguely on par with pyarrow convert:
   
   ```python
   import pyarrow as pa
   import decimal
   import numpy as np
   from nanoarrow.iterator import iter_py
   
   floats = np.random.random(int(1e6))
   items = [decimal.Decimal(item) for item in floats]
   array = pa.array(items)
   
   %timeit array.to_pylist()
   #> 799 ms ± 6.24 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
   
   %timeit list(iter_py(array))
   #> 431 ms ± 8.65 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
   ```


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat(python): Support Decimal types in convert to Python [arrow-nanoarrow]

Posted by "paleolimbot (via GitHub)" <gi...@apache.org>.
paleolimbot commented on code in PR #425:
URL: https://github.com/apache/arrow-nanoarrow/pull/425#discussion_r1565715320


##########
python/src/nanoarrow/iterator.py:
##########
@@ -263,6 +263,25 @@ def _binary_iter(self, offset, length):
             for start, end in zip(starts, ends):
                 yield bytes(data[start:end])
 
+    def _decimal_iter(self, offset, length):
+        from decimal import Context, Decimal
+        from sys import byteorder
+
+        storage = self._primitive_iter(offset, length)
+        precision = self._schema_view.decimal_precision
+        scaleb = Decimal(-self._schema_view.decimal_scale)

Review Comment:
   Apparently it does! https://github.com/python/cpython/blob/main/Lib/_pydecimal.py#L3684
   
   I'll add some comments here.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat(python): Support Decimal types in convert to Python [arrow-nanoarrow]

Posted by "jorisvandenbossche (via GitHub)" <gi...@apache.org>.
jorisvandenbossche commented on PR #425:
URL: https://github.com/apache/arrow-nanoarrow/pull/425#issuecomment-2056987482

   Not clear if this started with this PR, but on main the merged commit from this PR is failing in the wheel building: for Python 3.12, running the tests is segfaulting (https://github.com/apache/arrow-nanoarrow/actions/runs/8689330033/job/23826851854)


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat(python): Support Decimal types in convert to Python [arrow-nanoarrow]

Posted by "paleolimbot (via GitHub)" <gi...@apache.org>.
paleolimbot commented on PR #425:
URL: https://github.com/apache/arrow-nanoarrow/pull/425#issuecomment-2057148031

   I think I have seen that one time before but I was confused because the backtrace seems to suggest that it happens while collecting the tests? (Or is that backtrace generally known to be unreliable?)


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat(python): Support Decimal types in convert to Python [arrow-nanoarrow]

Posted by "jorisvandenbossche (via GitHub)" <gi...@apache.org>.
jorisvandenbossche commented on code in PR #425:
URL: https://github.com/apache/arrow-nanoarrow/pull/425#discussion_r1563082909


##########
python/src/nanoarrow/iterator.py:
##########
@@ -263,6 +263,25 @@ def _binary_iter(self, offset, length):
             for start, end in zip(starts, ends):
                 yield bytes(data[start:end])
 
+    def _decimal_iter(self, offset, length):
+        from decimal import Context, Decimal
+        from sys import byteorder
+
+        storage = self._primitive_iter(offset, length)
+        precision = self._schema_view.decimal_precision
+        scaleb = Decimal(-self._schema_view.decimal_scale)

Review Comment:
   Does this need to be converted to a Decimal? 
   The docs seem to suggest this can be just an integer (https://docs.python.org/3/library/decimal.html#decimal.Decimal.scaleb)
   
   Also, I might add some comments about what is going on for people not familiar with this method (I had to look it up to understand what you wrote, but certainly makes sense now)
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat(python): Support Decimal types in convert to Python [arrow-nanoarrow]

Posted by "paleolimbot (via GitHub)" <gi...@apache.org>.
paleolimbot merged PR #425:
URL: https://github.com/apache/arrow-nanoarrow/pull/425


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat(python): Support Decimal types in convert to Python [arrow-nanoarrow]

Posted by "paleolimbot (via GitHub)" <gi...@apache.org>.
paleolimbot commented on code in PR #425:
URL: https://github.com/apache/arrow-nanoarrow/pull/425#discussion_r1565715320


##########
python/src/nanoarrow/iterator.py:
##########
@@ -263,6 +263,25 @@ def _binary_iter(self, offset, length):
             for start, end in zip(starts, ends):
                 yield bytes(data[start:end])
 
+    def _decimal_iter(self, offset, length):
+        from decimal import Context, Decimal
+        from sys import byteorder
+
+        storage = self._primitive_iter(offset, length)
+        precision = self._schema_view.decimal_precision
+        scaleb = Decimal(-self._schema_view.decimal_scale)

Review Comment:
   Apparently it gets converted to one anyway: https://github.com/python/cpython/blob/main/Lib/_pydecimal.py#L3684 (although you can pass an int, float, or string as well).
   
   I'll add some comments here.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org