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

   Fixes #4514
   
   ## Summary
   
   `truncate_upper_bound_text_string()` builds the truncated upper bound by 
incrementing the last code point of the truncated value. When the last code 
point is U+D7FF, the successor is U+D800 — a lone surrogate. `chr(0xD7FF + 1)` 
does not raise, so the function returned a `str` that cannot be encoded as 
UTF-8, and the write path crashed later at manifest serialization (`to_bytes` → 
`UnicodeEncodeError: surrogates not allowed`) via the truncate-metrics stats 
collector (`pyiceberg/io/pyarrow.py`, 
`truncate_upper_bound_text_string(self.current_max, self.trunc_length)`).
   
   Repro on main (7864d4c):
   
   ```python
   from pyiceberg.utils.truncate import truncate_upper_bound_text_string
   from pyiceberg.conversions import to_bytes
   from pyiceberg.types import StringType
   
   bound = truncate_upper_bound_text_string("ab" + chr(0xD7FF) + "x", 3)
   # main: 'ab\ud800' — to_bytes(StringType(), bound) raises UnicodeEncodeError
   ```
   
   The input itself (`"ab\uD7FF"`) is perfectly valid UTF-8, so this is 
reachable from ordinary data through 
`write.metadata.metrics.default=truncate(N)`.
   
   ## Fix
   
   Mirrors Java's `UnicodeUtil.incrementCodePoint` 
([api/src/main/java/org/apache/iceberg/util/UnicodeUtil.java](https://github.com/apache/iceberg/blob/main/api/src/main/java/org/apache/iceberg/util/UnicodeUtil.java)),
 which pins the same rule: surrogate code points are not Unicode scalar values, 
so the next scalar value after U+D7FF is U+E000, and U+10FFFF has no successor 
(walk back to the previous code point). The tightest valid upper bound for a 
prefix ending in U+D7FF is therefore the prefix with U+E000 in that position — 
comparison stops at the first differing position, so every string with that 
prefix is below it, and no valid string lies in between.
   
   The explicit checks replace the `try/except ValueError` (which only caught 
the U+10FFFF overflow) with the same `None` when no position can be incremented.
   
   ## Tests
   
   Failing-first verified: the two lone-surrogate tests fail on the pre-fix 
head (`'ab\ud800' != 'ab\ue000'`) and pass after. Also added a 
code-point-increment parity test mirroring Java's 
`truncateStringUpperBoundIncrementsSurrogatePair` (U+1F7FF → U+1F800, passes on 
both heads — it pins the whole-code-point increment). 
`tests/utils/test_truncate.py` 5/5, `tests/io/test_pyarrow_stats.py` 20/20, 
ruff + mypy clean on the changed files.
   
   ## AI disclosure
   
   This PR was developed with AI assistance (Claude); the bug report in #4514 
was filed by a human contributor, the fix design was verified against Java's 
`UnicodeUtil` semantics, and every test was run locally.
   


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