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

Support Parsing Avro File Headers #4888

Merged
merged 7 commits into from
Oct 4, 2023
Merged

Conversation

tustvold
Copy link
Contributor

@tustvold tustvold commented Oct 3, 2023

Which issue does this PR close?

Part of #4886

Rationale for this change

This adds support for decoding the avro headers, and parsing the embedded schema.

This is a necessary first step towards supporting reading avro data

What changes are included in this PR?

Are there any user-facing changes?


//! Transfer data between the Arrow memory format and Avro

#![allow(unused)] // Temporary
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is necessary because currently nothing is public 😄


mod vlq;

/// Read a [`Header`] from the provided [`BufRead`]
Copy link
Contributor Author

Choose a reason for hiding this comment

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

These methods I expect to bundle up in a public Reader, but one step at a time 😄

Copy link
Contributor

@alamb alamb left a comment

Choose a reason for hiding this comment

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

👨‍🍳 👌 -- very nice @tustvold . I think the APIs and code make lots of sense and I have only suggstions on documentation and testing

cc @Samrose-Ahmed @sarutak @devinjdangelo @bjchambers

arrow-avro/src/lib.rs Outdated Show resolved Hide resolved

#[test]
fn test_header() {
let header = decode_file("../testing/data/avro/alltypes_plain.avro");
Copy link
Contributor

Choose a reason for hiding this comment

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

👍 for using the avro testing data

const MAGIC: &[u8; 4] = b"Obj\x01";

impl HeaderDecoder {
/// Parse [`Header`] from `buf`, returning the number of bytes read
Copy link
Contributor

Choose a reason for hiding this comment

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

I think it would help to mention in the docs that

  1. the header can be pushed in chunks at a time (by calling decode multiple times)
  2. when done, call finish()
  3. What to do with remaining buffered data (it has to be preprended to the next call to decode)
  4. What happens on error (I think the entire decoder state basically is "unknown" and it can't recover)

arrow-avro/src/reader/header.rs Outdated Show resolved Hide resolved
}

let mut decoder = HeaderDecoder::default();
decoder.decode(MAGIC).unwrap();
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe this should also verify that the decoder reports consuming the correct number of bytes (4)?

fn test_header() {
let header = decode_file("../testing/data/avro/alltypes_plain.avro");
let schema_json = header.get(SCHEMA_METADATA_KEY).unwrap();
let _schema: Schema<'_> = serde_json::from_slice(schema_json).unwrap();
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this would be a better test (more clearly show it obviously worked) if it compared the entire schema json (not just a single field, which I have no idea if the value of the sync byte is correct)

let file = File::open("../testing/data/avro/alltypes_plain.avro").unwrap();
let mut reader = BufReader::new(file);
let header = read_header(&mut reader).unwrap();
for result in read_blocks(reader) {
Copy link
Contributor

Choose a reason for hiding this comment

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

perhaps it could verify the number of blocks? Though maybe that will be covered as the reader becomes more sophisticated

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah this is really just a placeholder test

Comment on lines 18 to 20
/// Decoder for zig-zag encoded VLQ integers
///
/// <https://protobuf.dev/programming-guides/encoding/#varints>
Copy link
Contributor

Choose a reason for hiding this comment

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

FWIW the avro docs include a reference to lucene (not protobuf): https://lucene.apache.org/core/3_5_0/fileformats.html#VInt

Maybe not a big deal, but it was weird to see the protobuf reference here

Suggested change
/// Decoder for zig-zag encoded VLQ integers
///
/// <https://protobuf.dev/programming-guides/encoding/#varints>
/// Decoder for zig-zag encoded variable length (VLW) integers
/// See also:
/// <https://avro.apache.org/docs/1.11.1/specification/#primitive-types-1>
/// <https://protobuf.dev/programming-guides/encoding/#varints>

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Maybe not a big deal, but it was weird to see the protobuf reference here

Yeah, amusingly the Avro spec links to protobuf as well. Avro is very heavily inspired by protobuf, and isn't ashamed to admit it 😄

}))
);
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Eventually it would be great to have a arrow-avro/tests/reader.rs or something that reads the contents of th avro files in arrow-testing and verifies the conevrted schema.

tustvold and others added 2 commits October 4, 2023 11:45
Co-authored-by: Andrew Lamb <andrew@nerdnetworks.org>
@tustvold
Copy link
Contributor Author

tustvold commented Oct 4, 2023

The integration test failures are unrelated and appear to relate to apache/arrow#37990

@tustvold tustvold merged commit f0455d1 into apache:master Oct 4, 2023
30 of 31 checks passed
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.

None yet

2 participants