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')