Skip to content

truncate_upper_bound_text_string can return a lone surrogate that fails UTF-8 encoding #4514

Backlog

Description

@Areesha-Rizwan412

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:

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 (7864d4c) with the exact function logic:

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions