From 31befca0138b5f3284ff50ee69b30798ef945de5 Mon Sep 17 00:00:00 2001 From: Dan King Date: Thu, 22 Aug 2024 16:00:04 +0100 Subject: [PATCH] impl Display for Utf8Scalar and BinaryScalar (#678) Partially addresses #670. --------- Co-authored-by: Daniel King Co-authored-by: Robert Kruszewski --- vortex-scalar/src/display.rs | 87 ++++++++++++++++++++++++++++++------ 1 file changed, 74 insertions(+), 13 deletions(-) diff --git a/vortex-scalar/src/display.rs b/vortex-scalar/src/display.rs index 2fa4bd62a1..39d20e28c8 100644 --- a/vortex-scalar/src/display.rs +++ b/vortex-scalar/src/display.rs @@ -1,9 +1,12 @@ use std::fmt::{Display, Formatter}; +use itertools::Itertools; use vortex_dtype::{match_each_native_ptype, DType}; +use crate::binary::BinaryScalar; use crate::bool::BoolScalar; use crate::primitive::PrimitiveScalar; +use crate::utf8::Utf8Scalar; use crate::Scalar; impl Display for Scalar { @@ -11,14 +14,7 @@ impl Display for Scalar { match self.dtype() { DType::Null => write!(f, "null"), DType::Bool(_) => match BoolScalar::try_from(self) - .map_err(|err| { - debug_assert!( - false, - "Failed to parse bool from scalar with DType Bool: {}", - err - ); - std::fmt::Error - })? + .map_err(|_| std::fmt::Error)? .value() { None => write!(f, "null"), @@ -30,8 +26,30 @@ impl Display for Scalar { Some(v) => write!(f, "{}", v), } }), - DType::Utf8(_) => todo!(), - DType::Binary(_) => todo!(), + DType::Utf8(_) => { + match Utf8Scalar::try_from(self) + .map_err(|_| std::fmt::Error)? + .value() + { + None => write!(f, "null"), + Some(bs) => write!(f, "{}", bs.as_str()), + } + } + DType::Binary(_) => { + match BinaryScalar::try_from(self) + .map_err(|_| std::fmt::Error)? + .value() + { + None => write!(f, "null"), + Some(buf) => { + write!( + f, + "{}", + buf.as_slice().iter().map(|b| format!("{b:x}")).format(",") + ) + } + } + } DType::Struct(..) => todo!(), DType::List(..) => todo!(), DType::Extension(..) => todo!(), @@ -41,11 +59,54 @@ impl Display for Scalar { #[cfg(test)] mod tests { + use vortex_buffer::Buffer; + use vortex_dtype::Nullability::{NonNullable, Nullable}; + use vortex_dtype::{DType, PType}; + use crate::Scalar; #[test] - fn display() { - let scalar = Scalar::from(false); - assert_eq!(format!("{}", scalar), "false"); + fn display_bool() { + assert_eq!(format!("{}", Scalar::from(false)), "false"); + assert_eq!(format!("{}", Scalar::from(true)), "true"); + assert_eq!(format!("{}", Scalar::null(DType::Bool(Nullable))), "null"); + } + + #[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_u16)), "0"); + assert_eq!(format!("{}", Scalar::from(!0_u16)), "65535"); + + assert_eq!(format!("{}", Scalar::from(0_u32)), "0"); + assert_eq!(format!("{}", Scalar::from(!0_u32)), "4294967295"); + + assert_eq!(format!("{}", Scalar::from(0_u64)), "0"); + assert_eq!(format!("{}", Scalar::from(!0_u64)), "18446744073709551615"); + + assert_eq!( + format!("{}", Scalar::null(DType::Primitive(PType::U8, Nullable))), + "null" + ); + } + + #[test] + fn display_utf8() { + assert_eq!(format!("{}", Scalar::from("Hello World!")), "Hello World!"); + assert_eq!(format!("{}", Scalar::null(DType::Utf8(Nullable))), "null"); + } + + #[test] + fn display_binary() { + assert_eq!( + format!( + "{}", + Scalar::binary(Buffer::from("Hello World!".as_bytes()), NonNullable) + ), + "48,65,6c,6c,6f,20,57,6f,72,6c,64,21" + ); + assert_eq!(format!("{}", Scalar::null(DType::Binary(Nullable))), "null"); } }