From 090ffb0e25718d5521c4fc22a2c5b444443b1f06 Mon Sep 17 00:00:00 2001 From: Mikhail Cheshkov Date: Mon, 26 Aug 2024 17:20:58 +0300 Subject: [PATCH] Allow chrono deprecated functions Can drop this after rebase on commit ccc4417 "Fix clippy by avoiding deprecated functions in chrono (#3096)", first released in 28.0.0 --- arrow/benches/cast_kernels.rs | 2 ++ arrow/src/array/array_primitive.rs | 2 ++ arrow/src/compute/kernels/cast.rs | 5 +++++ arrow/src/compute/kernels/cast_utils.rs | 9 +++++++++ arrow/src/csv/reader.rs | 3 +++ arrow/src/json/writer.rs | 2 ++ arrow/src/temporal_conversions.rs | 10 ++++++++++ parquet/src/record/api.rs | 4 ++++ 8 files changed, 37 insertions(+) diff --git a/arrow/benches/cast_kernels.rs b/arrow/benches/cast_kernels.rs index d164e1facfd3..7f10f1f7062d 100644 --- a/arrow/benches/cast_kernels.rs +++ b/arrow/benches/cast_kernels.rs @@ -51,6 +51,7 @@ fn build_utf8_date_array(size: usize, with_nulls: bool) -> ArrayRef { if with_nulls && rng.gen::() > 0.8 { builder.append_null().unwrap(); } else { + #[allow(deprecated)] let string = NaiveDate::from_num_days_from_ce(rng.sample(range)) .format("%Y-%m-%d") .to_string(); @@ -72,6 +73,7 @@ fn build_utf8_date_time_array(size: usize, with_nulls: bool) -> ArrayRef { if with_nulls && rng.gen::() > 0.8 { builder.append_null().unwrap(); } else { + #[allow(deprecated)] let string = NaiveDateTime::from_timestamp(rng.sample(range), 0) .format("%Y-%m-%dT%H:%M:%S") .to_string(); diff --git a/arrow/src/array/array_primitive.rs b/arrow/src/array/array_primitive.rs index 76c1fcfe8c53..49f163a494fe 100644 --- a/arrow/src/array/array_primitive.rs +++ b/arrow/src/array/array_primitive.rs @@ -198,6 +198,7 @@ fn as_time(v: i64) -> Option { _ => None, }, DataType::Timestamp(_, _) => as_datetime::(v).map(|datetime| datetime.time()), + #[allow(deprecated)] DataType::Date32 | DataType::Date64 => Some(NaiveTime::from_hms(0, 0, 0)), DataType::Interval(_) => None, _ => None, @@ -535,6 +536,7 @@ mod tests { } #[test] + #[allow(deprecated)] fn test_date64_array_from_vec_option() { // Test building a primitive array with null values // we use Int32 and Int64 as a backing array, so all Int32 and Int64 conventions diff --git a/arrow/src/compute/kernels/cast.rs b/arrow/src/compute/kernels/cast.rs index 7474ae41c526..58a6ab7a8e15 100644 --- a/arrow/src/compute/kernels/cast.rs +++ b/arrow/src/compute/kernels/cast.rs @@ -1984,6 +1984,7 @@ fn cast_string_to_date64( if string_array.is_null(i) { None } else { + #[allow(deprecated)] string_array .value(i) .parse::() @@ -2006,6 +2007,7 @@ fn cast_string_to_date64( let string = string_array .value(i); + #[allow(deprecated)] let result = string .parse::() .map(|datetime| datetime.timestamp_millis()); @@ -4297,6 +4299,7 @@ mod tests { #[test] fn test_cast_utf8_to_date32() { use chrono::NaiveDate; + #[allow(deprecated)] let from_ymd = chrono::NaiveDate::from_ymd; let since = chrono::NaiveDate::signed_duration_since; @@ -4312,11 +4315,13 @@ mod tests { let c = b.as_any().downcast_ref::().unwrap(); // test valid inputs + #[allow(deprecated)] let date_value = since(NaiveDate::from_ymd(2000, 1, 1), from_ymd(1970, 1, 1)) .num_days() as i32; assert!(c.is_valid(0)); // "2000-01-01" assert_eq!(date_value, c.value(0)); + #[allow(deprecated)] let date_value = since(NaiveDate::from_ymd(2000, 2, 2), from_ymd(1970, 1, 1)) .num_days() as i32; assert!(c.is_valid(1)); // "2000-2-2" diff --git a/arrow/src/compute/kernels/cast_utils.rs b/arrow/src/compute/kernels/cast_utils.rs index 59862dd6104b..386dd3293ae7 100644 --- a/arrow/src/compute/kernels/cast_utils.rs +++ b/arrow/src/compute/kernels/cast_utils.rs @@ -73,6 +73,7 @@ pub fn string_to_timestamp_nanos(s: &str) -> Result { // Fast path: RFC3339 timestamp (with a T) // Example: 2020-09-08T13:42:29.190855Z if let Ok(ts) = DateTime::parse_from_rfc3339(s) { + #[allow(deprecated)] return Ok(ts.timestamp_nanos()); } @@ -109,6 +110,7 @@ pub fn string_to_timestamp_nanos(s: &str) -> Result { if let Ok(ts) = chrono::format::parse(&mut p, rest, FORMAT1.iter()) .and_then(|()| p.to_datetime()) { + #[allow(deprecated)] return Ok(ts.timestamp_nanos()); } @@ -119,6 +121,7 @@ pub fn string_to_timestamp_nanos(s: &str) -> Result { if let Ok(ts) = chrono::format::parse(&mut p, rest, FORMAT2.iter()) .and_then(|()| p.to_datetime_with_timezone(&Utc)) { + #[allow(deprecated)] return Ok(ts.timestamp_nanos()); } @@ -264,6 +267,7 @@ struct ParsedPrefix { /// nanosecond epoch timestamp relative to UTC. fn naive_datetime_to_timestamp(_s: &str, datetime: NaiveDateTime) -> Result { // CubeStore-specific: do not take timezones into account. + #[allow(deprecated)] Ok(Utc.from_utc_datetime(&datetime).timestamp_nanos()) } @@ -319,6 +323,7 @@ mod tests { /// Interprets a naive_datetime (with no explicit timzone offset) /// using the local timezone and returns the timestamp in UTC (0 /// offset) + #[allow(deprecated)] fn naive_datetime_to_timestamp(naive_datetime: &NaiveDateTime) -> i64 { // Note: Use chrono APIs that are different than // naive_datetime_to_timestamp to compute the utc offset to @@ -341,7 +346,9 @@ mod tests { // timezone the test machine is running. Thus it is still // somewhat suceptable to bugs in the use of chrono let naive_datetime = NaiveDateTime::new( + #[allow(deprecated)] NaiveDate::from_ymd(2020, 9, 8), + #[allow(deprecated)] NaiveTime::from_hms_nano(13, 42, 29, 190855), ); @@ -359,7 +366,9 @@ mod tests { // Also ensure that parsing timestamps with no fractional // second part works as well let naive_datetime_whole_secs = NaiveDateTime::new( + #[allow(deprecated)] NaiveDate::from_ymd(2020, 9, 8), + #[allow(deprecated)] NaiveTime::from_hms(13, 42, 29), ); diff --git a/arrow/src/csv/reader.rs b/arrow/src/csv/reader.rs index d41676bb1529..753a3ca6533c 100644 --- a/arrow/src/csv/reader.rs +++ b/arrow/src/csv/reader.rs @@ -683,6 +683,7 @@ impl Parser for Date64Type { match Self::DATA_TYPE { DataType::Date64 => { let date_time = string.parse::().ok()?; + #[allow(deprecated)] Self::Native::from_i64(date_time.timestamp_millis()) } _ => None, @@ -695,6 +696,7 @@ impl Parser for TimestampNanosecondType { match Self::DATA_TYPE { DataType::Timestamp(TimeUnit::Nanosecond, None) => { let date_time = string.parse::().ok()?; + #[allow(deprecated)] Self::Native::from_i64(date_time.timestamp_nanos()) } _ => None, @@ -707,6 +709,7 @@ impl Parser for TimestampMicrosecondType { match Self::DATA_TYPE { DataType::Timestamp(TimeUnit::Microsecond, None) => { let date_time = string.parse::().ok()?; + #[allow(deprecated)] Self::Native::from_i64(date_time.timestamp_nanos() / 1000) } _ => None, diff --git a/arrow/src/json/writer.rs b/arrow/src/json/writer.rs index 57d83bdc99e2..51bef2b1038d 100644 --- a/arrow/src/json/writer.rs +++ b/arrow/src/json/writer.rs @@ -768,6 +768,7 @@ mod tests { #[test] fn write_timestamps() { let ts_string = "2018-11-13T17:11:10.011375885995"; + #[allow(deprecated)] let ts_nanos = ts_string .parse::() .unwrap() @@ -824,6 +825,7 @@ mod tests { #[test] fn write_dates() { let ts_string = "2018-11-13T17:11:10.011375885995"; + #[allow(deprecated)] let ts_millis = ts_string .parse::() .unwrap() diff --git a/arrow/src/temporal_conversions.rs b/arrow/src/temporal_conversions.rs index 2d6d6776f59e..b37f64306d51 100644 --- a/arrow/src/temporal_conversions.rs +++ b/arrow/src/temporal_conversions.rs @@ -31,12 +31,14 @@ const NANOSECONDS: i64 = 1_000_000_000; /// converts a `i32` representing a `date32` to [`NaiveDateTime`] #[inline] pub fn date32_to_datetime(v: i32) -> NaiveDateTime { + #[allow(deprecated)] NaiveDateTime::from_timestamp(v as i64 * SECONDS_IN_DAY, 0) } /// converts a `i64` representing a `date64` to [`NaiveDateTime`] #[inline] pub fn date64_to_datetime(v: i64) -> NaiveDateTime { + #[allow(deprecated)] NaiveDateTime::from_timestamp( // extract seconds from milliseconds v / MILLISECONDS, @@ -48,6 +50,7 @@ pub fn date64_to_datetime(v: i64) -> NaiveDateTime { /// converts a `i32` representing a `time32(s)` to [`NaiveDateTime`] #[inline] pub fn time32s_to_time(v: i32) -> NaiveTime { + #[allow(deprecated)] NaiveTime::from_num_seconds_from_midnight(v as u32, 0) } @@ -55,6 +58,7 @@ pub fn time32s_to_time(v: i32) -> NaiveTime { #[inline] pub fn time32ms_to_time(v: i32) -> NaiveTime { let v = v as i64; + #[allow(deprecated)] NaiveTime::from_num_seconds_from_midnight( // extract seconds from milliseconds (v / MILLISECONDS) as u32, @@ -67,6 +71,7 @@ pub fn time32ms_to_time(v: i32) -> NaiveTime { /// converts a `i64` representing a `time64(us)` to [`NaiveDateTime`] #[inline] pub fn time64us_to_time(v: i64) -> NaiveTime { + #[allow(deprecated)] NaiveTime::from_num_seconds_from_midnight( // extract seconds from microseconds (v / MICROSECONDS) as u32, @@ -79,6 +84,7 @@ pub fn time64us_to_time(v: i64) -> NaiveTime { /// converts a `i64` representing a `time64(ns)` to [`NaiveDateTime`] #[inline] pub fn time64ns_to_time(v: i64) -> NaiveTime { + #[allow(deprecated)] NaiveTime::from_num_seconds_from_midnight( // extract seconds from nanoseconds (v / NANOSECONDS) as u32, @@ -90,12 +96,14 @@ pub fn time64ns_to_time(v: i64) -> NaiveTime { /// converts a `i64` representing a `timestamp(s)` to [`NaiveDateTime`] #[inline] pub fn timestamp_s_to_datetime(v: i64) -> NaiveDateTime { + #[allow(deprecated)] NaiveDateTime::from_timestamp(v, 0) } /// converts a `i64` representing a `timestamp(ms)` to [`NaiveDateTime`] #[inline] pub fn timestamp_ms_to_datetime(v: i64) -> NaiveDateTime { + #[allow(deprecated)] NaiveDateTime::from_timestamp( // extract seconds from milliseconds v / MILLISECONDS, @@ -107,6 +115,7 @@ pub fn timestamp_ms_to_datetime(v: i64) -> NaiveDateTime { /// converts a `i64` representing a `timestamp(us)` to [`NaiveDateTime`] #[inline] pub fn timestamp_us_to_datetime(v: i64) -> NaiveDateTime { + #[allow(deprecated)] NaiveDateTime::from_timestamp( // extract seconds from microseconds v / MICROSECONDS, @@ -118,6 +127,7 @@ pub fn timestamp_us_to_datetime(v: i64) -> NaiveDateTime { /// converts a `i64` representing a `timestamp(ns)` to [`NaiveDateTime`] #[inline] pub fn timestamp_ns_to_datetime(v: i64) -> NaiveDateTime { + #[allow(deprecated)] NaiveDateTime::from_timestamp( // extract seconds from nanoseconds v / NANOSECONDS, diff --git a/parquet/src/record/api.rs b/parquet/src/record/api.rs index 549132df8075..59c18fd73f08 100644 --- a/parquet/src/record/api.rs +++ b/parquet/src/record/api.rs @@ -777,6 +777,7 @@ impl fmt::Display for Field { #[inline] fn convert_date_to_string(value: u32) -> String { static NUM_SECONDS_IN_DAY: i64 = 60 * 60 * 24; + #[allow(deprecated)] let dt = Utc.timestamp(value as i64 * NUM_SECONDS_IN_DAY, 0).date(); format!("{}", dt.format("%Y-%m-%d %:z")) } @@ -786,6 +787,7 @@ fn convert_date_to_string(value: u32) -> String { /// Datetime is displayed in local timezone. #[inline] fn convert_timestamp_millis_to_string(value: u64) -> String { + #[allow(deprecated)] let dt = Utc.timestamp((value / 1000) as i64, 0); format!("{}", dt.format("%Y-%m-%d %H:%M:%S %:z")) } @@ -1049,6 +1051,7 @@ mod tests { #[test] fn test_convert_date_to_string() { fn check_date_conversion(y: u32, m: u32, d: u32) { + #[allow(deprecated)] let datetime = chrono::NaiveDate::from_ymd(y as i32, m, d).and_hms(0, 0, 0); let dt = Utc.from_utc_datetime(&datetime); let res = convert_date_to_string((dt.timestamp() / 60 / 60 / 24) as u32); @@ -1066,6 +1069,7 @@ mod tests { #[test] fn test_convert_timestamp_to_string() { fn check_datetime_conversion(y: u32, m: u32, d: u32, h: u32, mi: u32, s: u32) { + #[allow(deprecated)] let datetime = chrono::NaiveDate::from_ymd(y as i32, m, d).and_hms(h, mi, s); let dt = Utc.from_utc_datetime(&datetime); let res = convert_timestamp_millis_to_string(dt.timestamp_millis() as u64);