Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Magic Number

The magic number tells the parser whether it is reading a parquet file. This is a 4-byte PAR1 and is located at the beginning and at the end of a file.

magic number is located at the beginning and at the end of a file

Task

Implement the ensure_header_footer_magic function in src/magic.rs. It takes the entire file data as Bytes and returns an error if this is not a parquet file.

pub fn ensure_header_footer_magic(data: Bytes) -> Result<()> {
    todo!("step01: implement magic number")
}

Test

To verify the implementation, uncomment the test in tests/integration/mod.rs.

-// mod step01_magic;
+mod step01_magic;

And run:

cargo test

Hints and Solution

Hint

You can use starts_with and ends_with functions to check whether the magic number is correctly present at both ends of the file.

Solution
pub fn ensure_header_footer_magic(data: Bytes) -> Result<()> {
    if data.len() < 8 || !data.starts_with(b"PAR1") || !data.ends_with(b"PAR1") {
        bail!("Magic: not a parquet file")
    }
    Ok(())
}