Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support DictionaryArray in temporal kernels #2623

Merged
merged 7 commits into from
Sep 7, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions arrow/src/array/array_primitive.rs
Expand Up @@ -206,7 +206,7 @@ impl<'a, T: ArrowPrimitiveType> ArrayAccessor for &'a PrimitiveArray<T> {
}
}

fn as_datetime<T: ArrowPrimitiveType>(v: i64) -> Option<NaiveDateTime> {
pub(crate) fn as_datetime<T: ArrowPrimitiveType>(v: i64) -> Option<NaiveDateTime> {
match T::DATA_TYPE {
DataType::Date32 => Some(temporal_conversions::date32_to_datetime(v as i32)),
DataType::Date64 => Some(temporal_conversions::date64_to_datetime(v)),
Expand All @@ -233,7 +233,7 @@ fn as_date<T: ArrowPrimitiveType>(v: i64) -> Option<NaiveDate> {
as_datetime::<T>(v).map(|datetime| datetime.date())
}

fn as_time<T: ArrowPrimitiveType>(v: i64) -> Option<NaiveTime> {
pub(crate) fn as_time<T: ArrowPrimitiveType>(v: i64) -> Option<NaiveTime> {
match T::DATA_TYPE {
DataType::Time32(unit) => {
// safe to immediately cast to u32 as `self.value(i)` is positive i32
Expand Down
2 changes: 2 additions & 0 deletions arrow/src/array/mod.rs
Expand Up @@ -221,6 +221,8 @@ pub use self::array::make_array;
pub use self::array::new_empty_array;
pub use self::array::new_null_array;

pub(crate) use self::array_primitive::{as_datetime, as_time};

///
/// # Example: Using `collect`
/// ```
Expand Down
25 changes: 21 additions & 4 deletions arrow/src/compute/kernels/cast.rs
Expand Up @@ -42,6 +42,7 @@ use std::ops::{Div, Mul};
use std::str;
use std::sync::Arc;

use crate::array::as_datetime;
use crate::buffer::MutableBuffer;
use crate::compute::divide_scalar;
use crate::compute::kernels::arithmetic::{divide, multiply};
Expand Down Expand Up @@ -1502,21 +1503,37 @@ where

if let Some(tz) = tz {
let mut scratch = Parsed::new();
// The macro calls `value_as_datetime_with_tz` on timestamp values of the array.
// The macro calls `as_datetime` on timestamp values of the array.
// After applying timezone offset on the datatime, calling `to_string` to get
// the strings.
let iter = ArrayIter::new(array);
extract_component_from_array!(
array,
iter,
builder,
to_string,
value_as_datetime_with_tz,
|value, tz| as_datetime::<T>(<i64 as From<
<T as ArrowPrimitiveType>::Native,
>>::from(value))
.map(|datetime| datetime + tz),
tz,
scratch,
|value| as_datetime::<T>(
<i64 as From<<T as ArrowPrimitiveType>::Native>>::from(value)
),
|h| h
)
} else {
// No timezone available. Calling `to_string` on the datatime value simply.
extract_component_from_array!(array, builder, to_string, value_as_datetime, |h| h)
let iter = ArrayIter::new(array);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this ArrayIter needed? The macro calls into_iter?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea

extract_component_from_array!(
iter,
builder,
to_string,
|value| as_datetime::<T>(
<i64 as From<<T as ArrowPrimitiveType>::Native>>::from(value)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: we can just use <i64 as From<_>>::from(value) and let type inference do the work

),
|h| h
)
}

Ok(Arc::new(builder.finish()) as ArrayRef)
Expand Down