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 for plain encoded binary in data pages #8

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/encoding/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ pub mod delta_bitpacked;
pub mod delta_byte_array;
pub mod delta_length_byte_array;
pub mod hybrid_rle;
pub mod plain_byte_array;
pub mod uleb128;
pub mod zigzag_leb128;

Expand Down
46 changes: 46 additions & 0 deletions src/encoding/plain_byte_array/decoder.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use crate::encoding::get_length;

/// Decodes according to [Plain strings](https://github.com/apache/parquet-format/blob/master/Encodings.md#plain-plain--0),
/// prefixes, lengths and values
/// # Implementation
/// This struct does not allocate on the heap.
#[derive(Debug)]
pub struct Decoder<'a> {
values: &'a [u8],
remaining: usize,
}

impl<'a> Decoder<'a> {
#[inline]
pub fn new(values: &'a [u8], length: usize) -> Self {
Self {
values,
remaining: length,
}
}
}

impl<'a> Iterator for Decoder<'a> {
type Item = &'a [u8];

#[inline]
fn next(&mut self) -> Option<Self::Item> {
jorgecarleitao marked this conversation as resolved.
Show resolved Hide resolved
let values = self.values;
if values.len() >= 4 {
let next_len = get_length(values) as usize;
jorgecarleitao marked this conversation as resolved.
Show resolved Hide resolved
let values = &values[4..];

let result = Some(&values[0..next_len]);
self.values = &values[next_len..];
self.remaining -= 1;

result
} else {
None
}
}

fn size_hint(&self) -> (usize, Option<usize>) {
(self.remaining, None)
jorgecarleitao marked this conversation as resolved.
Show resolved Hide resolved
}
}
3 changes: 3 additions & 0 deletions src/encoding/plain_byte_array/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
mod decoder;

pub use decoder::Decoder;
39 changes: 38 additions & 1 deletion src/serialization/read/binary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ use super::levels::consume_level;
use crate::error::{ParquetError, Result};
use crate::metadata::ColumnDescriptor;
use crate::read::Page;
use crate::serialization::read::utils::ValuesDef;
use crate::{
encoding::{bitpacking, uleb128},
encoding::{bitpacking, plain_byte_array, uleb128},
read::BinaryPageDict,
};

Expand Down Expand Up @@ -62,3 +63,39 @@ pub fn page_dict_to_vec(
_ => todo!(),
}
}

fn read_plain_buffer(
values: &[u8],
length: u32,
def_level_encoding: (&Encoding, i16),
) -> Vec<Option<Vec<u8>>> {
let (values, def_levels) = consume_level(values, length, def_level_encoding);

let decoded_values =
plain_byte_array::Decoder::new(values, length as usize).map(|bytes| bytes.to_vec());

ValuesDef::new(
decoded_values,
def_levels.into_iter(),
def_level_encoding.1 as u32,
)
.collect()
}

pub fn page_to_vec(page: &Page, descriptor: &ColumnDescriptor) -> Result<Vec<Option<Vec<u8>>>> {
assert_eq!(descriptor.max_rep_level(), 0);
match page {
Page::V1(page) => match (&page.header.encoding, &page.dictionary_page) {
(Encoding::Plain, None) => Ok(read_plain_buffer(
&page.buffer,
page.header.num_values as u32,
(
&page.header.definition_level_encoding,
descriptor.max_def_level(),
),
)),
_ => todo!(),
},
_ => todo!(),
}
}
3 changes: 3 additions & 0 deletions src/serialization/read/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ pub fn page_to_array(page: CompressedPage, descriptor: &ColumnDescriptor) -> Res
PhysicalType::Double => {
Ok(Array::Float64(primitive::page_to_vec(&page, descriptor)?))
}
PhysicalType::ByteArray => {
jorgecarleitao marked this conversation as resolved.
Show resolved Hide resolved
Ok(Array::Binary(binary::page_to_vec(&page, descriptor)?))
}
_ => todo!(),
},
},
Expand Down