Improve arrow-avro decoding for one-record messages - #10713
Open
jordepic wants to merge 2 commits into
Open
Conversation
Contributor
|
FYI @jecsand838 -- could you help review this PR? |
Contributor
Absolutely! I'll have time tonight / tomorrow morning to review this. |
|
@jordepic is there currently a way to convert multiple Avro Datums in a single .avro to a RecordBatches directly? A Raw Binary Encoded Avro |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Which issue does this PR close?
Rationale for this change
Some messaging systems deliver records one at a time. For example, Kafka gives a consumer the complete byte array for one message, so the consumer already knows where that message begins and ends. If that message contains one Avro record, the decoder does not need an Avro header to identify the record boundary.
In Avro terminology, one encoded value is called a datum. A datum can be a primitive value or a complete record.
The existing
Decoder::decodemethod expects each record to include an Avro framing prefix. That prefix identifies the Avro format and the writer schema. A caller that already has one complete Kafka message and already knows its schema must therefore add a temporary prefix and copy the payload before decoding it, or separately inspect the schema to determine how many bytes belong to the record.Nullable nested records have a separate performance cost. Consider an event with three optional nested records where only one is populated on each row. For every absent record, the decoder currently walks through all of its child fields and immediately appends placeholder values. Long sequences of absent records repeat the same work row by row.
What changes are included in this PR?
Decoder::decode_datum, which decodes one complete Avro value directly from the supplied bytes using the writer schema already selected on the decoder. It returns the number of bytes used by that value, leaving any remaining bytes untouched.The second change only affects how the decoder builds arrays internally. The returned Arrow arrays are unchanged, and all child arrays are brought to the correct length before a batch is returned.
Are these changes tested?
Yes.
cargo test -p arrow-avro --all-features: 475 unit tests passed; 26 documentation tests passed; 1 documentation test ignored.cargo clippy -p arrow-avro --all-targets --all-features -- -D warningscargo fmt --all -- --checkThe benchmark below decodes all 10,000 rows into one batch. It was run on an Apple M1 Max against a saved
mainbaseline.Command:
cargo bench -p arrow-avro --bench decoder -- 'SparseNested\(Struct\)/10000' --baseline mainAre there any user-facing changes?
Yes.
Decodergains a new, non-breakingdecode_datummethod for callers that already have the complete bytes for one Avro value and have already selected its writer schema. Existing decoding methods and framed input behavior are unchanged.AI assistance disclosure: Codex was used to help port the implementation, draft tests and the benchmark, and prepare the issue and PR text. The resulting code and all reported outputs were reviewed before submission, and I take responsibility for the contribution.