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

Enhance Time32/Time64 support in date_part #5337

Merged

Conversation

Jefffrey
Copy link
Contributor

Which issue does this PR close?

Closes #5261

Rationale for this change

Want to be able to extract hour/minute/second/etc. from Time32/Time64 types via date_part temporal kernel

  • Note we shouldn't support extracting month/year/etc. for the time types since it wouldn't make sense

What changes are included in this PR?

Enhance Time32/Time64 support in date_part

Are there any user-facing changes?

@github-actions github-actions bot added the arrow Changes to the arrow crate label Jan 27, 2024
Comment on lines 323 to 364
DatePart::Hour => Ok(self.unary_opt(|ns| {
if (0..NANOSECONDS_IN_DAY).contains(&ns) {
Some((ns / 3_600 / NANOSECONDS) as i32)
} else {
None
}
})),
DatePart::Minute => Ok(self.unary_opt(|ns| {
if (0..NANOSECONDS_IN_DAY).contains(&ns) {
Some(((ns / 60 / NANOSECONDS) % 60) as i32)
} else {
None
}
})),
DatePart::Second => Ok(self.unary_opt(|ns| {
if (0..NANOSECONDS_IN_DAY).contains(&ns) {
Some(((ns / NANOSECONDS) % 60) as i32)
} else {
None
}
})),
DatePart::Millisecond => Ok(self.unary_opt(|ns| {
if (0..NANOSECONDS_IN_DAY).contains(&ns) {
Some(((ns % NANOSECONDS) / 1_000_000) as i32)
} else {
None
}
})),
DatePart::Microsecond => Ok(self.unary_opt(|ns| {
if (0..NANOSECONDS_IN_DAY).contains(&ns) {
Some(((ns % NANOSECONDS) / 1_000) as i32)
} else {
None
}
})),
DatePart::Nanosecond => Ok(self.unary_opt(|ns| {
if (0..NANOSECONDS_IN_DAY).contains(&ns) {
Some((ns % NANOSECONDS) as i32)
} else {
None
}
})),
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The Hour/Minute/Second arms could be deduplicated as they almost identical across seconds/milli/micro/nano, but the handling of milli/micro/nano isn't as straight forward I guess. Though could be handled with a macro I suppose? Let me know if prefer to try reduce this duplication or if it's fine as is

DatePart::Hour => Ok(self.unary_opt(|d| time32s_to_time(d).map(|c| c.hour() as i32))),
// TODO expand support for Time types, see: https://github.com/apache/arrow-rs/issues/5261
DatePart::Hour => Ok(self.unary_opt(|s| {
if (0..SECONDS_IN_DAY as i32).contains(&s) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Using Option::then might make this more concise

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Tried applying some refactoring as suggested. Clippy does push for use of Option::then_some so I wonder if its better to use that (even though it eagerly evaluates) or to try force usage of Option::then 🤔

@tustvold tustvold merged commit 1237c89 into apache:master Jan 31, 2024
13 checks passed
@Jefffrey Jefffrey deleted the enhance_temporal_kernel_for_time_types branch January 31, 2024 10:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
arrow Changes to the arrow crate
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support for extracting hours/minutes/seconds/etc. from Time32/Time64 type in temporal kernels
2 participants