Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 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: 2 additions & 1 deletion rust/parquet/src/file/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ use crate::file::{
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, TryClone};
use crate::util::io::{FileSink, Position};

// ----------------------------------------------------------------------
// APIs for file & row group writers
Expand Down Expand Up @@ -114,6 +114,7 @@ pub trait RowGroupWriter {
// ----------------------------------------------------------------------
// Serialized impl for file & row group writers

pub use crate::util::io::TryClone;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'd prefer if the import is still close to line 41. Here it's hidden, especially as a pub use

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

moved!

pub trait ParquetWriter: Write + Seek + TryClone {}
impl<T: Write + Seek + TryClone> ParquetWriter for T {}

Expand Down
83 changes: 83 additions & 0 deletions rust/parquet/tests/custom_writer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
use std::fs::File;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

nit: license header

use std::{
fs,
io::{prelude::*, SeekFrom},
rc::Rc,
};

use parquet::file::writer::TryClone;
use parquet::{
basic::Repetition, basic::Type, file::properties::WriterProperties,
file::writer::SerializedFileWriter, schema::types,
};
use std::env;

// Test creating some sort of custom writer to ensure the
// appropriate traits are exposed
struct CustomWriter {
file: File,
}

impl Write for CustomWriter {
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
self.file.write(buf)
}
fn flush(&mut self) -> std::io::Result<()> {
self.file.flush()
}
}

impl Seek for CustomWriter {
fn seek(&mut self, pos: SeekFrom) -> std::io::Result<u64> {
self.file.seek(pos)
}
}

impl TryClone for CustomWriter {
fn try_clone(&self) -> std::io::Result<Self> {
use std::io::{Error, ErrorKind};
Err(Error::new(ErrorKind::Other, "Clone not supported"))
}
}

#[test]
fn test_custom_writer() {
let schema = Rc::new(
types::Type::group_type_builder("schema")
.with_fields(&mut vec![Rc::new(
types::Type::primitive_type_builder("col1", Type::INT32)
.with_repetition(Repetition::REQUIRED)
.build()
.unwrap(),
)])
.build()
.unwrap(),
);
let props = Rc::new(WriterProperties::builder().build());

let file = get_temp_file("test_custom_file_writer");
let test_file = file.try_clone().unwrap();

let writer = CustomWriter { file };

// test is that this file can be created
let file_writer = SerializedFileWriter::new(writer, schema, props).unwrap();
std::mem::drop(file_writer);

// ensure the file now exists and has non zero size
let metadata = test_file.metadata().unwrap();
assert!(metadata.len() > 0);
}

/// Returns file handle for a temp file in 'target' directory with a provided content
fn get_temp_file(file_name: &str) -> fs::File {
// build tmp path to a file in "target/debug/testdata"
let mut path_buf = env::current_dir().unwrap();
path_buf.push("target");
path_buf.push("debug");
path_buf.push("testdata");
fs::create_dir_all(&path_buf).unwrap();
path_buf.push(file_name);

File::create(path_buf).unwrap()
}