Fokko commented on code in PR #4516:
URL: https://github.com/apache/iceberg-python/pull/4516#discussion_r4732144859
##########
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:
This doesn't hit the walk back: the truncated prefix ends in U+D7FF, so the
first iteration bumps it to U+E000 and returns, and the U+10FFFF `continue`
never runs. It's effectively the same as the previous test. Swapping the order
exercises it:
```python
value = chr(0xD7FF) + chr(0x10FFFF) + "x"
assert truncate_upper_bound_text_string(value, 2) == chr(0xE000) +
chr(0x10FFFF)
```
(or `chr(0xE000)` if the trailing code points get dropped, see the comment
on `truncate.py`).
##########
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:
Java only maps exactly U+D7FF to U+E000, and rejects surrogate input with a
`Preconditions.checkArgument`. Mapping the whole U+D800..U+DFFF range silently
hides invalid input (the lower bound would still fail to encode). Since Arrow
strings are valid UTF-8 this can't come from the write path anyway, so `to_inc
== 0xD7FF` would match Java. Otherwise the "like Java's UnicodeUtil" comment
should be adjusted.
##########
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:
Nit: the equality assert above already guarantees this is encodable.
`to_bytes(StringType(), bound)` would at least cover the actual path that
crashed.
##########
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:
Not introduced here, but since this mirrors Java: after walking back past a
U+10FFFF, the trailing code points are kept, so `"a\U0010FFFF"` gives
`"b\U0010FFFF"` while Java's `setLength(offset)` gives `"b"`. Still a valid
bound, just looser. Could be `return "".join(chars[: len(chars) + i + 1])`,
fine as a follow up too.
--
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]