This is an automated email from the ASF dual-hosted git repository.

kszucs pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/arrow.git


The following commit(s) were added to refs/heads/master by this push:
     new cfbe0ce  ARROW-4377: [Rust] Implement std::fmt::Debug for 
PrimitiveArrays
cfbe0ce is described below

commit cfbe0ce937c8587739dffea8345ec3c6afd6d032
Author: Nicolas Trinquier <[email protected]>
AuthorDate: Sun Feb 17 13:18:49 2019 +0100

    ARROW-4377: [Rust] Implement std::fmt::Debug for PrimitiveArrays
    
    Author: Nicolas Trinquier <[email protected]>
    
    Closes #3663 from ntrinquier/arrow-4377 and squashes the following commits:
    
    0dbf90cd <Nicolas Trinquier> Propagate Err
    bceddd01 <Nicolas Trinquier> Display arrays vertically
    c0e7d551 <Nicolas Trinquier> Handle null case
    802501ad <Nicolas Trinquier> :xImplement Debug for PrimitiveArrays
---
 rust/arrow/src/array.rs     | 75 +++++++++++++++++++++++++++++++++++++++++++++
 rust/arrow/src/datatypes.rs |  5 ++-
 2 files changed, 79 insertions(+), 1 deletion(-)

diff --git a/rust/arrow/src/array.rs b/rust/arrow/src/array.rs
index c887ab7..dc0a509 100644
--- a/rust/arrow/src/array.rs
+++ b/rust/arrow/src/array.rs
@@ -56,6 +56,7 @@
 
 use std::any::Any;
 use std::convert::From;
+use std::fmt;
 use std::io::Write;
 use std::mem;
 use std::sync::Arc;
@@ -248,6 +249,20 @@ impl<T: ArrowNumericType> PrimitiveArray<T> {
     }
 }
 
+impl<T: ArrowNumericType> fmt::Debug for PrimitiveArray<T> {
+    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+        write!(f, "PrimitiveArray<{:?}>\n[\n", T::get_data_type())?;
+        for i in 0..self.len() {
+            if self.is_null(i) {
+                write!(f, "  null,\n")?;
+            } else {
+                write!(f, "  {:?},\n", self.value(i))?;
+            }
+        }
+        write!(f, "]")
+    }
+}
+
 /// Specific implementation for Boolean arrays due to bit-packing
 impl PrimitiveArray<BooleanType> {
     pub fn new(length: usize, values: Buffer, null_count: usize, offset: 
usize) -> Self {
@@ -280,6 +295,20 @@ impl PrimitiveArray<BooleanType> {
     }
 }
 
+impl fmt::Debug for PrimitiveArray<BooleanType> {
+    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+        write!(f, "PrimitiveArray<{:?}>\n[\n", BooleanType::get_data_type())?;
+        for i in 0..self.len() {
+            if self.is_null(i) {
+                write!(f, "  null,\n")?
+            } else {
+                write!(f, "  {:?},\n", self.value(i))?
+            }
+        }
+        write!(f, "]")
+    }
+}
+
 // TODO: the macro is needed here because we'd get "conflicting 
implementations" error
 // otherwise with both `From<Vec<T::Native>>` and 
`From<Vec<Option<T::Native>>>`.
 // We should revisit this in future.
@@ -762,6 +791,52 @@ mod tests {
     }
 
     #[test]
+    fn test_int32_fmt_debug() {
+        let buf = Buffer::from(&[0, 1, 2, 3, 4].to_byte_slice());
+        let arr = Int32Array::new(5, buf, 0, 0);
+        assert_eq!(
+            "PrimitiveArray<Int32>\n[\n  0,\n  1,\n  2,\n  3,\n  4,\n]",
+            format!("{:?}", arr)
+        );
+    }
+
+    #[test]
+    fn test_int32_with_null_fmt_debug() {
+        let mut builder = Int32Array::builder(3);
+        builder.append_slice(&[0, 1]).unwrap();
+        builder.append_null().unwrap();
+        builder.append_slice(&[3, 4]).unwrap();
+        let arr = builder.finish();
+        assert_eq!(
+            "PrimitiveArray<Int32>\n[\n  0,\n  1,\n  null,\n  3,\n  4,\n]",
+            format!("{:?}", arr)
+        );
+    }
+
+    #[test]
+    fn test_boolean_fmt_debug() {
+        let buf = Buffer::from(&[true, false, false].to_byte_slice());
+        let arr = BooleanArray::new(3, buf, 0, 0);
+        assert_eq!(
+            "PrimitiveArray<Boolean>\n[\n  true,\n  false,\n  false,\n]",
+            format!("{:?}", arr)
+        );
+    }
+
+    #[test]
+    fn test_boolean_with_null_fmt_debug() {
+        let mut builder = BooleanArray::builder(3);
+        builder.append_value(true).unwrap();
+        builder.append_null().unwrap();
+        builder.append_value(false).unwrap();
+        let arr = builder.finish();
+        assert_eq!(
+            "PrimitiveArray<Boolean>\n[\n  true,\n  null,\n  false,\n]",
+            format!("{:?}", arr)
+        );
+    }
+
+    #[test]
     fn test_primitive_array_builder() {
         // Test building an primitive array with ArrayData builder and offset
         let buf = Buffer::from(&[0, 1, 2, 3, 4].to_byte_slice());
diff --git a/rust/arrow/src/datatypes.rs b/rust/arrow/src/datatypes.rs
index 36f7341..4dc55d6 100644
--- a/rust/arrow/src/datatypes.rs
+++ b/rust/arrow/src/datatypes.rs
@@ -96,7 +96,10 @@ pub struct Field {
     nullable: bool,
 }
 
-pub trait ArrowNativeType: Send + Sync + Copy + PartialOrd + FromStr + 'static 
{}
+pub trait ArrowNativeType:
+    fmt::Debug + Send + Sync + Copy + PartialOrd + FromStr + 'static
+{
+}
 
 /// Trait indicating a primitive fixed-width type (bool, ints and floats).
 pub trait ArrowPrimitiveType: 'static {

Reply via email to