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 Debug impls for ArrowWriter and SerializedFileWriter #4278

Merged
merged 2 commits into from
May 25, 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
25 changes: 25 additions & 0 deletions parquet/src/arrow/arrow_writer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
//! Contains writer which writes arrow data into parquet data.

use std::collections::VecDeque;
use std::fmt::Debug;
use std::io::Write;
use std::sync::Arc;

Expand Down Expand Up @@ -92,6 +93,30 @@ pub struct ArrowWriter<W: Write> {
max_row_group_size: usize,
}

impl<W: Write> Debug for ArrowWriter<W> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let buffered_batches = self.buffer.len();
let mut buffered_memory = 0;

for batch in self.buffer.iter() {
for arr in batch.iter() {
buffered_memory += arr.get_array_memory_size()
}
}

f.debug_struct("ArrowWriter")
.field("writer", &self.writer)
.field(
"buffer",
&format!("{buffered_batches} , {buffered_memory} bytes"),
)
.field("buffered_rows", &self.buffered_rows)
.field("arrow_schema", &self.arrow_schema)
.field("max_row_group_size", &self.max_row_group_size)
.finish()
}
}

impl<W: Write> ArrowWriter<W> {
/// Try to create a new Arrow writer
///
Expand Down
13 changes: 13 additions & 0 deletions parquet/src/file/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use crate::bloom_filter::Sbbf;
use crate::format as parquet;
use crate::format::{ColumnIndex, OffsetIndex, RowGroup};
use std::fmt::Debug;
use std::io::{BufWriter, IoSlice, Read};
use std::{io::Write, sync::Arc};
use thrift::protocol::{TCompactOutputProtocol, TSerializable};
Expand Down Expand Up @@ -147,6 +148,18 @@ pub struct SerializedFileWriter<W: Write> {
kv_metadatas: Vec<KeyValue>,
}

impl<W: Write> Debug for SerializedFileWriter<W> {
Copy link
Contributor

Choose a reason for hiding this comment

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

FWIW #[derive(Debug)] on TrackedWrite would allow similar here, without needing a manual impl. SerializedFileWriter doesn't contain any non-public types on it

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I tried to do this with

diff --git a/parquet/src/file/writer.rs b/parquet/src/file/writer.rs
index 93e8319b0..3ab50fb4b 100644
--- a/parquet/src/file/writer.rs
+++ b/parquet/src/file/writer.rs
@@ -48,6 +48,7 @@ use crate::schema::types::{
 /// A wrapper around a [`Write`] that keeps track of the number
 /// of bytes that have been written. The given [`Write`] is wrapped
 /// with a [`BufWriter`] to optimize writing performance.
+#[derive(Debug)]
 pub struct TrackedWrite<W: Write> {
     inner: BufWriter<W>,
     bytes_written: usize,
@@ -134,6 +135,7 @@ pub type OnCloseRowGroup<'a> = Box<
 /// - Once finished writing row group, close row group writer by calling `close`
 /// - Write subsequent row groups, if necessary.
 /// - After all row groups have been written, close the file writer using `close` method.
+#[derive(Debug)]
 pub struct SerializedFileWriter<W: Write> {
     buf: TrackedWrite<W>,
     schema: TypePtr,
@@ -148,17 +150,6 @@ pub struct SerializedFileWriter<W: Write> {
     kv_metadatas: Vec<KeyValue>,
 }
 
-impl<W: Write> Debug for SerializedFileWriter<W> {
-    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
-        // implement Debug so this can be used with #[derive(Debug)]
-        // in client code rather than actually listing all the fields
-        f.debug_struct("SerializedFileWriter")
-            .field("descr", &self.descr)
-            .field("row_group_index", &self.row_group_index)
-            .field("kv_metadatas", &self.kv_metadatas)
-            .finish_non_exhaustive()
-    }
-}
 
 impl<W: Write> SerializedFileWriter<W> {
     /// Creates new file writer.

However, the compiler is complains like this ( I think it is saying I need to restrict the bound on SerializedFileWriter which I would prefer not to do as it would be backwards incompatible)

    Checking parquet v40.0.0 (/Users/alamb/Software/arrow-rs/parquet)
error[E0277]: `W` doesn't implement `std::fmt::Debug`
   --> parquet/src/arrow/arrow_writer/mod.rs:108:30
    |
108 |             .field("writer", &self.writer)
    |                              ^^^^^^^^^^^^ `W` cannot be formatted using `{:?}` because it doesn't implement `std::fmt::Debug`
    |
note: required for `file::writer::SerializedFileWriter<W>` to implement `std::fmt::Debug`
   --> parquet/src/file/writer.rs:138:10
    |
138 | #[derive(Debug)]
    |          ^^^^^ unsatisfied trait bound introduced in this `derive` macro
    = note: required for the cast from `file::writer::SerializedFileWriter<W>` to the object type `dyn std::fmt::Debug`
    = note: this error originates in the derive macro `Debug` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider further restricting this bound
    |
96  | impl<W: Write + std::fmt::Debug> Debug for ArrowWriter<W> {
    |               +++++++++++++++++

Copy link
Contributor

Choose a reason for hiding this comment

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

Aah, yeah makes sense

fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
// implement Debug so this can be used with #[derive(Debug)]
// in client code rather than actually listing all the fields
f.debug_struct("SerializedFileWriter")
.field("descr", &self.descr)
.field("row_group_index", &self.row_group_index)
.field("kv_metadatas", &self.kv_metadatas)
.finish_non_exhaustive()
}
}

impl<W: Write> SerializedFileWriter<W> {
/// Creates new file writer.
pub fn new(buf: W, schema: TypePtr, properties: WriterPropertiesPtr) -> Result<Self> {
Expand Down