|
18 | 18 | //! Utilities for pretty printing record batches. Note this module is not
|
19 | 19 | //! available unless `feature = "prettyprint"` is enabled.
|
20 | 20 |
|
21 |
| -use crate::display::{ArrayFormatter, FormatOptions}; |
| 21 | +use std::fmt::Display; |
| 22 | + |
| 23 | +use comfy_table::{Cell, Table}; |
| 24 | + |
22 | 25 | use arrow_array::{Array, ArrayRef, RecordBatch};
|
23 | 26 | use arrow_schema::ArrowError;
|
24 |
| -use comfy_table::{Cell, Table}; |
25 |
| -use std::fmt::Display; |
| 27 | + |
| 28 | +use crate::display::{ArrayFormatter, FormatOptions}; |
26 | 29 |
|
27 | 30 | /// Create a visual representation of record batches
|
28 | 31 | pub fn pretty_format_batches(results: &[RecordBatch]) -> Result<impl Display, ArrowError> {
|
@@ -131,17 +134,20 @@ fn create_column(
|
131 | 134 |
|
132 | 135 | #[cfg(test)]
|
133 | 136 | mod tests {
|
| 137 | + use std::fmt::Write; |
| 138 | + use std::sync::Arc; |
| 139 | + |
| 140 | + use half::f16; |
134 | 141 |
|
135 |
| - use super::*; |
136 |
| - use crate::display::array_value_to_string; |
137 | 142 | use arrow_array::builder::*;
|
138 | 143 | use arrow_array::types::*;
|
139 | 144 | use arrow_array::*;
|
140 | 145 | use arrow_buffer::Buffer;
|
141 | 146 | 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::*; |
145 | 151 |
|
146 | 152 | #[test]
|
147 | 153 | fn test_pretty_format_batches() {
|
@@ -317,6 +323,82 @@ mod tests {
|
317 | 323 | assert_eq!(expected, actual, "Actual result:\n{table}");
|
318 | 324 | }
|
319 | 325 |
|
| 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 | + |
320 | 402 | #[test]
|
321 | 403 | fn test_pretty_format_fixed_size_binary() {
|
322 | 404 | // define a schema.
|
|
0 commit comments