You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@arrow.apache.org by ag...@apache.org on 2018/11/28 12:09:23 UTC

[arrow] branch master updated: ARROW-3855: [Rust] Schema/Field/Datatype now have derived serde traits

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

agrove 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 0066af8  ARROW-3855: [Rust] Schema/Field/Datatype now have derived serde traits
0066af8 is described below

commit 0066af8e953daa6665869e3ed392a1c7a8a777c9
Author: Andy Grove <an...@gmail.com>
AuthorDate: Wed Nov 28 05:09:13 2018 -0700

    ARROW-3855: [Rust] Schema/Field/Datatype now have derived serde traits
    
    This PR makes `Schema`, `Field`, `DataType` serializable using the serde crate. This approach supports serialization to numerous binary and text formats supported by the serde crate.
    
    The main benefit is to allow users of the Arrow crate to serialize structs that reference Arrow types (for example, allowing a logical query plan to be serialized and sent over the network).
    
    Note that this does not change the custom JSON serialization that is already in place for serializing in the specific format specified in `format/Metadata.md`.
    
    Author: Andy Grove <an...@gmail.com>
    
    Closes #3016 from andygrove/ARROW-3855 and squashes the following commits:
    
    329da92 <Andy Grove> Merge branch 'master' into ARROW-3855
    d988cc6 <Andy Grove> cargo fmt
    bd8375d <Andy Grove> Schema/Field/Datatype now have derived serde traits
---
 rust/Cargo.toml       |  2 ++
 rust/src/datatypes.rs | 42 +++++++++++++++++++++++++++++++++++++++---
 rust/src/lib.rs       |  5 +++++
 3 files changed, 46 insertions(+), 3 deletions(-)

diff --git a/rust/Cargo.toml b/rust/Cargo.toml
index d000445..b875094 100644
--- a/rust/Cargo.toml
+++ b/rust/Cargo.toml
@@ -36,6 +36,8 @@ path = "src/lib.rs"
 [dependencies]
 bytes = "0.4"
 libc = "0.2"
+serde = { version = "1.0.80", features = ["alloc", "rc"] }
+serde_derive = "1.0.80"
 serde_json = "1.0.13"
 rand = "0.5"
 csv = "1.0.0"
diff --git a/rust/src/datatypes.rs b/rust/src/datatypes.rs
index ec9b852..a9bd855 100644
--- a/rust/src/datatypes.rs
+++ b/rust/src/datatypes.rs
@@ -39,7 +39,7 @@ use serde_json::Value;
 /// Nested types can themselves be nested within other arrays.
 /// For more information on these types please see
 /// [here](https://arrow.apache.org/docs/memory_layout.html).
-#[derive(Debug, Clone, PartialEq, Eq)]
+#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
 pub enum DataType {
     Boolean,
     Int8,
@@ -61,7 +61,7 @@ pub enum DataType {
 /// Contains the meta-data for a single relative type.
 ///
 /// The `Schema` object is an ordered collection of `Field` objects.
-#[derive(Debug, Clone, PartialEq, Eq)]
+#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
 pub struct Field {
     name: String,
     data_type: DataType,
@@ -300,7 +300,7 @@ impl fmt::Display for Field {
 ///
 /// Note that this information is only part of the meta-data and not part of the physical memory
 /// layout.
-#[derive(Debug, Clone)]
+#[derive(Serialize, Deserialize, Debug, Clone)]
 pub struct Schema {
     fields: Vec<Field>,
 }
@@ -383,6 +383,42 @@ mod tests {
     }
 
     #[test]
+    fn serde_struct_type() {
+        let person = DataType::Struct(vec![
+            Field::new("first_name", DataType::Utf8, false),
+            Field::new("last_name", DataType::Utf8, false),
+            Field::new(
+                "address",
+                DataType::Struct(vec![
+                    Field::new("street", DataType::Utf8, false),
+                    Field::new("zip", DataType::UInt16, false),
+                ]),
+                false,
+            ),
+        ]);
+
+        let serialized = serde_json::to_string(&person).unwrap();
+
+        // NOTE that this is testing the default (derived) serialization format, not the
+        // JSON format specified in metadata.md
+
+        assert_eq!(
+            "{\"Struct\":[\
+             {\"name\":\"first_name\",\"data_type\":\"Utf8\",\"nullable\":false},\
+             {\"name\":\"last_name\",\"data_type\":\"Utf8\",\"nullable\":false},\
+             {\"name\":\"address\",\"data_type\":{\"Struct\":\
+             [{\"name\":\"street\",\"data_type\":\"Utf8\",\"nullable\":false},\
+             {\"name\":\"zip\",\"data_type\":\"UInt16\",\"nullable\":false}\
+             ]},\"nullable\":false}]}",
+            serialized
+        );
+
+        let deserialized = serde_json::from_str(&serialized).unwrap();
+
+        assert_eq!(person, deserialized);
+    }
+
+    #[test]
     fn struct_field_to_json() {
         let f = Field::new(
             "address",
diff --git a/rust/src/lib.rs b/rust/src/lib.rs
index 859b0a1..cc6d3ff 100644
--- a/rust/src/lib.rs
+++ b/rust/src/lib.rs
@@ -20,8 +20,13 @@ extern crate csv as csv_crate;
 extern crate libc;
 
 #[macro_use]
+extern crate serde_derive;
+
+#[macro_use]
 extern crate serde_json;
 
+extern crate serde;
+
 extern crate rand;
 
 pub mod array;