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

Respect Page Size Limits in ArrowWriter (#2853) #2890

Merged
merged 5 commits into from Oct 24, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions parquet/Cargo.toml
Expand Up @@ -81,6 +81,10 @@ experimental = []
# Enable async APIs
async = ["futures", "tokio"]

[[test]]
name = "arrow_writer_layout"
required-features = ["arrow"]

[[bin]]
name = "parquet-read"
required-features = ["cli"]
Expand Down
17 changes: 7 additions & 10 deletions parquet/src/arrow/arrow_writer/byte_array.rs
Expand Up @@ -379,8 +379,7 @@ impl DictEncoder {

fn estimated_data_page_size(&self) -> usize {
let bit_width = self.bit_width();
1 + RleEncoder::min_buffer_size(bit_width)
+ RleEncoder::max_buffer_size(bit_width, self.indices.len())
1 + RleEncoder::max_buffer_size(bit_width, self.indices.len())
}

fn estimated_dict_page_size(&self) -> usize {
Expand Down Expand Up @@ -427,7 +426,6 @@ impl DictEncoder {
struct ByteArrayEncoder {
fallback: FallbackEncoder,
dict_encoder: Option<DictEncoder>,
num_values: usize,
min_value: Option<ByteArray>,
max_value: Option<ByteArray>,
}
Expand Down Expand Up @@ -466,7 +464,6 @@ impl ColumnValueEncoder for ByteArrayEncoder {
Ok(Self {
fallback,
dict_encoder: dictionary,
num_values: 0,
min_value: None,
max_value: None,
})
Expand All @@ -487,7 +484,10 @@ impl ColumnValueEncoder for ByteArrayEncoder {
}

fn num_values(&self) -> usize {
self.num_values
match &self.dict_encoder {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Without this, we would only ever have a single dictionary encoded page per column chunk. This is the other half of the fix in #2854

Copy link
Contributor

Choose a reason for hiding this comment

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

maybe it is worth a comment

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The confusion was that we tracked num_values in two places, that is now gone so I'm not sure what to write in a comment...

Some(encoder) => encoder.indices.len(),
None => self.fallback.num_values,
}
}

fn has_dictionary(&self) -> bool {
Expand All @@ -508,7 +508,7 @@ impl ColumnValueEncoder for ByteArrayEncoder {
fn flush_dict_page(&mut self) -> Result<Option<DictionaryPage>> {
match self.dict_encoder.take() {
Some(encoder) => {
if self.num_values != 0 {
if !encoder.indices.is_empty() {
return Err(general_err!(
"Must flush data pages before flushing dictionary"
));
Expand Down Expand Up @@ -551,10 +551,7 @@ where

match &mut encoder.dict_encoder {
Some(dict_encoder) => dict_encoder.encode(values, indices),
None => {
encoder.num_values += indices.len();
encoder.fallback.encode(values, indices)
}
None => encoder.fallback.encode(values, indices),
}
}

Expand Down
1 change: 1 addition & 0 deletions parquet/src/column/writer/encoder.rs
Expand Up @@ -201,6 +201,7 @@ impl<T: DataType> ColumnValueEncoder for ColumnValueEncoderImpl<T> {
}

fn write_gather(&mut self, values: &Self::Values, indices: &[usize]) -> Result<()> {
self.num_values += indices.len();
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 the fix for #2853

let slice: Vec<_> = indices.iter().map(|idx| values[*idx].clone()).collect();
self.write_slice(&slice)
}
Expand Down
12 changes: 5 additions & 7 deletions parquet/src/column/writer/mod.rs
Expand Up @@ -1825,7 +1825,7 @@ mod tests {
let page_writer = Box::new(SerializedPageWriter::new(&mut writer));
let props = Arc::new(
WriterProperties::builder()
.set_data_pagesize_limit(15) // actually each page will have size 15-18 bytes
.set_data_pagesize_limit(10)
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't understand this change

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The RLE size estimation has changed, and so this test needed updating

.set_write_batch_size(3) // write 3 values at a time
.build(),
);
Expand All @@ -1846,16 +1846,14 @@ mod tests {
);
let mut res = Vec::new();
while let Some(page) = page_reader.get_next_page().unwrap() {
res.push((page.page_type(), page.num_values()));
res.push((page.page_type(), page.num_values(), page.buffer().len()));
}
assert_eq!(
res,
vec![
(PageType::DICTIONARY_PAGE, 10),
(PageType::DATA_PAGE, 3),
(PageType::DATA_PAGE, 3),
(PageType::DATA_PAGE, 3),
(PageType::DATA_PAGE, 1)
(PageType::DICTIONARY_PAGE, 10, 40),
(PageType::DATA_PAGE, 9, 10),
(PageType::DATA_PAGE, 1, 3),
]
);
}
Expand Down
3 changes: 1 addition & 2 deletions parquet/src/encodings/encoding/dict_encoder.rs
Expand Up @@ -162,8 +162,7 @@ impl<T: DataType> Encoder<T> for DictEncoder<T> {

fn estimated_data_encoded_size(&self) -> usize {
let bit_width = self.bit_width();
1 + RleEncoder::min_buffer_size(bit_width)
+ RleEncoder::max_buffer_size(bit_width, self.indices.len())
RleEncoder::max_buffer_size(bit_width, self.indices.len())
}

fn flush_buffer(&mut self) -> Result<ByteBufferPtr> {
Expand Down
2 changes: 1 addition & 1 deletion parquet/src/encodings/encoding/mod.rs
Expand Up @@ -888,7 +888,7 @@ mod tests {
// DICTIONARY
// NOTE: The final size is almost the same because the dictionary entries are
// preserved after encoded values have been written.
run_test::<Int32Type>(Encoding::RLE_DICTIONARY, -1, &[123, 1024], 11, 68, 66);
run_test::<Int32Type>(Encoding::RLE_DICTIONARY, -1, &[123, 1024], 0, 2, 0);
Copy link
Contributor

Choose a reason for hiding this comment

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

can you explain these changes?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The RLE size estimation was updated as part of #2889


// DELTA_BINARY_PACKED
run_test::<Int32Type>(Encoding::DELTA_BINARY_PACKED, -1, &[123; 1024], 0, 35, 0);
Expand Down
9 changes: 2 additions & 7 deletions parquet/src/encodings/levels.rs
Expand Up @@ -38,13 +38,8 @@ pub fn max_buffer_size(
) -> usize {
let bit_width = num_required_bits(max_level as u64);
match encoding {
Encoding::RLE => {
RleEncoder::max_buffer_size(bit_width, num_buffered_values)
+ RleEncoder::min_buffer_size(bit_width)
}
Encoding::BIT_PACKED => {
ceil((num_buffered_values * bit_width as usize) as i64, 8) as usize
}
Encoding::RLE => RleEncoder::max_buffer_size(bit_width, num_buffered_values),
Copy link
Contributor

Choose a reason for hiding this comment

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

why is this different than the other estimated sizes?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What other estimated sizes, I think I updated them all?

Encoding::BIT_PACKED => ceil(num_buffered_values * bit_width as usize, 8),
_ => panic!("Unsupported encoding type {}", encoding),
}
}
Expand Down
43 changes: 23 additions & 20 deletions parquet/src/encodings/rle.rs
Expand Up @@ -42,9 +42,8 @@ use crate::util::{
/// repeated-value := value that is repeated, using a fixed-width of
/// round-up-to-next-byte(bit-width)

/// Maximum groups per bit-packed run. Current value is 64.
/// Maximum groups of 8 values per bit-packed run. Current value is 64.
const MAX_GROUPS_PER_BIT_PACKED_RUN: usize = 1 << 6;
const MAX_VALUES_PER_BIT_PACKED_RUN: usize = MAX_GROUPS_PER_BIT_PACKED_RUN * 8;

/// A RLE/Bit-Packing hybrid encoder.
// TODO: tracking memory usage
Expand Down Expand Up @@ -101,29 +100,33 @@ impl RleEncoder {

/// Returns the minimum buffer size needed to use the encoder for `bit_width`.
/// This is the maximum length of a single run for `bit_width`.
#[allow(unused)]
tustvold marked this conversation as resolved.
Show resolved Hide resolved
pub fn min_buffer_size(bit_width: u8) -> usize {
let max_bit_packed_run_size = 1 + bit_util::ceil(
(MAX_VALUES_PER_BIT_PACKED_RUN * bit_width as usize) as i64,
8,
);
let max_rle_run_size =
bit_util::MAX_VLQ_BYTE_LEN + bit_util::ceil(bit_width as i64, 8) as usize;
std::cmp::max(max_bit_packed_run_size as usize, max_rle_run_size)
let b = bit_width as usize;
let max_bit_packed_run_size = 1 + MAX_GROUPS_PER_BIT_PACKED_RUN * b;
let max_rle_run_size = 1 + bit_util::ceil(b, 8);
tustvold marked this conversation as resolved.
Show resolved Hide resolved
max_bit_packed_run_size.max(max_rle_run_size)
}

/// Returns the maximum buffer size takes to encode `num_values` values with
/// Returns the maximum buffer size to encode `num_values` values with
/// `bit_width`.
pub fn max_buffer_size(bit_width: u8, num_values: usize) -> usize {
// First the maximum size for bit-packed run
let bytes_per_run = bit_width;
let num_runs = bit_util::ceil(num_values as i64, 8) as usize;
let bit_packed_max_size = num_runs + num_runs * bytes_per_run as usize;

// Second the maximum size for RLE run
let min_rle_run_size = 1 + bit_util::ceil(bit_width as i64, 8) as usize;
let rle_max_size =
bit_util::ceil(num_values as i64, 8) as usize * min_rle_run_size;
std::cmp::max(bit_packed_max_size, rle_max_size) as usize
// The maximum size occurs with the shortest possible runs of 8
let num_runs = bit_util::ceil(num_values, 8);

// The number of bytes in a run of 8
let bytes_per_run = bit_width as usize;

// The maximum size if stored as shortest possible bit packed runs of 8
let bit_packed_max_size = num_runs + num_runs * bytes_per_run;

// The length of an RLE run of 8
let min_rle_run_size = 1 + bit_util::ceil(bit_width as usize, 8);

// The maximum size if stored as shortest possible RLE runs of 8
let rle_max_size = num_runs * min_rle_run_size;

bit_packed_max_size.max(rle_max_size)
}

/// Encodes `value`, which must be representable with `bit_width` bits.
Expand Down