Skip to content

Commit e3b4cc4

Browse files
committed
Add documentation and examples for pretty pringing
1 parent 0e73524 commit e3b4cc4

File tree

2 files changed

+74
-4
lines changed

2 files changed

+74
-4
lines changed

arrow-cast/src/pretty.rs

Lines changed: 70 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,21 +31,84 @@ 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+
/// ```
63+
3564
pub fn pretty_format_batches(results: &[RecordBatch]) -> Result<impl Display, ArrowError> {
3665
let options = FormatOptions::default().with_display_error(true);
3766
pretty_format_batches_with_options(results, &options)
3867
}
3968

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

48-
/// Create a visual representation of columns
107+
/// Create a visual representation of [`ArrayRef`]
108+
///
109+
/// Uses default values for display. See [`pretty_format_columns_with_options`]
110+
///
111+
/// See [`pretty_format_batches`] for an example
49112
pub fn pretty_format_columns(
50113
col_name: &str,
51114
results: &[ArrayRef],
@@ -54,7 +117,10 @@ pub fn pretty_format_columns(
54117
pretty_format_columns_with_options(col_name, results, &options)
55118
}
56119

57-
/// Utility function to create a visual representation of columns with options
120+
/// Create a visual representation of [`ArrayRef`] with formatting options.
121+
///
122+
/// See [`pretty_format_batches_with_options`] for an example
123+
58124
fn pretty_format_columns_with_options(
59125
col_name: &str,
60126
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)