-
Notifications
You must be signed in to change notification settings - Fork 4k
ARROW-8289: [Rust] Implement Arrow writer for Parquet [DRAFT] #6785
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
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 hidden or 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,27 +18,32 @@ | |
| //! Contains file writer API, and provides methods to write row groups and columns by | ||
| //! using row group writers and column writers respectively. | ||
|
|
||
| use std::fs::File; | ||
| use std::{ | ||
| io::{Seek, SeekFrom, Write}, | ||
| rc::Rc, | ||
| }; | ||
|
|
||
| use arrow::array; | ||
| use arrow::datatypes::Schema; | ||
| use byteorder::{ByteOrder, LittleEndian}; | ||
| use parquet_format as parquet; | ||
| use thrift::protocol::{TCompactOutputProtocol, TOutputProtocol}; | ||
|
|
||
| use crate::basic::PageType; | ||
| use crate::basic::{PageType, Repetition, Type}; | ||
| use crate::column::{ | ||
| page::{CompressedPage, Page, PageWriteSpec, PageWriter}, | ||
| writer::{get_column_writer, ColumnWriter}, | ||
| }; | ||
| use crate::errors::{ParquetError, Result}; | ||
| use crate::file::properties::WriterProperties; | ||
| use crate::file::{ | ||
| metadata::*, properties::WriterPropertiesPtr, reader::TryClone, | ||
| statistics::to_thrift as statistics_to_thrift, FOOTER_SIZE, PARQUET_MAGIC, | ||
| }; | ||
| use crate::schema::types::{self, SchemaDescPtr, SchemaDescriptor, TypePtr}; | ||
| use crate::util::io::{FileSink, Position}; | ||
| use arrow::record_batch::RecordBatch; | ||
|
|
||
| // ---------------------------------------------------------------------- | ||
| // APIs for file & row group writers | ||
|
|
@@ -521,6 +526,75 @@ impl<T: Write + Position> PageWriter for SerializedPageWriter<T> { | |
| } | ||
| } | ||
|
|
||
| struct ArrowWriter { | ||
| writer: SerializedFileWriter<File>, | ||
| rows: i64, | ||
| } | ||
|
|
||
| impl ArrowWriter { | ||
| pub fn new(file: File, _arrow_schema: &Schema) -> Self { | ||
| //TODO convert Arrow schema to Parquet schema | ||
| let schema = Rc::new( | ||
| types::Type::group_type_builder("schema") | ||
| .with_fields(&mut vec![ | ||
| Rc::new( | ||
| types::Type::primitive_type_builder("a", Type::INT32) | ||
| .with_repetition(Repetition::REQUIRED) | ||
| .build() | ||
| .unwrap(), | ||
| ), | ||
| Rc::new( | ||
| types::Type::primitive_type_builder("b", Type::INT32) | ||
| .with_repetition(Repetition::REQUIRED) | ||
| .build() | ||
| .unwrap(), | ||
| ), | ||
| ]) | ||
| .build() | ||
| .unwrap(), | ||
| ); | ||
| let props = Rc::new(WriterProperties::builder().build()); | ||
| let file_writer = | ||
| SerializedFileWriter::new(file.try_clone().unwrap(), schema, props).unwrap(); | ||
|
|
||
| Self { | ||
| writer: file_writer, | ||
| rows: 0, | ||
| } | ||
| } | ||
|
|
||
| pub fn write(&mut self, batch: &RecordBatch) { | ||
| let mut row_group_writer = self.writer.next_row_group().unwrap(); | ||
| for i in 0..batch.schema().fields().len() { | ||
| let col_writer = row_group_writer.next_column().unwrap(); | ||
| if let Some(mut writer) = col_writer { | ||
| match writer { | ||
| ColumnWriter::Int32ColumnWriter(ref mut typed) => { | ||
| let array = batch | ||
| .column(i) | ||
| .as_any() | ||
| .downcast_ref::<array::Int32Array>() | ||
| .unwrap(); | ||
| self.rows += typed | ||
| .write_batch(array.value_slice(0, array.len()), None, None) | ||
| .unwrap() as i64; | ||
| } | ||
| //TODO add other types | ||
|
Member
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. A couple of initial things to keep in mind
|
||
| _ => { | ||
| unimplemented!(); | ||
| } | ||
| } | ||
| row_group_writer.close_column(writer).unwrap(); | ||
| } | ||
| } | ||
| self.writer.close_row_group(row_group_writer).unwrap(); | ||
| } | ||
|
|
||
| pub fn close(&mut self) { | ||
| self.writer.close().unwrap(); | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
@@ -538,6 +612,36 @@ mod tests { | |
| use crate::record::RowAccessor; | ||
| use crate::util::{memory::ByteBufferPtr, test_common::get_temp_file}; | ||
|
|
||
| use arrow::array::Int32Array; | ||
| use arrow::datatypes::{DataType, Field, Schema}; | ||
| use arrow::record_batch::RecordBatch; | ||
| use std::sync::Arc; | ||
|
|
||
| #[test] | ||
| fn arrow_writer() { | ||
| // define schema | ||
| let schema = Schema::new(vec![ | ||
| Field::new("a", DataType::Int32, false), | ||
| Field::new("b", DataType::Int32, false), | ||
| ]); | ||
|
|
||
| // create some data | ||
| let a = Int32Array::from(vec![1, 2, 3, 4, 5]); | ||
| let b = Int32Array::from(vec![1, 2, 3, 4, 5]); | ||
|
|
||
| // build a record batch | ||
| let batch = RecordBatch::try_new( | ||
| Arc::new(schema.clone()), | ||
| vec![Arc::new(a), Arc::new(b)], | ||
| ) | ||
| .unwrap(); | ||
|
|
||
| let file = File::create("test.parquet").unwrap(); | ||
| let mut writer = ArrowWriter::new(file, &schema); | ||
| writer.write(&batch); | ||
| writer.close(); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_file_writer_error_after_close() { | ||
| let file = get_temp_file("test_file_writer_error_after_close", &[]); | ||
|
|
||
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.
it is more appropriate to put this in
src/arrow/writer?