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
2 changes: 1 addition & 1 deletion pyiceberg/avro/decoder_fast.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ cdef class CythonBinaryDecoder:
"""
return float(STRUCT_FLOAT.unpack(self.read(4))[0])

cpdef float read_double(self):
cpdef double read_double(self):
"""Reads a value from the stream as a double.

A double is written as 8 bytes.
Expand Down
17 changes: 17 additions & 0 deletions tests/avro/test_decoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,23 @@ def test_read_double(decoder_class: Callable[[bytes], BinaryDecoder]) -> None:
assert decoder.read_double() == 19.25


@pytest.mark.parametrize("decoder_class", AVAILABLE_DECODERS)
@pytest.mark.parametrize(
"value",
[
3.141592653589793,
429496729622.314,
0.1,
1.0000000000000002, # smallest double above 1.0
1e308, # overflows to inf in single precision
5e-324, # underflows to 0.0 in single precision
],
)
def test_read_double_keeps_full_precision(decoder_class: Callable[[bytes], BinaryDecoder], value: float) -> None:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is a valid regression test. It passes even if we revert decoder_fast.pyx's change.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for checking. The test does fail on the unfixed tree, but only if the Cython extension is actually rebuilt — and setup.py makes that easy to miss.

The cythonize call is wrapped in try/except Exception with allowed_to_fail = os.environ.get("CIBUILDWHEEL", "0") != "1", so if the interpreter running it can't import Cython, ext_modules stays [] and build_ext --inplace prints running build_ext, exits 0, and builds nothing. The previously built decoder_fast.*.so stays on disk with the fix compiled into it, and the test passes against that stale artifact.

Reverting the source and forcing a real rebuild:

$ git checkout HEAD~1 -- pyiceberg/avro/decoder_fast.pyx   # back to cpdef float
$ .venv/bin/python setup.py build_ext --inplace
Compiling pyiceberg/avro/decoder_fast.pyx because it changed.
building 'pyiceberg.avro.decoder_fast' extension

$ .venv/bin/python -m pytest tests/avro/test_decoder.py -k full_precision -q
6 failed, 6 passed, 44 deselected

All six failures are the CythonBinaryDecoder parameters. The six StreamingBinaryDecoder ones pass, which is expected — the pure-Python decoder was always correct. Decoded values on the unfixed build:

3.141592653589793 -> 3.1415927410125732
1e+308            -> inf
5e-324            -> 0.0

With the fix restored and rebuilt, tests/avro/test_decoder.py is 56 passed.

decoder = decoder_class(struct.pack("<d", value))
assert decoder.read_double() == value


@pytest.mark.parametrize("decoder_class", AVAILABLE_DECODERS)
def test_skip_double(decoder_class: Callable[[bytes], BinaryDecoder]) -> None:
decoder = decoder_class(b"\x00\x00\x00\x00\x00\x40\x33\x40")
Expand Down