From e1ac2bb8f75f302bdcd1e8214b0909a5be745f32 Mon Sep 17 00:00:00 2001 From: Xinyu Zeng <30414224+XinyuZeng@users.noreply.github.com> Date: Wed, 6 Nov 2024 17:57:29 +0800 Subject: [PATCH] Let std::fmt::Debug for StructArray output Null/Validity info (#6655) * add validity fmt for StrutArray * better * truncate * better --- arrow-array/src/array/struct_array.rs | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/arrow-array/src/array/struct_array.rs b/arrow-array/src/array/struct_array.rs index 059bc0b5e65b..d8bb1ace7a8c 100644 --- a/arrow-array/src/array/struct_array.rs +++ b/arrow-array/src/array/struct_array.rs @@ -15,6 +15,7 @@ // specific language governing permissions and limitations // under the License. +use crate::array::print_long_array; use crate::{make_array, new_null_array, Array, ArrayRef, RecordBatch}; use arrow_buffer::{BooleanBuffer, Buffer, NullBuffer}; use arrow_data::{ArrayData, ArrayDataBuilder}; @@ -404,7 +405,11 @@ impl From> for StructArray { impl std::fmt::Debug for StructArray { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - write!(f, "StructArray\n[\n")?; + writeln!(f, "StructArray")?; + writeln!(f, "-- validity: ")?; + writeln!(f, "[")?; + print_long_array(self, f, |_array, _index, f| write!(f, "valid"))?; + writeln!(f, "]\n[")?; for (child_index, name) in self.column_names().iter().enumerate() { let column = self.column(child_index); writeln!( @@ -731,4 +736,16 @@ mod tests { Arc::new(Int32Array::from(vec![Some(42), None, Some(19)])) as ArrayRef, )])); } + + #[test] + fn test_struct_array_fmt_debug() { + let arr: StructArray = StructArray::new( + vec![Arc::new(Field::new("c", DataType::Int32, true))].into(), + vec![Arc::new(Int32Array::from((0..30).collect::>())) as ArrayRef], + Some(NullBuffer::new(BooleanBuffer::from( + (0..30).map(|i| i % 2 == 0).collect::>(), + ))), + ); + assert_eq!(format!("{arr:?}"), "StructArray\n-- validity: \n[\n valid,\n null,\n valid,\n null,\n valid,\n null,\n valid,\n null,\n valid,\n null,\n ...10 elements...,\n valid,\n null,\n valid,\n null,\n valid,\n null,\n valid,\n null,\n valid,\n null,\n]\n[\n-- child 0: \"c\" (Int32)\nPrimitiveArray\n[\n 0,\n 1,\n 2,\n 3,\n 4,\n 5,\n 6,\n 7,\n 8,\n 9,\n ...10 elements...,\n 20,\n 21,\n 22,\n 23,\n 24,\n 25,\n 26,\n 27,\n 28,\n 29,\n]\n]") + } }