Browse Source

Now support Date32,Date64,Tim32second,Time32millisecond,Time64microsecond,Time64nanosecond

tags/v0.3.11-rc1
Shar-jeel-Sajid haixuantao 1 year ago
parent
commit
a2ce18ae2b
2 changed files with 67 additions and 31 deletions
  1. +62
    -26
      libraries/arrow-convert/src/from_impls.rs
  2. +5
    -5
      libraries/arrow-convert/src/into_impls.rs

+ 62
- 26
libraries/arrow-convert/src/from_impls.rs View File

@@ -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<Self, Self::Error> {
let string_array: Vec<String> =
<Arc<dyn arrow::array::Array> as Clone>::clone(&value).try_into_collection()?;
return Ok(string_array[0].clone());
<Arc<dyn arrow::array::Array> 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<Self, Self::Error> {
let array = value
.as_primitive_opt::<arrow::datatypes::Date32Type>()
.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::<arrow::array::Date32Array>() {
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::<arrow::array::Date64Array>()
.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<Self, Self::Error> {
let array = value
.as_primitive_opt::<arrow::datatypes::Time64NanosecondType>()
.context("not a primitive Time64NanosecondType array")?;
if array.is_empty() {
eyre::bail!("empty array");
if let Some(array) = value
.as_any()
.downcast_ref::<arrow::array::Time32SecondArray>()
{
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::<arrow::array::Time32MillisecondArray>()
{
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::<arrow::array::Time64MicrosecondArray>()
{
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::<arrow::datatypes::Time64NanosecondType>()
.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<T>(array: &PrimitiveArray<T>) -> bool
where
T: ArrowTemporalType,
{
array.is_empty() || array.len() != 1 || array.null_count() != 0
}
fn extract_single_primitive<T>(array: &PrimitiveArray<T>) -> Result<T::Native, eyre::Error>
where
T: ArrowPrimitiveType,


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

@@ -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),
])
}
}


Loading…
Cancel
Save