use arrow2::array::Array; use arrow2::buffer::Buffer; use arrow2::chunk::Chunk; use arrow2_convert::field::{ArrowField, FixedSizeBinary}; use arrow2_convert::serialize::*; use std::sync::Arc; #[test] fn test_error_exceed_fixed_size_binary() { let strs = [b"abc".to_vec()]; let r: arrow2::error::Result> = strs.try_into_arrow_as_type::>(); assert!(r.is_err()) } #[test] fn test_chunk() { let strs = [b"abc".to_vec()]; let r: Chunk> = strs.try_into_arrow_as_type::>().unwrap(); assert_eq!(r.len(), 1); assert_eq!( r.arrays()[0].data_type(), & as ArrowField>::data_type() ); let r: Chunk> = strs.try_into_arrow().unwrap(); assert_eq!(r.len(), 1); assert_eq!( r.arrays()[0].data_type(), & as ArrowField>::data_type() ); let r: Chunk> = strs.try_into_arrow_as_type::>().unwrap(); assert_eq!(r.len(), 1); assert_eq!( r.arrays()[0].data_type(), & as ArrowField>::data_type() ); let r: Chunk> = strs.try_into_arrow().unwrap(); assert_eq!(r.len(), 1); assert_eq!( r.arrays()[0].data_type(), & as ArrowField>::data_type() ); } #[test] fn test_array() { let strs = [b"abc".to_vec()]; let r: Box = strs.try_into_arrow_as_type::>().unwrap(); assert_eq!( r.data_type(), & as ArrowField>::data_type() ); let r: Box = strs.try_into_arrow().unwrap(); assert_eq!(r.len(), 1); assert_eq!(r.data_type(), & as ArrowField>::data_type()); let r: Arc = strs.try_into_arrow_as_type::>().unwrap(); assert_eq!(r.len(), 1); assert_eq!( r.data_type(), & as ArrowField>::data_type() ); let r: Arc = strs.try_into_arrow().unwrap(); assert_eq!(r.len(), 1); assert_eq!(r.data_type(), & as ArrowField>::data_type()); } #[test] fn test_buffer() { // Buffer and Vec should serialize into BinaryArray let dat: Vec> = vec![(0..10).collect()]; let r: Box = dat.try_into_arrow().unwrap(); assert_eq!(r.len(), 1); assert_eq!(r.data_type(), & as ArrowField>::data_type()); assert_eq!(r.data_type(), & as ArrowField>::data_type()); // Buffer and Vec should serialize into ListArray let dat: Vec> = vec![(0..10).collect()]; let r: Box = dat.try_into_arrow().unwrap(); assert_eq!(r.len(), 1); assert_eq!(r.data_type(), & as ArrowField>::data_type()); assert_eq!(r.data_type(), & as ArrowField>::data_type()); } #[test] fn test_field_serialize_error() { pub struct CustomType(u64); impl arrow2_convert::field::ArrowField for CustomType { type Type = Self; #[inline] fn data_type() -> arrow2::datatypes::DataType { arrow2::datatypes::DataType::Extension( "custom".to_string(), Box::new(arrow2::datatypes::DataType::UInt64), None, ) } } impl arrow2_convert::serialize::ArrowSerialize for CustomType { type MutableArrayType = arrow2::array::MutablePrimitiveArray; #[inline] fn new_array() -> Self::MutableArrayType { Self::MutableArrayType::from(::data_type()) } #[inline] fn arrow_serialize(_: &Self, _: &mut Self::MutableArrayType) -> arrow2::error::Result<()> { Err(arrow2::error::Error::NotYetImplemented("".to_owned())) } } impl arrow2_convert::deserialize::ArrowDeserialize for CustomType { type ArrayType = arrow2::array::PrimitiveArray; #[inline] fn arrow_deserialize(v: Option<&u64>) -> Option { v.map(|t| CustomType(*t)) } } let arr = vec![CustomType(0)]; let r: arrow2::error::Result> = arr.try_into_arrow(); assert!(r.is_err()) }