use arrow::array::{Array, ArrayRef};
use arrow::datatypes::DataType;
use crate::datatype::InfallibleBuild;
use crate::{ColumnError, Datatype};
impl<L: Datatype> Datatype for Option<L> {
const NULLABLE: bool = true;
type Typed = L::Typed;
type Value<'a>
= Option<L::Value<'a>>
where
Self: 'a;
type Owned = Option<L::Owned>;
fn datatype() -> DataType {
L::datatype()
}
fn downcast(array: &dyn Array) -> Result<Self::Typed, ColumnError> {
L::downcast(array)
}
fn is_null(typed: &Self::Typed, index: usize) -> bool {
L::is_null(typed, index)
}
fn value(typed: &Self::Typed, index: usize) -> Self::Value<'_> {
if L::is_null(typed, index) {
None
} else {
Some(L::value(typed, index))
}
}
fn build(values: impl Iterator<Item = Option<Self::Owned>>) -> Result<ArrayRef, ColumnError> {
L::build(values.map(Option::flatten))
}
fn to_owned_value(value: Self::Value<'_>) -> Self::Owned {
value.map(L::to_owned_value)
}
}
impl<L: InfallibleBuild> InfallibleBuild for Option<L> {}