You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by GitBox <gi...@apache.org> on 2021/05/11 10:52:53 UTC

[GitHub] [arrow-datafusion] alamb commented on a change in pull request #295: Add json print format mode to datafusion cli

alamb commented on a change in pull request #295:
URL: https://github.com/apache/arrow-datafusion/pull/295#discussion_r630059838



##########
File path: datafusion-cli/src/format/print_format.rs
##########
@@ -65,7 +80,89 @@ impl PrintFormat {
             Self::Csv => println!("{}", print_batches_with_sep(batches, b',')?),
             Self::Tsv => println!("{}", print_batches_with_sep(batches, b'\t')?),
             Self::Table => pretty::print_batches(batches)?,
+            Self::Json => println!("{}", print_batches_to_json(batches)?),
         }
         Ok(())
     }
 }
+
+#[cfg(test)]
+mod tests {
+    use super::*;
+    use arrow::array::Int32Array;
+    use arrow::datatypes::{DataType, Field, Schema};
+    use std::sync::Arc;
+
+    #[test]
+    fn test_from_str() {
+        let format = "csv".parse::<PrintFormat>().unwrap();
+        assert_eq!(PrintFormat::Csv, format);
+
+        let format = "tsv".parse::<PrintFormat>().unwrap();
+        assert_eq!(PrintFormat::Tsv, format);
+
+        let format = "json".parse::<PrintFormat>().unwrap();
+        assert_eq!(PrintFormat::Json, format);
+
+        let format = "table".parse::<PrintFormat>().unwrap();
+        assert_eq!(PrintFormat::Table, format);
+    }
+
+    #[test]
+    fn test_from_str_failure() {
+        assert_eq!(true, "pretty".parse::<PrintFormat>().is_err());
+    }
+
+    #[test]
+    fn test_print_batches_with_sep() {
+        let batches = vec![];
+        assert_eq!("", print_batches_with_sep(&batches, b',').unwrap());
+
+        let schema = Arc::new(Schema::new(vec![
+            Field::new("a", DataType::Int32, false),
+            Field::new("b", DataType::Int32, false),
+            Field::new("c", DataType::Int32, false),
+        ]));
+
+        let batch = RecordBatch::try_new(
+            schema,
+            vec![
+                Arc::new(Int32Array::from(vec![1, 2, 3])),
+                Arc::new(Int32Array::from(vec![4, 5, 6])),
+                Arc::new(Int32Array::from(vec![7, 8, 9])),
+            ],
+        )
+        .unwrap();
+
+        let batches = vec![batch];
+        let r = print_batches_with_sep(&batches, b',').unwrap();
+        assert_eq!("a,b,c\n1,4,7\n2,5,8\n3,6,9\n", r);
+    }
+
+    #[test]
+    fn test_print_batches_to_json_empty() {
+        let batches = vec![];
+        let r = print_batches_to_json(&batches).unwrap();
+        assert_eq!("", r);
+
+        let schema = Arc::new(Schema::new(vec![
+            Field::new("a", DataType::Int32, false),
+            Field::new("b", DataType::Int32, false),
+            Field::new("c", DataType::Int32, false),
+        ]));
+
+        let batch = RecordBatch::try_new(
+            schema,
+            vec![
+                Arc::new(Int32Array::from(vec![1, 2, 3])),
+                Arc::new(Int32Array::from(vec![4, 5, 6])),
+                Arc::new(Int32Array::from(vec![7, 8, 9])),
+            ],
+        )
+        .unwrap();
+
+        let batches = vec![batch];
+        let r = print_batches_to_json(&batches).unwrap();
+        assert_eq!("[{\"a\":1,\"b\":4,\"c\":7},{\"a\":2,\"b\":5,\"c\":8},{\"a\":3,\"b\":6,\"c\":9}]", r);

Review comment:
       💯  for the tests




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org