use arrow::array::{Array, ArrayRef};
use arrow::datatypes::DataType;
use crate::datatype::{ColumnError, Datatype, InfallibleBuild, RefDatatype, downcast_array};
pub struct Binary;
pub struct LargeBinary;
macro_rules! impl_binary_datatype {
($marker:ty, $array:ty, $datatype:expr) => {
impl Datatype for $marker {
type Typed = $array;
type Value<'a> = &'a [u8];
type Owned = Vec<u8>;
fn datatype() -> DataType {
$datatype
}
fn downcast(array: &dyn Array) -> Result<Self::Typed, ColumnError> {
downcast_array::<$array>(array)
}
fn is_null(typed: &Self::Typed, index: usize) -> bool {
typed.is_null(index)
}
fn value(typed: &Self::Typed, index: usize) -> Self::Value<'_> {
typed.value(index)
}
fn build(
values: impl Iterator<Item = Option<Self::Owned>>,
) -> Result<ArrayRef, ColumnError> {
Ok(std::sync::Arc::new(<$array>::from_iter(values)))
}
fn to_owned_value(value: Self::Value<'_>) -> Self::Owned {
value.to_vec()
}
}
impl InfallibleBuild for $marker {}
impl RefDatatype for $marker {
type Ref = [u8];
fn value_ref(typed: &Self::Typed, index: usize) -> &[u8] {
typed.value(index)
}
}
};
}
impl_binary_datatype!(Binary, arrow::array::BinaryArray, DataType::Binary);
impl_binary_datatype!(
LargeBinary,
arrow::array::LargeBinaryArray,
DataType::LargeBinary
);