Skip to content

Commit

Permalink
Add arrow-array feature for zero-copy array interop
Browse files Browse the repository at this point in the history
  • Loading branch information
mbrobbel committed Aug 4, 2023
1 parent 7b5dab9 commit 26e746c
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 1 deletion.
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

Expand Down
56 changes: 56 additions & 0 deletions src/array/fixed_size_primitive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,62 @@ impl<T: FixedSize, Buffer: BufferType> BitmapRefMut for FixedSizePrimitiveArray<

impl<T: FixedSize, Buffer: BufferType> ValidityBitmap for FixedSizePrimitiveArray<T, true, Buffer> {}

#[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<T: FixedSize, U: ArrowPrimitiveType<Native = T>>
From<FixedSizePrimitiveArray<T, false, ArrowBuffer>> for PrimitiveArray<U>
{
fn from(mut value: FixedSizePrimitiveArray<T, false, ArrowBuffer>) -> Self {
let len = value.len();
Self::new(ScalarBuffer::new(value.0.finish(), 0, len), None)
}
}

impl<T: FixedSize, U: ArrowPrimitiveType<Native = T>>
From<FixedSizePrimitiveArray<T, true, ArrowBuffer>> for PrimitiveArray<U>
{
fn from(mut value: FixedSizePrimitiveArray<T, true, ArrowBuffer>) -> 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::<Int8Array<false, ArrowBuffer>>();
let array = PrimitiveArray::<Int8Type>::from(array);
assert_eq!(array.len(), 4);

let input = [Some(1), None, Some(3), Some(4)];
let array = input.into_iter().collect::<Int8Array<true, ArrowBuffer>>();
let array = PrimitiveArray::<Int8Type>::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::*;
Expand Down
15 changes: 15 additions & 0 deletions src/bitmap/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,21 @@ impl<Buffer: BufferType> Length for Bitmap<Buffer> {

impl<Buffer: BufferType> ValidityBitmap for Bitmap<Buffer> {}

#[cfg(feature = "arrow-buffer")]
mod arrow {
use super::Bitmap;
use crate::buffer::ArrowBuffer;
use arrow_buffer::BooleanBuffer;

impl From<Bitmap<ArrowBuffer>> for BooleanBuffer {
fn from(mut value: Bitmap<ArrowBuffer>) -> Self {
BooleanBuffer::new(value.buffer.finish(), 0, value.bits)
}
}
}

pub use arrow::*;

#[cfg(test)]
mod tests {
use crate::buffer::{ArrayBuffer, BoxBuffer, BufferRefMut, SliceBuffer};
Expand Down

0 comments on commit 26e746c

Please sign in to comment.