Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

impl Display for Utf8Scalar and BinaryScalar #678

Merged
merged 5 commits into from
Aug 22, 2024
Merged
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
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");
}
}
Loading