You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by GitBox <gi...@apache.org> on 2022/09/01 07:16:14 UTC
[GitHub] [arrow-rs] viirya opened a new pull request, #2623: Support DictionaryArray in temporal kernels
viirya opened a new pull request, #2623:
URL: https://github.com/apache/arrow-rs/pull/2623
# Which issue does this PR close?
<!--
We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes #123` indicates that this PR will close issue #123.
-->
Closes #2622.
# Rationale for this change
<!--
Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed.
Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes.
-->
# What changes are included in this PR?
<!--
There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR.
-->
# Are there any user-facing changes?
<!--
If there are user-facing changes then we may require documentation to be updated before approving the PR.
-->
<!---
If there are any breaking changes to public APIs, please add the `breaking change` label.
-->
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow-rs] viirya commented on a diff in pull request #2623: Support DictionaryArray in temporal kernels
Posted by GitBox <gi...@apache.org>.
viirya commented on code in PR #2623:
URL: https://github.com/apache/arrow-rs/pull/2623#discussion_r964113724
##########
arrow/src/compute/kernels/temporal.rs:
##########
@@ -171,335 +172,747 @@ pub fn using_chrono_tz_and_utc_naive_date_time(
.ok()
}
-/// Extracts the hours of a given temporal array as an array of integers
+/// Extracts the hours of a given temporal primitive array as an array of integers
pub fn hour<T>(array: &PrimitiveArray<T>) -> Result<Int32Array>
+where
+ T: ArrowTemporalType + ArrowNumericType,
+ i64: std::convert::From<T::Native>,
+{
+ hour_generic::<T, _>(array)
+}
+
+/// Extracts the hours of a given temporal array as an array of integers
+pub fn hour_generic<T, A: ArrayAccessor<Item = T::Native>>(array: A) -> Result<Int32Array>
+where
+ T: ArrowTemporalType + ArrowNumericType,
+ i64: std::convert::From<T::Native>,
+{
+ match array.data_type().clone() {
+ DataType::Dictionary(_, value_type) => {
+ hour_internal::<T, A>(array, value_type.as_ref())
+ }
+ dt => hour_internal::<T, A>(array, &dt),
+ }
+}
+
+/// Extracts the hours of a given temporal array as an array of integers
+fn hour_internal<T, A: ArrayAccessor<Item = T::Native>>(
+ array: A,
+ dt: &DataType,
+) -> Result<Int32Array>
where
T: ArrowTemporalType + ArrowNumericType,
i64: std::convert::From<T::Native>,
{
let mut b = Int32Builder::with_capacity(array.len());
- match array.data_type() {
- &DataType::Time32(_) | &DataType::Time64(_) => {
- extract_component_from_array!(array, b, hour, value_as_time, |h| h as i32)
+ match dt {
+ DataType::Time32(_) | DataType::Time64(_) => {
+ let iter = ArrayIter::new(array);
+ extract_component_from_array!(
+ iter,
+ b,
+ hour,
+ |value| as_time::<T>(i64::from(value)),
+ |h| h as i32
+ );
}
- &DataType::Date32 | &DataType::Date64 | &DataType::Timestamp(_, None) => {
- extract_component_from_array!(array, b, hour, value_as_datetime, |h| h as i32)
+ DataType::Date32 | DataType::Date64 | DataType::Timestamp(_, None) => {
+ let iter = ArrayIter::new(array);
+ extract_component_from_array!(
+ iter,
+ b,
+ hour,
+ |value| as_datetime::<T>(i64::from(value)),
+ |h| h as i32
+ )
}
- &DataType::Timestamp(_, Some(ref tz)) => {
+ DataType::Timestamp(_, Some(tz)) => {
let mut scratch = Parsed::new();
+ let iter = ArrayIter::new(array);
extract_component_from_array!(
- array,
+ iter,
b,
hour,
- value_as_datetime_with_tz,
+ |value, tz| as_datetime::<T>(i64::from(value))
+ .map(|datetime| datetime + tz),
tz,
scratch,
+ |value| as_datetime::<T>(i64::from(value)),
|h| h as i32
)
}
- dt => return_compute_error_with!("hour does not support", dt),
+ _ => return_compute_error_with!("hour does not support", array.data_type()),
}
Ok(b.finish())
}
-/// Extracts the years of a given temporal array as an array of integers
+/// Extracts the years of a given temporal primitive array as an array of integers
pub fn year<T>(array: &PrimitiveArray<T>) -> Result<Int32Array>
+where
+ T: ArrowTemporalType + ArrowNumericType,
+ i64: std::convert::From<T::Native>,
+{
+ year_generic::<T, _>(array)
+}
+
+/// Extracts the years of a given temporal array as an array of integers
+pub fn year_generic<T, A: ArrayAccessor<Item = T::Native>>(array: A) -> Result<Int32Array>
+where
+ T: ArrowTemporalType + ArrowNumericType,
+ i64: std::convert::From<T::Native>,
+{
+ match array.data_type().clone() {
+ DataType::Dictionary(_, value_type) => {
+ year_internal::<T, A>(array, value_type.as_ref())
+ }
+ dt => year_internal::<T, A>(array, &dt),
+ }
+}
+
+/// Extracts the years of a given temporal array as an array of integers
+fn year_internal<T, A: ArrayAccessor<Item = T::Native>>(
+ array: A,
+ dt: &DataType,
+) -> Result<Int32Array>
where
T: ArrowTemporalType + ArrowNumericType,
i64: std::convert::From<T::Native>,
{
let mut b = Int32Builder::with_capacity(array.len());
- match array.data_type() {
- &DataType::Date32 | &DataType::Date64 | &DataType::Timestamp(_, _) => {
- extract_component_from_array!(array, b, year, value_as_datetime, |h| h as i32)
+ match dt {
+ DataType::Date32 | DataType::Date64 | DataType::Timestamp(_, _) => {
+ let iter = ArrayIter::new(array);
+ extract_component_from_array!(
+ iter,
+ b,
+ year,
+ |value| as_datetime::<T>(i64::from(value)),
+ |h| h as i32
+ )
}
- dt => return_compute_error_with!("year does not support", dt),
+ _t => return_compute_error_with!("year does not support", array.data_type()),
}
Ok(b.finish())
}
-/// Extracts the quarter of a given temporal array as an array of integers
+/// Extracts the quarter of a given temporal primitive array as an array of integers
Review Comment:
It is the quarter within the range of [1, 4].
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow-rs] viirya commented on a diff in pull request #2623: Support DictionaryArray in temporal kernels
Posted by GitBox <gi...@apache.org>.
viirya commented on code in PR #2623:
URL: https://github.com/apache/arrow-rs/pull/2623#discussion_r961907131
##########
arrow/src/compute/kernels/temporal.rs:
##########
@@ -172,109 +173,227 @@ pub fn using_chrono_tz_and_utc_naive_date_time(
}
/// Extracts the hours of a given temporal array as an array of integers
-pub fn hour<T>(array: &PrimitiveArray<T>) -> Result<Int32Array>
+pub fn hour<T, A: ArrayAccessor<Item = T::Native>>(array: A) -> Result<Int32Array>
where
T: ArrowTemporalType + ArrowNumericType,
+ T::Native: ArrowNativeType,
+ i64: std::convert::From<T::Native>,
+{
+ match array.data_type().clone() {
+ DataType::Dictionary(_, value_type) => {
+ hour_internal::<T, A>(array, value_type.as_ref().clone())
+ }
+ dt => hour_internal::<T, A>(array, dt),
+ }
+}
+
+/// Extracts the hours of a given temporal array as an array of integers
+fn hour_internal<T, A: ArrayAccessor<Item = T::Native>>(
+ array: A,
+ dt: DataType,
+) -> Result<Int32Array>
+where
+ T: ArrowTemporalType + ArrowNumericType,
+ T::Native: ArrowNativeType,
i64: std::convert::From<T::Native>,
{
let mut b = Int32Builder::with_capacity(array.len());
- match array.data_type() {
- &DataType::Time32(_) | &DataType::Time64(_) => {
- extract_component_from_array!(array, b, hour, value_as_time, |h| h as i32)
+ match dt {
+ DataType::Time32(_) | DataType::Time64(_) => {
+ let iter = ArrayIter::new(array);
+ extract_component_from_array!(
+ iter,
+ b,
+ hour,
+ |value| as_time::<T>(i64::from(value)),
+ |h| h as i32
+ );
}
- &DataType::Date32 | &DataType::Date64 | &DataType::Timestamp(_, None) => {
- extract_component_from_array!(array, b, hour, value_as_datetime, |h| h as i32)
+ DataType::Date32 | DataType::Date64 | DataType::Timestamp(_, None) => {
+ let iter = ArrayIter::new(array);
+ extract_component_from_array!(
+ iter,
+ b,
+ hour,
+ |value| as_datetime::<T>(i64::from(value)),
+ |h| h as i32
+ )
}
- &DataType::Timestamp(_, Some(ref tz)) => {
+ DataType::Timestamp(_, Some(tz)) => {
let mut scratch = Parsed::new();
+ let iter = ArrayIter::new(array);
extract_component_from_array!(
- array,
+ iter,
b,
hour,
- value_as_datetime_with_tz,
+ |value, tz| as_datetime::<T>(i64::from(value))
+ .map(|datetime| datetime + tz),
tz,
scratch,
+ |value| as_datetime::<T>(i64::from(value)),
|h| h as i32
)
}
- dt => return_compute_error_with!("hour does not support", dt),
+ _ => return_compute_error_with!("hour does not support", array.data_type()),
}
Ok(b.finish())
}
/// Extracts the years of a given temporal array as an array of integers
-pub fn year<T>(array: &PrimitiveArray<T>) -> Result<Int32Array>
+pub fn year<T, A: ArrayAccessor<Item = T::Native>>(array: A) -> Result<Int32Array>
+where
+ T: ArrowTemporalType + ArrowNumericType,
+ T::Native: ArrowNativeType,
Review Comment:
Removed.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow-rs] viirya commented on a diff in pull request #2623: Support DictionaryArray in temporal kernels
Posted by GitBox <gi...@apache.org>.
viirya commented on code in PR #2623:
URL: https://github.com/apache/arrow-rs/pull/2623#discussion_r960301233
##########
arrow/src/compute/kernels/temporal.rs:
##########
@@ -27,6 +28,85 @@ use chrono::format::strftime::StrftimeItems;
use chrono::format::{parse, Parsed};
use chrono::FixedOffset;
+macro_rules! _extract_component_from_array {
Review Comment:
To avoid conflict with another PR which changes the macro too, I made a copy and modified it. I will rename it later.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow-rs] viirya commented on pull request #2623: Support DictionaryArray in temporal kernels
Posted by GitBox <gi...@apache.org>.
viirya commented on PR #2623:
URL: https://github.com/apache/arrow-rs/pull/2623#issuecomment-1235010906
cc @sunchao
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow-rs] viirya commented on pull request #2623: Support DictionaryArray in temporal kernels
Posted by GitBox <gi...@apache.org>.
viirya commented on PR #2623:
URL: https://github.com/apache/arrow-rs/pull/2623#issuecomment-1235220059
> Perhaps we could keep the signature the same, but provide a more generic version with a suffix, e.g. `_generic`. Otherwise this is an ergonomic step-back for the case of a non-dictionary encoded array, which is likely by far the common case
Yea, I was hesitating between adding `_generic` version and replacing existing kernel names. Ideally I'd like to provide the kernels of same names but are generic version. But as you seen, it comes along with the loss of automatic type hinting in the end.
I will add `_generic` suffix to these new kernels and add back the original ones with same signature.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow-rs] viirya commented on pull request #2623: Support DictionaryArray in temporal kernels
Posted by GitBox <gi...@apache.org>.
viirya commented on PR #2623:
URL: https://github.com/apache/arrow-rs/pull/2623#issuecomment-1236427636
Any more comments? @sunchao @tustvold
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow-rs] viirya commented on a diff in pull request #2623: Support DictionaryArray in temporal kernels
Posted by GitBox <gi...@apache.org>.
viirya commented on code in PR #2623:
URL: https://github.com/apache/arrow-rs/pull/2623#discussion_r961907465
##########
arrow/src/compute/kernels/temporal.rs:
##########
@@ -286,35 +405,65 @@ where
/// Monday is encoded as `0`, Tuesday as `1`, etc.
///
/// See also [`num_days_from_sunday`] which starts at Sunday.
-pub fn num_days_from_monday<T>(array: &PrimitiveArray<T>) -> Result<Int32Array>
+pub fn num_days_from_monday<T, A: ArrayAccessor<Item = T::Native>>(
+ array: A,
+) -> Result<Int32Array>
where
T: ArrowTemporalType + ArrowNumericType,
+ T::Native: ArrowNativeType,
+ i64: std::convert::From<T::Native>,
+{
+ match array.data_type().clone() {
+ DataType::Dictionary(_, value_type) => {
+ num_days_from_monday_internal::<T, A>(array, value_type.as_ref().clone())
Review Comment:
Yea, `&DataType` is okay. Removed `clone`.
##########
arrow/src/compute/kernels/temporal.rs:
##########
@@ -516,7 +832,7 @@ mod tests {
let a: PrimitiveArray<Date64Type> =
vec![Some(1514764800000), None, Some(1550636625000)].into();
- let b = hour(&a).unwrap();
+ let b = hour::<Date64Type, _>(&a).unwrap();
Review Comment:
Restored.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow-rs] viirya commented on a diff in pull request #2623: Support DictionaryArray in temporal kernels
Posted by GitBox <gi...@apache.org>.
viirya commented on code in PR #2623:
URL: https://github.com/apache/arrow-rs/pull/2623#discussion_r964103198
##########
arrow/src/compute/kernels/temporal.rs:
##########
@@ -171,335 +172,747 @@ pub fn using_chrono_tz_and_utc_naive_date_time(
.ok()
}
-/// Extracts the hours of a given temporal array as an array of integers
+/// Extracts the hours of a given temporal primitive array as an array of integers
pub fn hour<T>(array: &PrimitiveArray<T>) -> Result<Int32Array>
+where
+ T: ArrowTemporalType + ArrowNumericType,
+ i64: std::convert::From<T::Native>,
+{
+ hour_generic::<T, _>(array)
+}
+
+/// Extracts the hours of a given temporal array as an array of integers
+pub fn hour_generic<T, A: ArrayAccessor<Item = T::Native>>(array: A) -> Result<Int32Array>
Review Comment:
Please see https://github.com/apache/arrow-rs/pull/2623#discussion_r961346254. I began with a generic version of `hour` to replace existing one. But it will be loss of automatic type hinting as we always need to specify at least `T` when calling the generic `hour`. Seems a step back ergonomically for the common case.
So currently I provide a generic version and keep original `hour` signature untouched.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow-rs] viirya commented on a diff in pull request #2623: Support DictionaryArray in temporal kernels
Posted by GitBox <gi...@apache.org>.
viirya commented on code in PR #2623:
URL: https://github.com/apache/arrow-rs/pull/2623#discussion_r960301233
##########
arrow/src/compute/kernels/temporal.rs:
##########
@@ -27,6 +28,85 @@ use chrono::format::strftime::StrftimeItems;
use chrono::format::{parse, Parsed};
use chrono::FixedOffset;
+macro_rules! _extract_component_from_array {
Review Comment:
To avoid conflict with another PR which changes the macro too, I made a copy and modified it. I will merge it to the `extract_component_from_array` macro later.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow-rs] viirya commented on a diff in pull request #2623: Support DictionaryArray in temporal kernels
Posted by GitBox <gi...@apache.org>.
viirya commented on code in PR #2623:
URL: https://github.com/apache/arrow-rs/pull/2623#discussion_r960998136
##########
arrow/src/compute/kernels/temporal.rs:
##########
@@ -28,40 +29,40 @@ use chrono::format::{parse, Parsed};
use chrono::FixedOffset;
macro_rules! extract_component_from_array {
- ($array:ident, $builder:ident, $extract_fn:ident, $using:ident, $convert:expr) => {
- for i in 0..$array.len() {
- if $array.is_null(i) {
- $builder.append_null();
- } else {
- match $array.$using(i) {
+ ($iter:ident, $builder:ident, $extract_fn:ident, $using:expr, $convert:expr) => {
+ $iter.into_iter().for_each(|value| {
+ if let Some(value) = value {
+ match $using(value) {
Some(dt) => $builder.append_value($convert(dt.$extract_fn())),
None => $builder.append_null(),
}
+ } else {
+ $builder.append_null();
}
- }
+ })
Review Comment:
This change basically rewrites the macro using ArrayAccessor API. Otherwise the logic is the same.
Previously the macro can call `value_as_datetime` or `value_as_datetime_with_tz` APIs on temporal array, but now we need to call `as_datetime` on the value instead.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow-rs] viirya commented on pull request #2623: Support DictionaryArray in temporal kernels
Posted by GitBox <gi...@apache.org>.
viirya commented on PR #2623:
URL: https://github.com/apache/arrow-rs/pull/2623#issuecomment-1238997352
Thanks for review.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow-rs] sunchao commented on a diff in pull request #2623: Support DictionaryArray in temporal kernels
Posted by GitBox <gi...@apache.org>.
sunchao commented on code in PR #2623:
URL: https://github.com/apache/arrow-rs/pull/2623#discussion_r964216595
##########
arrow/src/compute/kernels/temporal.rs:
##########
@@ -171,335 +172,751 @@ pub fn using_chrono_tz_and_utc_naive_date_time(
.ok()
}
-/// Extracts the hours of a given temporal array as an array of integers
+/// Extracts the hours of a given temporal primitive array as an array of integers within
+/// the range of [0, 23].
pub fn hour<T>(array: &PrimitiveArray<T>) -> Result<Int32Array>
+where
+ T: ArrowTemporalType + ArrowNumericType,
+ i64: std::convert::From<T::Native>,
+{
+ hour_generic::<T, _>(array)
+}
+
+/// Extracts the hours of a given temporal array as an array of integers within
+/// the range of [0, 23].
+pub fn hour_generic<T, A: ArrayAccessor<Item = T::Native>>(array: A) -> Result<Int32Array>
+where
+ T: ArrowTemporalType + ArrowNumericType,
+ i64: std::convert::From<T::Native>,
+{
+ match array.data_type().clone() {
+ DataType::Dictionary(_, value_type) => {
+ hour_internal::<T, A>(array, value_type.as_ref())
+ }
+ dt => hour_internal::<T, A>(array, &dt),
+ }
+}
+
+/// Extracts the hours of a given temporal array as an array of integers
+fn hour_internal<T, A: ArrayAccessor<Item = T::Native>>(
+ array: A,
+ dt: &DataType,
+) -> Result<Int32Array>
where
T: ArrowTemporalType + ArrowNumericType,
i64: std::convert::From<T::Native>,
{
let mut b = Int32Builder::with_capacity(array.len());
- match array.data_type() {
- &DataType::Time32(_) | &DataType::Time64(_) => {
- extract_component_from_array!(array, b, hour, value_as_time, |h| h as i32)
+ match dt {
+ DataType::Time32(_) | DataType::Time64(_) => {
+ let iter = ArrayIter::new(array);
+ extract_component_from_array!(
+ iter,
+ b,
+ hour,
+ |value| as_time::<T>(i64::from(value)),
+ |h| h as i32
+ );
}
- &DataType::Date32 | &DataType::Date64 | &DataType::Timestamp(_, None) => {
- extract_component_from_array!(array, b, hour, value_as_datetime, |h| h as i32)
+ DataType::Date32 | DataType::Date64 | DataType::Timestamp(_, None) => {
+ let iter = ArrayIter::new(array);
+ extract_component_from_array!(
+ iter,
+ b,
+ hour,
+ |value| as_datetime::<T>(i64::from(value)),
+ |h| h as i32
+ )
}
- &DataType::Timestamp(_, Some(ref tz)) => {
+ DataType::Timestamp(_, Some(tz)) => {
let mut scratch = Parsed::new();
+ let iter = ArrayIter::new(array);
extract_component_from_array!(
- array,
+ iter,
b,
hour,
- value_as_datetime_with_tz,
+ |value, tz| as_datetime::<T>(i64::from(value))
+ .map(|datetime| datetime + tz),
tz,
scratch,
+ |value| as_datetime::<T>(i64::from(value)),
|h| h as i32
)
}
- dt => return_compute_error_with!("hour does not support", dt),
+ _ => return_compute_error_with!("hour does not support", array.data_type()),
}
Ok(b.finish())
}
-/// Extracts the years of a given temporal array as an array of integers
+/// Extracts the years of a given temporal primitive array as an array of integers
pub fn year<T>(array: &PrimitiveArray<T>) -> Result<Int32Array>
+where
+ T: ArrowTemporalType + ArrowNumericType,
+ i64: std::convert::From<T::Native>,
+{
+ year_generic::<T, _>(array)
+}
+
+/// Extracts the years of a given temporal array as an array of integers
+pub fn year_generic<T, A: ArrayAccessor<Item = T::Native>>(array: A) -> Result<Int32Array>
+where
+ T: ArrowTemporalType + ArrowNumericType,
+ i64: std::convert::From<T::Native>,
+{
+ match array.data_type().clone() {
+ DataType::Dictionary(_, value_type) => {
+ year_internal::<T, A>(array, value_type.as_ref())
+ }
+ dt => year_internal::<T, A>(array, &dt),
+ }
+}
+
+/// Extracts the years of a given temporal array as an array of integers
+fn year_internal<T, A: ArrayAccessor<Item = T::Native>>(
+ array: A,
+ dt: &DataType,
+) -> Result<Int32Array>
where
T: ArrowTemporalType + ArrowNumericType,
i64: std::convert::From<T::Native>,
{
let mut b = Int32Builder::with_capacity(array.len());
- match array.data_type() {
- &DataType::Date32 | &DataType::Date64 | &DataType::Timestamp(_, _) => {
- extract_component_from_array!(array, b, year, value_as_datetime, |h| h as i32)
+ match dt {
+ DataType::Date32 | DataType::Date64 | DataType::Timestamp(_, _) => {
+ let iter = ArrayIter::new(array);
+ extract_component_from_array!(
+ iter,
+ b,
+ year,
+ |value| as_datetime::<T>(i64::from(value)),
+ |h| h as i32
+ )
}
- dt => return_compute_error_with!("year does not support", dt),
+ _t => return_compute_error_with!("year does not support", array.data_type()),
}
Ok(b.finish())
}
-/// Extracts the quarter of a given temporal array as an array of integers
+/// Extracts the quarter of a given temporal primitive array as an array of integers within
+/// the range of [1, 4].
pub fn quarter<T>(array: &PrimitiveArray<T>) -> Result<Int32Array>
+where
+ T: ArrowTemporalType + ArrowNumericType,
+ i64: std::convert::From<T::Native>,
+{
+ quarter_generic::<T, _>(array)
+}
+
+/// Extracts the quarter of a given temporal array as an array of integersa within
+/// the range of [1, 4].
+pub fn quarter_generic<T, A: ArrayAccessor<Item = T::Native>>(
+ array: A,
+) -> Result<Int32Array>
+where
+ T: ArrowTemporalType + ArrowNumericType,
+ i64: std::convert::From<T::Native>,
+{
+ match array.data_type().clone() {
+ DataType::Dictionary(_, value_type) => {
+ quarter_internal::<T, A>(array, value_type.as_ref())
+ }
+ dt => quarter_internal::<T, A>(array, &dt),
+ }
+}
+
+/// Extracts the quarter of a given temporal array as an array of integers
+fn quarter_internal<T, A: ArrayAccessor<Item = T::Native>>(
+ array: A,
+ dt: &DataType,
+) -> Result<Int32Array>
where
T: ArrowTemporalType + ArrowNumericType,
i64: std::convert::From<T::Native>,
{
let mut b = Int32Builder::with_capacity(array.len());
- match array.data_type() {
- &DataType::Date32 | &DataType::Date64 | &DataType::Timestamp(_, None) => {
- extract_component_from_array!(array, b, quarter, value_as_datetime, |h| h
- as i32)
+ match dt {
+ DataType::Date32 | DataType::Date64 | DataType::Timestamp(_, None) => {
+ let iter = ArrayIter::new(array);
+ extract_component_from_array!(
+ iter,
+ b,
+ quarter,
+ |value| as_datetime::<T>(i64::from(value)),
+ |h| h as i32
+ )
}
- &DataType::Timestamp(_, Some(ref tz)) => {
+ DataType::Timestamp(_, Some(tz)) => {
let mut scratch = Parsed::new();
+ let iter = ArrayIter::new(array);
extract_component_from_array!(
- array,
+ iter,
b,
quarter,
- value_as_datetime_with_tz,
+ |value, tz| as_datetime::<T>(i64::from(value))
+ .map(|datetime| datetime + tz),
tz,
scratch,
+ |value| as_datetime::<T>(i64::from(value)),
|h| h as i32
)
}
- dt => return_compute_error_with!("quarter does not support", dt),
+ _ => return_compute_error_with!("quarter does not support", array.data_type()),
}
Ok(b.finish())
}
-/// Extracts the month of a given temporal array as an array of integers
+/// Extracts the month of a given temporal primitive array as an array of integers
Review Comment:
nit: is the returned integers 0-based or 1-based? might add a comment. We should follow the methods like `num_days_from_monday` which have some nice documentation.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow-rs] sunchao commented on a diff in pull request #2623: Support DictionaryArray in temporal kernels
Posted by GitBox <gi...@apache.org>.
sunchao commented on code in PR #2623:
URL: https://github.com/apache/arrow-rs/pull/2623#discussion_r963904942
##########
arrow/src/compute/kernels/cast.rs:
##########
@@ -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);
+ extract_component_from_array!(
+ iter,
+ builder,
+ to_string,
+ |value| as_datetime::<T>(
+ <i64 as From<<T as ArrowPrimitiveType>::Native>>::from(value)
Review Comment:
nit: we can just use `<i64 as From<_>>::from(value)` and let type inference do the work
##########
arrow/src/compute/kernels/temporal.rs:
##########
@@ -171,335 +172,747 @@ pub fn using_chrono_tz_and_utc_naive_date_time(
.ok()
}
-/// Extracts the hours of a given temporal array as an array of integers
+/// Extracts the hours of a given temporal primitive array as an array of integers
Review Comment:
nit: add comments explaining what exactly are the returned integers, are they within range `[0, 24)`?
##########
arrow/src/compute/kernels/temporal.rs:
##########
@@ -171,335 +172,747 @@ pub fn using_chrono_tz_and_utc_naive_date_time(
.ok()
}
-/// Extracts the hours of a given temporal array as an array of integers
+/// Extracts the hours of a given temporal primitive array as an array of integers
pub fn hour<T>(array: &PrimitiveArray<T>) -> Result<Int32Array>
+where
+ T: ArrowTemporalType + ArrowNumericType,
+ i64: std::convert::From<T::Native>,
+{
+ hour_generic::<T, _>(array)
+}
+
+/// Extracts the hours of a given temporal array as an array of integers
+pub fn hour_generic<T, A: ArrayAccessor<Item = T::Native>>(array: A) -> Result<Int32Array>
+where
+ T: ArrowTemporalType + ArrowNumericType,
+ i64: std::convert::From<T::Native>,
+{
+ match array.data_type().clone() {
+ DataType::Dictionary(_, value_type) => {
+ hour_internal::<T, A>(array, value_type.as_ref())
+ }
+ dt => hour_internal::<T, A>(array, &dt),
+ }
+}
+
+/// Extracts the hours of a given temporal array as an array of integers
+fn hour_internal<T, A: ArrayAccessor<Item = T::Native>>(
+ array: A,
+ dt: &DataType,
+) -> Result<Int32Array>
where
T: ArrowTemporalType + ArrowNumericType,
i64: std::convert::From<T::Native>,
{
let mut b = Int32Builder::with_capacity(array.len());
- match array.data_type() {
- &DataType::Time32(_) | &DataType::Time64(_) => {
- extract_component_from_array!(array, b, hour, value_as_time, |h| h as i32)
+ match dt {
+ DataType::Time32(_) | DataType::Time64(_) => {
+ let iter = ArrayIter::new(array);
+ extract_component_from_array!(
+ iter,
+ b,
+ hour,
+ |value| as_time::<T>(i64::from(value)),
+ |h| h as i32
+ );
}
- &DataType::Date32 | &DataType::Date64 | &DataType::Timestamp(_, None) => {
- extract_component_from_array!(array, b, hour, value_as_datetime, |h| h as i32)
+ DataType::Date32 | DataType::Date64 | DataType::Timestamp(_, None) => {
+ let iter = ArrayIter::new(array);
+ extract_component_from_array!(
+ iter,
+ b,
+ hour,
+ |value| as_datetime::<T>(i64::from(value)),
+ |h| h as i32
+ )
}
- &DataType::Timestamp(_, Some(ref tz)) => {
+ DataType::Timestamp(_, Some(tz)) => {
let mut scratch = Parsed::new();
+ let iter = ArrayIter::new(array);
extract_component_from_array!(
- array,
+ iter,
b,
hour,
- value_as_datetime_with_tz,
+ |value, tz| as_datetime::<T>(i64::from(value))
+ .map(|datetime| datetime + tz),
tz,
scratch,
+ |value| as_datetime::<T>(i64::from(value)),
|h| h as i32
)
}
- dt => return_compute_error_with!("hour does not support", dt),
+ _ => return_compute_error_with!("hour does not support", array.data_type()),
}
Ok(b.finish())
}
-/// Extracts the years of a given temporal array as an array of integers
+/// Extracts the years of a given temporal primitive array as an array of integers
pub fn year<T>(array: &PrimitiveArray<T>) -> Result<Int32Array>
+where
+ T: ArrowTemporalType + ArrowNumericType,
+ i64: std::convert::From<T::Native>,
+{
+ year_generic::<T, _>(array)
+}
+
+/// Extracts the years of a given temporal array as an array of integers
+pub fn year_generic<T, A: ArrayAccessor<Item = T::Native>>(array: A) -> Result<Int32Array>
+where
+ T: ArrowTemporalType + ArrowNumericType,
+ i64: std::convert::From<T::Native>,
+{
+ match array.data_type().clone() {
+ DataType::Dictionary(_, value_type) => {
+ year_internal::<T, A>(array, value_type.as_ref())
+ }
+ dt => year_internal::<T, A>(array, &dt),
+ }
+}
+
+/// Extracts the years of a given temporal array as an array of integers
+fn year_internal<T, A: ArrayAccessor<Item = T::Native>>(
+ array: A,
+ dt: &DataType,
+) -> Result<Int32Array>
where
T: ArrowTemporalType + ArrowNumericType,
i64: std::convert::From<T::Native>,
{
let mut b = Int32Builder::with_capacity(array.len());
- match array.data_type() {
- &DataType::Date32 | &DataType::Date64 | &DataType::Timestamp(_, _) => {
- extract_component_from_array!(array, b, year, value_as_datetime, |h| h as i32)
+ match dt {
+ DataType::Date32 | DataType::Date64 | DataType::Timestamp(_, _) => {
+ let iter = ArrayIter::new(array);
+ extract_component_from_array!(
+ iter,
+ b,
+ year,
+ |value| as_datetime::<T>(i64::from(value)),
+ |h| h as i32
+ )
}
- dt => return_compute_error_with!("year does not support", dt),
+ _t => return_compute_error_with!("year does not support", array.data_type()),
}
Ok(b.finish())
}
-/// Extracts the quarter of a given temporal array as an array of integers
+/// Extracts the quarter of a given temporal primitive array as an array of integers
Review Comment:
ditto: what do the returned integers represent?
##########
arrow/src/compute/kernels/temporal.rs:
##########
@@ -28,40 +29,40 @@ use chrono::format::{parse, Parsed};
use chrono::FixedOffset;
macro_rules! extract_component_from_array {
- ($array:ident, $builder:ident, $extract_fn:ident, $using:ident, $convert:expr) => {
- for i in 0..$array.len() {
- if $array.is_null(i) {
- $builder.append_null();
- } else {
- match $array.$using(i) {
+ ($iter:ident, $builder:ident, $extract_fn:ident, $using:expr, $convert:expr) => {
Review Comment:
+1. It'd be better to improve the readability here.
##########
arrow/src/compute/kernels/temporal.rs:
##########
@@ -171,335 +172,747 @@ pub fn using_chrono_tz_and_utc_naive_date_time(
.ok()
}
-/// Extracts the hours of a given temporal array as an array of integers
+/// Extracts the hours of a given temporal primitive array as an array of integers
pub fn hour<T>(array: &PrimitiveArray<T>) -> Result<Int32Array>
+where
+ T: ArrowTemporalType + ArrowNumericType,
+ i64: std::convert::From<T::Native>,
+{
+ hour_generic::<T, _>(array)
+}
+
+/// Extracts the hours of a given temporal array as an array of integers
+pub fn hour_generic<T, A: ArrayAccessor<Item = T::Native>>(array: A) -> Result<Int32Array>
Review Comment:
why we need this `hour_generic` method? I feel we just need `hour`.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow-rs] viirya commented on a diff in pull request #2623: Support DictionaryArray in temporal kernels
Posted by GitBox <gi...@apache.org>.
viirya commented on code in PR #2623:
URL: https://github.com/apache/arrow-rs/pull/2623#discussion_r961904069
##########
arrow/src/compute/kernels/cast.rs:
##########
@@ -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);
Review Comment:
Yea
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow-rs] ursabot commented on pull request #2623: Support DictionaryArray in temporal kernels
Posted by GitBox <gi...@apache.org>.
ursabot commented on PR #2623:
URL: https://github.com/apache/arrow-rs/pull/2623#issuecomment-1238998612
Benchmark runs are scheduled for baseline = d73d78f0bb3dd06ea2f7c5975df53a91a20f2e69 and contender = c8bf1ca39d28d2983feb2639309bac0f5318b04a. c8bf1ca39d28d2983feb2639309bac0f5318b04a is a master commit associated with this PR. Results will be available as each benchmark for each run completes.
Conbench compare runs links:
[Skipped :warning: Benchmarking of arrow-rs-commits is not supported on ec2-t3-xlarge-us-east-2] [ec2-t3-xlarge-us-east-2](https://conbench.ursa.dev/compare/runs/e196acedaebd4894a164370095beff4c...807ba8a6277f41e1a005af358b12ce23/)
[Skipped :warning: Benchmarking of arrow-rs-commits is not supported on test-mac-arm] [test-mac-arm](https://conbench.ursa.dev/compare/runs/fcd1138986b8467dab292ac2079ffae2...6bfa88e266074242890b4bb6c2a33632/)
[Skipped :warning: Benchmarking of arrow-rs-commits is not supported on ursa-i9-9960x] [ursa-i9-9960x](https://conbench.ursa.dev/compare/runs/85a969df5890409f98ef44eaff3c8670...ad4cf4bfb9c440d381a53174f5856a19/)
[Skipped :warning: Benchmarking of arrow-rs-commits is not supported on ursa-thinkcentre-m75q] [ursa-thinkcentre-m75q](https://conbench.ursa.dev/compare/runs/009aef05e5024d74bb1fb5994e524b5a...1aae327aed1c4d5ebe6442724e8626cf/)
Buildkite builds:
Supported benchmarks:
ec2-t3-xlarge-us-east-2: Supported benchmark langs: Python, R. Runs only benchmarks with cloud = True
test-mac-arm: Supported benchmark langs: C++, Python, R
ursa-i9-9960x: Supported benchmark langs: Python, R, JavaScript
ursa-thinkcentre-m75q: Supported benchmark langs: C++, Java
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow-rs] viirya merged pull request #2623: Support DictionaryArray in temporal kernels
Posted by GitBox <gi...@apache.org>.
viirya merged PR #2623:
URL: https://github.com/apache/arrow-rs/pull/2623
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow-rs] viirya commented on a diff in pull request #2623: Support DictionaryArray in temporal kernels
Posted by GitBox <gi...@apache.org>.
viirya commented on code in PR #2623:
URL: https://github.com/apache/arrow-rs/pull/2623#discussion_r964103872
##########
arrow/src/compute/kernels/temporal.rs:
##########
@@ -28,40 +29,40 @@ use chrono::format::{parse, Parsed};
use chrono::FixedOffset;
macro_rules! extract_component_from_array {
- ($array:ident, $builder:ident, $extract_fn:ident, $using:ident, $convert:expr) => {
- for i in 0..$array.len() {
- if $array.is_null(i) {
- $builder.append_null();
- } else {
- match $array.$using(i) {
+ ($iter:ident, $builder:ident, $extract_fn:ident, $using:expr, $convert:expr) => {
Review Comment:
Yea, I will find some time trying to simplify it. 😅
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow-rs] viirya commented on a diff in pull request #2623: Support DictionaryArray in temporal kernels
Posted by GitBox <gi...@apache.org>.
viirya commented on code in PR #2623:
URL: https://github.com/apache/arrow-rs/pull/2623#discussion_r964220717
##########
arrow/src/compute/kernels/temporal.rs:
##########
@@ -171,335 +172,751 @@ pub fn using_chrono_tz_and_utc_naive_date_time(
.ok()
}
-/// Extracts the hours of a given temporal array as an array of integers
+/// Extracts the hours of a given temporal primitive array as an array of integers within
+/// the range of [0, 23].
pub fn hour<T>(array: &PrimitiveArray<T>) -> Result<Int32Array>
+where
+ T: ArrowTemporalType + ArrowNumericType,
+ i64: std::convert::From<T::Native>,
+{
+ hour_generic::<T, _>(array)
+}
+
+/// Extracts the hours of a given temporal array as an array of integers within
+/// the range of [0, 23].
+pub fn hour_generic<T, A: ArrayAccessor<Item = T::Native>>(array: A) -> Result<Int32Array>
+where
+ T: ArrowTemporalType + ArrowNumericType,
+ i64: std::convert::From<T::Native>,
+{
+ match array.data_type().clone() {
+ DataType::Dictionary(_, value_type) => {
+ hour_internal::<T, A>(array, value_type.as_ref())
+ }
+ dt => hour_internal::<T, A>(array, &dt),
+ }
+}
+
+/// Extracts the hours of a given temporal array as an array of integers
+fn hour_internal<T, A: ArrayAccessor<Item = T::Native>>(
+ array: A,
+ dt: &DataType,
+) -> Result<Int32Array>
where
T: ArrowTemporalType + ArrowNumericType,
i64: std::convert::From<T::Native>,
{
let mut b = Int32Builder::with_capacity(array.len());
- match array.data_type() {
- &DataType::Time32(_) | &DataType::Time64(_) => {
- extract_component_from_array!(array, b, hour, value_as_time, |h| h as i32)
+ match dt {
+ DataType::Time32(_) | DataType::Time64(_) => {
+ let iter = ArrayIter::new(array);
+ extract_component_from_array!(
+ iter,
+ b,
+ hour,
+ |value| as_time::<T>(i64::from(value)),
+ |h| h as i32
+ );
}
- &DataType::Date32 | &DataType::Date64 | &DataType::Timestamp(_, None) => {
- extract_component_from_array!(array, b, hour, value_as_datetime, |h| h as i32)
+ DataType::Date32 | DataType::Date64 | DataType::Timestamp(_, None) => {
+ let iter = ArrayIter::new(array);
+ extract_component_from_array!(
+ iter,
+ b,
+ hour,
+ |value| as_datetime::<T>(i64::from(value)),
+ |h| h as i32
+ )
}
- &DataType::Timestamp(_, Some(ref tz)) => {
+ DataType::Timestamp(_, Some(tz)) => {
let mut scratch = Parsed::new();
+ let iter = ArrayIter::new(array);
extract_component_from_array!(
- array,
+ iter,
b,
hour,
- value_as_datetime_with_tz,
+ |value, tz| as_datetime::<T>(i64::from(value))
+ .map(|datetime| datetime + tz),
tz,
scratch,
+ |value| as_datetime::<T>(i64::from(value)),
|h| h as i32
)
}
- dt => return_compute_error_with!("hour does not support", dt),
+ _ => return_compute_error_with!("hour does not support", array.data_type()),
}
Ok(b.finish())
}
-/// Extracts the years of a given temporal array as an array of integers
+/// Extracts the years of a given temporal primitive array as an array of integers
pub fn year<T>(array: &PrimitiveArray<T>) -> Result<Int32Array>
+where
+ T: ArrowTemporalType + ArrowNumericType,
+ i64: std::convert::From<T::Native>,
+{
+ year_generic::<T, _>(array)
+}
+
+/// Extracts the years of a given temporal array as an array of integers
+pub fn year_generic<T, A: ArrayAccessor<Item = T::Native>>(array: A) -> Result<Int32Array>
+where
+ T: ArrowTemporalType + ArrowNumericType,
+ i64: std::convert::From<T::Native>,
+{
+ match array.data_type().clone() {
+ DataType::Dictionary(_, value_type) => {
+ year_internal::<T, A>(array, value_type.as_ref())
+ }
+ dt => year_internal::<T, A>(array, &dt),
+ }
+}
+
+/// Extracts the years of a given temporal array as an array of integers
+fn year_internal<T, A: ArrayAccessor<Item = T::Native>>(
+ array: A,
+ dt: &DataType,
+) -> Result<Int32Array>
where
T: ArrowTemporalType + ArrowNumericType,
i64: std::convert::From<T::Native>,
{
let mut b = Int32Builder::with_capacity(array.len());
- match array.data_type() {
- &DataType::Date32 | &DataType::Date64 | &DataType::Timestamp(_, _) => {
- extract_component_from_array!(array, b, year, value_as_datetime, |h| h as i32)
+ match dt {
+ DataType::Date32 | DataType::Date64 | DataType::Timestamp(_, _) => {
+ let iter = ArrayIter::new(array);
+ extract_component_from_array!(
+ iter,
+ b,
+ year,
+ |value| as_datetime::<T>(i64::from(value)),
+ |h| h as i32
+ )
}
- dt => return_compute_error_with!("year does not support", dt),
+ _t => return_compute_error_with!("year does not support", array.data_type()),
}
Ok(b.finish())
}
-/// Extracts the quarter of a given temporal array as an array of integers
+/// Extracts the quarter of a given temporal primitive array as an array of integers within
+/// the range of [1, 4].
pub fn quarter<T>(array: &PrimitiveArray<T>) -> Result<Int32Array>
+where
+ T: ArrowTemporalType + ArrowNumericType,
+ i64: std::convert::From<T::Native>,
+{
+ quarter_generic::<T, _>(array)
+}
+
+/// Extracts the quarter of a given temporal array as an array of integersa within
+/// the range of [1, 4].
+pub fn quarter_generic<T, A: ArrayAccessor<Item = T::Native>>(
+ array: A,
+) -> Result<Int32Array>
+where
+ T: ArrowTemporalType + ArrowNumericType,
+ i64: std::convert::From<T::Native>,
+{
+ match array.data_type().clone() {
+ DataType::Dictionary(_, value_type) => {
+ quarter_internal::<T, A>(array, value_type.as_ref())
+ }
+ dt => quarter_internal::<T, A>(array, &dt),
+ }
+}
+
+/// Extracts the quarter of a given temporal array as an array of integers
+fn quarter_internal<T, A: ArrayAccessor<Item = T::Native>>(
+ array: A,
+ dt: &DataType,
+) -> Result<Int32Array>
where
T: ArrowTemporalType + ArrowNumericType,
i64: std::convert::From<T::Native>,
{
let mut b = Int32Builder::with_capacity(array.len());
- match array.data_type() {
- &DataType::Date32 | &DataType::Date64 | &DataType::Timestamp(_, None) => {
- extract_component_from_array!(array, b, quarter, value_as_datetime, |h| h
- as i32)
+ match dt {
+ DataType::Date32 | DataType::Date64 | DataType::Timestamp(_, None) => {
+ let iter = ArrayIter::new(array);
+ extract_component_from_array!(
+ iter,
+ b,
+ quarter,
+ |value| as_datetime::<T>(i64::from(value)),
+ |h| h as i32
+ )
}
- &DataType::Timestamp(_, Some(ref tz)) => {
+ DataType::Timestamp(_, Some(tz)) => {
let mut scratch = Parsed::new();
+ let iter = ArrayIter::new(array);
extract_component_from_array!(
- array,
+ iter,
b,
quarter,
- value_as_datetime_with_tz,
+ |value, tz| as_datetime::<T>(i64::from(value))
+ .map(|datetime| datetime + tz),
tz,
scratch,
+ |value| as_datetime::<T>(i64::from(value)),
|h| h as i32
)
}
- dt => return_compute_error_with!("quarter does not support", dt),
+ _ => return_compute_error_with!("quarter does not support", array.data_type()),
}
Ok(b.finish())
}
-/// Extracts the month of a given temporal array as an array of integers
+/// Extracts the month of a given temporal primitive array as an array of integers
Review Comment:
It's 1-based. I will add a comment.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow-rs] viirya commented on a diff in pull request #2623: Support DictionaryArray in temporal kernels
Posted by GitBox <gi...@apache.org>.
viirya commented on code in PR #2623:
URL: https://github.com/apache/arrow-rs/pull/2623#discussion_r960998136
##########
arrow/src/compute/kernels/temporal.rs:
##########
@@ -28,40 +29,40 @@ use chrono::format::{parse, Parsed};
use chrono::FixedOffset;
macro_rules! extract_component_from_array {
- ($array:ident, $builder:ident, $extract_fn:ident, $using:ident, $convert:expr) => {
- for i in 0..$array.len() {
- if $array.is_null(i) {
- $builder.append_null();
- } else {
- match $array.$using(i) {
+ ($iter:ident, $builder:ident, $extract_fn:ident, $using:expr, $convert:expr) => {
+ $iter.into_iter().for_each(|value| {
+ if let Some(value) = value {
+ match $using(value) {
Some(dt) => $builder.append_value($convert(dt.$extract_fn())),
None => $builder.append_null(),
}
+ } else {
+ $builder.append_null();
}
- }
+ })
Review Comment:
This change basically rewrites the macro using ArrayAccessor API. Otherwise the logic is the same.
Previously the macro can call `value_as_datetime`, `value_as_datetime_with_tz` APIs on temporal array, but now we need to call `as_datetime` on the value instead.
##########
arrow/src/compute/kernels/temporal.rs:
##########
@@ -28,40 +29,40 @@ use chrono::format::{parse, Parsed};
use chrono::FixedOffset;
macro_rules! extract_component_from_array {
- ($array:ident, $builder:ident, $extract_fn:ident, $using:ident, $convert:expr) => {
- for i in 0..$array.len() {
- if $array.is_null(i) {
- $builder.append_null();
- } else {
- match $array.$using(i) {
+ ($iter:ident, $builder:ident, $extract_fn:ident, $using:expr, $convert:expr) => {
+ $iter.into_iter().for_each(|value| {
+ if let Some(value) = value {
+ match $using(value) {
Some(dt) => $builder.append_value($convert(dt.$extract_fn())),
None => $builder.append_null(),
}
+ } else {
+ $builder.append_null();
}
- }
+ })
Review Comment:
This change basically rewrites the macro using ArrayAccessor API. Otherwise the logic is the same.
Previously the macro can call `value_as_datetime`or `value_as_datetime_with_tz` APIs on temporal array, but now we need to call `as_datetime` on the value instead.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow-rs] tustvold commented on a diff in pull request #2623: Support DictionaryArray in temporal kernels
Posted by GitBox <gi...@apache.org>.
tustvold commented on code in PR #2623:
URL: https://github.com/apache/arrow-rs/pull/2623#discussion_r961346254
##########
arrow/src/compute/kernels/temporal.rs:
##########
@@ -516,7 +832,7 @@ mod tests {
let a: PrimitiveArray<Date64Type> =
vec![Some(1514764800000), None, Some(1550636625000)].into();
- let b = hour(&a).unwrap();
+ let b = hour::<Date64Type, _>(&a).unwrap();
Review Comment:
This loss of automatic type hinting is a bit unfortunate, it feels like a step-back ergonomically for the common case
##########
arrow/src/compute/kernels/temporal.rs:
##########
@@ -286,35 +405,65 @@ where
/// Monday is encoded as `0`, Tuesday as `1`, etc.
///
/// See also [`num_days_from_sunday`] which starts at Sunday.
-pub fn num_days_from_monday<T>(array: &PrimitiveArray<T>) -> Result<Int32Array>
+pub fn num_days_from_monday<T, A: ArrayAccessor<Item = T::Native>>(
+ array: A,
+) -> Result<Int32Array>
where
T: ArrowTemporalType + ArrowNumericType,
+ T::Native: ArrowNativeType,
+ i64: std::convert::From<T::Native>,
+{
+ match array.data_type().clone() {
+ DataType::Dictionary(_, value_type) => {
+ num_days_from_monday_internal::<T, A>(array, value_type.as_ref().clone())
Review Comment:
Is this `clone` really necessary? Can't num_days_from_monday_internal take `&DataType`
##########
arrow/src/compute/kernels/temporal.rs:
##########
@@ -172,109 +173,227 @@ pub fn using_chrono_tz_and_utc_naive_date_time(
}
/// Extracts the hours of a given temporal array as an array of integers
-pub fn hour<T>(array: &PrimitiveArray<T>) -> Result<Int32Array>
+pub fn hour<T, A: ArrayAccessor<Item = T::Native>>(array: A) -> Result<Int32Array>
where
T: ArrowTemporalType + ArrowNumericType,
+ T::Native: ArrowNativeType,
+ i64: std::convert::From<T::Native>,
+{
+ match array.data_type().clone() {
+ DataType::Dictionary(_, value_type) => {
+ hour_internal::<T, A>(array, value_type.as_ref().clone())
+ }
+ dt => hour_internal::<T, A>(array, dt),
+ }
+}
+
+/// Extracts the hours of a given temporal array as an array of integers
+fn hour_internal<T, A: ArrayAccessor<Item = T::Native>>(
+ array: A,
+ dt: DataType,
+) -> Result<Int32Array>
+where
+ T: ArrowTemporalType + ArrowNumericType,
+ T::Native: ArrowNativeType,
i64: std::convert::From<T::Native>,
{
let mut b = Int32Builder::with_capacity(array.len());
- match array.data_type() {
- &DataType::Time32(_) | &DataType::Time64(_) => {
- extract_component_from_array!(array, b, hour, value_as_time, |h| h as i32)
+ match dt {
+ DataType::Time32(_) | DataType::Time64(_) => {
+ let iter = ArrayIter::new(array);
+ extract_component_from_array!(
+ iter,
+ b,
+ hour,
+ |value| as_time::<T>(i64::from(value)),
+ |h| h as i32
+ );
}
- &DataType::Date32 | &DataType::Date64 | &DataType::Timestamp(_, None) => {
- extract_component_from_array!(array, b, hour, value_as_datetime, |h| h as i32)
+ DataType::Date32 | DataType::Date64 | DataType::Timestamp(_, None) => {
+ let iter = ArrayIter::new(array);
+ extract_component_from_array!(
+ iter,
+ b,
+ hour,
+ |value| as_datetime::<T>(i64::from(value)),
+ |h| h as i32
+ )
}
- &DataType::Timestamp(_, Some(ref tz)) => {
+ DataType::Timestamp(_, Some(tz)) => {
let mut scratch = Parsed::new();
+ let iter = ArrayIter::new(array);
extract_component_from_array!(
- array,
+ iter,
b,
hour,
- value_as_datetime_with_tz,
+ |value, tz| as_datetime::<T>(i64::from(value))
+ .map(|datetime| datetime + tz),
tz,
scratch,
+ |value| as_datetime::<T>(i64::from(value)),
|h| h as i32
)
}
- dt => return_compute_error_with!("hour does not support", dt),
+ _ => return_compute_error_with!("hour does not support", array.data_type()),
}
Ok(b.finish())
}
/// Extracts the years of a given temporal array as an array of integers
-pub fn year<T>(array: &PrimitiveArray<T>) -> Result<Int32Array>
+pub fn year<T, A: ArrayAccessor<Item = T::Native>>(array: A) -> Result<Int32Array>
+where
+ T: ArrowTemporalType + ArrowNumericType,
+ T::Native: ArrowNativeType,
Review Comment:
I think this should be implied by the definition of `ArrowPrimitiveType`
##########
arrow/src/compute/kernels/temporal.rs:
##########
@@ -28,40 +29,40 @@ use chrono::format::{parse, Parsed};
use chrono::FixedOffset;
macro_rules! extract_component_from_array {
- ($array:ident, $builder:ident, $extract_fn:ident, $using:ident, $convert:expr) => {
- for i in 0..$array.len() {
- if $array.is_null(i) {
- $builder.append_null();
- } else {
- match $array.$using(i) {
+ ($iter:ident, $builder:ident, $extract_fn:ident, $using:expr, $convert:expr) => {
Review Comment:
I have a really hard time following this macro, I really wonder if we can't replace it with generics or at the very least simplify it... Something for a future PR I suspect
##########
arrow/src/compute/kernels/cast.rs:
##########
@@ -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);
Review Comment:
Why is this `ArrayIter` needed? The macro calls `into_iter`?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow-rs] viirya commented on pull request #2623: Support DictionaryArray in temporal kernels
Posted by GitBox <gi...@apache.org>.
viirya commented on PR #2623:
URL: https://github.com/apache/arrow-rs/pull/2623#issuecomment-1238617104
> Looks OK. Do you know whether `arrow-rs` follows [Gregorian calendar](https://en.wikipedia.org/wiki/Gregorian_calendar) or [Proleptic Gregorian calendar](https://en.wikipedia.org/wiki/Proleptic_Gregorian_calendar) in terms of handling timestamp prior to 1582-10-15?
It is not explicitly mentioned in this crate. But as datetime operation in arrow-rs uses chrono crate which follows ISO 8601. I think it follows Proleptic Gregorian calendar.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow-rs] viirya commented on a diff in pull request #2623: Support DictionaryArray in temporal kernels
Posted by GitBox <gi...@apache.org>.
viirya commented on code in PR #2623:
URL: https://github.com/apache/arrow-rs/pull/2623#discussion_r964113401
##########
arrow/src/compute/kernels/temporal.rs:
##########
@@ -171,335 +172,747 @@ pub fn using_chrono_tz_and_utc_naive_date_time(
.ok()
}
-/// Extracts the hours of a given temporal array as an array of integers
+/// Extracts the hours of a given temporal primitive array as an array of integers
Review Comment:
Yea, [0, 23].
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org