ngli-me commented on code in PR #6758:
URL: https://github.com/apache/arrow-rs/pull/6758#discussion_r1855738736


##########
arrow-array/src/record_batch.rs:
##########
@@ -1206,6 +1258,145 @@ mod tests {
         assert_ne!(batch1, batch2);
     }
 
+    #[test]
+    fn normalize() {
+        let animals: ArrayRef = Arc::new(StringArray::from(vec!["Parrot", 
""]));
+        let n_legs: ArrayRef = Arc::new(Int64Array::from(vec![Some(2), 
Some(4)]));
+        let year: ArrayRef = Arc::new(Int64Array::from(vec![None, 
Some(2022)]));
+
+        let animals_field = Arc::new(Field::new("animals", DataType::Utf8, 
true));
+        let n_legs_field = Arc::new(Field::new("n_legs", DataType::Int64, 
true));
+        let year_field = Arc::new(Field::new("year", DataType::Int64, true));
+
+        let a = Arc::new(StructArray::from(vec![
+            (animals_field.clone(), Arc::new(animals.clone()) as ArrayRef),
+            (n_legs_field.clone(), Arc::new(n_legs.clone()) as ArrayRef),
+            (year_field.clone(), Arc::new(year.clone()) as ArrayRef),
+        ]));
+
+        let month = Arc::new(Int64Array::from(vec![Some(4), Some(6)]));
+
+        let schema = Schema::new(vec![
+            Field::new(
+                "a",
+                DataType::Struct(Fields::from(vec![animals_field, 
n_legs_field, year_field])),
+                false,
+            ),
+            Field::new("month", DataType::Int64, true),
+        ]);
+
+        let normalized = RecordBatch::try_new(Arc::new(schema), vec![a, 
month.clone()])
+            .expect("valid conversion")
+            .normalize(".", 0)
+            .expect("valid normalization");
+
+        let expected = RecordBatch::try_from_iter_with_nullable(vec![
+            ("a.animals", animals.clone(), true),
+            ("a.n_legs", n_legs.clone(), true),
+            ("a.year", year.clone(), true),
+            ("month", month.clone(), true),
+        ])
+        .expect("valid conversion");
+
+        assert_eq!(expected, normalized);
+    }
+
+    #[test]
+    fn normalize_nested() {
+        // Initialize schema
+        let a = Arc::new(Field::new("a", DataType::Utf8, true));
+        let b = Arc::new(Field::new("b", DataType::Int64, false));
+        let c = Arc::new(Field::new("c", DataType::Int64, true));
+
+        let d = Arc::new(Field::new("d", DataType::Utf8, true));
+        let e = Arc::new(Field::new("e", DataType::Int64, false));
+        let f = Arc::new(Field::new("f", DataType::Int64, true));
+
+        let one = Arc::new(Field::new(
+            "1",
+            DataType::Struct(Fields::from(vec![a.clone(), b.clone(), 
c.clone()])),
+            false,
+        ));
+        let two = Arc::new(Field::new(
+            "2",
+            DataType::Struct(Fields::from(vec![d.clone(), e.clone(), 
f.clone()])),
+            true,
+        ));
+
+        let exclamation = Arc::new(Field::new(
+            "!",
+            DataType::Struct(Fields::from(vec![one, two])),
+            false,
+        ));
+
+        // Initialize fields
+        let a_field: ArrayRef = 
Arc::new(StringArray::from(vec!["a1_field_data", "a1_field_data"]));
+        let b_field: ArrayRef = Arc::new(Int64Array::from(vec![Some(0), 
Some(1)]));
+        let c_field: ArrayRef = Arc::new(Int64Array::from(vec![None, 
Some(2)]));
+
+        let d_field: ArrayRef = 
Arc::new(StringArray::from(vec!["d1_field_data", "d2_field_data"]));
+        let e_field: ArrayRef = Arc::new(Int64Array::from(vec![Some(3), 
Some(4)]));
+        let f_field: ArrayRef = Arc::new(Int64Array::from(vec![None, 
Some(5)]));
+
+        let one_field = Arc::new(StructArray::from(vec![
+            (a.clone(), Arc::new(a_field.clone()) as ArrayRef),
+            (b.clone(), Arc::new(b_field.clone()) as ArrayRef),
+            (c.clone(), Arc::new(c_field.clone()) as ArrayRef),
+        ]));
+        let two_field = Arc::new(StructArray::from(vec![
+            (a.clone(), Arc::new(a_field.clone()) as ArrayRef),
+            (b.clone(), Arc::new(b_field.clone()) as ArrayRef),
+            (c.clone(), Arc::new(c_field.clone()) as ArrayRef),
+        ]));
+
+        /*let exclamation_field = Arc::new(StructArray::from(vec![

Review Comment:
   It looks like this should work to me, based on the schema, but I get an 
error when trying to construct this `Field`, not sure what I'm missing here.
   
   `
   error[E0277]: the trait bound `std::sync::Arc<struct_array::StructArray>: 
array::Array` is not satisfied
       --> arrow-array/src/record_batch.rs:1353:27
        |
   1353 |             (one.clone(), Arc::new(one_field.clone()) as ArrayRef),
        |                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait 
`array::Array` is not implemented for 
`std::sync::Arc<struct_array::StructArray>`
        |
        = help: the trait `array::Array` is implemented for 
`std::sync::Arc<(dyn array::Array + 'static)>`
        = note: required for the cast from 
`std::sync::Arc<std::sync::Arc<struct_array::StructArray>>` to 
`std::sync::Arc<(dyn array::Array + 'static)>`
        `



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to