rdblue commented on code in PR #8067:
URL: https://github.com/apache/iceberg/pull/8067#discussion_r1285264740


##########
python/pyiceberg/utils/decimal.py:
##########
@@ -88,3 +107,25 @@ def truncate_decimal(value: Decimal, width: int) -> Decimal:
     unscaled_value = decimal_to_unscaled(value)
     applied_value = unscaled_value - (((unscaled_value % width) + width) % 
width)
     return unscaled_to_decimal(applied_value, 
abs(int(value.as_tuple().exponent)))
+
+
+@lru_cache
+def decimal_required_bytes(precision: int) -> int:

Review Comment:
   I think I prefer the way Java does this because it would be a couple of list 
comprehensions rather than computing the max precision for each byte length for 
each call.
   
   ```python
   MAX_PRECISION = tuple( math.floor(math.log10(math.fabs(math.pow(2, 8 * l - 
1) - 1))) for l in range(24) )
   REQUIRED_LENGTH = tuple( next(l for l in range(24) if p <= MAX_PRECISION[l]) 
for p in range(40) )
   
   def required_bytes(precision: int) -> int:
       if precision <= 0 or precision >= len(REQUIRED_LENGTH):
           raise ValueError(...)
       return REQUIRED_LENGTH[precision]
   ```



-- 
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]

Reply via email to