From 74591a5c96042e5d6dde7e015decb7358bfc33a0 Mon Sep 17 00:00:00 2001 From: Francis Le Roy <36069799+GrandChaman@users.noreply.github.com> Date: Wed, 29 Dec 2021 20:50:40 +0100 Subject: [PATCH] Print the 'FixedSizeBinaryArray' like a normal 'BinaryArray' (#1097) * Print the 'FixedBinaryArray' like a normal 'BinaryArray' * apply cargo fmt Co-authored-by: Andrew Lamb --- arrow/src/util/display.rs | 3 +++ arrow/src/util/pretty.rs | 42 +++++++++++++++++++++++++++++++++++---- 2 files changed, 41 insertions(+), 4 deletions(-) diff --git a/arrow/src/util/display.rs b/arrow/src/util/display.rs index e427dcca91be..91f64dbf3317 100644 --- a/arrow/src/util/display.rs +++ b/arrow/src/util/display.rs @@ -301,6 +301,9 @@ pub fn array_value_to_string(column: &array::ArrayRef, row: usize) -> Result make_string!(array::LargeStringArray, column, row), DataType::Binary => make_string_hex!(array::BinaryArray, column, row), DataType::LargeBinary => make_string_hex!(array::LargeBinaryArray, column, row), + DataType::FixedSizeBinary(_) => { + make_string_hex!(array::FixedSizeBinaryArray, column, row) + } DataType::Boolean => make_string!(array::BooleanArray, column, row), DataType::Int8 => make_string!(array::Int8Array, column, row), DataType::Int16 => make_string!(array::Int16Array, column, row), diff --git a/arrow/src/util/pretty.rs b/arrow/src/util/pretty.rs index ac7caa8ac39a..44fe05034851 100644 --- a/arrow/src/util/pretty.rs +++ b/arrow/src/util/pretty.rs @@ -108,10 +108,11 @@ fn create_column(field: &str, columns: &[ArrayRef]) -> Result { mod tests { use crate::{ array::{ - self, new_null_array, Array, Date32Array, Date64Array, PrimitiveBuilder, - StringArray, StringBuilder, StringDictionaryBuilder, StructArray, - Time32MillisecondArray, Time32SecondArray, Time64MicrosecondArray, - Time64NanosecondArray, TimestampMicrosecondArray, TimestampMillisecondArray, + self, new_null_array, Array, Date32Array, Date64Array, + FixedSizeBinaryBuilder, PrimitiveBuilder, StringArray, StringBuilder, + StringDictionaryBuilder, StructArray, Time32MillisecondArray, + Time32SecondArray, Time64MicrosecondArray, Time64NanosecondArray, + TimestampMicrosecondArray, TimestampMillisecondArray, TimestampNanosecondArray, TimestampSecondArray, }, datatypes::{DataType, Field, Int32Type, Schema}, @@ -308,6 +309,39 @@ mod tests { Ok(()) } + #[test] + fn test_pretty_format_fixed_size_binary() -> Result<()> { + // define a schema. + let field_type = DataType::FixedSizeBinary(3); + let schema = Arc::new(Schema::new(vec![Field::new("d1", field_type, true)])); + + let mut builder = FixedSizeBinaryBuilder::new(3, 3); + + builder.append_value(&[1, 2, 3]).unwrap(); + builder.append_null(); + builder.append_value(&[7, 8, 9]).unwrap(); + + let array = Arc::new(builder.finish()); + + let batch = RecordBatch::try_new(schema, vec![array])?; + let table = pretty_format_batches(&[batch])?.to_string(); + let expected = vec![ + "+--------+", + "| d1 |", + "+--------+", + "| 010203 |", + "| |", + "| 070809 |", + "+--------+", + ]; + + let actual: Vec<&str> = table.lines().collect(); + + assert_eq!(expected, actual, "Actual result:\n{}", table); + + Ok(()) + } + /// Generate an array with type $ARRAYTYPE with a numeric value of /// $VALUE, and compare $EXPECTED_RESULT to the output of /// formatting that array with `pretty_format_batches`