Areesha-Rizwan412 opened a new issue, #4514:
URL: https://github.com/apache/iceberg-python/issues/4514

   ### Apache Iceberg version
   
   main (development)
   
   ### Please describe the bug
   
   `truncate_upper_bound_text_string()` in `pyiceberg/utils/truncate.py` builds 
a truncated upper bound by incrementing the last character of the truncated 
value:
   
   ```python
   to_inc = ord(chars[i])
   # will raise exception if the highest unicode code is reached
   _next = chr(to_inc + 1)
   ```
   
   The comment covers only the `chr()` overflow case. It misses the surrogate 
range: when the character being incremented is U+D7FF, the successor is U+D800, 
a lone surrogate. `chr()` accepts it without raising, so the function returns a 
string that cannot be encoded as UTF-8, and the failure surfaces later at 
serialization.
   
   Reproduced on current main (7864d4c34e90711da363041bc5de4bdeff777823) with 
the exact function logic:
   
   ```python
   value = "a\ud7ffb"
   out = truncate_upper_bound_text_string(value, 2)
   # out == "a\ud800"
   out.encode("utf-8")
   # UnicodeEncodeError: 'utf-8' codec can't encode character '\ud800' in 
position 1: surrogates not allowed
   ```
   
   This is reachable from ordinary writes. `StatsAggregator.max_as_bytes()` in 
`pyiceberg/io/pyarrow.py` calls this helper and then serializes the result, and 
it runs under the default metrics mode (`DEFAULT_TRUNCATION_LENGTH = 16`). Any 
string value longer than the truncation length whose character at the 
truncation boundary is U+D7FF aborts the write.
   
   PR #3880 proposed skipping the surrogate range when incrementing (successor 
of U+D7FF is U+E000) but was closed as stale without merging, and the bug is 
still present on main. Filing this so the fix can be revived. Happy to 
contribute a fix with guidance from the community.
   
   ### Willingness to contribute
   
   - [ ] I can contribute a fix for this bug independently
   - [x] I would be willing to contribute a fix for this bug with guidance from 
the Iceberg community
   - [ ] I cannot contribute a fix for this bug at this time
   


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