Skip to content

fix(avro): advance the decoder when skipping an enum - #4009

Open
Rodrigo-Palma wants to merge 1 commit into
apache:mainfrom
Rodrigo-Palma:fix/enum-reader-skip
Open

Rodrigo-Palma wants to merge 1 commit into
apache:mainfrom
Rodrigo-Palma:fix/enum-reader-skip

Conversation

@Rodrigo-Palma

Copy link
Copy Markdown
Contributor

Closes #4006

Rationale for this change

EnumReader.skip() was pass, so skipping an enum field left its bytes in the stream and every field after it in the record was decoded from the wrong offset. read() already delegates to the wrapped reader; skip() now does the same.

This is reachable from ordinary reads: the manifest entry schema starts with the status enum (field id 0), so any read that projects status out decodes the rest of the entry from the enum's bytes.

decoder = CythonBinaryDecoder(b"\x02\x18")  # enum ordinal 1, then 12
reader = EnumReader(ManifestEntryStatus, IntegerReader())
reader.skip(decoder)
IntegerReader().read(decoder)
# before: 1   (the enum's own bytes, re-read)
# after:  12

Are these changes tested?

Yes, two tests in tests/avro/test_resolver.py, each parametrized over StreamingBinaryDecoder and CythonBinaryDecoder (4 cases, all failing before this change):

  • test_enum_reader_skip_advances_the_decoder: after skipping, the next field reads its own value.
  • test_enum_reader_skip_matches_read: reading and skipping leave the decoder at the same position.

pytest tests/ -m unit --ignore=tests/integration gives 4215 passed. The 9 failures in tests/avro/test_decoder.py are present identically on an unmodified main in this checkout (a stale compiled Cython extension) and are unrelated to this change.

Are there any user-facing changes?

Yes: reads that skip an enum field now return correct values instead of misaligned ones. No API change.

Comment thread tests/avro/test_resolver.py Outdated
assert actual == expected


@pytest.mark.parametrize("decoder_class", [StreamingBinaryDecoder, CythonBinaryDecoder])

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

There's a list of AVAILABLE_DECODERS in tests/avro/test_reader.py that we should try to reuse

That'll involve moving that list to some shared file, but that way we can have future tests all share the same list of decoders.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Moved the list to tests/avro/conftest.py, with a decoder_class fixture that parametrizes it, so these tests no longer carry their own copy.

I went with a fixture instead of importing the constant. CI runs pytest tests/avro/test_decoder.py on its own (python-ci.yml, and the cibuildwheel test command in the artifact workflows), and in that invocation the repository root is not on sys.path, so from tests.avro.conftest import AVAILABLE_DECODERS fails with ModuleNotFoundError: No module named 'tests'. I hit it locally before settling on the fixture. The existing from tests.conftest import ... imports have the same limitation and only work under python -m pytest. A fixture needs no import, so it works under either invocation.

test_decoder.py and test_reader.py still define their own copies. Direct parametrization takes precedence over a fixture of the same name, so they keep working untouched. Happy to migrate them in a follow-up if you would like: it is 22 decorators plus one itertools.product call, which felt like a separate change from this bug fix.

EnumReader.skip() was a no-op, so the enum's bytes stayed in the stream and
the next field was decoded from them. Reading a manifest with the status
field projected out returns wrong values for every field after it.

Delegate to the wrapped reader, which is what read() already does.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Avro EnumReader.skip() does not advance the decoder

2 participants