Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions pyiceberg/utils/truncate.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
26 changes: 26 additions & 0 deletions tests/utils/test_truncate.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -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)