You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@iceberg.apache.org by "fallintoplace (via GitHub)" <gi...@apache.org> on 2026/05/22 23:51:59 UTC

[PR] Fix precision loss in large integral string conversions [iceberg-python]

fallintoplace opened a new pull request, #3405:
URL: https://github.com/apache/iceberg-python/pull/3405

   ## Summary
   
   Fixes precision loss when converting large integral strings in two runtime paths:
   
   - `StringLiteral.to(IntegerType/LongType)`
   - `partition_to_py(...)` for integral and time-based partition values backed by integers
   
   ## Root cause
   
   Both paths were converting through `float` before converting to `int`, which loses precision for values outside the IEEE-754 exact integer range.
   
   That caused valid 64-bit integers like `LongType.max` and `9007199254740993` to be corrupted.
   
   ## What changed
   
   - Replaced `int(float(...))` with exact integer parsing in `partition_to_py`
   - Replaced float-based string-to-int/long literal conversion with decimal-based truncation that preserves the existing `"3.141" -> 3` behavior
   - Added regression tests for `LongType.max` and `9007199254740993`
   
   ## Validation
   
   - `uv run pytest tests/expressions/test_literals.py tests/test_conversions.py`
   
   Closes #3404.
   


-- 
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: issues-unsubscribe@iceberg.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


Re: [PR] Fix precision loss in large integral string conversions [iceberg-python]

Posted by "ndrluis (via GitHub)" <gi...@apache.org>.
ndrluis commented on code in PR #3405:
URL: https://github.com/apache/iceberg-python/pull/3405#discussion_r3294817872


##########
pyiceberg/expressions/literals.py:
##########
@@ -68,6 +68,10 @@
 UUID_BYTES_LENGTH = 16
 
 
+def _truncate_numeric_string_to_int(value: str) -> int:
+    return int(Decimal(value))

Review Comment:
   `int(Decimal(value))` fixes the precision issue, but it also means we materialize the full integer before doing the bounds check. For example, `literal("1e1000000").to(IntegerType())` eventually returns `IntAboveMax`, but only after constructing a very large Python `int` first. In my local check this took ~17s.
   
   Can we compare the parsed `Decimal` against `IntegerType.max/min` or `LongType.max/min` before converting to `int`?
   
   ```python
   number = Decimal(self.value)
   
   if number > IntegerType.max:
       return IntAboveMax()
   elif number < IntegerType.min:
       return IntBelowMin()
   
   return LongLiteral(int(number))
   ```
   
   That should preserve the precision fix while avoiding excessive CPU/memory use for obviously out-of-range values.
   



-- 
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: issues-unsubscribe@iceberg.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


Re: [PR] Fix precision loss in large integral string conversions [iceberg-python]

Posted by "fallintoplace (via GitHub)" <gi...@apache.org>.
fallintoplace commented on code in PR #3405:
URL: https://github.com/apache/iceberg-python/pull/3405#discussion_r3294823191


##########
pyiceberg/expressions/literals.py:
##########
@@ -68,6 +68,10 @@
 UUID_BYTES_LENGTH = 16
 
 
+def _truncate_numeric_string_to_int(value: str) -> int:
+    return int(Decimal(value))

Review Comment:
   Thanks, pushed a follow-up in 29f7d36b.



-- 
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: issues-unsubscribe@iceberg.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


Re: [PR] Fix precision loss in large integral string conversions [iceberg-python]

Posted by "Fokko (via GitHub)" <gi...@apache.org>.
Fokko merged PR #3405:
URL: https://github.com/apache/iceberg-python/pull/3405


-- 
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: issues-unsubscribe@iceberg.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


Re: [PR] Fix precision loss in large integral string conversions [iceberg-python]

Posted by "ndrluis (via GitHub)" <gi...@apache.org>.
ndrluis commented on PR #3405:
URL: https://github.com/apache/iceberg-python/pull/3405#issuecomment-4527116644

   @fallintoplace Thank you for your contribution.
   
   Can we also handle and add tests for large integral-looking strings that are not plain integer literals, e.g. "9007199254740993.0", "9007199254740993e0", and f"{LongType.max}.0"? These still go through the float path today and either lose precision or overflow incorrectly. A test for "1e3" would also catch the scientific-notation regression from the previous behavior.


-- 
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: issues-unsubscribe@iceberg.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


Re: [PR] Fix precision loss in large integral string conversions [iceberg-python]

Posted by "fallintoplace (via GitHub)" <gi...@apache.org>.
fallintoplace commented on PR #3405:
URL: https://github.com/apache/iceberg-python/pull/3405#issuecomment-4528357584

   Thanks, pushed a follow-up in d443f379 to handle the remaining integral-looking string cases without going through float, and added regressions for "9007199254740993.0", "9007199254740993e0", f"{LongType.max}.0", and "1e3".
   
   
   
   
   
   


-- 
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: issues-unsubscribe@iceberg.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org