From cd7c2ec72747009799074fb3ba996257e84b9412 Mon Sep 17 00:00:00 2001 From: goutamadwant Date: Mon, 28 Sep 2026 21:51:02 -0700 Subject: [PATCH] Fix overflow in LexicographicKeyRangeTracker --- sdks/python/apache_beam/io/range_trackers.py | 2 +- sdks/python/apache_beam/io/range_trackers_test.py | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/sdks/python/apache_beam/io/range_trackers.py b/sdks/python/apache_beam/io/range_trackers.py index 626564b00948..7f6b53502886 100644 --- a/sdks/python/apache_beam/io/range_trackers.py +++ b/sdks/python/apache_beam/io/range_trackers.py @@ -429,7 +429,7 @@ def position_to_fraction( istart = cls._bytestring_to_int(start, prec) ikey = cls._bytestring_to_int(key, prec) iend = cls._bytestring_to_int(end, prec) if end else 1 << (prec * 8) - return float(ikey - istart) / (iend - istart) + return (ikey - istart) / (iend - istart) @staticmethod def _bytestring_to_int(s: Union[bytes, str], prec: int) -> int: diff --git a/sdks/python/apache_beam/io/range_trackers_test.py b/sdks/python/apache_beam/io/range_trackers_test.py index 0bf37997f2ce..8b829616bf52 100644 --- a/sdks/python/apache_beam/io/range_trackers_test.py +++ b/sdks/python/apache_beam/io/range_trackers_test.py @@ -432,6 +432,13 @@ def test_key_to_fraction_common_prefix(self): end='a' * 100 + 'e', fraction=1.0) + def test_key_to_fraction_long_keys(self): + for start, key, end in ( + (b'a' * 400, b'm' * 400, b'z' * 400), + ('a' * 400, 'm' * 400, 'z' * 400)): + with self.subTest(key_type=type(key).__name__): + self.assertAlmostEqual(0.48, self.key_to_fraction(key, start, end)) + def test_tiny(self): # test bytes keys self._check(fraction=.5**20, key=b'\0\0\x10')