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

Add close method to RecordBatchWriter trait #4228

Merged
merged 4 commits into from
May 17, 2023
Merged
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
3 changes: 3 additions & 0 deletions arrow-array/src/record_batch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ pub trait RecordBatchReader: Iterator<Item = Result<RecordBatch, ArrowError>> {
pub trait RecordBatchWriter {
/// Write a single batch to the writer.
fn write(&mut self, batch: &RecordBatch) -> Result<(), ArrowError>;

/// Write footer or termination data, then mark the writer as done.
fn close(self) -> Result<(), ArrowError>;
}

/// A two-dimensional batch of column-oriented data with a defined
Expand Down
4 changes: 4 additions & 0 deletions arrow-csv/src/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,10 @@ impl<W: Write> RecordBatchWriter for Writer<W> {
fn write(&mut self, batch: &RecordBatch) -> Result<(), ArrowError> {
self.write(batch)
}

fn close(self) -> Result<(), ArrowError> {
Ok(())
}
}

/// A CSV writer builder
Expand Down
8 changes: 8 additions & 0 deletions arrow-ipc/src/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -861,6 +861,10 @@ impl<W: Write> RecordBatchWriter for FileWriter<W> {
fn write(&mut self, batch: &RecordBatch) -> Result<(), ArrowError> {
self.write(batch)
}

fn close(mut self) -> Result<(), ArrowError> {
self.finish()
}
}

pub struct StreamWriter<W: Write> {
Expand Down Expand Up @@ -1001,6 +1005,10 @@ impl<W: Write> RecordBatchWriter for StreamWriter<W> {
fn write(&mut self, batch: &RecordBatch) -> Result<(), ArrowError> {
self.write(batch)
}

fn close(mut self) -> Result<(), ArrowError> {
self.finish()
}
}

/// Stores the encoded data, which is an crate::Message, and optional Arrow data
Expand Down
5 changes: 5 additions & 0 deletions arrow-json/src/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,10 @@ where
fn write(&mut self, batch: &RecordBatch) -> Result<(), ArrowError> {
self.write(batch)
}

fn close(mut self) -> Result<(), ArrowError> {
self.finish()
}
}

#[cfg(test)]
Expand Down Expand Up @@ -1265,6 +1269,7 @@ mod tests {
writer.finish().unwrap();
assert_eq!(String::from_utf8(writer.into_inner()).unwrap(), "");
}

#[test]
fn json_writer_one_row() {
let mut writer = ArrayWriter::new(vec![] as Vec<u8>);
Expand Down
5 changes: 5 additions & 0 deletions parquet/src/arrow/arrow_writer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,11 @@ impl<W: Write> RecordBatchWriter for ArrowWriter<W> {
fn write(&mut self, batch: &RecordBatch) -> Result<(), ArrowError> {
self.write(batch).map_err(|e| e.into())
}

fn close(self) -> std::result::Result<(), ArrowError> {
self.close()?;
Ok(())
}
}

fn write_leaves<W: Write>(
Expand Down