-
Notifications
You must be signed in to change notification settings - Fork 784
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
Change parquet writers to use standard std:io::Write
rather custom ParquetWriter
trait (#1717) (#1163)
#1719
Merged
Merged
Changes from 7 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
f0e869a
Rustify parquet writer (#1717) (#1163)
tustvold fe0958c
Fix parquet_derive
tustvold 5d53dc5
Fix benches
tustvold 2620f45
Fix parquet_derive tests
tustvold c57aa9e
Use raw vec instead of Cursor
tustvold e39fa42
Merge remote-tracking branch 'upstream/master' into rustify-parquet-w…
tustvold cd5eda8
Review feedback
tustvold 196dd76
Fix unnecessary unwrap
tustvold d699960
Merge remote-tracking branch 'upstream/master' into rustify-parquet-w…
tustvold File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ | |
//! Contains writer which writes arrow data into parquet data. | ||
|
||
use std::collections::VecDeque; | ||
use std::io::Write; | ||
use std::sync::Arc; | ||
|
||
use arrow::array as arrow_array; | ||
|
@@ -35,18 +36,16 @@ use super::schema::{ | |
use crate::column::writer::ColumnWriter; | ||
use crate::errors::{ParquetError, Result}; | ||
use crate::file::properties::WriterProperties; | ||
use crate::{ | ||
data_type::*, | ||
file::writer::{FileWriter, ParquetWriter, RowGroupWriter, SerializedFileWriter}, | ||
}; | ||
use crate::file::writer::{SerializedColumnWriter, SerializedRowGroupWriter}; | ||
use crate::{data_type::*, file::writer::SerializedFileWriter}; | ||
|
||
/// Arrow writer | ||
/// | ||
/// Writes Arrow `RecordBatch`es to a Parquet writer, buffering up `RecordBatch` in order | ||
/// to produce row groups with `max_row_group_size` rows. Any remaining rows will be | ||
/// flushed on close, leading the final row group in the output file to potentially | ||
/// contain fewer than `max_row_group_size` rows | ||
pub struct ArrowWriter<W: ParquetWriter> { | ||
pub struct ArrowWriter<W: Write> { | ||
/// Underlying Parquet writer | ||
writer: SerializedFileWriter<W>, | ||
|
||
|
@@ -65,7 +64,7 @@ pub struct ArrowWriter<W: ParquetWriter> { | |
max_row_group_size: usize, | ||
} | ||
|
||
impl<W: 'static + ParquetWriter> ArrowWriter<W> { | ||
impl<W: Write> ArrowWriter<W> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seemingly small change means you can pass in |
||
/// Try to create a new Arrow writer | ||
/// | ||
/// The writer will fail if: | ||
|
@@ -185,33 +184,35 @@ impl<W: 'static + ParquetWriter> ArrowWriter<W> { | |
}) | ||
.collect(); | ||
|
||
write_leaves(row_group_writer.as_mut(), &arrays, &mut levels)?; | ||
write_leaves(&mut row_group_writer, &arrays, &mut levels)?; | ||
} | ||
|
||
self.writer.close_row_group(row_group_writer)?; | ||
row_group_writer.close().unwrap(); | ||
tustvold marked this conversation as resolved.
Show resolved
Hide resolved
|
||
self.buffered_rows -= num_rows; | ||
|
||
Ok(()) | ||
} | ||
|
||
/// Close and finalize the underlying Parquet writer | ||
pub fn close(&mut self) -> Result<parquet_format::FileMetaData> { | ||
pub fn close(mut self) -> Result<parquet_format::FileMetaData> { | ||
self.flush()?; | ||
self.writer.close() | ||
} | ||
} | ||
|
||
/// Convenience method to get the next ColumnWriter from the RowGroupWriter | ||
#[inline] | ||
fn get_col_writer(row_group_writer: &mut dyn RowGroupWriter) -> Result<ColumnWriter> { | ||
fn get_col_writer<'a, W: Write>( | ||
row_group_writer: &'a mut SerializedRowGroupWriter<'_, W>, | ||
) -> Result<SerializedColumnWriter<'a>> { | ||
let col_writer = row_group_writer | ||
.next_column()? | ||
.expect("Unable to get column writer"); | ||
Ok(col_writer) | ||
} | ||
|
||
fn write_leaves( | ||
row_group_writer: &mut dyn RowGroupWriter, | ||
fn write_leaves<W: Write>( | ||
row_group_writer: &mut SerializedRowGroupWriter<'_, W>, | ||
arrays: &[ArrayRef], | ||
levels: &mut [Vec<LevelInfo>], | ||
) -> Result<()> { | ||
|
@@ -250,12 +251,12 @@ fn write_leaves( | |
let mut col_writer = get_col_writer(row_group_writer)?; | ||
for (array, levels) in arrays.iter().zip(levels.iter_mut()) { | ||
write_leaf( | ||
&mut col_writer, | ||
col_writer.untyped(), | ||
array, | ||
levels.pop().expect("Levels exhausted"), | ||
)?; | ||
} | ||
row_group_writer.close_column(col_writer)?; | ||
col_writer.close()?; | ||
Ok(()) | ||
} | ||
ArrowDataType::List(_) | ArrowDataType::LargeList(_) => { | ||
|
@@ -313,12 +314,12 @@ fn write_leaves( | |
// cast dictionary to a primitive | ||
let array = arrow::compute::cast(array, value_type)?; | ||
write_leaf( | ||
&mut col_writer, | ||
col_writer.untyped(), | ||
&array, | ||
levels.pop().expect("Levels exhausted"), | ||
)?; | ||
} | ||
row_group_writer.close_column(col_writer)?; | ||
col_writer.close()?; | ||
Ok(()) | ||
} | ||
ArrowDataType::Float16 => Err(ParquetError::ArrowError( | ||
|
@@ -336,8 +337,8 @@ fn write_leaves( | |
} | ||
|
||
fn write_leaf( | ||
writer: &mut ColumnWriter, | ||
column: &arrow_array::ArrayRef, | ||
writer: &mut ColumnWriter<'_>, | ||
column: &ArrayRef, | ||
levels: LevelInfo, | ||
) -> Result<i64> { | ||
let indices = levels.filter_array_indices(); | ||
|
@@ -705,7 +706,6 @@ mod tests { | |
use crate::file::{ | ||
reader::{FileReader, SerializedFileReader}, | ||
statistics::Statistics, | ||
writer::InMemoryWriteableCursor, | ||
}; | ||
|
||
#[test] | ||
|
@@ -744,16 +744,14 @@ mod tests { | |
let expected_batch = | ||
RecordBatch::try_new(schema.clone(), vec![Arc::new(a), Arc::new(b)]).unwrap(); | ||
|
||
let cursor = InMemoryWriteableCursor::default(); | ||
let mut buffer = vec![]; | ||
|
||
{ | ||
let mut writer = ArrowWriter::try_new(cursor.clone(), schema, None).unwrap(); | ||
let mut writer = ArrowWriter::try_new(&mut buffer, schema, None).unwrap(); | ||
writer.write(&expected_batch).unwrap(); | ||
writer.close().unwrap(); | ||
} | ||
|
||
let buffer = cursor.into_inner().unwrap(); | ||
|
||
let cursor = crate::file::serialized_reader::SliceableCursor::new(buffer); | ||
let reader = SerializedFileReader::new(cursor).unwrap(); | ||
let mut arrow_reader = ParquetFileArrowReader::new(Arc::new(reader)); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
here is a nice example of the new API in action: use something that does
std::io::Write