Skip to content

Commit

Permalink
fix: rely on ScalarValue Display in Scalar Display (#978)
Browse files Browse the repository at this point in the history
Curious for your thoughts on Utf8 and Buffer. It feels a little wrong to
change them to truncate by default? I think I really want two kinds of
`Display`, one that's lossless and one that's lossy and meant to be
"small".
  • Loading branch information
danking authored Oct 8, 2024
1 parent 8e3d227 commit 6075520
Showing 1 changed file with 15 additions and 27 deletions.
42 changes: 15 additions & 27 deletions vortex-scalar/src/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,18 @@ use std::fmt::{Display, Formatter};

use itertools::Itertools;
use vortex_datetime_dtype::{is_temporal_ext_type, TemporalMetadata};
use vortex_dtype::{match_each_native_ptype, DType};
use vortex_dtype::DType;

use crate::binary::BinaryScalar;
use crate::bool::BoolScalar;
use crate::extension::ExtScalar;
use crate::primitive::PrimitiveScalar;
use crate::struct_::StructScalar;
use crate::utf8::Utf8Scalar;
use crate::{PValue, Scalar, ScalarValue};

impl Display for Scalar {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self.dtype() {
DType::Null => write!(f, "null"),
DType::Bool(_) => match BoolScalar::try_from(self)
.map_err(|_| std::fmt::Error)?
.value()
{
None => write!(f, "null"),
Some(b) => write!(f, "{}", b),
},
DType::Primitive(ptype, _) => match_each_native_ptype!(ptype, |$T| {
match PrimitiveScalar::try_from(self).expect("primitive").typed_value::<$T>() {
None => write!(f, "null"),
Some(v) => write!(f, "{}", v),
}
}),
DType::Null | DType::Bool(_) | DType::Primitive(..) => Display::fmt(&self.value, f),
DType::Utf8(_) => {
match Utf8Scalar::try_from(self)
.map_err(|_| std::fmt::Error)?
Expand Down Expand Up @@ -123,17 +108,20 @@ mod tests {

#[test]
fn display_primitive() {
assert_eq!(format!("{}", Scalar::from(0_u8)), "0");
assert_eq!(format!("{}", Scalar::from(255_u8)), "255");
assert_eq!(format!("{}", Scalar::from(0_u8)), "0_u8");
assert_eq!(format!("{}", Scalar::from(255_u8)), "255_u8");

assert_eq!(format!("{}", Scalar::from(0_u16)), "0");
assert_eq!(format!("{}", Scalar::from(!0_u16)), "65535");
assert_eq!(format!("{}", Scalar::from(0_u16)), "0_u16");
assert_eq!(format!("{}", Scalar::from(!0_u16)), "65535_u16");

assert_eq!(format!("{}", Scalar::from(0_u32)), "0");
assert_eq!(format!("{}", Scalar::from(!0_u32)), "4294967295");
assert_eq!(format!("{}", Scalar::from(0_u32)), "0_u32");
assert_eq!(format!("{}", Scalar::from(!0_u32)), "4294967295_u32");

assert_eq!(format!("{}", Scalar::from(0_u64)), "0");
assert_eq!(format!("{}", Scalar::from(!0_u64)), "18446744073709551615");
assert_eq!(format!("{}", Scalar::from(0_u64)), "0_u64");
assert_eq!(
format!("{}", Scalar::from(!0_u64)),
"18446744073709551615_u64"
);

assert_eq!(
format!("{}", Scalar::null(DType::Primitive(PType::U8, Nullable))),
Expand Down Expand Up @@ -194,7 +182,7 @@ mod tests {
"{}",
Scalar::r#struct(dtype(), vec![ScalarValue::Primitive(PValue::U32(32))])
),
"{foo:32}"
"{foo:32_u32}"
);
}

Expand Down Expand Up @@ -239,7 +227,7 @@ mod tests {
]
)
),
"{foo:true,bar:32}"
"{foo:true,bar:32_u32}"
);
}

Expand Down

0 comments on commit 6075520

Please sign in to comment.