alamb commented on code in PR #3096:
URL: https://github.com/apache/arrow-rs/pull/3096#discussion_r1020881738
##########
arrow-array/src/delta.rs:
##########
@@ -105,75 +105,186 @@ mod tests {
#[test]
fn test_shift_months() {
- let base = NaiveDate::from_ymd(2020, 1, 31);
-
- assert_eq!(shift_months(base, 0), NaiveDate::from_ymd(2020, 1, 31));
- assert_eq!(shift_months(base, 1), NaiveDate::from_ymd(2020, 2, 29));
- assert_eq!(shift_months(base, 2), NaiveDate::from_ymd(2020, 3, 31));
- assert_eq!(shift_months(base, 3), NaiveDate::from_ymd(2020, 4, 30));
- assert_eq!(shift_months(base, 4), NaiveDate::from_ymd(2020, 5, 31));
- assert_eq!(shift_months(base, 5), NaiveDate::from_ymd(2020, 6, 30));
- assert_eq!(shift_months(base, 6), NaiveDate::from_ymd(2020, 7, 31));
- assert_eq!(shift_months(base, 7), NaiveDate::from_ymd(2020, 8, 31));
- assert_eq!(shift_months(base, 8), NaiveDate::from_ymd(2020, 9, 30));
- assert_eq!(shift_months(base, 9), NaiveDate::from_ymd(2020, 10, 31));
- assert_eq!(shift_months(base, 10), NaiveDate::from_ymd(2020, 11, 30));
- assert_eq!(shift_months(base, 11), NaiveDate::from_ymd(2020, 12, 31));
- assert_eq!(shift_months(base, 12), NaiveDate::from_ymd(2021, 1, 31));
- assert_eq!(shift_months(base, 13), NaiveDate::from_ymd(2021, 2, 28));
-
- assert_eq!(shift_months(base, -1), NaiveDate::from_ymd(2019, 12, 31));
- assert_eq!(shift_months(base, -2), NaiveDate::from_ymd(2019, 11, 30));
- assert_eq!(shift_months(base, -3), NaiveDate::from_ymd(2019, 10, 31));
- assert_eq!(shift_months(base, -4), NaiveDate::from_ymd(2019, 9, 30));
- assert_eq!(shift_months(base, -5), NaiveDate::from_ymd(2019, 8, 31));
- assert_eq!(shift_months(base, -6), NaiveDate::from_ymd(2019, 7, 31));
- assert_eq!(shift_months(base, -7), NaiveDate::from_ymd(2019, 6, 30));
- assert_eq!(shift_months(base, -8), NaiveDate::from_ymd(2019, 5, 31));
- assert_eq!(shift_months(base, -9), NaiveDate::from_ymd(2019, 4, 30));
- assert_eq!(shift_months(base, -10), NaiveDate::from_ymd(2019, 3, 31));
- assert_eq!(shift_months(base, -11), NaiveDate::from_ymd(2019, 2, 28));
- assert_eq!(shift_months(base, -12), NaiveDate::from_ymd(2019, 1, 31));
- assert_eq!(shift_months(base, -13), NaiveDate::from_ymd(2018, 12, 31));
-
- assert_eq!(shift_months(base, 1265), NaiveDate::from_ymd(2125, 6, 30));
+ let base = NaiveDate::from_ymd_opt(2020, 1, 31).unwrap();
+
+ assert_eq!(
+ shift_months(base, 0),
+ NaiveDate::from_ymd_opt(2020, 1, 31).unwrap()
+ );
+ assert_eq!(
+ shift_months(base, 1),
+ NaiveDate::from_ymd_opt(2020, 2, 29).unwrap()
+ );
+ assert_eq!(
+ shift_months(base, 2),
+ NaiveDate::from_ymd_opt(2020, 3, 31).unwrap()
+ );
+ assert_eq!(
+ shift_months(base, 3),
+ NaiveDate::from_ymd_opt(2020, 4, 30).unwrap()
+ );
+ assert_eq!(
+ shift_months(base, 4),
+ NaiveDate::from_ymd_opt(2020, 5, 31).unwrap()
+ );
+ assert_eq!(
+ shift_months(base, 5),
+ NaiveDate::from_ymd_opt(2020, 6, 30).unwrap()
+ );
+ assert_eq!(
+ shift_months(base, 6),
+ NaiveDate::from_ymd_opt(2020, 7, 31).unwrap()
+ );
+ assert_eq!(
+ shift_months(base, 7),
+ NaiveDate::from_ymd_opt(2020, 8, 31).unwrap()
+ );
+ assert_eq!(
+ shift_months(base, 8),
+ NaiveDate::from_ymd_opt(2020, 9, 30).unwrap()
+ );
+ assert_eq!(
+ shift_months(base, 9),
+ NaiveDate::from_ymd_opt(2020, 10, 31).unwrap()
+ );
+ assert_eq!(
+ shift_months(base, 10),
+ NaiveDate::from_ymd_opt(2020, 11, 30).unwrap()
+ );
+ assert_eq!(
+ shift_months(base, 11),
+ NaiveDate::from_ymd_opt(2020, 12, 31).unwrap()
+ );
+ assert_eq!(
+ shift_months(base, 12),
+ NaiveDate::from_ymd_opt(2021, 1, 31).unwrap()
+ );
+ assert_eq!(
+ shift_months(base, 13),
+ NaiveDate::from_ymd_opt(2021, 2, 28).unwrap()
+ );
+
+ assert_eq!(
+ shift_months(base, -1),
+ NaiveDate::from_ymd_opt(2019, 12, 31).unwrap()
+ );
+ assert_eq!(
+ shift_months(base, -2),
+ NaiveDate::from_ymd_opt(2019, 11, 30).unwrap()
+ );
+ assert_eq!(
+ shift_months(base, -3),
+ NaiveDate::from_ymd_opt(2019, 10, 31).unwrap()
+ );
+ assert_eq!(
+ shift_months(base, -4),
+ NaiveDate::from_ymd_opt(2019, 9, 30).unwrap()
+ );
+ assert_eq!(
+ shift_months(base, -5),
+ NaiveDate::from_ymd_opt(2019, 8, 31).unwrap()
+ );
+ assert_eq!(
+ shift_months(base, -6),
+ NaiveDate::from_ymd_opt(2019, 7, 31).unwrap()
+ );
+ assert_eq!(
+ shift_months(base, -7),
+ NaiveDate::from_ymd_opt(2019, 6, 30).unwrap()
+ );
+ assert_eq!(
+ shift_months(base, -8),
+ NaiveDate::from_ymd_opt(2019, 5, 31).unwrap()
+ );
+ assert_eq!(
+ shift_months(base, -9),
+ NaiveDate::from_ymd_opt(2019, 4, 30).unwrap()
+ );
+ assert_eq!(
+ shift_months(base, -10),
+ NaiveDate::from_ymd_opt(2019, 3, 31).unwrap()
+ );
+ assert_eq!(
+ shift_months(base, -11),
+ NaiveDate::from_ymd_opt(2019, 2, 28).unwrap()
+ );
+ assert_eq!(
+ shift_months(base, -12),
+ NaiveDate::from_ymd_opt(2019, 1, 31).unwrap()
+ );
+ assert_eq!(
+ shift_months(base, -13),
+ NaiveDate::from_ymd_opt(2018, 12, 31).unwrap()
+ );
+
+ assert_eq!(
+ shift_months(base, 1265),
+ NaiveDate::from_ymd_opt(2125, 6, 30).unwrap()
+ );
}
#[test]
fn test_shift_months_with_overflow() {
- let base = NaiveDate::from_ymd(2020, 12, 31);
+ let base = NaiveDate::from_ymd_opt(2020, 12, 31).unwrap();
assert_eq!(shift_months(base, 0), base);
- assert_eq!(shift_months(base, 1), NaiveDate::from_ymd(2021, 1, 31));
Review Comment:
🤔 this feels very familiar / similar to the work in
https://github.com/apache/arrow-datafusion/pull/4189/files#diff-880863b84a09b210db1fc00012b1f3fbf0ade7f23c4297e15b54474ec65cb814R107
I'll see if I can find a way to remove the copy in DataFusion
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]