1 // Licensed to the Apache Software Foundation (ASF) under one
2 // or more contributor license agreements.  See the NOTICE file
3 // distributed with this work for additional information
4 // regarding copyright ownership.  The ASF licenses this file
5 // to you under the Apache License, Version 2.0 (the
6 // "License"); you may not use this file except in compliance
7 // with the License.  You may obtain a copy of the License at
8 //
9 //   http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing,
12 // software distributed under the License is distributed on an
13 // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 // KIND, either express or implied.  See the License for the
15 // specific language governing permissions and limitations
16 // under the License.
17 
18 ///! This example demonstrates dealing with mixed types dynamically at runtime
19 use std::sync::Arc;
20 
21 extern crate arrow;
22 
23 use arrow::array::*;
24 use arrow::datatypes::*;
25 use arrow::error::Result;
26 use arrow::record_batch::*;
27 
main() -> Result<()>28 fn main() -> Result<()> {
29     // define schema
30     let schema = Schema::new(vec![
31         Field::new("id", DataType::Int32, false),
32         Field::new(
33             "nested",
34             DataType::Struct(vec![
35                 Field::new("a", DataType::Utf8, false),
36                 Field::new("b", DataType::Float64, false),
37                 Field::new("c", DataType::Float64, false),
38             ]),
39             false,
40         ),
41     ]);
42 
43     // create some data
44     let id = Int32Array::from(vec![1, 2, 3, 4, 5]);
45 
46     let nested = StructArray::from(vec![
47         (
48             Field::new("a", DataType::Utf8, false),
49             Arc::new(StringArray::from(vec!["a", "b", "c", "d", "e"])) as Arc<dyn Array>,
50         ),
51         (
52             Field::new("b", DataType::Float64, false),
53             Arc::new(Float64Array::from(vec![1.1, 2.2, 3.3, 4.4, 5.5])),
54         ),
55         (
56             Field::new("c", DataType::Float64, false),
57             Arc::new(Float64Array::from(vec![2.2, 3.3, 4.4, 5.5, 6.6])),
58         ),
59     ]);
60 
61     // build a record batch
62     let batch =
63         RecordBatch::try_new(Arc::new(schema), vec![Arc::new(id), Arc::new(nested)])?;
64 
65     process(&batch);
66     Ok(())
67 }
68 
69 /// Create a new batch by performing a projection of id, nested.c
process(batch: &RecordBatch)70 fn process(batch: &RecordBatch) {
71     let id = batch.column(0);
72     let nested = batch
73         .column(1)
74         .as_any()
75         .downcast_ref::<StructArray>()
76         .unwrap();
77 
78     let _nested_b = nested
79         .column(1)
80         .as_any()
81         .downcast_ref::<Float64Array>()
82         .unwrap();
83     let nested_c: &Float64Array = nested
84         .column(2)
85         .as_any()
86         .downcast_ref::<Float64Array>()
87         .unwrap();
88 
89     let projected_schema = Schema::new(vec![
90         Field::new("id", DataType::Int32, false),
91         Field::new("sum", DataType::Float64, false),
92     ]);
93 
94     let _ = RecordBatch::try_new(
95         Arc::new(projected_schema),
96         vec![
97             id.clone(), // NOTE: this is cloning the Arc not the array data
98             Arc::new(Float64Array::from(nested_c.data())),
99         ],
100     );
101 }
102