Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

potential rounding error with use of divmod #18

Closed
tswast opened this issue Sep 30, 2021 · 1 comment · Fixed by #20
Closed

potential rounding error with use of divmod #18

tswast opened this issue Sep 30, 2021 · 1 comment · Fixed by #20
Assignees
Labels
api: bigquery Issues related to the googleapis/python-db-dtypes-pandas API. priority: p1 Important issue which blocks shipping the next release. Will be fixed prior to next release. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns.

Comments

@tswast
Copy link
Collaborator

tswast commented Sep 30, 2021

In googleapis/python-bigquery#840, I switched to using divmod as is done here and got the following test failures:

__________________________________ test_w_string_values[32-11 45 67:16:23.987654-expected30] __________________________________

mut = <module 'google.cloud.bigquery._helpers' from '/Users/swast/src/github.com/googleapis/python-bigquery/google/cloud/bigquery/_helpers.py'>
value = '32-11 45 67:16:23.987654'
expected = relativedelta(years=+32, months=+11, days=+47, hours=+19, minutes=+16, seconds=+23, microseconds=+987654)

    @pytest.mark.parametrize(
        ("value", "expected"),
        (
            ("0-0 0 0:0:0", relativedelta()),
            # SELECT INTERVAL X YEAR
            ("-10000-0 0 0:0:0", relativedelta(years=-10000)),
            ("-1-0 0 0:0:0", relativedelta(years=-1)),
            ("1-0 0 0:0:0", relativedelta(years=1)),
            ("10000-0 0 0:0:0", relativedelta(years=10000)),
            # SELECT INTERVAL X MONTH
            ("-0-11 0 0:0:0", relativedelta(months=-11)),
            ("-0-1 0 0:0:0", relativedelta(months=-1)),
            ("0-1 0 0:0:0", relativedelta(months=1)),
            ("0-11 0 0:0:0", relativedelta(months=11)),
            # SELECT INTERVAL X DAY
            ("0-0 -3660000 0:0:0", relativedelta(days=-3660000)),
            ("0-0 -1 0:0:0", relativedelta(days=-1)),
            ("0-0 1 0:0:0", relativedelta(days=1)),
            ("0-0 3660000 0:0:0", relativedelta(days=3660000)),
            # SELECT INTERVAL X HOUR
            ("0-0 0 -87840000:0:0", relativedelta(hours=-87840000)),
            ("0-0 0 -1:0:0", relativedelta(hours=-1)),
            ("0-0 0 1:0:0", relativedelta(hours=1)),
            ("0-0 0 87840000:0:0", relativedelta(hours=87840000)),
            # SELECT INTERVAL X MINUTE
            ("0-0 0 -0:59:0", relativedelta(minutes=-59)),
            ("0-0 0 -0:1:0", relativedelta(minutes=-1)),
            ("0-0 0 0:1:0", relativedelta(minutes=1)),
            ("0-0 0 0:59:0", relativedelta(minutes=59)),
            # SELECT INTERVAL X SECOND
            ("0-0 0 -0:0:59", relativedelta(seconds=-59)),
            ("0-0 0 -0:0:1", relativedelta(seconds=-1)),
            ("0-0 0 0:0:1", relativedelta(seconds=1)),
            ("0-0 0 0:0:59", relativedelta(seconds=59)),
            # SELECT (INTERVAL -1 SECOND) / 1000000
            ("0-0 0 -0:0:0.000001", relativedelta(microseconds=-1)),
            ("0-0 0 -0:0:59.999999", relativedelta(seconds=-59, microseconds=-999999)),
            ("0-0 0 -0:0:59.999", relativedelta(seconds=-59, microseconds=-999000)),
            ("0-0 0 0:0:59.999", relativedelta(seconds=59, microseconds=999000)),
            ("0-0 0 0:0:59.999999", relativedelta(seconds=59, microseconds=999999)),
            # Test with multiple digits in each section.
            (
                "32-11 45 67:16:23.987654",
                relativedelta(
                    years=32,
                    months=11,
                    days=45,
                    hours=67,
                    minutes=16,
                    seconds=23,
                    microseconds=987654,
                ),
            ),
            (
                "-32-11 -45 -67:16:23.987654",
                relativedelta(
                    years=-32,
                    months=-11,
                    days=-45,
                    hours=-67,
                    minutes=-16,
                    seconds=-23,
                    microseconds=-987654,
                ),
            ),
            # Test with mixed +/- sections.
            (
                "9999-9 -999999 9999999:59:59.999999",
                relativedelta(
                    years=9999,
                    months=9,
                    days=-999999,
                    hours=9999999,
                    minutes=59,
                    seconds=59,
                    microseconds=999999,
                ),
            ),
            # Test with fraction that is not microseconds.
            ("0-0 0 0:0:42.", relativedelta(seconds=42)),
            ("0-0 0 0:0:59.1", relativedelta(seconds=59, microseconds=100000)),
            ("0-0 0 0:0:0.12", relativedelta(microseconds=120000)),
            ("0-0 0 0:0:0.123", relativedelta(microseconds=123000)),
            ("0-0 0 0:0:0.1234", relativedelta(microseconds=123400)),
        ),
    )
    def test_w_string_values(mut, value, expected):
        got = mut._interval_from_json(value, create_field())
>       assert got == expected
E       assert relativedelta(years=+32, months=+11, days=+47, hours=+19, minutes=+16, seconds=+23, microseconds=+987653) == relativedelta(years=+32, months=+11, days=+47, hours=+19, minutes=+16, seconds=+23, microseconds=+987654)

tests/unit/helpers/test_from_json.py:135: AssertionError
________________________________ test_w_string_values[-32-11 -45 -67:16:23.987654-expected31] _________________________________

mut = <module 'google.cloud.bigquery._helpers' from '/Users/swast/src/github.com/googleapis/python-bigquery/google/cloud/bigquery/_helpers.py'>
value = '-32-11 -45 -67:16:23.987654'
expected = relativedelta(years=-32, months=-11, days=-47, hours=-19, minutes=-16, seconds=-23, microseconds=-987654)

    @pytest.mark.parametrize(
        ("value", "expected"),
        (
            ("0-0 0 0:0:0", relativedelta()),
            # SELECT INTERVAL X YEAR
            ("-10000-0 0 0:0:0", relativedelta(years=-10000)),
            ("-1-0 0 0:0:0", relativedelta(years=-1)),
            ("1-0 0 0:0:0", relativedelta(years=1)),
            ("10000-0 0 0:0:0", relativedelta(years=10000)),
            # SELECT INTERVAL X MONTH
            ("-0-11 0 0:0:0", relativedelta(months=-11)),
            ("-0-1 0 0:0:0", relativedelta(months=-1)),
            ("0-1 0 0:0:0", relativedelta(months=1)),
            ("0-11 0 0:0:0", relativedelta(months=11)),
            # SELECT INTERVAL X DAY
            ("0-0 -3660000 0:0:0", relativedelta(days=-3660000)),
            ("0-0 -1 0:0:0", relativedelta(days=-1)),
            ("0-0 1 0:0:0", relativedelta(days=1)),
            ("0-0 3660000 0:0:0", relativedelta(days=3660000)),
            # SELECT INTERVAL X HOUR
            ("0-0 0 -87840000:0:0", relativedelta(hours=-87840000)),
            ("0-0 0 -1:0:0", relativedelta(hours=-1)),
            ("0-0 0 1:0:0", relativedelta(hours=1)),
            ("0-0 0 87840000:0:0", relativedelta(hours=87840000)),
            # SELECT INTERVAL X MINUTE
            ("0-0 0 -0:59:0", relativedelta(minutes=-59)),
            ("0-0 0 -0:1:0", relativedelta(minutes=-1)),
            ("0-0 0 0:1:0", relativedelta(minutes=1)),
            ("0-0 0 0:59:0", relativedelta(minutes=59)),
            # SELECT INTERVAL X SECOND
            ("0-0 0 -0:0:59", relativedelta(seconds=-59)),
            ("0-0 0 -0:0:1", relativedelta(seconds=-1)),
            ("0-0 0 0:0:1", relativedelta(seconds=1)),
            ("0-0 0 0:0:59", relativedelta(seconds=59)),
            # SELECT (INTERVAL -1 SECOND) / 1000000
            ("0-0 0 -0:0:0.000001", relativedelta(microseconds=-1)),
            ("0-0 0 -0:0:59.999999", relativedelta(seconds=-59, microseconds=-999999)),
            ("0-0 0 -0:0:59.999", relativedelta(seconds=-59, microseconds=-999000)),
            ("0-0 0 0:0:59.999", relativedelta(seconds=59, microseconds=999000)),
            ("0-0 0 0:0:59.999999", relativedelta(seconds=59, microseconds=999999)),
            # Test with multiple digits in each section.
            (
                "32-11 45 67:16:23.987654",
                relativedelta(
                    years=32,
                    months=11,
                    days=45,
                    hours=67,
                    minutes=16,
                    seconds=23,
                    microseconds=987654,
                ),
            ),
            (
                "-32-11 -45 -67:16:23.987654",
                relativedelta(
                    years=-32,
                    months=-11,
                    days=-45,
                    hours=-67,
                    minutes=-16,
                    seconds=-23,
                    microseconds=-987654,
                ),
            ),
            # Test with mixed +/- sections.
            (
                "9999-9 -999999 9999999:59:59.999999",
                relativedelta(
                    years=9999,
                    months=9,
                    days=-999999,
                    hours=9999999,
                    minutes=59,
                    seconds=59,
                    microseconds=999999,
                ),
            ),
            # Test with fraction that is not microseconds.
            ("0-0 0 0:0:42.", relativedelta(seconds=42)),
            ("0-0 0 0:0:59.1", relativedelta(seconds=59, microseconds=100000)),
            ("0-0 0 0:0:0.12", relativedelta(microseconds=120000)),
            ("0-0 0 0:0:0.123", relativedelta(microseconds=123000)),
            ("0-0 0 0:0:0.1234", relativedelta(microseconds=123400)),
        ),
    )
    def test_w_string_values(mut, value, expected):
        got = mut._interval_from_json(value, create_field())
>       assert got == expected
E       assert relativedelta(years=-32, months=-11, days=-47, hours=-19, minutes=-16, seconds=-23, microseconds=-987653) == relativedelta(years=-32, months=-11, days=-47, hours=-19, minutes=-16, seconds=-23, microseconds=-987654)

tests/unit/helpers/test_from_json.py:135: AssertionError
=================================================== short test summary info ===================================================
FAILED tests/unit/helpers/test_from_json.py::test_w_string_values[32-11 45 67:16:23.987654-expected30] - assert relativedelt...
FAILED tests/unit/helpers/test_from_json.py::test_w_string_values[-32-11 -45 -67:16:23.987654-expected31] - assert relatived...

I think the problem is the conversion to floating point isn't exact due to difference between base-2 and base-10 representation.

@product-auto-label product-auto-label bot added the api: bigquery Issues related to the googleapis/python-db-dtypes-pandas API. label Sep 30, 2021
@tswast tswast added priority: p1 Important issue which blocks shipping the next release. Will be fixed prior to next release. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns. and removed api: bigquery Issues related to the googleapis/python-db-dtypes-pandas API. labels Sep 30, 2021
@tswast tswast self-assigned this Sep 30, 2021
@tswast
Copy link
Collaborator Author

tswast commented Sep 30, 2021

Started a fix (or at least added failing tests) in #20

@product-auto-label product-auto-label bot added the api: bigquery Issues related to the googleapis/python-db-dtypes-pandas API. label Oct 1, 2021
@tswast tswast closed this as completed in #20 Oct 4, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: bigquery Issues related to the googleapis/python-db-dtypes-pandas API. priority: p1 Important issue which blocks shipping the next release. Will be fixed prior to next release. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant