Skip to content

Commit

Permalink
impl Display for Utf8Scalar and BinaryScalar (#678)
Browse files Browse the repository at this point in the history
Partially addresses #670.

---------

Co-authored-by: Daniel King <[email protected]>
Co-authored-by: Robert Kruszewski <[email protected]>
  • Loading branch information
3 people authored Aug 22, 2024
1 parent fef8b54 commit 31befca
Showing 1 changed file with 74 additions and 13 deletions.
87 changes: 74 additions & 13 deletions vortex-scalar/src/display.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,20 @@
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 {
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(|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"),
Expand All @@ -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!(),
Expand All @@ -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");
}
}

0 comments on commit 31befca

Please sign in to comment.