AP24110011695 commented on code in PR #4516:
URL: https://github.com/apache/iceberg-python/pull/4516#discussion_r4732634739
##########
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"
Review Comment:
Right — in that order the first iteration already bumps U+D7FF and returns,
so the `continue` never ran. Swapped to `chr(0xD7FF) + chr(0x10FFFF) + "x"`;
with the trailing code points now dropped (3530b49) the expected bound is just
`chr(0xE000)`.
##########
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:
Review Comment:
Agreed — narrowed to `to_inc == 0xD7FF` so the behavior matches Java's
`incrementCodePoint` exactly. Arrow strings are valid UTF-8, so surrogate input
can't reach this from the write path and there is no reason to map it silently.
##########
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)
Review Comment:
Applied — the return is now `"".join(chars[: len(chars) + i + 1])`, so
walking back past a U+10FFFF yields `"b"` for `"a\u{10FFFF}"` like Java's
`setLength`, and the bound is byte-identical to Java's. Tighter, and it also
simplified the walk-back test's expectation.
##########
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")
Review Comment:
Done — the test now runs `to_bytes(StringType(), bound)`, the exact
serialization path that crashed, instead of a bare encode.
--
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]