Skip to content

Commit

Permalink
Fix prettyprint for Interval second fractions (#3093)
Browse files Browse the repository at this point in the history
Co-authored-by: Raphael Taylor-Davies <r.taylordavies@googlemail.com>
  • Loading branch information
Jefffrey and tustvold committed Nov 13, 2022
1 parent ccc4417 commit aaf030f
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 2 deletions.
4 changes: 2 additions & 2 deletions arrow-cast/src/display.rs
Expand Up @@ -89,7 +89,7 @@ macro_rules! make_string_interval_day_time {
let mins = mins - (hours * 60);

format!(
"0 years 0 mons {} days {} hours {} mins {}.{:02} secs",
"0 years 0 mons {} days {} hours {} mins {}.{:03} secs",
days_parts,
hours,
mins,
Expand Down Expand Up @@ -127,7 +127,7 @@ macro_rules! make_string_interval_month_day_nano {
let mins = mins - (hours * 60);

format!(
"0 years {} mons {} days {} hours {} mins {}.{:02} secs",
"0 years {} mons {} days {} hours {} mins {}.{:09} secs",
months_part,
days_part,
hours,
Expand Down
84 changes: 84 additions & 0 deletions arrow/src/util/pretty.rs
Expand Up @@ -987,4 +987,88 @@ mod tests {

Ok(())
}

#[test]
fn test_pretty_format_interval_day_time() -> Result<()> {
let arr = Arc::new(arrow_array::IntervalDayTimeArray::from(vec![
Some(1),
Some(10),
Some(100),
]));

let schema = Arc::new(Schema::new(vec![Field::new(
"IntervalDayTime",
arr.data_type().clone(),
true,
)]));

let batch = RecordBatch::try_new(schema, vec![arr])?;

let table = pretty_format_batches(&[batch])?.to_string();

let expected = vec![
"+-------------------------------------------------+",
"| IntervalDayTime |",
"+-------------------------------------------------+",
"| 0 years 0 mons 0 days 0 hours 0 mins 0.001 secs |",
"| 0 years 0 mons 0 days 0 hours 0 mins 0.010 secs |",
"| 0 years 0 mons 0 days 0 hours 0 mins 0.100 secs |",
"+-------------------------------------------------+",
];

let actual: Vec<&str> = table.lines().collect();

assert_eq!(expected, actual, "Actual result:\n{}", table);

Ok(())
}

#[test]
fn test_pretty_format_interval_month_day_nano_array() -> Result<()> {
let arr = Arc::new(arrow_array::IntervalMonthDayNanoArray::from(vec![
Some(1),
Some(10),
Some(100),
Some(1_000),
Some(10_000),
Some(100_000),
Some(1_000_000),
Some(10_000_000),
Some(100_000_000),
Some(1_000_000_000),
]));

let schema = Arc::new(Schema::new(vec![Field::new(
"IntervalMonthDayNano",
arr.data_type().clone(),
true,
)]));

let batch = RecordBatch::try_new(schema, vec![arr])?;

let table = pretty_format_batches(&[batch])?.to_string();

let expected = vec![
"+-------------------------------------------------------+",
"| IntervalMonthDayNano |",
"+-------------------------------------------------------+",
"| 0 years 0 mons 0 days 0 hours 0 mins 0.000000001 secs |",
"| 0 years 0 mons 0 days 0 hours 0 mins 0.000000010 secs |",
"| 0 years 0 mons 0 days 0 hours 0 mins 0.000000100 secs |",
"| 0 years 0 mons 0 days 0 hours 0 mins 0.000001000 secs |",
"| 0 years 0 mons 0 days 0 hours 0 mins 0.000010000 secs |",
"| 0 years 0 mons 0 days 0 hours 0 mins 0.000100000 secs |",
"| 0 years 0 mons 0 days 0 hours 0 mins 0.001000000 secs |",
"| 0 years 0 mons 0 days 0 hours 0 mins 0.010000000 secs |",
"| 0 years 0 mons 0 days 0 hours 0 mins 0.100000000 secs |",
"| 0 years 0 mons 0 days 0 hours 0 mins 1.000000000 secs |",
"+-------------------------------------------------------+",
];

let actual: Vec<&str> = table.lines().collect();

assert_eq!(expected, actual, "Actual result:\n{}", table);

Ok(())
}
}

0 comments on commit aaf030f

Please sign in to comment.