diff --git a/libraries/arrow-convert/src/from_impls.rs b/libraries/arrow-convert/src/from_impls.rs index 89e64170..1a15683e 100644 --- a/libraries/arrow-convert/src/from_impls.rs +++ b/libraries/arrow-convert/src/from_impls.rs @@ -1,6 +1,6 @@ use arrow::{ array::{Array, AsArray, PrimitiveArray, StringArray}, - datatypes::ArrowPrimitiveType, + datatypes::{ArrowPrimitiveType, ArrowTemporalType}, }; use chrono::{NaiveDate, NaiveTime}; @@ -351,53 +351,89 @@ impl<'a> TryFrom<&'a ArrowData> for String { type Error = eyre::Report; fn try_from(value: &'a ArrowData) -> Result { let string_array: Vec = - as Clone>::clone(&value).try_into_collection()?; - return Ok(string_array[0].clone()); + as Clone>::clone(value).try_into_collection()?; + Ok(string_array[0].clone()) } } impl TryFrom<&ArrowData> for NaiveDate { type Error = eyre::Report; fn try_from(value: &ArrowData) -> Result { - let array = value - .as_primitive_opt::() - .context("not a primitive Date32Type array")?; - if array.is_empty() { - eyre::bail!("empty array"); - } - if array.len() != 1 { - eyre::bail!("expected length 1"); + if let Some(array) = value.as_any().downcast_ref::() { + if check_single_datetime(array) { + eyre::bail!("Not a valid array"); + } + return array + .value_as_date(0) + .context("data type cannot be converted to NaiveDate"); } - if array.null_count() != 0 { - eyre::bail!("array has nulls"); + let array = value + .as_any() + .downcast_ref::() + .context("Refrence is neither to a Date32Array nor a Date64Array")?; + if check_single_datetime(array) { + eyre::bail!("Not a valid array"); } - Ok(array + array .value_as_date(0) - .context("data type cannot be converted to NaiveDate")?) + .context("data type cannot be converted to NaiveDate") } } impl TryFrom<&ArrowData> for NaiveTime { type Error = eyre::Report; fn try_from(value: &ArrowData) -> Result { - let array = value - .as_primitive_opt::() - .context("not a primitive Time64NanosecondType array")?; - if array.is_empty() { - eyre::bail!("empty array"); + if let Some(array) = value + .as_any() + .downcast_ref::() + { + if check_single_datetime(array) { + eyre::bail!("Not a valid array"); + } + return array + .value_as_time(0) + .context("data type cannot be converted to NaiveDate"); } - if array.len() != 1 { - eyre::bail!("expected length 1"); + if let Some(array) = value + .as_any() + .downcast_ref::() + { + if check_single_datetime(array) { + eyre::bail!("Not a valid array"); + } + return array + .value_as_time(0) + .context("data type cannot be converted to NaiveDate"); } - if array.null_count() != 0 { - eyre::bail!("array has nulls"); + if let Some(array) = value + .as_any() + .downcast_ref::() + { + if check_single_datetime(array) { + eyre::bail!("Not a valid array"); + } + return array + .value_as_time(0) + .context("data type cannot be converted to NaiveDate"); } - Ok(array + let array = value + .as_primitive_opt::() + .context("not any of the primitive Time arrays")?; + if check_single_datetime(array) { + eyre::bail!("Not a vaild array"); + } + array .value_as_time(0) - .context("data type cannot be converted to NaiveTime")?) + .context("data type cannot be converted to NaiveTime") } } +fn check_single_datetime(array: &PrimitiveArray) -> bool +where + T: ArrowTemporalType, +{ + array.is_empty() || array.len() != 1 || array.null_count() != 0 +} fn extract_single_primitive(array: &PrimitiveArray) -> Result where T: ArrowPrimitiveType, diff --git a/libraries/arrow-convert/src/into_impls.rs b/libraries/arrow-convert/src/into_impls.rs index f15035d6..7597bd51 100644 --- a/libraries/arrow-convert/src/into_impls.rs +++ b/libraries/arrow-convert/src/into_impls.rs @@ -148,17 +148,17 @@ impl IntoArrow for () { } impl IntoArrow for NaiveDate { - type A = arrow::array::Date32Array; + type A = arrow::array::Date64Array; fn into_arrow(self) -> Self::A { - arrow::array::Date32Array::from(vec![arrow::datatypes::Date32Type::from_naive_date(self)]) + arrow::array::Date64Array::from(vec![arrow::datatypes::Date64Type::from_naive_date(self)]) } } impl IntoArrow for NaiveTime { - type A = arrow::array::Time64NanosecondArray; + type A = arrow::array::Time64MicrosecondArray; fn into_arrow(self) -> Self::A { - arrow::array::Time64NanosecondArray::from(vec![ - arrow::array::temporal_conversions::time_to_time64ns(self), + arrow::array::Time64MicrosecondArray::from(vec![ + arrow::array::temporal_conversions::time_to_time64us(self), ]) } }