diff --git a/Cargo.toml b/Cargo.toml index 9ee539e4..97b8b05a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -28,12 +28,14 @@ keywords.workspace = true categories.workspace = true [features] -default = ["arrow-buffer", "derive", "unsafe"] +default = ["arrow-array", "arrow-buffer", "derive", "unsafe"] +arrow-array = ["dep:arrow-array", "arrow-buffer"] arrow-buffer = ["dep:arrow-buffer"] derive = ["dep:narrow-derive"] unsafe = [] [dependencies] +arrow-array = { git = "https://github.com/mbrobbel/arrow-rs.git", branch = "arrow-native-type", optional = true } arrow-buffer = { git = "https://github.com/mbrobbel/arrow-rs.git", branch = "arrow-native-type", optional = true } narrow-derive = { path = "narrow-derive", version = "^0.3.4", optional = true } diff --git a/src/array/fixed_size_primitive.rs b/src/array/fixed_size_primitive.rs index 1751be74..27559f5d 100644 --- a/src/array/fixed_size_primitive.rs +++ b/src/array/fixed_size_primitive.rs @@ -136,6 +136,62 @@ impl BitmapRefMut for FixedSizePrimitiveArray< impl ValidityBitmap for FixedSizePrimitiveArray {} +#[cfg(feature = "arrow-array")] +mod arrow { + use super::FixedSizePrimitiveArray; + use crate::{buffer::ArrowBuffer, FixedSize, Length}; + use arrow_array::{types::ArrowPrimitiveType, PrimitiveArray}; + use arrow_buffer::{NullBuffer, ScalarBuffer}; + + impl> + From> for PrimitiveArray + { + fn from(mut value: FixedSizePrimitiveArray) -> Self { + let len = value.len(); + Self::new(ScalarBuffer::new(value.0.finish(), 0, len), None) + } + } + + impl> + From> for PrimitiveArray + { + fn from(mut value: FixedSizePrimitiveArray) -> Self { + let len = value.len(); + Self::new( + ScalarBuffer::new(value.0.data.finish(), 0, len), + Some(NullBuffer::new(value.0.validity.into())), + ) + } + } + + #[cfg(test)] + mod test { + + #[test] + #[cfg(feature = "arrow-array")] + fn arrow_array() { + use crate::{array::Int8Array, buffer::ArrowBuffer}; + use arrow_array::{types::Int8Type, Array, PrimitiveArray}; + + let input = [1, 2, 3, 4]; + let array = input.into_iter().collect::>(); + let array = PrimitiveArray::::from(array); + assert_eq!(array.len(), 4); + + let input = [Some(1), None, Some(3), Some(4)]; + let array = input.into_iter().collect::>(); + let array = PrimitiveArray::::from(array); + assert_eq!(array.len(), 4); + assert_eq!(array.null_count(), 1); + } + + #[test] + fn convert() {} + } +} + +pub use arrow::*; + #[cfg(test)] mod tests { use super::*; diff --git a/src/bitmap/mod.rs b/src/bitmap/mod.rs index dabacf5d..6dc4947c 100644 --- a/src/bitmap/mod.rs +++ b/src/bitmap/mod.rs @@ -299,6 +299,21 @@ impl Length for Bitmap { impl ValidityBitmap for Bitmap {} +#[cfg(feature = "arrow-buffer")] +mod arrow { + use super::Bitmap; + use crate::buffer::ArrowBuffer; + use arrow_buffer::BooleanBuffer; + + impl From> for BooleanBuffer { + fn from(mut value: Bitmap) -> Self { + BooleanBuffer::new(value.buffer.finish(), 0, value.bits) + } + } +} + +pub use arrow::*; + #[cfg(test)] mod tests { use crate::buffer::{ArrayBuffer, BoxBuffer, BufferRefMut, SliceBuffer};