Skip to content

Commit 905c46b

Browse files
authored
PrettyPrint support for StringViewArray and BinaryViewArray (#5634)
1 parent a999fb8 commit 905c46b

File tree

2 files changed

+109
-8
lines changed

2 files changed

+109
-8
lines changed

arrow-cast/src/display.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,9 @@ fn make_formatter<'a>(
282282
DataType::Boolean => array_format(as_boolean_array(array), options),
283283
DataType::Utf8 => array_format(array.as_string::<i32>(), options),
284284
DataType::LargeUtf8 => array_format(array.as_string::<i64>(), options),
285+
DataType::Utf8View => array_format(array.as_string_view(), options),
285286
DataType::Binary => array_format(array.as_binary::<i32>(), options),
287+
DataType::BinaryView => array_format(array.as_binary_view(), options),
286288
DataType::LargeBinary => array_format(array.as_binary::<i64>(), options),
287289
DataType::FixedSizeBinary(_) => {
288290
let a = array.as_any().downcast_ref::<FixedSizeBinaryArray>().unwrap();
@@ -733,6 +735,13 @@ impl<'a, O: OffsetSizeTrait> DisplayIndex for &'a GenericStringArray<O> {
733735
}
734736
}
735737

738+
impl<'a> DisplayIndex for &'a StringViewArray {
739+
fn write(&self, idx: usize, f: &mut dyn Write) -> FormatResult {
740+
write!(f, "{}", self.value(idx))?;
741+
Ok(())
742+
}
743+
}
744+
736745
impl<'a, O: OffsetSizeTrait> DisplayIndex for &'a GenericBinaryArray<O> {
737746
fn write(&self, idx: usize, f: &mut dyn Write) -> FormatResult {
738747
let v = self.value(idx);
@@ -743,6 +752,16 @@ impl<'a, O: OffsetSizeTrait> DisplayIndex for &'a GenericBinaryArray<O> {
743752
}
744753
}
745754

755+
impl<'a> DisplayIndex for &'a BinaryViewArray {
756+
fn write(&self, idx: usize, f: &mut dyn Write) -> FormatResult {
757+
let v = self.value(idx);
758+
for byte in v {
759+
write!(f, "{byte:02x}")?;
760+
}
761+
Ok(())
762+
}
763+
}
764+
746765
impl<'a> DisplayIndex for &'a FixedSizeBinaryArray {
747766
fn write(&self, idx: usize, f: &mut dyn Write) -> FormatResult {
748767
let v = self.value(idx);

arrow-cast/src/pretty.rs

Lines changed: 90 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,14 @@
1818
//! Utilities for pretty printing record batches. Note this module is not
1919
//! available unless `feature = "prettyprint"` is enabled.
2020
21-
use crate::display::{ArrayFormatter, FormatOptions};
21+
use std::fmt::Display;
22+
23+
use comfy_table::{Cell, Table};
24+
2225
use arrow_array::{Array, ArrayRef, RecordBatch};
2326
use arrow_schema::ArrowError;
24-
use comfy_table::{Cell, Table};
25-
use std::fmt::Display;
27+
28+
use crate::display::{ArrayFormatter, FormatOptions};
2629

2730
/// Create a visual representation of record batches
2831
pub fn pretty_format_batches(results: &[RecordBatch]) -> Result<impl Display, ArrowError> {
@@ -131,17 +134,20 @@ fn create_column(
131134

132135
#[cfg(test)]
133136
mod tests {
137+
use std::fmt::Write;
138+
use std::sync::Arc;
139+
140+
use half::f16;
134141

135-
use super::*;
136-
use crate::display::array_value_to_string;
137142
use arrow_array::builder::*;
138143
use arrow_array::types::*;
139144
use arrow_array::*;
140145
use arrow_buffer::Buffer;
141146
use arrow_schema::*;
142-
use half::f16;
143-
use std::fmt::Write;
144-
use std::sync::Arc;
147+
148+
use crate::display::array_value_to_string;
149+
150+
use super::*;
145151

146152
#[test]
147153
fn test_pretty_format_batches() {
@@ -317,6 +323,82 @@ mod tests {
317323
assert_eq!(expected, actual, "Actual result:\n{table}");
318324
}
319325

326+
#[test]
327+
fn test_pretty_format_string_view() {
328+
let schema = Arc::new(Schema::new(vec![Field::new(
329+
"d1",
330+
DataType::Utf8View,
331+
true,
332+
)]));
333+
334+
// Use a small capacity so we end up with multiple views
335+
let mut builder = StringViewBuilder::with_capacity(20);
336+
builder.append_value("hello");
337+
builder.append_null();
338+
builder.append_value("longer than 12 bytes");
339+
builder.append_value("another than 12 bytes");
340+
builder.append_null();
341+
builder.append_value("small");
342+
343+
let array: ArrayRef = Arc::new(builder.finish());
344+
let batch = RecordBatch::try_new(schema, vec![array]).unwrap();
345+
let table = pretty_format_batches(&[batch]).unwrap().to_string();
346+
let expected = vec![
347+
"+-----------------------+",
348+
"| d1 |",
349+
"+-----------------------+",
350+
"| hello |",
351+
"| |",
352+
"| longer than 12 bytes |",
353+
"| another than 12 bytes |",
354+
"| |",
355+
"| small |",
356+
"+-----------------------+",
357+
];
358+
359+
let actual: Vec<&str> = table.lines().collect();
360+
361+
assert_eq!(expected, actual, "Actual result:\n{table:#?}");
362+
}
363+
364+
#[test]
365+
fn test_pretty_format_binary_view() {
366+
let schema = Arc::new(Schema::new(vec![Field::new(
367+
"d1",
368+
DataType::BinaryView,
369+
true,
370+
)]));
371+
372+
// Use a small capacity so we end up with multiple views
373+
let mut builder = BinaryViewBuilder::with_capacity(20);
374+
builder.append_value(b"hello");
375+
builder.append_null();
376+
builder.append_value(b"longer than 12 bytes");
377+
builder.append_value(b"another than 12 bytes");
378+
builder.append_null();
379+
builder.append_value(b"small");
380+
381+
let array: ArrayRef = Arc::new(builder.finish());
382+
let batch = RecordBatch::try_new(schema, vec![array]).unwrap();
383+
let table = pretty_format_batches(&[batch]).unwrap().to_string();
384+
let expected = vec![
385+
"+--------------------------------------------+",
386+
"| d1 |",
387+
"+--------------------------------------------+",
388+
"| 68656c6c6f |",
389+
"| |",
390+
"| 6c6f6e676572207468616e203132206279746573 |",
391+
"| 616e6f74686572207468616e203132206279746573 |",
392+
"| |",
393+
"| 736d616c6c |",
394+
"+--------------------------------------------+",
395+
];
396+
397+
let actual: Vec<&str> = table.lines().collect();
398+
399+
assert_eq!(expected, actual, "Actual result:\n\n{table:#?}");
400+
}
401+
320402
#[test]
321403
fn test_pretty_format_fixed_size_binary() {
322404
// define a schema.

0 commit comments

Comments
 (0)