apache / arrow-rs

Official Rust implementation of Apache Arrow
https://arrow.apache.org/
Apache License 2.0
2.33k stars 686 forks source link

API for encoding/decoding ParquetMetadata with more control #6002

Open alamb opened 4 days ago

alamb commented 4 days ago

Is your feature request related to a problem or challenge? Please describe what you are trying to do. There are several cases where we would like to have more control over the encoding/deocing of Parquet metadata:

  1. serialize and deserialize it outside of a parquet file so I can store it in a cache outside of parquet files and avoid slow object store requests (https://github.com/apache/arrow-rs/discussions/5988)
  2. https://github.com/apache/arrow-rs/issues/5855
  3. https://github.com/apache/arrow-rs/issues/5999
  4. Ensuring that the Page index structures are read properly requires setting some non obvious settings on the reader scuh as ArrowReaderOptions::with_page_index

At the time of writing, the current APIs exposed

  1. No API exposed for creating / writing parquet metadata
  2. The API exposed for reading parquet metadata, decode_metadata, has no way for finer grained control

Describe the solution you'd like I would like an API that allows more fine grained control over reading/writing metadata and that permits adding additional features over time in a backwards compatible way

Describe alternatives you've considered

Here is one potential idea -- to create Encoder / Decoder structs that can encode and decode the metadata along with various configuration options.

Ideally this struct would be integrated into the rest of the crate, e.g. used in SerializedFileWriter?

let encoder = ParquetMetadataEncoder::new()
   .with_some_options(foo);
let mut buffer = vec![]
encoder.encode(metadata, &mut buffer)

Similarly for decoding

let decoder = ParquetMetadataDecoder::new()
   .with_offset_index(true)
   .with_column_index(true);

let result = decoder.decode(&buffer);
// decoder need to have some way to communicate
// if it doesn't have sufficient information (e.g the PageIndex 
// wasn't present). Maybe this should just be an error?
match result {
  FullDecode(metadata) => return metadata,
  NeedMoreData(range) => {
    // fetch additional data and pass to deocder?
    todo!()
  } 
  ..
};

Additional context This ticket is based on the discussion with @adriangb here https://github.com/apache/arrow-rs/discussions/5988

There are a bunch of discussions on metadata speed here https://github.com/apache/arrow-rs/issues/5770

Here is a PR with a proposed 'encode_metadata' function: https://github.com/apache/arrow-rs/pull/6000

tustvold commented 4 days ago

This sounds quite a lot like https://docs.rs/parquet/latest/parquet/arrow/async_reader/struct.MetadataLoader.html ?

alamb commented 3 days ago

This sounds quite a lot like https://docs.rs/parquet/latest/parquet/arrow/async_reader/struct.MetadataLoader.html ?

That is quite similar -- thank you. Some differences might be also be the with a normal (non async) API as well as an equivalent encoder

adriangb commented 1 day ago

Ye I think the asyncness would be an important difference. Also that the existing APIs kind of want to load from an entire file. I suppose you could give it a "file" with just the footer and tell it to load just that range... but it feels a bit forced? Same with the asyncness. For my use case I could do some pointless async work (as in, make an async file like thing that just points to a Vec<u8> but in general unnecessary async work is not ideal. My general experience is that it's nice to decouple IO from encoding / decoding logic.

alamb commented 7 hours ago

My general experience is that it's nice to decouple IO from encoding / decoding logic.

Yes I agree this would be ideal. Having two things:

  1. Something that handles the actual encode/decode of bytes
  2. Something that reads data from a remote source + decodes them (what MetadataLoader seems to do)