From 127776e2a94fca74117c7d66c82f54701c290c68 Mon Sep 17 00:00:00 2001 From: "Jorge C. Leitao" Date: Thu, 26 May 2022 20:08:48 +0000 Subject: [PATCH] Fixed panic on debug print of invalid tz --- src/array/primitive/fmt.rs | 25 ++++++++++++++++++------- tests/it/array/primitive/fmt.rs | 12 ++++++++++++ 2 files changed, 30 insertions(+), 7 deletions(-) diff --git a/src/array/primitive/fmt.rs b/src/array/primitive/fmt.rs index a5c028850bd..86ad35b127f 100644 --- a/src/array/primitive/fmt.rs +++ b/src/array/primitive/fmt.rs @@ -66,15 +66,26 @@ pub fn get_write_value<'a, T: NativeType, F: Write>( } #[cfg(feature = "chrono-tz")] Err(_) => { - let timezone = temporal_conversions::parse_offset_tz(tz).unwrap(); - dyn_primitive!(array, i64, |time| { - temporal_conversions::timestamp_to_datetime(time, *time_unit, &timezone) - }) + let timezone = temporal_conversions::parse_offset_tz(tz); + match timezone { + Ok(timezone) => dyn_primitive!(array, i64, |time| { + temporal_conversions::timestamp_to_datetime( + time, *time_unit, &timezone, + ) + }), + Err(_) => { + let tz = tz.clone(); + Box::new(move |f, index| { + write!(f, "{} ({})", array.value(index), tz) + }) + } + } } #[cfg(not(feature = "chrono-tz"))] - _ => panic!( - "Invalid Offset format (must be [-]00:00) or chrono-tz feature not active" - ), + _ => { + let tz = tz.clone(); + Box::new(move |f, index| write!(f, "{} ({})", array.value(index), tz)) + } } } else { dyn_primitive!(array, i64, |time| { diff --git a/tests/it/array/primitive/fmt.rs b/tests/it/array/primitive/fmt.rs index e8870b14412..4f62e0bf22c 100644 --- a/tests/it/array/primitive/fmt.rs +++ b/tests/it/array/primitive/fmt.rs @@ -132,6 +132,18 @@ fn debug_timestamp_tz_ns() { ); } +#[test] +fn debug_timestamp_tz_not_parsable() { + let array = Int64Array::from(&[Some(1), None, Some(2)]).to(DataType::Timestamp( + TimeUnit::Nanosecond, + Some("aa".to_string()), + )); + assert_eq!( + format!("{:?}", array), + "Timestamp(Nanosecond, Some(\"aa\"))[1 (aa), None, 2 (aa)]" + ); +} + #[cfg(feature = "chrono-tz")] #[test] fn debug_timestamp_tz1_ns() {