From 7818b63c51651efcd544950c33b63fced6bf0a22 Mon Sep 17 00:00:00 2001 From: Ayush Kumar Saha <225407166+AP24110011695@users.noreply.github.com> Date: Sun, 29 Nov 2026 15:41:29 +0530 Subject: [PATCH 1/2] Fix truncate string upper bound producing an unencodable lone surrogate Signed-off-by: Ayush Kumar Saha <225407166+AP24110011695@users.noreply.github.com> --- pyiceberg/utils/truncate.py | 19 +++++++++++-------- tests/utils/test_truncate.py | 24 ++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 8 deletions(-) diff --git a/pyiceberg/utils/truncate.py b/pyiceberg/utils/truncate.py index feaa7d342d..3ba08e2523 100644 --- a/pyiceberg/utils/truncate.py +++ b/pyiceberg/utils/truncate.py @@ -22,14 +22,17 @@ 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 0xD7FF <= to_inc <= 0xDFFF: + # U+D800..U+DFFF are lone surrogates and cannot be encoded as UTF-8, so the + # next valid scalar value after U+D7FF is U+E000, like Java's UnicodeUtil + chars[i] = "\ue000" + else: + chars[i] = chr(to_inc + 1) + return "".join(chars) 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..1039c6784f 100644 --- a/tests/utils/test_truncate.py +++ b/tests/utils/test_truncate.py @@ -25,3 +25,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 serialization into the manifest + bound.encode("utf-8") + + +def test_upper_bound_string_truncation_d7ff_after_max_code_point() -> None: + # the last code point has no successor (U+10FFFF), so the previous position is + # incremented, and there the same lone-surrogate rule applies + value = chr(0x10FFFF) + chr(0xD7FF) + "x" + assert truncate_upper_bound_text_string(value, 2) == chr(0x10FFFF) + chr(0xE000) From 3530b49697f58f170fa9e20609b5a9be6813bdeb Mon Sep 17 00:00:00 2001 From: Ayush Kumar Saha <225407166+AP24110011695@users.noreply.github.com> Date: Mon, 30 Nov 2026 00:05:43 +0530 Subject: [PATCH 2/2] Address review: match Java's UnicodeUtil exactly on surrogate and trailing code points Signed-off-by: Ayush Kumar Saha <225407166+AP24110011695@users.noreply.github.com> --- pyiceberg/utils/truncate.py | 9 +++++---- tests/utils/test_truncate.py | 14 ++++++++------ 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/pyiceberg/utils/truncate.py b/pyiceberg/utils/truncate.py index 3ba08e2523..815bbf8a0f 100644 --- a/pyiceberg/utils/truncate.py +++ b/pyiceberg/utils/truncate.py @@ -26,13 +26,14 @@ def truncate_upper_bound_text_string(value: str, trunc_length: int | None) -> st if to_inc == 0x10FFFF: # no code point above this exists, try incrementing the previous one continue - if 0xD7FF <= to_inc <= 0xDFFF: - # U+D800..U+DFFF are lone surrogates and cannot be encoded as UTF-8, so the - # next valid scalar value after U+D7FF is U+E000, like Java's UnicodeUtil + 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) - return "".join(chars) + # 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 1039c6784f..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 @@ -40,12 +42,12 @@ def test_upper_bound_string_truncation_skips_lone_surrogate_range() -> None: value = "ab" + chr(0xD7FF) + "x" bound = truncate_upper_bound_text_string(value, 3) assert bound == "ab" + chr(0xE000) - # the bound must survive serialization into the manifest - bound.encode("utf-8") + # the bound must survive the manifest serialization path that crashed + to_bytes(StringType(), bound) -def test_upper_bound_string_truncation_d7ff_after_max_code_point() -> None: +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 there the same lone-surrogate rule applies - value = chr(0x10FFFF) + chr(0xD7FF) + "x" - assert truncate_upper_bound_text_string(value, 2) == chr(0x10FFFF) + chr(0xE000) + # 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)