Skip to content

Commit

Permalink
merge
Browse files Browse the repository at this point in the history
  • Loading branch information
gatesn committed May 7, 2024
1 parent d7a9b13 commit 64d31ae
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 30 deletions.
18 changes: 12 additions & 6 deletions vortex-scalar/src/binary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,19 @@ use vortex_error::{vortex_bail, vortex_err, VortexError, VortexResult};
use crate::value::ScalarValue;
use crate::Scalar;

pub struct BinaryScalar<'a>(&'a Scalar);
pub struct BinaryScalar<'a> {
dtype: &'a DType,
value: Option<Buffer>,
}

impl<'a> BinaryScalar<'a> {
#[inline]
pub fn dtype(&self) -> &'a DType {
self.0.dtype()
self.dtype
}

pub fn value(&self) -> Option<Buffer> {
self.0.value.as_bytes()
self.value.as_ref().cloned()
}

pub fn cast(&self, _dtype: &DType) -> VortexResult<Scalar> {
Expand All @@ -34,11 +38,13 @@ impl<'a> TryFrom<&'a Scalar> for BinaryScalar<'a> {
type Error = VortexError;

fn try_from(value: &'a Scalar) -> Result<Self, Self::Error> {
if matches!(value.dtype(), DType::Binary(_)) {
Ok(Self(value))
} else {
if !matches!(value.dtype(), DType::Binary(_)) {
vortex_bail!("Expected binary scalar, found {}", value.dtype())
}
Ok(Self {
dtype: value.dtype(),
value: value.value.as_bytes()?,
})
}
}

Expand Down
15 changes: 11 additions & 4 deletions vortex-scalar/src/bool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,19 @@ use vortex_error::{vortex_bail, vortex_err, VortexError, VortexResult};
use crate::value::ScalarValue;
use crate::Scalar;

pub struct BoolScalar<'a>(&'a Scalar);
pub struct BoolScalar<'a> {
dtype: &'a DType,
value: Option<bool>,
}

impl<'a> BoolScalar<'a> {
#[inline]
pub fn dtype(&self) -> &'a DType {
self.0.dtype()
self.dtype
}

pub fn value(&self) -> Option<bool> {
self.0.value.as_bool()
self.value
}

pub fn cast(&self, dtype: &DType) -> VortexResult<Scalar> {
Expand Down Expand Up @@ -43,7 +47,10 @@ impl<'a> TryFrom<&'a Scalar> for BoolScalar<'a> {
if !matches!(value.dtype(), DType::Bool(_)) {
vortex_bail!("Expected bool scalar, found {}", value.dtype())
}
Ok(Self(value))
Ok(Self {
dtype: value.dtype(),
value: value.value.as_bool()?,
})
}
}

Expand Down
30 changes: 16 additions & 14 deletions vortex-scalar/src/utf8.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@ use vortex_error::{vortex_bail, vortex_err, VortexError, VortexResult};
use crate::value::ScalarValue;
use crate::Scalar;

pub struct Utf8Scalar<'a>(&'a Scalar);
pub struct Utf8Scalar<'a> {
dtype: &'a DType,
value: Option<BufferString>,
}

impl<'a> Utf8Scalar<'a> {
#[inline]
pub fn dtype(&self) -> &'a DType {
self.0.dtype()
self.dtype
}

pub fn value(&self) -> Option<BufferString> {
self.0
.value
.as_bytes()
// Checked on construction that the buffer is valid UTF-8
.map(|buffer| unsafe { BufferString::new_unchecked(buffer) })
self.value.as_ref().cloned()
}

pub fn cast(&self, _dtype: &DType) -> VortexResult<Scalar> {
Expand All @@ -43,15 +43,17 @@ impl<'a> TryFrom<&'a Scalar> for Utf8Scalar<'a> {
type Error = VortexError;

fn try_from(value: &'a Scalar) -> Result<Self, Self::Error> {
if matches!(value.dtype(), DType::Utf8(_)) {
// Validate here that the buffer is indeed UTF-8
if let Some(buffer) = value.value.as_bytes() {
let _ = std::str::from_utf8(buffer.as_ref())?;
}
Ok(Self(value))
} else {
if !matches!(value.dtype(), DType::Utf8(_)) {
vortex_bail!("Expected utf8 scalar, found {}", value.dtype())
}
Ok(Self {
dtype: value.dtype(),
value: value
.value
.as_bytes()?
.map(|b| BufferString::try_from(b))
.transpose()?,
})
}
}

Expand Down
15 changes: 9 additions & 6 deletions vortex-scalar/src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use paste::paste;
use vortex_buffer::Buffer;
use vortex_dtype::half::f16;
use vortex_dtype::NativePType;
use vortex_error::{vortex_err, VortexResult};

/// Represents the internal data of a scalar value. Can only be interpreted by wrapping
/// up with a DType to make a Scalar.
Expand All @@ -26,10 +27,11 @@ impl ScalarValue {
matches!(self, ScalarValue::Null)
}

pub fn as_bool(&self) -> Option<bool> {
pub fn as_bool(&self) -> VortexResult<Option<bool>> {
match self {
ScalarValue::Bool(b) => Some(*b),
_ => None,
ScalarValue::Null => Ok(None),
ScalarValue::Bool(b) => Ok(Some(*b)),
_ => Err(vortex_err!("Not a bool scalar")),
}
}

Expand All @@ -40,10 +42,11 @@ impl ScalarValue {
}
}

pub fn as_bytes(&self) -> Option<Buffer> {
pub fn as_bytes(&self) -> VortexResult<Option<Buffer>> {
match self {
ScalarValue::Buffer(b) => Some(b.clone()),
_ => None,
ScalarValue::Null => Ok(None),
ScalarValue::Buffer(b) => Ok(Some(b.clone())),
_ => Err(vortex_err!("Not a binary scalar")),
}
}

Expand Down

0 comments on commit 64d31ae

Please sign in to comment.