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

Fix interval to duration casting (#4553) #4562

Merged
merged 3 commits into from
Jul 26, 2023

Conversation

tustvold
Copy link
Contributor

Which issue does this PR close?

Closes #4553

Rationale for this change

What changes are included in this PR?

Are there any user-facing changes?

@github-actions github-actions bot added the arrow Changes to the arrow crate label Jul 22, 2023
let iter = array.iter().map(|v| {
v.and_then(|v| {
let v = v / scale;
if v > i64::MAX as i128 {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This formulation failed to handle negatives correctly

});
let iter = array
.iter()
.map(|v| v.and_then(|v| (v >> 64 == 0).then(|| (v as i64) / scale)));
Copy link
Contributor Author

Choose a reason for hiding this comment

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

As an added bonus we are now doing 64-bit division which should be cheaper than 128-bit

@@ -8658,10 +8621,6 @@ mod tests {
&CastOptions::default(),
)
.unwrap();
assert_eq!(
Copy link
Contributor Author

Choose a reason for hiding this comment

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

These tests are redundant as they're already encoded in the types returned

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't understand this rationale. While the types are encoded in the return type (e,.g IntervalMonthDayNanoArray) they aren't checked by the test. Thus, if they changed to IntervalDayTime or something I don't think the compiler would fail.

Maybe we could add explicit types, like

 let casted_array: IntervalMonthDayNanoArray = cast_from_duration_to_interval::<DurationSecondType>(...)

Copy link
Contributor

@alamb alamb left a comment

Choose a reason for hiding this comment

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

The code looks very nice to me. I have some questions / suggestions about the tests

@@ -447,20 +447,11 @@ fn cast_interval_day_time_to_interval_month_day_nano(
}

/// Cast the array from interval to duration
fn cast_interval_to_duration<D: ArrowTemporalType<Native = i64>>(
fn cast_month_day_none_to_duration<D: ArrowTemporalType<Native = i64>>(
Copy link
Contributor

Choose a reason for hiding this comment

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

What is the meaning of the _none_ in this name? I couldn't figure it out. Maybe it should be_nano_ 🤔

}
})
});
let iter = array
Copy link
Contributor

Choose a reason for hiding this comment

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

This is very clever ❤️

@@ -8658,10 +8621,6 @@ mod tests {
&CastOptions::default(),
)
.unwrap();
assert_eq!(
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't understand this rationale. While the types are encoded in the return type (e,.g IntervalMonthDayNanoArray) they aren't checked by the test. Thus, if they changed to IntervalDayTime or something I don't think the compiler would fail.

Maybe we could add explicit types, like

 let casted_array: IntervalMonthDayNanoArray = cast_from_duration_to_interval::<DurationSecondType>(...)

let casted_array =
cast_from_interval_to_duration::<DurationNanosecondType>(&array, &nullable)
.unwrap();
assert!(!casted_array.is_valid(0));
Copy link
Contributor

@alamb alamb Jul 24, 2023

Choose a reason for hiding this comment

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

I think this test should also explicitly verify the values for .values(4) (not just that they are valid or not)

@tustvold tustvold merged commit 9688630 into apache:master Jul 26, 2023
25 checks passed
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.

Incorrect Interval to Duration Casting
2 participants