Skip to content
This repository has been archived by the owner on Feb 18, 2024. It is now read-only.

Replaced Display by Debug for Array #694

Merged
merged 1 commit into from
Dec 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/csv_read_async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ async fn main() -> Result<()> {
0,
deserialize_column,
)?;
println!("{}", batch.column(0));
println!("{:?}", batch.column(0));
Ok(())
}
2 changes: 1 addition & 1 deletion examples/parquet_read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@ fn main() -> Result<()> {
let row_group = args[3].parse::<usize>().unwrap();

let array = read_field(file_path, row_group, field)?;
println!("{}", array);
println!("{:?}", array);
Ok(())
}
4 changes: 2 additions & 2 deletions src/array/boolean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub use mutable::*;

/// The Arrow's equivalent to an immutable `Vec<Option<bool>>`, but with `1/16` of its size.
/// Cloning and slicing this struct is `O(1)`.
#[derive(Debug, Clone)]
#[derive(Clone)]
pub struct BooleanArray {
data_type: DataType,
values: Bitmap,
Expand Down Expand Up @@ -168,7 +168,7 @@ impl Array for BooleanArray {
}
}

impl std::fmt::Display for BooleanArray {
impl std::fmt::Debug for BooleanArray {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
display_fmt(self.iter(), "BooleanArray", f, false)
}
Expand Down
2 changes: 1 addition & 1 deletion src/array/dictionary/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ where
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let display = get_value_display(self);
let new_lines = false;
let head = &format!("{}", self.data_type());
let head = &format!("{:?}", self.data_type());
let iter = self.iter().enumerate().map(|(i, x)| x.map(|_| display(i)));
display_fmt(iter, head, f, new_lines)
}
Expand Down
4 changes: 2 additions & 2 deletions src/array/fixed_size_binary/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub use mutable::*;

/// The Arrow's equivalent to an immutable `Vec<Option<[u8; size]>>`.
/// Cloning and slicing this struct is `O(1)`.
#[derive(Debug, Clone)]
#[derive(Clone)]
pub struct FixedSizeBinaryArray {
size: usize, // this is redundant with `data_type`, but useful to not have to deconstruct the data_type.
data_type: DataType,
Expand Down Expand Up @@ -205,7 +205,7 @@ impl Array for FixedSizeBinaryArray {
}
}

impl std::fmt::Display for FixedSizeBinaryArray {
impl std::fmt::Debug for FixedSizeBinaryArray {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let iter = self.iter().map(|x| x.map(|x| format!("{:?}", x)));
display_fmt(iter, "FixedSizeBinaryArray", f, false)
Expand Down
8 changes: 4 additions & 4 deletions src/array/fixed_size_list/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
datatypes::{DataType, Field},
};

use super::{display_fmt, new_empty_array, new_null_array, Array};
use super::{debug_fmt, new_empty_array, new_null_array, Array};

mod ffi;
mod iterator;
Expand All @@ -15,7 +15,7 @@ pub use mutable::*;

/// The Arrow's equivalent to an immutable `Vec<Option<[T; size]>>` where `T` is an Arrow type.
/// Cloning and slicing this struct is `O(1)`.
#[derive(Debug, Clone)]
#[derive(Clone)]
pub struct FixedSizeListArray {
size: usize, // this is redundant with `data_type`, but useful to not have to deconstruct the data_type.
data_type: DataType,
Expand Down Expand Up @@ -196,8 +196,8 @@ impl Array for FixedSizeListArray {
}
}

impl std::fmt::Display for FixedSizeListArray {
impl std::fmt::Debug for FixedSizeListArray {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
display_fmt(self.iter(), "FixedSizeListArray", f, true)
debug_fmt(self.iter(), "FixedSizeListArray", f, true)
}
}
8 changes: 4 additions & 4 deletions src/array/list/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{
};

use super::{
display_fmt, new_empty_array,
debug_fmt, new_empty_array,
specification::{check_offsets, Offset},
Array,
};
Expand All @@ -19,7 +19,7 @@ mod mutable;
pub use mutable::*;

/// An [`Array`] semantically equivalent to `Vec<Option<Vec<Option<T>>>>` with Arrow's in-memory.
#[derive(Debug, Clone)]
#[derive(Clone)]
pub struct ListArray<O: Offset> {
data_type: DataType,
offsets: Buffer<O>,
Expand Down Expand Up @@ -241,13 +241,13 @@ impl<O: Offset> Array for ListArray<O> {
}
}

impl<O: Offset> std::fmt::Display for ListArray<O> {
impl<O: Offset> std::fmt::Debug for ListArray<O> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let head = if O::is_large() {
"LargeListArray"
} else {
"ListArray"
};
display_fmt(self.iter(), head, f, true)
debug_fmt(self.iter(), head, f, true)
}
}
56 changes: 29 additions & 27 deletions src/array/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
//! Most arrays contain a [`MutableArray`] counterpart that is neither clonable nor slicable, but
//! can be operated in-place.
use std::any::Any;
use std::fmt::Display;

use crate::error::Result;
use crate::types::{days_ms, months_days_ns};
Expand All @@ -29,7 +28,7 @@ use crate::{

/// A trait representing an immutable Arrow array. Arrow arrays are trait objects
/// that are infalibly downcasted to concrete types according to the [`Array::data_type`].
pub trait Array: std::fmt::Debug + Send + Sync {
pub trait Array: Send + Sync {
/// Convert to trait object.
fn as_any(&self) -> &dyn Any;

Expand Down Expand Up @@ -214,7 +213,7 @@ macro_rules! with_match_primitive_type {(
}
})}

impl Display for dyn Array {
impl std::fmt::Debug for dyn Array + '_ {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
use crate::datatypes::PhysicalType::*;
match self.data_type().to_physical_type() {
Expand Down Expand Up @@ -394,31 +393,34 @@ pub trait TryPush<A> {
}

fn display_helper<T: std::fmt::Display, I: IntoIterator<Item = Option<T>>>(iter: I) -> Vec<String> {
let iterator = iter.into_iter();
let len = iterator.size_hint().0;
if len <= 100 {
iterator
.map(|x| match x {
Some(x) => x.to_string(),
None => "".to_string(),
})
.collect::<Vec<_>>()
iter.into_iter()
.map(|x| match x {
Some(x) => x.to_string(),
None => "None".to_string(),
})
.collect::<Vec<_>>()
}

fn debug_helper<T: std::fmt::Debug, I: IntoIterator<Item = Option<T>>>(iter: I) -> Vec<String> {
iter.into_iter()
.map(|x| match x {
Some(x) => format!("{:?}", x),
None => "None".to_string(),
})
.collect::<Vec<_>>()
}

fn debug_fmt<T: std::fmt::Debug, I: IntoIterator<Item = Option<T>>>(
iter: I,
head: &str,
f: &mut std::fmt::Formatter<'_>,
new_lines: bool,
) -> std::fmt::Result {
let result = debug_helper(iter);
if new_lines {
write!(f, "{}[\n{}\n]", head, result.join(",\n"))
} else {
iterator
.enumerate()
.filter_map(|(i, x)| {
if i == 5 {
Some(format!("...({})...", len - 10))
} else if i < 5 || i > (len - 5) {
Some(match x {
Some(x) => x.to_string(),
None => "".to_string(),
})
} else {
None
}
})
.collect::<Vec<_>>()
write!(f, "{}[{}]", head, result.join(", "))
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/array/null.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{
};

/// The concrete [`Array`] of [`DataType::Null`].
#[derive(Debug, Clone)]
#[derive(Clone)]
pub struct NullArray {
data_type: DataType,
length: usize,
Expand Down Expand Up @@ -74,7 +74,7 @@ impl Array for NullArray {
}
}

impl std::fmt::Display for NullArray {
impl std::fmt::Debug for NullArray {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "NullArray({})", self.len())
}
Expand Down
6 changes: 3 additions & 3 deletions src/array/primitive/display.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use crate::types::NativeType;

use super::super::display::get_value_display;
use super::super::{display_fmt, Array};
use super::super::display_fmt;
use super::PrimitiveArray;

impl<T: NativeType> std::fmt::Display for PrimitiveArray<T> {
impl<T: NativeType> std::fmt::Debug for PrimitiveArray<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let display = get_value_display(self);
let new_lines = false;
let head = &format!("{}", self.data_type());
let head = &format!("{:?}", self.data_type());
let iter = self.iter().enumerate().map(|(i, x)| x.map(|_| display(i)));
display_fmt(iter, head, f, new_lines)
}
Expand Down
13 changes: 9 additions & 4 deletions src/array/primitive/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub use mutable::*;
/// assert_eq!(array.validity(), Some(&Bitmap::from([true, false, true])));
/// # }
/// ```
#[derive(Debug, Clone)]
#[derive(Clone)]
pub struct PrimitiveArray<T: NativeType> {
data_type: DataType,
values: Buffer<T>,
Expand Down Expand Up @@ -61,7 +61,7 @@ impl<T: NativeType> PrimitiveArray<T> {
pub fn from_data(data_type: DataType, values: Buffer<T>, validity: Option<Bitmap>) -> Self {
if !T::is_valid(&data_type) {
Err(ArrowError::InvalidArgumentError(format!(
"Type {} does not support logical type {}",
"Type {} does not support logical type {:?}",
std::any::type_name::<T>(),
data_type
)))
Expand Down Expand Up @@ -135,6 +135,11 @@ impl<T: NativeType> PrimitiveArray<T> {
self.validity.as_ref()
}

#[inline]
fn data_type(&self) -> &DataType {
&self.data_type
}

/// The values [`Buffer`].
/// Values on null slots are undetermined (they can be anything).
#[inline]
Expand Down Expand Up @@ -166,7 +171,7 @@ impl<T: NativeType> PrimitiveArray<T> {
pub fn to(self, data_type: DataType) -> Self {
if !T::is_valid(&data_type) {
Err(ArrowError::InvalidArgumentError(format!(
"Type {} does not support logical type {}",
"Type {} does not support logical type {:?}",
std::any::type_name::<T>(),
data_type
)))
Expand All @@ -193,7 +198,7 @@ impl<T: NativeType> Array for PrimitiveArray<T> {

#[inline]
fn data_type(&self) -> &DataType {
&self.data_type
self.data_type()
}

fn validity(&self) -> Option<&Bitmap> {
Expand Down
2 changes: 1 addition & 1 deletion src/array/primitive/mutable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ impl<T: NativeType + NaturalDataType> MutablePrimitiveArray<T> {
) -> Self {
if !T::is_valid(&data_type) {
Err(ArrowError::InvalidArgumentError(format!(
"Type {} does not support logical type {}",
"Type {} does not support logical type {:?}",
std::any::type_name::<T>(),
data_type
)))
Expand Down
6 changes: 3 additions & 3 deletions src/array/struct_/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ mod iterator;
///
/// let array = StructArray::from_data(DataType::Struct(fields), vec![boolean, int], None);
/// ```
#[derive(Debug, Clone)]
#[derive(Clone)]
pub struct StructArray {
data_type: DataType,
values: Vec<Arc<dyn Array>>,
Expand Down Expand Up @@ -214,11 +214,11 @@ impl Array for StructArray {
}
}

impl std::fmt::Display for StructArray {
impl std::fmt::Debug for StructArray {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
writeln!(f, "StructArray{{")?;
for (field, column) in self.fields().iter().zip(self.values()) {
writeln!(f, "{}: {},", field.name(), column)?;
writeln!(f, "{}: {:?},", field.name(), column)?;
}
write!(f, "}}")
}
Expand Down
4 changes: 2 additions & 2 deletions src/array/union/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type FieldEntry = (usize, Arc<dyn Array>);
// let field = field.as_any().downcast to correct type;
// let value = field.value(offset);
// ```
#[derive(Debug, Clone)]
#[derive(Clone)]
pub struct UnionArray {
types: Buffer<i8>,
// None represents when there is no typeid
Expand Down Expand Up @@ -274,7 +274,7 @@ impl UnionArray {
}
}

impl std::fmt::Display for UnionArray {
impl std::fmt::Debug for UnionArray {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let display = get_value_display(self);
let new_lines = false;
Expand Down
2 changes: 1 addition & 1 deletion src/array/utf8/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ impl<O: Offset> Array for Utf8Array<O> {

impl<O: Offset> std::fmt::Display for Utf8Array<O> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
display_fmt(self.iter(), &format!("{}", self.data_type()), f, false)
display_fmt(self.iter(), &format!("{:?}", self.data_type()), f, false)
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/compute/aggregate/min_max.rs
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ pub fn max(array: &dyn Array) -> Result<Box<dyn Scalar>> {
}
_ => {
return Err(ArrowError::InvalidArgumentError(format!(
"The `max` operator does not support type `{}`",
"The `max` operator does not support type `{:?}`",
array.data_type(),
)))
}
Expand Down Expand Up @@ -435,7 +435,7 @@ pub fn min(array: &dyn Array) -> Result<Box<dyn Scalar>> {
}
_ => {
return Err(ArrowError::InvalidArgumentError(format!(
"The `max` operator does not support type `{}`",
"The `max` operator does not support type `{:?}`",
array.data_type(),
)))
}
Expand Down
2 changes: 1 addition & 1 deletion src/compute/aggregate/sum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ pub fn sum(array: &dyn Array) -> Result<Box<dyn Scalar>> {
DataType::Float64 => dyn_sum!(f64, array),
_ => {
return Err(ArrowError::InvalidArgumentError(format!(
"The `sum` operator does not support type `{}`",
"The `sum` operator does not support type `{:?}`",
array.data_type(),
)))
}
Expand Down
2 changes: 1 addition & 1 deletion src/compute/if_then_else.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub fn if_then_else(
) -> Result<Box<dyn Array>> {
if lhs.data_type() != rhs.data_type() {
return Err(ArrowError::InvalidArgumentError(format!(
"If then else requires the arguments to have the same datatypes ({} != {})",
"If then else requires the arguments to have the same datatypes ({:?} != {:?})",
lhs.data_type(),
rhs.data_type()
)));
Expand Down
2 changes: 1 addition & 1 deletion src/compute/nullif.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ pub fn nullif(lhs: &dyn Array, rhs: &dyn Array) -> Result<Box<dyn Array>> {
)
.map(|x| Box::new(x) as Box<dyn Array>),
other => Err(ArrowError::NotYetImplemented(format!(
"Nullif is not implemented for logical datatype {}",
"Nullif is not implemented for logical datatype {:?}",
other
))),
}
Expand Down
Loading