Skip to content
This repository has been archived by the owner on Feb 18, 2024. It is now read-only.

Improved performance of writing to parquet #1392

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
6 changes: 3 additions & 3 deletions src/io/parquet/write/binary/nested.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ where
} else {
unreachable!("")
}
let num_values = nested::num_values(&nested);

let mut buffer = vec![];
let (repetition_levels_byte_length, definition_levels_byte_length) =
nested::write_rep_and_def(options.version, &nested, &mut buffer)?;
nested::write_rep_and_def(options.version, &nested, num_values, &mut buffer)?;

let array = array.slice(start, len);
encode_plain(&array, is_optional, &mut buffer);

let statistics = if options.write_statistics {
Expand All @@ -50,7 +50,7 @@ where

utils::build_plain_page(
buffer,
nested::num_values(&nested),
num_values,
nested[0].len(),
array.null_count(),
repetition_levels_byte_length,
Expand Down
5 changes: 3 additions & 2 deletions src/io/parquet/write/boolean/nested.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,11 @@ pub fn array_to_page(
} else {
unreachable!("")
}
let num_values = nested::num_values(&nested);

let mut buffer = vec![];
let (repetition_levels_byte_length, definition_levels_byte_length) =
nested::write_rep_and_def(options.version, &nested, &mut buffer)?;
nested::write_rep_and_def(options.version, &nested, num_values, &mut buffer)?;

encode_plain(&array, is_optional, &mut buffer)?;

Expand All @@ -45,7 +46,7 @@ pub fn array_to_page(

utils::build_plain_page(
buffer,
nested::num_values(&nested),
num_values,
nested[0].len(),
array.null_count(),
repetition_levels_byte_length,
Expand Down
14 changes: 6 additions & 8 deletions src/io/parquet/write/dictionary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ fn serialize_levels(
length: usize,
type_: &PrimitiveType,
nested: &[Nested],
num_values: usize,
options: WriteOptions,
buffer: &mut Vec<u8>,
) -> Result<(usize, usize)> {
Expand All @@ -82,7 +83,7 @@ fn serialize_levels(
let definition_levels_byte_length = buffer.len();
Ok((0, definition_levels_byte_length))
} else {
nested::write_rep_and_def(options.version, nested, buffer)
nested::write_rep_and_def(options.version, nested, num_values, buffer)
}
}

Expand Down Expand Up @@ -122,27 +123,24 @@ fn serialize_keys<K: DictionaryKey>(
unreachable!("")
}

let num_values = nested::num_values(&nested);

let (repetition_levels_byte_length, definition_levels_byte_length) = serialize_levels(
validity.as_ref(),
array.len(),
&type_,
&nested,
num_values,
options,
&mut buffer,
)?;

serialize_keys_values(&array, validity.as_ref(), &mut buffer)?;

let (num_values, num_rows) = if nested.len() == 1 {
(array.len(), array.len())
} else {
(nested::num_values(&nested), nested[0].len())
};

utils::build_plain_page(
buffer,
num_values,
num_rows,
array.len(),
array.null_count(),
repetition_levels_byte_length,
definition_levels_byte_length,
Expand Down
10 changes: 4 additions & 6 deletions src/io/parquet/write/nested/def.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::{bitmap::Bitmap, offset::Offset};

use super::super::pages::{ListNested, Nested};
use super::rep::num_values;
use super::to_length;

trait DebugIter: Iterator<Item = (u32, usize)> + std::fmt::Debug {}
Expand Down Expand Up @@ -87,9 +86,7 @@ pub struct DefLevelsIter<'a> {
}

impl<'a> DefLevelsIter<'a> {
pub fn new(nested: &'a [Nested]) -> Self {
let remaining_values = num_values(nested);

pub fn new(nested: &'a [Nested], num_values: usize) -> Self {
let iter = iter(nested);
let remaining = vec![0; iter.len()];
let validity = vec![0; iter.len()];
Expand All @@ -100,7 +97,7 @@ impl<'a> DefLevelsIter<'a> {
validity,
total: 0,
current_level: 0,
remaining_values,
remaining_values: num_values,
}
}
}
Expand Down Expand Up @@ -170,10 +167,11 @@ impl<'a> Iterator for DefLevelsIter<'a> {

#[cfg(test)]
mod tests {
use super::super::num_values;
use super::*;

fn test(nested: Vec<Nested>, expected: Vec<u32>) {
let mut iter = DefLevelsIter::new(&nested);
let mut iter = DefLevelsIter::new(&nested, num_values(&nested));
assert_eq!(iter.size_hint().0, expected.len());
let result = iter.by_ref().collect::<Vec<_>>();
assert_eq!(result, expected);
Expand Down
23 changes: 17 additions & 6 deletions src/io/parquet/write/nested/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,19 @@ fn write_levels_v1<F: FnOnce(&mut Vec<u8>) -> Result<()>>(
}

/// writes the rep levels to a `Vec<u8>`.
fn write_rep_levels(buffer: &mut Vec<u8>, nested: &[Nested], version: Version) -> Result<()> {
fn write_rep_levels(
buffer: &mut Vec<u8>,
nested: &[Nested],
num_values: usize,
version: Version,
) -> Result<()> {
let max_level = max_rep_level(nested) as i16;
if max_level == 0 {
return Ok(());
}
let num_bits = get_bit_width(max_level);

let levels = rep::RepLevelsIter::new(nested);
let levels = rep::RepLevelsIter::new(nested, num_values);

match version {
Version::V1 => {
Expand All @@ -53,14 +58,19 @@ fn write_rep_levels(buffer: &mut Vec<u8>, nested: &[Nested], version: Version) -
}

/// writes the rep levels to a `Vec<u8>`.
fn write_def_levels(buffer: &mut Vec<u8>, nested: &[Nested], version: Version) -> Result<()> {
fn write_def_levels(
buffer: &mut Vec<u8>,
nested: &[Nested],
num_values: usize,
version: Version,
) -> Result<()> {
let max_level = max_def_level(nested) as i16;
if max_level == 0 {
return Ok(());
}
let num_bits = get_bit_width(max_level);

let levels = def::DefLevelsIter::new(nested);
let levels = def::DefLevelsIter::new(nested, num_values);

match version {
Version::V1 => write_levels_v1(buffer, move |buffer: &mut Vec<u8>| {
Expand Down Expand Up @@ -105,12 +115,13 @@ fn to_length<O: Offset>(
pub fn write_rep_and_def(
page_version: Version,
nested: &[Nested],
num_values: usize,
buffer: &mut Vec<u8>,
) -> Result<(usize, usize)> {
write_rep_levels(buffer, nested, page_version)?;
write_rep_levels(buffer, nested, num_values, page_version)?;
let repetition_levels_byte_length = buffer.len();

write_def_levels(buffer, nested, page_version)?;
write_def_levels(buffer, nested, num_values, page_version)?;
let definition_levels_byte_length = buffer.len() - repetition_levels_byte_length;

Ok((repetition_levels_byte_length, definition_levels_byte_length))
Expand Down
8 changes: 3 additions & 5 deletions src/io/parquet/write/nested/rep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,7 @@ pub struct RepLevelsIter<'a> {
}

impl<'a> RepLevelsIter<'a> {
pub fn new(nested: &'a [Nested]) -> Self {
let remaining_values = num_values(nested);

pub fn new(nested: &'a [Nested], num_values: usize) -> Self {
let iter = iter(nested);
let remaining = vec![0; iter.len()];

Expand All @@ -71,7 +69,7 @@ impl<'a> RepLevelsIter<'a> {
remaining,
total: 0,
current_level: 0,
remaining_values,
remaining_values: num_values,
}
}
}
Expand Down Expand Up @@ -138,7 +136,7 @@ mod tests {
use super::*;

fn test(nested: Vec<Nested>, expected: Vec<u32>) {
let mut iter = RepLevelsIter::new(&nested);
let mut iter = RepLevelsIter::new(&nested, num_values(&nested));
assert_eq!(iter.size_hint().0, expected.len());
assert_eq!(iter.by_ref().collect::<Vec<_>>(), expected);
assert_eq!(iter.size_hint().0, 0);
Expand Down
6 changes: 4 additions & 2 deletions src/io/parquet/write/primitive/nested.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,10 @@ where
unreachable!("")
}

let num_values = nested::num_values(&nested);

let (repetition_levels_byte_length, definition_levels_byte_length) =
nested::write_rep_and_def(options.version, &nested, &mut buffer)?;
nested::write_rep_and_def(options.version, &nested, num_values, &mut buffer)?;

let buffer = encode_plain(&array, is_optional, buffer);

Expand All @@ -58,7 +60,7 @@ where

utils::build_plain_page(
buffer,
nested::num_values(&nested),
num_values,
nested[0].len(),
array.null_count(),
repetition_levels_byte_length,
Expand Down
6 changes: 4 additions & 2 deletions src/io/parquet/write/utf8/nested.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,11 @@ where
unreachable!("")
}

let num_values = nested::num_values(&nested);

let mut buffer = vec![];
let (repetition_levels_byte_length, definition_levels_byte_length) =
nested::write_rep_and_def(options.version, &nested, &mut buffer)?;
nested::write_rep_and_def(options.version, &nested, num_values, &mut buffer)?;

encode_plain(&array, is_optional, &mut buffer);

Expand All @@ -48,7 +50,7 @@ where

utils::build_plain_page(
buffer,
nested::num_values(&nested),
num_values,
nested[0].len(),
array.null_count(),
repetition_levels_byte_length,
Expand Down