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.

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.
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.

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