Browse Source

procedural macros in from_impls file

tags/v0.3.11-rc1
Shar-jeel-Sajid 10 months ago
parent
commit
77532746ff
2 changed files with 92 additions and 302 deletions
  1. +79
    -289
      libraries/arrow-convert/src/from_impls.rs
  2. +13
    -13
      libraries/arrow-convert/src/into_impls.rs

+ 79
- 289
libraries/arrow-convert/src/from_impls.rs View File

@@ -36,98 +36,88 @@ impl TryFrom<&ArrowData> for bool {
Ok(bool_array.value(0))
}
}
impl TryFrom<&ArrowData> for u8 {
type Error = eyre::Report;
fn try_from(value: &ArrowData) -> Result<Self, Self::Error> {
let array = value
.as_primitive_opt::<arrow::datatypes::UInt8Type>()
.context("not a primitive UInt8Type array")?;
extract_single_primitive(array)
}
}
impl TryFrom<&ArrowData> for u16 {
type Error = eyre::Report;
fn try_from(value: &ArrowData) -> Result<Self, Self::Error> {
let array = value
.as_primitive_opt::<arrow::datatypes::UInt16Type>()
.context("not a primitive UInt16Type array")?;
extract_single_primitive(array)
}
}
impl TryFrom<&ArrowData> for u32 {
type Error = eyre::Report;
fn try_from(value: &ArrowData) -> Result<Self, Self::Error> {
let array = value
.as_primitive_opt::<arrow::datatypes::UInt32Type>()
.context("not a primitive UInt32Type array")?;
extract_single_primitive(array)
}
}
impl TryFrom<&ArrowData> for u64 {
type Error = eyre::Report;
fn try_from(value: &ArrowData) -> Result<Self, Self::Error> {
let array = value
.as_primitive_opt::<arrow::datatypes::UInt64Type>()
.context("not a primitive UInt64Type array")?;
extract_single_primitive(array)
}
}
impl TryFrom<&ArrowData> for i8 {
type Error = eyre::Report;
fn try_from(value: &ArrowData) -> Result<Self, Self::Error> {
let array = value
.as_primitive_opt::<arrow::datatypes::Int8Type>()
.context("not a primitive Int8Type array")?;
extract_single_primitive(array)
}
}
impl TryFrom<&ArrowData> for i16 {
type Error = eyre::Report;
fn try_from(value: &ArrowData) -> Result<Self, Self::Error> {
let array = value
.as_primitive_opt::<arrow::datatypes::Int16Type>()
.context("not a primitive Int16Type array")?;
extract_single_primitive(array)
}
}
impl TryFrom<&ArrowData> for i32 {
type Error = eyre::Report;
fn try_from(value: &ArrowData) -> Result<Self, Self::Error> {
let array = value
.as_primitive_opt::<arrow::datatypes::Int32Type>()
.context("not a primitive Int32Type array")?;
extract_single_primitive(array)
}
}
impl TryFrom<&ArrowData> for i64 {
type Error = eyre::Report;
fn try_from(value: &ArrowData) -> Result<Self, Self::Error> {
let array = value
.as_primitive_opt::<arrow::datatypes::Int64Type>()
.context("not a primitive Int64Type array")?;
extract_single_primitive(array)
}
}

impl TryFrom<&ArrowData> for f32 {
type Error = eyre::Report;
fn try_from(value: &ArrowData) -> Result<Self, Self::Error> {
let array = value
.as_primitive_opt::<arrow::datatypes::Float32Type>()
.context("not a primitive Float32Type array")?;
extract_single_primitive(array)
}
}
impl TryFrom<&ArrowData> for f64 {
type Error = eyre::Report;
fn try_from(value: &ArrowData) -> Result<Self, Self::Error> {
let array = value
.as_primitive_opt::<arrow::datatypes::Float64Type>()
.context("not a primitive Float64Type array")?;
extract_single_primitive(array)
}
macro_rules! impl_try_from_arrow_data {
($($t:ty => $arrow_type:ident),*) => {
$(
impl TryFrom<&ArrowData> for $t {
type Error = eyre::Report;

fn try_from(value: &ArrowData) -> Result<Self, Self::Error> {
let array = value
.as_primitive_opt::<arrow::datatypes::$arrow_type>()
.context(concat!("not a primitive ", stringify!($arrow_type), " array"))?;
extract_single_primitive(array)
}
}
)*
};
}

impl_try_from_arrow_data!(
u8 => UInt8Type,
u16 => UInt16Type,
u32 => UInt32Type,
u64 => UInt64Type,
i8 => Int8Type,
i16 =>Int16Type,
i32 => Int32Type,
i64 => Int64Type,
f32 => Float32Type,
f64 => Float64Type
);

macro_rules! impl_try_from_arrow_data_for_slice {
($($t:ty => $arrow_type:ident),*) => {
$(
impl<'a> TryFrom<&'a ArrowData> for &'a [$t] {
type Error = eyre::Report;

fn try_from(value: &'a ArrowData) -> Result<Self, Self::Error> {
let array: &PrimitiveArray<arrow::datatypes::$arrow_type> = value
.as_primitive_opt()
.wrap_err(concat!("not a primitive ", stringify!($arrow_type), " array"))?;
if array.null_count() != 0 {
eyre::bail!("array has nulls");
}
Ok(array.values())
}
}
)*
};
}

impl_try_from_arrow_data_for_slice!(
u8 => UInt8Type,
u16 => UInt16Type,
u32 => UInt32Type,
u64 => UInt64Type,
i8 => Int8Type,
i16 =>Int16Type,
i32 => Int32Type,
i64 => Int64Type,
f32 => Float32Type,
f64 => Float64Type
);

macro_rules! impl_try_from_arrow_data_for_vec {
($($t:ty),*) => {
$(
impl<'a> TryFrom<&'a ArrowData> for Vec<$t> {
type Error = eyre::Report;

fn try_from(value: &'a ArrowData) -> Result<Self, Self::Error> {
value
.try_into()
.map(|slice: &'a [$t]| slice.to_vec())
}
}
)*
};
}

impl_try_from_arrow_data_for_vec!(u8, u16, u32, u64, i8, i16, i32, i64, f32, f64);

impl<'a> TryFrom<&'a ArrowData> for &'a str {
type Error = eyre::Report;
fn try_from(value: &'a ArrowData) -> Result<Self, Self::Error> {
@@ -145,206 +135,6 @@ impl<'a> TryFrom<&'a ArrowData> for &'a str {
}
}

impl<'a> TryFrom<&'a ArrowData> for &'a [u8] {
type Error = eyre::Report;
fn try_from(value: &'a ArrowData) -> Result<Self, Self::Error> {
let array: &PrimitiveArray<arrow::datatypes::UInt8Type> = value
.as_primitive_opt()
.wrap_err("not a primitive UInt8Type array")?;
if array.null_count() != 0 {
eyre::bail!("array has nulls");
}
Ok(array.values())
}
}

impl<'a> TryFrom<&'a ArrowData> for Vec<u8> {
type Error = eyre::Report;
fn try_from(value: &'a ArrowData) -> Result<Self, Self::Error> {
value.try_into().map(|slice: &'a [u8]| slice.to_vec())
}
}

impl<'a> TryFrom<&'a ArrowData> for &'a [u16] {
type Error = eyre::Report;
fn try_from(value: &'a ArrowData) -> Result<Self, Self::Error> {
let array: &PrimitiveArray<arrow::datatypes::UInt16Type> = value
.as_primitive_opt()
.wrap_err("not a primitive UInt16Type array")?;
if array.null_count() != 0 {
eyre::bail!("array has nulls");
}
Ok(array.values())
}
}

impl<'a> TryFrom<&'a ArrowData> for Vec<u16> {
type Error = eyre::Report;
fn try_from(value: &'a ArrowData) -> Result<Self, Self::Error> {
value.try_into().map(|slice: &'a [u16]| slice.to_vec())
}
}

impl<'a> TryFrom<&'a ArrowData> for &'a [u32] {
type Error = eyre::Report;
fn try_from(value: &'a ArrowData) -> Result<Self, Self::Error> {
let array: &PrimitiveArray<arrow::datatypes::UInt32Type> = value
.as_primitive_opt()
.wrap_err("not a primitive UInt32Type array")?;
if array.null_count() != 0 {
eyre::bail!("array has nulls");
}
Ok(array.values())
}
}

impl<'a> TryFrom<&'a ArrowData> for Vec<u32> {
type Error = eyre::Report;
fn try_from(value: &'a ArrowData) -> Result<Self, Self::Error> {
value.try_into().map(|slice: &'a [u32]| slice.to_vec())
}
}

impl<'a> TryFrom<&'a ArrowData> for &'a [u64] {
type Error = eyre::Report;
fn try_from(value: &'a ArrowData) -> Result<Self, Self::Error> {
let array: &PrimitiveArray<arrow::datatypes::UInt64Type> = value
.as_primitive_opt()
.wrap_err("not a primitive UInt64Type array")?;
if array.null_count() != 0 {
eyre::bail!("array has nulls");
}
Ok(array.values())
}
}

impl<'a> TryFrom<&'a ArrowData> for Vec<u64> {
type Error = eyre::Report;
fn try_from(value: &'a ArrowData) -> Result<Self, Self::Error> {
value.try_into().map(|slice: &'a [u64]| slice.to_vec())
}
}

impl<'a> TryFrom<&'a ArrowData> for &'a [i8] {
type Error = eyre::Report;
fn try_from(value: &'a ArrowData) -> Result<Self, Self::Error> {
let array: &PrimitiveArray<arrow::datatypes::Int8Type> = value
.as_primitive_opt()
.wrap_err("not a primitive Int8Type array")?;
if array.null_count() != 0 {
eyre::bail!("array has nulls");
}
Ok(array.values())
}
}

impl<'a> TryFrom<&'a ArrowData> for Vec<i8> {
type Error = eyre::Report;
fn try_from(value: &'a ArrowData) -> Result<Self, Self::Error> {
value.try_into().map(|slice: &'a [i8]| slice.to_vec())
}
}

impl<'a> TryFrom<&'a ArrowData> for &'a [i16] {
type Error = eyre::Report;
fn try_from(value: &'a ArrowData) -> Result<Self, Self::Error> {
let array: &PrimitiveArray<arrow::datatypes::Int16Type> = value
.as_primitive_opt()
.wrap_err("not a primitive Int16Type array")?;
if array.null_count() != 0 {
eyre::bail!("array has nulls");
}
Ok(array.values())
}
}

impl<'a> TryFrom<&'a ArrowData> for Vec<i16> {
type Error = eyre::Report;
fn try_from(value: &'a ArrowData) -> Result<Self, Self::Error> {
value.try_into().map(|slice: &'a [i16]| slice.to_vec())
}
}

impl<'a> TryFrom<&'a ArrowData> for &'a [i32] {
type Error = eyre::Report;
fn try_from(value: &'a ArrowData) -> Result<Self, Self::Error> {
let array: &PrimitiveArray<arrow::datatypes::Int32Type> = value
.as_primitive_opt()
.wrap_err("not a primitive Int32Type array")?;
if array.null_count() != 0 {
eyre::bail!("array has nulls");
}
Ok(array.values())
}
}

impl<'a> TryFrom<&'a ArrowData> for Vec<i32> {
type Error = eyre::Report;
fn try_from(value: &'a ArrowData) -> Result<Self, Self::Error> {
value.try_into().map(|slice: &'a [i32]| slice.to_vec())
}
}

impl<'a> TryFrom<&'a ArrowData> for &'a [i64] {
type Error = eyre::Report;
fn try_from(value: &'a ArrowData) -> Result<Self, Self::Error> {
let array: &PrimitiveArray<arrow::datatypes::Int64Type> = value
.as_primitive_opt()
.wrap_err("not a primitive Int64Type array")?;
if array.null_count() != 0 {
eyre::bail!("array has nulls");
}
Ok(array.values())
}
}

impl<'a> TryFrom<&'a ArrowData> for Vec<i64> {
type Error = eyre::Report;
fn try_from(value: &'a ArrowData) -> Result<Self, Self::Error> {
value.try_into().map(|slice: &'a [i64]| slice.to_vec())
}
}

impl<'a> TryFrom<&'a ArrowData> for &'a [f32] {
type Error = eyre::Report;
fn try_from(value: &'a ArrowData) -> Result<Self, Self::Error> {
let array: &PrimitiveArray<arrow::datatypes::Float32Type> = value
.as_primitive_opt()
.wrap_err("not a primitive Float32Type array")?;
if array.null_count() != 0 {
eyre::bail!("array has nulls");
}
Ok(array.values())
}
}

impl<'a> TryFrom<&'a ArrowData> for Vec<f32> {
type Error = eyre::Report;
fn try_from(value: &'a ArrowData) -> Result<Self, Self::Error> {
value.try_into().map(|slice: &'a [f32]| slice.to_vec())
}
}

impl<'a> TryFrom<&'a ArrowData> for &'a [f64] {
type Error = eyre::Report;
fn try_from(value: &'a ArrowData) -> Result<Self, Self::Error> {
let array: &PrimitiveArray<arrow::datatypes::Float64Type> = value
.as_primitive_opt()
.wrap_err("not a primitive Float64Type array")?;
if array.null_count() != 0 {
eyre::bail!("array has nulls");
}
Ok(array.values())
}
}

impl<'a> TryFrom<&'a ArrowData> for Vec<f64> {
type Error = eyre::Report;
fn try_from(value: &'a ArrowData) -> Result<Self, Self::Error> {
value.try_into().map(|slice: &'a [f64]| slice.to_vec())
}
}

impl<'a> TryFrom<&'a ArrowData> for String {
type Error = eyre::Report;
fn try_from(value: &'a ArrowData) -> Result<Self, Self::Error> {


+ 13
- 13
libraries/arrow-convert/src/into_impls.rs View File

@@ -26,19 +26,6 @@ macro_rules! impl_into_arrow_for_uint {
};
}

macro_rules! impl_into_arrow_for_vec {
($($t:ty => $arrow_type:ty),*) => {
$(
impl IntoArrow for Vec<$t> {
type A = PrimitiveArray<$arrow_type>;
fn into_arrow(self) -> Self::A {
self.into()
}
}
)*
};
}

impl_into_arrow_for_uint!(
u8 => UInt8Type,
u16 => UInt16Type,
@@ -52,6 +39,19 @@ impl_into_arrow_for_uint!(
f64 => Float64Type
);

macro_rules! impl_into_arrow_for_vec {
($($t:ty => $arrow_type:ty),*) => {
$(
impl IntoArrow for Vec<$t> {
type A = PrimitiveArray<$arrow_type>;
fn into_arrow(self) -> Self::A {
self.into()
}
}
)*
};
}

impl_into_arrow_for_vec!(
u8 => UInt8Type,
u16 => UInt16Type,


Loading…
Cancel
Save