Skip to content

Commit 57c27ad

Browse files
authored
Add an option to show column type (#7335)
1 parent 4d55c0a commit 57c27ad

2 files changed

Lines changed: 56 additions & 11 deletions

File tree

arrow-cast/src/display.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ pub struct FormatOptions<'a> {
7272
time_format: TimeFormat<'a>,
7373
/// Duration format
7474
duration_format: DurationFormat,
75+
/// Show types in visual representation batches
76+
types_info: bool,
7577
}
7678

7779
impl Default for FormatOptions<'_> {
@@ -92,6 +94,7 @@ impl<'a> FormatOptions<'a> {
9294
timestamp_tz_format: None,
9395
time_format: None,
9496
duration_format: DurationFormat::ISO8601,
97+
types_info: false,
9598
}
9699
}
97100

@@ -158,6 +161,18 @@ impl<'a> FormatOptions<'a> {
158161
..self
159162
}
160163
}
164+
165+
/// Overrides if types should be shown
166+
///
167+
/// Defaults to [`false`]
168+
pub const fn with_types_info(self, types_info: bool) -> Self {
169+
Self { types_info, ..self }
170+
}
171+
172+
/// Returns true if type info should be included in visual representation of batches
173+
pub const fn types_info(&self) -> bool {
174+
self.types_info
175+
}
161176
}
162177

163178
/// Implements [`Display`] for a specific array value

arrow-cast/src/pretty.rs

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,15 @@ fn create_table(results: &[RecordBatch], options: &FormatOptions) -> Result<Tabl
8888

8989
let mut header = Vec::new();
9090
for field in schema.fields() {
91-
header.push(Cell::new(field.name()));
91+
if options.types_info() {
92+
header.push(Cell::new(format!(
93+
"{}\n{}",
94+
field.name(),
95+
field.data_type()
96+
)))
97+
} else {
98+
header.push(Cell::new(field.name()));
99+
}
92100
}
93101
table.set_header(header);
94102

@@ -1059,9 +1067,18 @@ mod tests {
10591067

10601068
#[test]
10611069
fn test_format_options() {
1062-
let options = FormatOptions::default().with_null("null");
1063-
let array = Int32Array::from(vec![Some(1), Some(2), None, Some(3), Some(4)]);
1064-
let batch = RecordBatch::try_from_iter([("my_column_name", Arc::new(array) as _)]).unwrap();
1070+
let options = FormatOptions::default()
1071+
.with_null("null")
1072+
.with_types_info(true);
1073+
let int32_array = Int32Array::from(vec![Some(1), Some(2), None, Some(3), Some(4)]);
1074+
let string_array =
1075+
StringArray::from(vec![Some("foo"), Some("bar"), None, Some("baz"), None]);
1076+
1077+
let batch = RecordBatch::try_from_iter([
1078+
("my_int32_name", Arc::new(int32_array) as _),
1079+
("my_string_name", Arc::new(string_array) as _),
1080+
])
1081+
.unwrap();
10651082

10661083
let column = pretty_format_columns_with_options(
10671084
"my_column_name",
@@ -1071,11 +1088,7 @@ mod tests {
10711088
.unwrap()
10721089
.to_string();
10731090

1074-
let batch = pretty_format_batches_with_options(&[batch], &options)
1075-
.unwrap()
1076-
.to_string();
1077-
1078-
let expected = vec![
1091+
let expected_column = vec![
10791092
"+----------------+",
10801093
"| my_column_name |",
10811094
"+----------------+",
@@ -1088,9 +1101,26 @@ mod tests {
10881101
];
10891102

10901103
let actual: Vec<&str> = column.lines().collect();
1091-
assert_eq!(expected, actual, "Actual result:\n{column}");
1104+
assert_eq!(expected_column, actual, "Actual result:\n{column}");
1105+
1106+
let batch = pretty_format_batches_with_options(&[batch], &options)
1107+
.unwrap()
1108+
.to_string();
1109+
1110+
let expected_table = vec![
1111+
"+---------------+----------------+",
1112+
"| my_int32_name | my_string_name |",
1113+
"| Int32 | Utf8 |",
1114+
"+---------------+----------------+",
1115+
"| 1 | foo |",
1116+
"| 2 | bar |",
1117+
"| null | null |",
1118+
"| 3 | baz |",
1119+
"| 4 | null |",
1120+
"+---------------+----------------+",
1121+
];
10921122

10931123
let actual: Vec<&str> = batch.lines().collect();
1094-
assert_eq!(expected, actual, "Actual result:\n{batch}");
1124+
assert_eq!(expected_table, actual, "Actual result:\n{batch}");
10951125
}
10961126
}

0 commit comments

Comments
 (0)