diff --git a/pyiceberg/utils/truncate.py b/pyiceberg/utils/truncate.py index feaa7d342d..815bbf8a0f 100644 --- a/pyiceberg/utils/truncate.py +++ b/pyiceberg/utils/truncate.py @@ -22,14 +22,18 @@ def truncate_upper_bound_text_string(value: str, trunc_length: int | None) -> st chars = [*result] for i in range(-1, -len(result) - 1, -1): - try: - to_inc = ord(chars[i]) - # will raise exception if the highest unicode code is reached - _next = chr(to_inc + 1) - chars[i] = _next - return "".join(chars) - except ValueError: - pass + to_inc = ord(chars[i]) + if to_inc == 0x10FFFF: + # no code point above this exists, try incrementing the previous one + continue + if to_inc == 0xD7FF: + # the successor is the lone surrogate U+D800, which is not a Unicode scalar + # value; the next scalar is U+E000, like Java's UnicodeUtil + chars[i] = "\ue000" + else: + chars[i] = chr(to_inc + 1) + # drop the code points after the incremented position, like Java's setLength + return "".join(chars[: len(chars) + i + 1]) return None # didn't find a valid upper bound return result diff --git a/tests/utils/test_truncate.py b/tests/utils/test_truncate.py index b9c3c10335..022bf1c85a 100644 --- a/tests/utils/test_truncate.py +++ b/tests/utils/test_truncate.py @@ -14,6 +14,8 @@ # KIND, either express or implied. See the License for the # specific language governing permissions and limitations # under the License. +from pyiceberg.conversions import to_bytes +from pyiceberg.types import StringType from pyiceberg.utils.truncate import truncate_upper_bound_binary_string, truncate_upper_bound_text_string @@ -25,3 +27,27 @@ def test_upper_bound_string_truncation() -> None: def test_upper_bound_binary_truncation() -> None: assert truncate_upper_bound_binary_string(b"\x01\x02\x03", 2) == b"\x01\x03" assert truncate_upper_bound_binary_string(b"\xff\xff\x00", 2) is None + + +def test_upper_bound_string_truncation_increments_whole_code_point() -> None: + # U+1F7FF is a single code point; the increment must advance it as a whole to U+1F800, + # mirroring Java's truncateStringUpperBoundIncrementsSurrogatePair + value = "ab" + chr(0x1F7FF) + "x" + assert truncate_upper_bound_text_string(value, 3) == "ab" + chr(0x1F800) + + +def test_upper_bound_string_truncation_skips_lone_surrogate_range() -> None: + # the successor of U+D7FF is the lone surrogate U+D800, which is not a Unicode scalar + # value and cannot be encoded as UTF-8; the next scalar, U+E000, is the tightest bound + value = "ab" + chr(0xD7FF) + "x" + bound = truncate_upper_bound_text_string(value, 3) + assert bound == "ab" + chr(0xE000) + # the bound must survive the manifest serialization path that crashed + to_bytes(StringType(), bound) + + +def test_upper_bound_string_truncation_walks_back_past_max_code_point() -> None: + # the last code point has no successor (U+10FFFF), so the previous position is + # incremented and the code points after it are dropped, like Java's setLength + value = chr(0xD7FF) + chr(0x10FFFF) + "x" + assert truncate_upper_bound_text_string(value, 2) == chr(0xE000)