From 88abcd4197e207bf7919e1975fcfc4c8423fda55 Mon Sep 17 00:00:00 2001 From: alamb Date: Sat, 3 Oct 2020 06:44:11 -0400 Subject: [PATCH 1/3] ARROW-10169: [Rust] Pretty print null PrimitiveTypes as empty strings --- rust/arrow/src/util/pretty.rs | 39 +++++++++++++++++++++++------------ 1 file changed, 26 insertions(+), 13 deletions(-) diff --git a/rust/arrow/src/util/pretty.rs b/rust/arrow/src/util/pretty.rs index 089aa2f0251..3553ec9c2c7 100644 --- a/rust/arrow/src/util/pretty.rs +++ b/rust/arrow/src/util/pretty.rs @@ -71,12 +71,15 @@ fn create_table(results: &[RecordBatch]) -> Result { macro_rules! make_string { ($array_type:ty, $column: ident, $row: ident) => {{ - Ok($column - .as_any() - .downcast_ref::<$array_type>() - .unwrap() - .value($row) - .to_string()) + let array = $column.as_any().downcast_ref::<$array_type>().unwrap(); + + let s = if array.is_null($row) { + "".to_string() + } else { + array.value($row).to_string() + }; + + Ok(s) }}; } @@ -143,16 +146,26 @@ mod tests { fn test_pretty_format_batches() -> Result<()> { // define a schema. let schema = Arc::new(Schema::new(vec![ - Field::new("a", DataType::Utf8, false), - Field::new("b", DataType::Int32, false), + 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!["a", "b", "c", "d"])), - Arc::new(array::Int32Array::from(vec![1, 10, 10, 100])), + 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), + ])), ], )?; @@ -163,15 +176,15 @@ mod tests { "| a | b |", "+---+-----+", "| a | 1 |", - "| b | 10 |", - "| c | 10 |", + "| b | |", + "| | 10 |", "| d | 100 |", "+---+-----+", ]; let actual: Vec<&str> = table.lines().collect(); - assert_eq!(expected, actual); + assert_eq!(expected, actual, "Actual result:\n{}", table); Ok(()) } From a7b772e59abab124f57da5dc1c735d3d2a685927 Mon Sep 17 00:00:00 2001 From: alamb Date: Sat, 3 Oct 2020 06:46:31 -0400 Subject: [PATCH 2/3] Use same macro for Utf8 --- rust/arrow/src/util/pretty.rs | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/rust/arrow/src/util/pretty.rs b/rust/arrow/src/util/pretty.rs index 3553ec9c2c7..ec65a0f6700 100644 --- a/rust/arrow/src/util/pretty.rs +++ b/rust/arrow/src/util/pretty.rs @@ -86,12 +86,7 @@ macro_rules! make_string { /// Get the value at the given row in an array as a string fn array_value_to_string(column: array::ArrayRef, row: usize) -> Result { match column.data_type() { - DataType::Utf8 => Ok(column - .as_any() - .downcast_ref::() - .unwrap() - .value(row) - .to_string()), + DataType::Utf8 => make_string!(array::StringArray, column, row), DataType::Boolean => make_string!(array::BooleanArray, column, row), DataType::Int16 => make_string!(array::Int16Array, column, row), DataType::Int32 => make_string!(array::Int32Array, column, row), From 3cdbe64b7be949427872c9fe323fd9620428b66c Mon Sep 17 00:00:00 2001 From: alamb Date: Sat, 3 Oct 2020 07:10:11 -0400 Subject: [PATCH 3/3] fixup use --- rust/arrow/src/util/pretty.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rust/arrow/src/util/pretty.rs b/rust/arrow/src/util/pretty.rs index ec65a0f6700..dc564ddb4aa 100644 --- a/rust/arrow/src/util/pretty.rs +++ b/rust/arrow/src/util/pretty.rs @@ -18,7 +18,7 @@ //! Utilities for printing record batches use crate::array; -use crate::array::PrimitiveArrayOps; +use crate::array::{Array, PrimitiveArrayOps}; use crate::datatypes::{DataType, TimeUnit}; use crate::record_batch::RecordBatch;