>, ) -> Self { Self { name, is_deleted, a1, a2, a3, a4, nullable_list, required_list, } } } fn main() { // an item let item = Foo::new( Some("a".to_string()), false, Some(0.1), 1, Some(b"aa".to_vec()), NaiveDate::from_ymd(1970, 1, 2), None, vec![Some("aa".to_string()), Some("bb".to_string())], ); let mut array = FooArray::default(); array.push(item); // convert it to an Arrow array let array: StructArray = array.into(); assert_eq!(array.len(), 1); // which will have a schema: assert_eq!( array.fields(), &[ Field::new("name", DataType::Utf8, true), Field::new("is_deleted", DataType::Boolean, false), Field::new("a1", DataType::Float64, true), Field::new("a2", DataType::Int64, false), Field::new("a3", DataType::Binary, true), Field::new("a4", DataType::Date32, false), Field::new( "nullable_list", DataType::List(Box::new(Field::new("item", DataType::Utf8, true))), true ), Field::new( "required_list", DataType::List(Box::new(Field::new("item", DataType::Utf8, true))), false ), ] ); // `StructArray` can then be converted to arrow's `RecordBatch` let batch: RecordBatch = array.into(); assert_eq!(batch.num_columns(), 8); assert_eq!(batch.num_rows(), 1); // which can be used in IPC, FFI, to parquet, analytics, etc. } ">
use arrow2::datatypes::{DataType, Field};
use arrow2::{array::*, record_batch::RecordBatch};
use arrow2_derive::{ArrowStruct, StructOfArrow};
use chrono::naive::NaiveDate;
#[derive(Debug, Clone, PartialEq, StructOfArrow)]
#[arrow2_derive = "Debug"]
pub struct Foo {
name: Option<String>,
is_deleted: bool,
a1: Option<f64>,
a2: i64,
// binary
a3: Option<Vec<u8>>,
// date32
a4: NaiveDate,
// optional list array of optional strings
nullable_list: Option<Vec<Option<String>>>,
// optional list array of required strings
required_list: Vec<Option<String>>,
}
impl Foo {
#[allow(clippy::too_many_arguments)]
pub fn new(
name: Option<String>,
is_deleted: bool,
a1: Option<f64>,
a2: i64,
a3: Option<Vec<u8>>,
a4: NaiveDate,
nullable_list: Option<Vec<Option<String>>>,
required_list: Vec<Option<String>>,
) -> Self {
Self {
name,
is_deleted,
a1,
a2,
a3,
a4,
nullable_list,
required_list,
}
}
}
fn main() {
// an item
let item = Foo::new(
Some("a".to_string()),
false,
Some(0.1),
1,
Some(b"aa".to_vec()),
NaiveDate::from_ymd(1970, 1, 2),
None,
vec![Some("aa".to_string()), Some("bb".to_string())],
);
let mut array = FooArray::default();
array.push(item);
// convert it to an Arrow array
let array: StructArray = array.into();
assert_eq!(array.len(), 1);
// which will have a schema:
assert_eq!(
array.fields(),
&[
Field::new("name", DataType::Utf8, true),
Field::new("is_deleted", DataType::Boolean, false),
Field::new("a1", DataType::Float64, true),
Field::new("a2", DataType::Int64, false),
Field::new("a3", DataType::Binary, true),
Field::new("a4", DataType::Date32, false),
Field::new(
"nullable_list",
DataType::List(Box::new(Field::new("item", DataType::Utf8, true))),
true
),
Field::new(
"required_list",
DataType::List(Box::new(Field::new("item", DataType::Utf8, true))),
false
),
]
);
// `StructArray` can then be converted to arrow's `RecordBatch`
let batch: RecordBatch = array.into();
assert_eq!(batch.num_columns(), 8);
assert_eq!(batch.num_rows(), 1);
// which can be used in IPC, FFI, to parquet, analytics, etc.
}