|
20 | 20 | use arrow::array::{Array, ArrayRef, NullArray};
|
21 | 21 | use arrow::compute::{kernels, CastOptions};
|
22 | 22 | use arrow::datatypes::DataType;
|
| 23 | +use arrow::util::pretty::pretty_format_columns; |
23 | 24 | use datafusion_common::format::DEFAULT_CAST_OPTIONS;
|
24 | 25 | use datafusion_common::{internal_err, Result, ScalarValue};
|
| 26 | +use std::fmt; |
25 | 27 | use std::sync::Arc;
|
26 | 28 |
|
27 | 29 | /// The result of evaluating an expression.
|
@@ -218,9 +220,34 @@ impl ColumnarValue {
|
218 | 220 | }
|
219 | 221 | }
|
220 | 222 |
|
| 223 | +// Implement Display trait for ColumnarValue |
| 224 | +impl fmt::Display for ColumnarValue { |
| 225 | + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 226 | + let formatted = match self { |
| 227 | + ColumnarValue::Array(array) => { |
| 228 | + pretty_format_columns("ColumnarValue(ArrayRef)", &[Arc::clone(array)]) |
| 229 | + } |
| 230 | + ColumnarValue::Scalar(_) => { |
| 231 | + if let Ok(array) = self.to_array(1) { |
| 232 | + pretty_format_columns("ColumnarValue(ScalarValue)", &[array]) |
| 233 | + } else { |
| 234 | + return write!(f, "Error formatting columnar value"); |
| 235 | + } |
| 236 | + } |
| 237 | + }; |
| 238 | + |
| 239 | + if let Ok(formatted) = formatted { |
| 240 | + write!(f, "{}", formatted) |
| 241 | + } else { |
| 242 | + write!(f, "Error formatting columnar value") |
| 243 | + } |
| 244 | + } |
| 245 | +} |
| 246 | + |
221 | 247 | #[cfg(test)]
|
222 | 248 | mod tests {
|
223 | 249 | use super::*;
|
| 250 | + use arrow::array::Int32Array; |
224 | 251 |
|
225 | 252 | #[test]
|
226 | 253 | fn values_to_arrays() {
|
@@ -329,6 +356,39 @@ mod tests {
|
329 | 356 |
|
330 | 357 | /// Makes an array of length `len` with all elements set to `val`
|
331 | 358 | fn make_array(val: i32, len: usize) -> ArrayRef {
|
332 |
| - Arc::new(arrow::array::Int32Array::from(vec![val; len])) |
| 359 | + Arc::new(Int32Array::from(vec![val; len])) |
| 360 | + } |
| 361 | + |
| 362 | + #[test] |
| 363 | + fn test_display_scalar() { |
| 364 | + let column = ColumnarValue::from(ScalarValue::from("foo")); |
| 365 | + assert_eq!( |
| 366 | + column.to_string(), |
| 367 | + concat!( |
| 368 | + "+----------------------------+\n", |
| 369 | + "| ColumnarValue(ScalarValue) |\n", |
| 370 | + "+----------------------------+\n", |
| 371 | + "| foo |\n", |
| 372 | + "+----------------------------+" |
| 373 | + ) |
| 374 | + ); |
| 375 | + } |
| 376 | + |
| 377 | + #[test] |
| 378 | + fn test_display_array() { |
| 379 | + let array: ArrayRef = Arc::new(Int32Array::from_iter_values(vec![1, 2, 3])); |
| 380 | + let column = ColumnarValue::from(array); |
| 381 | + assert_eq!( |
| 382 | + column.to_string(), |
| 383 | + concat!( |
| 384 | + "+-------------------------+\n", |
| 385 | + "| ColumnarValue(ArrayRef) |\n", |
| 386 | + "+-------------------------+\n", |
| 387 | + "| 1 |\n", |
| 388 | + "| 2 |\n", |
| 389 | + "| 3 |\n", |
| 390 | + "+-------------------------+" |
| 391 | + ) |
| 392 | + ); |
333 | 393 | }
|
334 | 394 | }
|
0 commit comments