#[macro_export]
macro_rules! newtype_datatype {
($newtype:ty, $repr:ty) => {
$crate::newtype_datatype!($newtype, $repr, noref);
impl $crate::RefType for $newtype {
type Ref = <$repr as $crate::RefType>::Ref;
fn value_ref(typed: &Self::Typed, index: usize) -> &Self::Ref {
<$repr as $crate::RefType>::value_ref(typed, index)
}
}
};
($newtype:ty, $repr:ty, primitive) => {
$crate::newtype_datatype!($newtype, $repr);
impl $crate::PrimitiveType for $newtype {
type Native = <$repr as $crate::PrimitiveType>::Native;
fn values(typed: &Self::Typed) -> &[Self::Native] {
<$repr as $crate::PrimitiveType>::values(typed)
}
}
};
($newtype:ty, $repr:ty, noref) => {
impl $crate::LogicalType for $newtype {
const NULLABLE: bool = <$repr as $crate::LogicalType>::NULLABLE;
type Typed = <$repr as $crate::LogicalType>::Typed;
type Value<'a>
= <$repr as $crate::LogicalType>::Value<'a>
where
Self: 'a;
type Owned = $newtype;
fn downcast(
array: &dyn $crate::arrow::array::Array,
) -> ::core::result::Result<Self::Typed, $crate::ColumnError> {
<$repr as $crate::LogicalType>::downcast(array)
}
#[inline]
fn is_null(typed: &Self::Typed, index: usize) -> bool {
<$repr as $crate::LogicalType>::is_null(typed, index)
}
#[inline]
unsafe fn is_null_unchecked(typed: &Self::Typed, index: usize) -> bool {
unsafe { <$repr as $crate::LogicalType>::is_null_unchecked(typed, index) }
}
#[inline]
fn value(typed: &Self::Typed, index: usize) -> Self::Value<'_> {
<$repr as $crate::LogicalType>::value(typed, index)
}
#[inline]
unsafe fn value_unchecked(typed: &Self::Typed, index: usize) -> Self::Value<'_> {
unsafe { <$repr as $crate::LogicalType>::value_unchecked(typed, index) }
}
fn to_owned_value(value: Self::Value<'_>) -> Self::Owned {
::core::convert::From::from(<$repr as $crate::LogicalType>::to_owned_value(value))
}
}
impl $crate::ConcreteType for $newtype
where
$repr: $crate::ConcreteType,
{
fn datatype() -> $crate::arrow::datatypes::DataType {
<$repr as $crate::ConcreteType>::datatype()
}
fn build(
values: impl ::core::iter::Iterator<Item = ::core::option::Option<Self::Owned>>,
) -> ::core::result::Result<$crate::arrow::array::ArrayRef, $crate::ColumnError> {
<$repr as $crate::ConcreteType>::build(
values.map(|value| value.map(::core::convert::Into::into)),
)
}
}
impl $crate::InfallibleBuild for $newtype where $repr: $crate::InfallibleBuild {}
};
}
use std::marker::PhantomData;
use crate::datatype::{ColumnError, InfallibleBuild, LogicalType, PrimitiveType, RefType};
pub struct As<T, Repr> {
_marker: PhantomData<fn() -> (T, Repr)>,
}
impl<T, Repr> LogicalType for As<T, Repr>
where
T: 'static,
Repr: LogicalType + 'static,
T: From<Repr::Owned>,
Repr::Owned: From<T>,
{
const NULLABLE: bool = Repr::NULLABLE;
type Typed = Repr::Typed;
type Value<'a>
= Repr::Value<'a>
where
Self: 'a;
type Owned = T;
fn downcast(array: &dyn arrow::array::Array) -> Result<Self::Typed, ColumnError> {
Repr::downcast(array)
}
#[inline]
fn is_null(typed: &Self::Typed, index: usize) -> bool {
Repr::is_null(typed, index)
}
#[inline]
unsafe fn is_null_unchecked(typed: &Self::Typed, index: usize) -> bool {
unsafe { Repr::is_null_unchecked(typed, index) }
}
#[inline]
fn value(typed: &Self::Typed, index: usize) -> Self::Value<'_> {
Repr::value(typed, index)
}
#[inline]
unsafe fn value_unchecked(typed: &Self::Typed, index: usize) -> Self::Value<'_> {
unsafe { Repr::value_unchecked(typed, index) }
}
fn to_owned_value(value: Self::Value<'_>) -> Self::Owned {
T::from(Repr::to_owned_value(value))
}
}
impl<T, Repr> crate::ConcreteType for As<T, Repr>
where
T: 'static,
Repr: crate::ConcreteType + 'static,
T: From<Repr::Owned>,
Repr::Owned: From<T>,
{
fn datatype() -> arrow::datatypes::DataType {
Repr::datatype()
}
fn build(
values: impl Iterator<Item = Option<Self::Owned>>,
) -> Result<arrow::array::ArrayRef, ColumnError> {
Repr::build(values.map(|value| value.map(Repr::Owned::from)))
}
}
impl<T, Repr> InfallibleBuild for As<T, Repr>
where
T: 'static,
Repr: LogicalType + InfallibleBuild + 'static,
T: From<Repr::Owned>,
Repr::Owned: From<T>,
{
}
impl<T, Repr> RefType for As<T, Repr>
where
T: 'static,
Repr: RefType + 'static,
T: From<Repr::Owned>,
Repr::Owned: From<T>,
{
type Ref = Repr::Ref;
fn value_ref(typed: &Self::Typed, index: usize) -> &Self::Ref {
Repr::value_ref(typed, index)
}
}
impl<T, Repr> PrimitiveType for As<T, Repr>
where
T: 'static,
Repr: PrimitiveType + 'static,
T: From<Repr::Owned>,
Repr::Owned: From<T>,
{
type Native = Repr::Native;
fn values(typed: &Self::Typed) -> &[Self::Native] {
Repr::values(typed)
}
}