bigluck commented on issue #669:
URL: https://github.com/apache/iceberg-python/issues/669#issuecomment-2084497090
@Fokko `decimal_to_bytes`, when invoked without `byte_length`, uses
`bytes_required` to get the required number of bytes.
```python
for v in ['32767', '32768', '32769', '32999']:
d = Decimal(v)
print(f'decimal_to_unscaled[{v}]', decimal_to_unscaled(d))
x = decimal_to_unscaled(d)
print(f'bytes_required[{v}]', bytes_required(d))
```
```
decimal_to_unscaled[32767] 32767
bytes_required[32767] 2
decimal_to_unscaled[32768] 32768
bytes_required[32768] 2
decimal_to_unscaled[32769] 32769
bytes_required[32769] 2
decimal_to_unscaled[32999] 32999
bytes_required[32999] 2
```
But it overflows because `decimal_to_bytes` creates a signed value:
```python
unscaled_value = decimal_to_unscaled(value)
if byte_length is None:
byte_length = bytes_required(unscaled_value)
return unscaled_value.to_bytes(byte_length, byteorder="big", signed=True)
```
2 bytes, as a signed int, can store values in the `-32,768` to `32,767`
range.
Indeed the same code does not crash when using `Decimal ('32967')`
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]