This is an automated email from the ASF dual-hosted git repository.
tustvold pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/arrow-rs.git
The following commit(s) were added to refs/heads/master by this push:
new 37cf8a6c19e Improve debug output of Time32/Time64 arrays (#5428)
37cf8a6c19e is described below
commit 37cf8a6c19e02a4dc822fadf331c2fae9e4b5656
Author: Clide S <[email protected]>
AuthorDate: Mon Feb 26 19:14:57 2024 -0500
Improve debug output of Time32/Time64 arrays (#5428)
* Improve debug output of Time32/Time64 arrays
* Update arrow-array/src/array/primitive_array.rs
Co-authored-by: Raphael Taylor-Davies
<[email protected]>
* Update arrow-array/src/array/primitive_array.rs
Co-authored-by: Raphael Taylor-Davies
<[email protected]>
* Fix
---------
Co-authored-by: Raphael Taylor-Davies
<[email protected]>
Co-authored-by: Raphael Taylor-Davies <[email protected]>
---
arrow-array/src/array/primitive_array.rs | 85 ++++++++++++++++++++++++++++++--
1 file changed, 82 insertions(+), 3 deletions(-)
diff --git a/arrow-array/src/array/primitive_array.rs
b/arrow-array/src/array/primitive_array.rs
index ca437a569a1..a800aa6bf92 100644
--- a/arrow-array/src/array/primitive_array.rs
+++ b/arrow-array/src/array/primitive_array.rs
@@ -1093,20 +1093,31 @@ where
impl<T: ArrowPrimitiveType> std::fmt::Debug for PrimitiveArray<T> {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
let data_type = self.data_type();
+
write!(f, "PrimitiveArray<{data_type:?}>\n[\n")?;
print_long_array(self, f, |array, index, f| match data_type {
DataType::Date32 | DataType::Date64 => {
let v = self.value(index).to_isize().unwrap() as i64;
match as_date::<T>(v) {
Some(date) => write!(f, "{date:?}"),
- None => write!(f, "null"),
+ None => {
+ write!(
+ f,
+ "Cast error: Failed to convert {v} to temporal for
{data_type:?}"
+ )
+ }
}
}
DataType::Time32(_) | DataType::Time64(_) => {
let v = self.value(index).to_isize().unwrap() as i64;
match as_time::<T>(v) {
Some(time) => write!(f, "{time:?}"),
- None => write!(f, "null"),
+ None => {
+ write!(
+ f,
+ "Cast error: Failed to convert {v} to temporal for
{data_type:?}"
+ )
+ }
}
}
DataType::Timestamp(_, tz_string_opt) => {
@@ -1948,7 +1959,8 @@ mod tests {
// chrono::NaiveDatetime::from_timestamp_opt returns None while input
is invalid
let arr: PrimitiveArray<Time32SecondType> = vec![-7201, -60054].into();
assert_eq!(
- "PrimitiveArray<Time32(Second)>\n[\n null,\n null,\n]",
+ "PrimitiveArray<Time32(Second)>\n[\n Cast error: Failed to convert
-7201 to temporal for Time32(Second),\n Cast error: Failed to convert -60054
to temporal for Time32(Second),\n]",
+ // "PrimitiveArray<Time32(Second)>\n[\n null,\n null,\n]",
format!("{arr:?}")
)
}
@@ -2482,4 +2494,71 @@ mod tests {
fn test_with_data_type() {
Int32Array::new(vec![1, 2, 3, 4].into(),
None).with_data_type(DataType::Date32);
}
+
+ #[test]
+ fn test_time_32second_output() {
+ let array: Time32SecondArray = vec![
+ Some(-1),
+ Some(0),
+ Some(86_399),
+ Some(86_400),
+ Some(86_401),
+ None,
+ ]
+ .into();
+ let debug_str = format!("{:?}", array);
+ assert_eq!("PrimitiveArray<Time32(Second)>\n[\n Cast error: Failed to
convert -1 to temporal for Time32(Second),\n 00:00:00,\n 23:59:59,\n Cast
error: Failed to convert 86400 to temporal for Time32(Second),\n Cast error:
Failed to convert 86401 to temporal for Time32(Second),\n null,\n]",
+ debug_str
+ );
+ }
+
+ #[test]
+ fn test_time_32millisecond_debug_output() {
+ let array: Time32MillisecondArray = vec![
+ Some(-1),
+ Some(0),
+ Some(86_399_000),
+ Some(86_400_000),
+ Some(86_401_000),
+ None,
+ ]
+ .into();
+ let debug_str = format!("{:?}", array);
+ assert_eq!("PrimitiveArray<Time32(Millisecond)>\n[\n Cast error:
Failed to convert -1 to temporal for Time32(Millisecond),\n 00:00:00,\n
23:59:59,\n Cast error: Failed to convert 86400000 to temporal for
Time32(Millisecond),\n Cast error: Failed to convert 86401000 to temporal for
Time32(Millisecond),\n null,\n]",
+ debug_str
+ );
+ }
+
+ #[test]
+ fn test_time_64nanosecond_debug_output() {
+ let array: Time64NanosecondArray = vec![
+ Some(-1),
+ Some(0),
+ Some(86_399 * 1_000_000_000),
+ Some(86_400 * 1_000_000_000),
+ Some(86_401 * 1_000_000_000),
+ None,
+ ]
+ .into();
+ let debug_str = format!("{:?}", array);
+ assert_eq!(
+ "PrimitiveArray<Time64(Nanosecond)>\n[\n Cast error: Failed to
convert -1 to temporal for Time64(Nanosecond),\n 00:00:00,\n 23:59:59,\n
Cast error: Failed to convert 86400000000000 to temporal for
Time64(Nanosecond),\n Cast error: Failed to convert 86401000000000 to temporal
for Time64(Nanosecond),\n null,\n]",
+ debug_str
+ );
+ }
+
+ #[test]
+ fn test_time_64microsecond_debug_output() {
+ let array: Time64MicrosecondArray = vec![
+ Some(-1),
+ Some(0),
+ Some(86_399 * 1_000_000),
+ Some(86_400 * 1_000_000),
+ Some(86_401 * 1_000_000),
+ None,
+ ]
+ .into();
+ let debug_str = format!("{:?}", array);
+ assert_eq!("PrimitiveArray<Time64(Microsecond)>\n[\n Cast error:
Failed to convert -1 to temporal for Time64(Microsecond),\n 00:00:00,\n
23:59:59,\n Cast error: Failed to convert 86400000000 to temporal for
Time64(Microsecond),\n Cast error: Failed to convert 86401000000 to temporal
for Time64(Microsecond),\n null,\n]", debug_str);
+ }
}