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

Change pretty_format_batches to return Result<impl Display> #975

Merged
merged 5 commits into from
Dec 1, 2021
Merged
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
79 changes: 66 additions & 13 deletions arrow/src/util/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
//! available unless `feature = "prettyprint"` is enabled.

use crate::{array::ArrayRef, record_batch::RecordBatch};
use std::fmt::Display;

use comfy_table::{Cell, Table};

Expand All @@ -27,13 +28,16 @@ use crate::error::Result;
use super::display::array_value_to_string;

///! Create a visual representation of record batches
pub fn pretty_format_batches(results: &[RecordBatch]) -> Result<String> {
Ok(create_table(results)?.to_string())
pub fn pretty_format_batches(results: &[RecordBatch]) -> Result<impl Display> {
create_table(results)
}

///! Create a visual representation of columns
pub fn pretty_format_columns(col_name: &str, results: &[ArrayRef]) -> Result<String> {
Ok(create_column(col_name, results)?.to_string())
pub fn pretty_format_columns(
col_name: &str,
results: &[ArrayRef],
) -> Result<impl Display> {
create_column(col_name, results)
}

///! Prints a visual representation of record batches to stdout
Expand Down Expand Up @@ -115,6 +119,7 @@ mod tests {

use super::*;
use crate::array::{DecimalBuilder, FixedSizeListBuilder, Int32Array};
use std::fmt::Write;
use std::sync::Arc;

#[test]
Expand Down Expand Up @@ -144,7 +149,7 @@ mod tests {
],
)?;

let table = pretty_format_batches(&[batch])?;
let table = pretty_format_batches(&[batch])?.to_string();

let expected = vec![
"+---+-----+",
Expand Down Expand Up @@ -176,7 +181,7 @@ mod tests {
Arc::new(array::StringArray::from(vec![Some("e"), None, Some("g")])),
];

let table = pretty_format_columns("a", &columns)?;
let table = pretty_format_columns("a", &columns)?.to_string();

let expected = vec![
"+---+", "| a |", "+---+", "| a |", "| b |", "| |", "| d |", "| e |",
Expand Down Expand Up @@ -208,7 +213,7 @@ mod tests {
// define data (null)
let batch = RecordBatch::try_new(schema, arrays).unwrap();

let table = pretty_format_batches(&[batch]).unwrap();
let table = pretty_format_batches(&[batch]).unwrap().to_string();

let expected = vec![
"+---+---+---+",
Expand Down Expand Up @@ -244,7 +249,7 @@ mod tests {

let batch = RecordBatch::try_new(schema, vec![array])?;

let table = pretty_format_batches(&[batch])?;
let table = pretty_format_batches(&[batch])?.to_string();

let expected = vec![
"+-------+",
Expand Down Expand Up @@ -285,7 +290,7 @@ mod tests {
let array = Arc::new(builder.finish());

let batch = RecordBatch::try_new(schema, vec![array])?;
let table = pretty_format_batches(&[batch])?;
let table = pretty_format_batches(&[batch])?.to_string();
let expected = vec![
"+-----------+",
"| d1 |",
Expand Down Expand Up @@ -320,7 +325,9 @@ mod tests {
)]));
let batch = RecordBatch::try_new(schema, vec![Arc::new(array)]).unwrap();

let table = pretty_format_batches(&[batch]).expect("formatting batches");
let table = pretty_format_batches(&[batch])
.expect("formatting batches")
.to_string();

let expected = $EXPECTED_RESULT;
let actual: Vec<&str> = table.lines().collect();
Expand Down Expand Up @@ -494,7 +501,7 @@ mod tests {

let batch = RecordBatch::try_new(schema, vec![dm])?;

let table = pretty_format_batches(&[batch])?;
let table = pretty_format_batches(&[batch])?.to_string();

let expected = vec![
"+-------+",
Expand Down Expand Up @@ -535,7 +542,7 @@ mod tests {

let batch = RecordBatch::try_new(schema, vec![dm])?;

let table = pretty_format_batches(&[batch])?;
let table = pretty_format_batches(&[batch])?.to_string();
let expected = vec![
"+------+", "| f |", "+------+", "| 101 |", "| |", "| 200 |",
"| 3040 |", "+------+",
Expand Down Expand Up @@ -589,7 +596,7 @@ mod tests {
RecordBatch::try_new(Arc::new(schema), vec![Arc::new(c1), Arc::new(c2)])
.unwrap();

let table = pretty_format_batches(&[batch])?;
let table = pretty_format_batches(&[batch])?.to_string();
let expected = vec![
r#"+-------------------------------------+----+"#,
r#"| c1 | c2 |"#,
Expand All @@ -605,4 +612,50 @@ mod tests {

Ok(())
}

#[test]
fn test_writing_formatted_batches() -> Result<()> {
// define a schema.
let schema = Arc::new(Schema::new(vec![
Field::new("a", DataType::Utf8, true),
Field::new("b", DataType::Int32, true),
]));

// define data.
let batch = RecordBatch::try_new(
schema,
vec![
Arc::new(array::StringArray::from(vec![
Some("a"),
Some("b"),
None,
Some("d"),
])),
Arc::new(array::Int32Array::from(vec![
Some(1),
None,
Some(10),
Some(100),
])),
],
)?;

let mut buf = String::new();
write!(&mut buf, "{}", pretty_format_batches(&[batch])?.to_string()).unwrap();

let s = vec![
"+---+-----+",
"| a | b |",
"+---+-----+",
"| a | 1 |",
"| b | |",
"| | 10 |",
"| d | 100 |",
"+---+-----+",
];
let expected = String::from(s.join("\n"));
assert_eq!(expected, buf);

Ok(())
}
}