Skip to content

Commit 14f31b6

Browse files
committed
Add documentation and examples for pretty pringing
1 parent 2c2e514 commit 14f31b6

File tree

2 files changed

+71
-4
lines changed

2 files changed

+71
-4
lines changed

arrow-cast/src/pretty.rs

Lines changed: 67 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,21 +31,82 @@ use arrow_schema::ArrowError;
3131

3232
use crate::display::{ArrayFormatter, FormatOptions};
3333

34-
/// Create a visual representation of record batches
34+
/// Create a visual representation of [`RecordBatch`]es
35+
///
36+
/// Uses default values for display. See [`pretty_format_batches_with_options`]
37+
/// for more control.
38+
///
39+
/// # Example
40+
/// ```
41+
/// # use std::sync::Arc;
42+
/// # use arrow_array::{ArrayRef, Int32Array, RecordBatch, StringArray};
43+
/// # use arrow_cast::pretty::pretty_format_batches;
44+
/// # let batch = RecordBatch::try_from_iter(vec![
45+
/// # ("a", Arc::new(Int32Array::from(vec![1, 2, 3, 4, 5])) as ArrayRef),
46+
/// # ("b", Arc::new(StringArray::from(vec![Some("a"), Some("b"), None, Some("d"), Some("e")]))),
47+
/// # ]).unwrap();
48+
/// // Note, returned object implements `Display`
49+
/// let pretty_table = pretty_format_batches(&[batch]).unwrap();
50+
/// let table_str = format!("Batches:\n{}", pretty_table);
51+
/// assert_eq!(table_str,
52+
/// r#"Batches:
53+
/// +---+---+
54+
/// | a | b |
55+
/// +---+---+
56+
/// | 1 | a |
57+
/// | 2 | b |
58+
/// | 3 | |
59+
/// | 4 | d |
60+
/// | 5 | e |
61+
/// +---+---+"#);
62+
/// ```
3563
pub fn pretty_format_batches(results: &[RecordBatch]) -> Result<impl Display, ArrowError> {
3664
let options = FormatOptions::default().with_display_error(true);
3765
pretty_format_batches_with_options(results, &options)
3866
}
3967

40-
/// Create a visual representation of record batches
68+
/// Create a visual representation of [`RecordBatch`]es with formatting options.
69+
///
70+
/// # Arguments
71+
/// * `results` - A slice of record batches to display
72+
/// * `options` - [`FormatOptions`] that control the resulting display
73+
///
74+
/// # Example
75+
/// ```
76+
/// # use std::sync::Arc;
77+
/// # use arrow_array::{ArrayRef, Int32Array, RecordBatch, StringArray};
78+
/// # use arrow_cast::display::FormatOptions;
79+
/// # use arrow_cast::pretty::{pretty_format_batches, pretty_format_batches_with_options};
80+
/// # let batch = RecordBatch::try_from_iter(vec![
81+
/// # ("a", Arc::new(Int32Array::from(vec![1, 2])) as ArrayRef),
82+
/// # ("b", Arc::new(StringArray::from(vec![Some("a"), None]))),
83+
/// # ]).unwrap();
84+
/// let options = FormatOptions::new()
85+
/// .with_null("<NULL>");
86+
/// // Note, returned object implements `Display`
87+
/// let pretty_table = pretty_format_batches_with_options(&[batch], &options).unwrap();
88+
/// let table_str = format!("Batches:\n{}", pretty_table);
89+
/// assert_eq!(table_str,
90+
/// r#"Batches:
91+
/// +---+--------+
92+
/// | a | b |
93+
/// +---+--------+
94+
/// | 1 | a |
95+
/// | 2 | <NULL> |
96+
/// +---+--------+"#);
97+
/// ```
4198
pub fn pretty_format_batches_with_options(
4299
results: &[RecordBatch],
43100
options: &FormatOptions,
44101
) -> Result<impl Display, ArrowError> {
45102
create_table(results, options)
46103
}
47104

48-
/// Create a visual representation of columns
105+
/// Create a visual representation of [`ArrayRef`]
106+
///
107+
/// Uses default values for display. See [`pretty_format_columns_with_options`]
108+
///
109+
/// See [`pretty_format_batches`] for an example
49110
pub fn pretty_format_columns(
50111
col_name: &str,
51112
results: &[ArrayRef],
@@ -54,7 +115,9 @@ pub fn pretty_format_columns(
54115
pretty_format_columns_with_options(col_name, results, &options)
55116
}
56117

57-
/// Utility function to create a visual representation of columns with options
118+
/// Create a visual representation of [`ArrayRef`] with formatting options.
119+
///
120+
/// See [`pretty_format_batches_with_options`] for an example
58121
fn pretty_format_columns_with_options(
59122
col_name: &str,
60123
results: &[ArrayRef],

arrow/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,10 @@
243243
//! let batch = RecordBatch::try_from_iter([("col1", col_1), ("col_2", col_2)]).unwrap();
244244
//! ```
245245
//!
246+
//! # Pretty Printing
247+
//!
248+
//! See the [`util::pretty`] module (requires the `prettyprint` crate feature)
249+
//!
246250
//! # IO
247251
//!
248252
//! This crate provides readers and writers for various formats to/from [`RecordBatch`]

0 commit comments

Comments
 (0)