You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by "alamb (via GitHub)" <gi...@apache.org> on 2023/02/14 13:55:07 UTC

[GitHub] [arrow-rs] alamb commented on a diff in pull request #3703: Add raw MapArrayReader

alamb commented on code in PR #3703:
URL: https://github.com/apache/arrow-rs/pull/3703#discussion_r1105850468


##########
arrow-json/src/raw/mod.rs:
##########
@@ -541,6 +544,41 @@ mod tests {
         assert!(c.is_null(1));
     }
 
+    #[test]
+    fn test_map() {
+        let buf = r#"
+           {"map": {"a": ["foo", null]}}
+           {"map": {"a": [null], "b": []}}
+           {"map": {"c": null, "a": ["baz"]}}
+        "#;
+        let list = DataType::List(Box::new(Field::new("element", DataType::Utf8, true)));
+        let entries = DataType::Struct(vec![
+            Field::new("key", DataType::Utf8, false),
+            Field::new("value", list, true),
+        ]);
+
+        let map = DataType::Map(Box::new(Field::new("entries", entries, true)), false);
+        let schema = Arc::new(Schema::new(vec![Field::new("map", map, true)]));
+
+        let batches = do_read(buf, 1024, schema);
+        assert_eq!(batches.len(), 1);
+
+        let map = as_map_array(batches[0].column(0).as_ref());
+        let map_keys = as_string_array(map.keys().as_ref());
+        let map_values = as_list_array(map.values().as_ref());
+        assert_eq!(map.value_offsets(), &[0, 1, 3, 5]);
+
+        let k: Vec<_> = map_keys.iter().map(|x| x.unwrap()).collect();
+        assert_eq!(&k, &["a", "a", "b", "c", "a"]);

Review Comment:
   👍 



##########
arrow-json/src/raw/mod.rs:
##########
@@ -541,6 +544,41 @@ mod tests {
         assert!(c.is_null(1));
     }
 
+    #[test]
+    fn test_map() {
+        let buf = r#"
+           {"map": {"a": ["foo", null]}}
+           {"map": {"a": [null], "b": []}}
+           {"map": {"c": null, "a": ["baz"]}}
+        "#;
+        let list = DataType::List(Box::new(Field::new("element", DataType::Utf8, true)));
+        let entries = DataType::Struct(vec![
+            Field::new("key", DataType::Utf8, false),
+            Field::new("value", list, true),
+        ]);
+
+        let map = DataType::Map(Box::new(Field::new("entries", entries, true)), false);
+        let schema = Arc::new(Schema::new(vec![Field::new("map", map, true)]));
+
+        let batches = do_read(buf, 1024, schema);
+        assert_eq!(batches.len(), 1);
+
+        let map = as_map_array(batches[0].column(0).as_ref());
+        let map_keys = as_string_array(map.keys().as_ref());
+        let map_values = as_list_array(map.values().as_ref());
+        assert_eq!(map.value_offsets(), &[0, 1, 3, 5]);
+
+        let k: Vec<_> = map_keys.iter().map(|x| x.unwrap()).collect();
+        assert_eq!(&k, &["a", "a", "b", "c", "a"]);
+
+        let list_values = as_string_array(map_values.values().as_ref());
+        let lv: Vec<_> = list_values.iter().collect();
+        assert_eq!(&lv, &[Some("foo"), None, None, Some("baz")]);
+        assert_eq!(map_values.value_offsets(), &[0, 2, 3, 3, 3, 4]);
+        assert_eq!(map_values.null_count(), 1);
+        assert!(map_values.is_null(3))

Review Comment:
   I wonder if we can use our fancy new arrow display writer here to write out the contents of the arrays as well 🤔 



-- 
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.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

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