diff --git a/src/arrow/array/union.rs b/src/arrow/array/union.rs index 00a34da..f67651a 100644 --- a/src/arrow/array/union.rs +++ b/src/arrow/array/union.rs @@ -100,6 +100,25 @@ where } } +impl< + T: UnionArrayType, + const VARIANTS: usize, + Buffer: BufferType, + OffsetItem: OffsetElement, + > From> + for Arc +where + for<'a> i8: From<&'a T>, + >::Array: + UnionArrayTypeFields + Into>>, + arrow_buffer::ScalarBuffer: From>, + UnionArray: crate::arrow::Array, +{ + fn from(value: UnionArray) -> Self { + Arc::new(arrow_array::UnionArray::from(value)) + } +} + impl< T: UnionArrayType, const VARIANTS: usize, @@ -132,6 +151,25 @@ where } } +impl< + T: UnionArrayType, + const VARIANTS: usize, + Buffer: BufferType, + OffsetItem: OffsetElement, + > From> for Arc +where + for<'a> i8: From<&'a T>, + >::Array: + UnionArrayTypeFields + Into>>, + arrow_buffer::ScalarBuffer: From>, + arrow_buffer::ScalarBuffer: From>, + UnionArray: crate::arrow::Array, +{ + fn from(value: UnionArray) -> Self { + Arc::new(arrow_array::UnionArray::from(value)) + } +} + impl< T: UnionArrayType, const VARIANTS: usize, @@ -156,6 +194,25 @@ where } } +impl< + T: UnionArrayType, + const VARIANTS: usize, + Buffer: BufferType, + OffsetItem: OffsetElement, + > From> + for UnionArray +where + for<'a> i8: From<&'a T>, + FixedSizePrimitiveArray: From>, + >::Array: + FromIterator>, +{ + fn from(value: Arc) -> Self { + let array = arrow_array::UnionArray::from(value.to_data()); + Self::from(array) + } +} + impl< T: UnionArrayType, const VARIANTS: usize, @@ -182,20 +239,53 @@ where } } +impl< + T: UnionArrayType, + const VARIANTS: usize, + Buffer: BufferType, + OffsetItem: OffsetElement, + > From> for UnionArray +where + for<'a> i8: From<&'a T>, + FixedSizePrimitiveArray: From>, + FixedSizePrimitiveArray: From>, + >::Array: + FromIterator>, +{ + fn from(value: Arc) -> Self { + let array = arrow_array::UnionArray::from(value.to_data()); + Self::from(array) + } +} + #[cfg(test)] #[cfg(feature = "derive")] mod tests { - use crate::Length; + use arrow_array::RecordBatch; + + use crate::{array::StructArray, Length}; use super::*; - #[derive(crate::ArrayType, Clone)] + #[derive(crate::ArrayType, Clone, Debug, PartialEq)] enum FooBar { Foo, Bar(u8), Baz { a: bool }, } + #[derive(crate::ArrayType, Clone, Debug, PartialEq)] + struct Wrap(FooBar); + + #[test] + fn via_dyn_array() { + let input = [Wrap(FooBar::Foo), Wrap(FooBar::Bar(123))]; + let struct_array = input.clone().into_iter().collect::>(); + let record_batch = RecordBatch::from(struct_array); + let read = StructArray::::from(record_batch); + assert_eq!(read.into_iter().collect::>(), input); + } + #[test] fn from() { let input = [