Skip to content

Commit

Permalink
ugh what a fight
Browse files Browse the repository at this point in the history
  • Loading branch information
danking committed Oct 30, 2024
1 parent f2ccd90 commit ae65a37
Show file tree
Hide file tree
Showing 7 changed files with 278 additions and 122 deletions.
6 changes: 6 additions & 0 deletions vortex-buffer/src/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,9 @@ impl AsRef<str> for BufferString {
self.as_str()
}
}

impl AsRef<[u8]> for BufferString {
fn as_ref(&self) -> &[u8] {
self.as_str().as_bytes()
}
}
33 changes: 33 additions & 0 deletions vortex-scalar/src/binary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,36 @@ impl<'a> TryFrom<&'a Scalar> for Buffer {
.ok_or_else(|| vortex_err!("Can't extract present value from null scalar"))
}
}

impl TryFrom<&ScalarValue> for Buffer {
type Error = VortexError;

fn try_from(value: &ScalarValue) -> Result<Self, Self::Error> {
Option::<Buffer>::try_from(value)?
.ok_or_else(|| vortex_err!("Can't extract present value from null scalar"))
}
}

impl TryFrom<ScalarValue> for Buffer {
type Error = VortexError;

fn try_from(value: ScalarValue) -> Result<Self, Self::Error> {
Buffer::try_from(&value)
}
}

impl TryFrom<&ScalarValue> for Option<Buffer> {
type Error = VortexError;

fn try_from(value: &ScalarValue) -> Result<Self, Self::Error> {
value.as_buffer()
}
}

impl TryFrom<ScalarValue> for Option<Buffer> {
type Error = VortexError;

fn try_from(value: ScalarValue) -> Result<Self, Self::Error> {
Option::<Buffer>::try_from(&value)
}
}
27 changes: 25 additions & 2 deletions vortex-scalar/src/bool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,16 +73,39 @@ impl From<bool> for Scalar {
}
}

impl TryFrom<&ScalarValue> for Option<bool> {
type Error = VortexError;

fn try_from(value: &ScalarValue) -> VortexResult<Self> {
value.as_bool()
}
}

impl TryFrom<ScalarValue> for Option<bool> {
type Error = VortexError;

fn try_from(value: ScalarValue) -> VortexResult<Self> {
Option::<bool>::try_from(&value)
}
}

impl TryFrom<&ScalarValue> for bool {
type Error = VortexError;

fn try_from(value: &ScalarValue) -> VortexResult<Self> {
value
.as_bool()?
Option::<bool>::try_from(value)?
.ok_or_else(|| vortex_err!("Can't extract present value from null scalar"))
}
}

impl TryFrom<ScalarValue> for bool {
type Error = VortexError;

fn try_from(value: ScalarValue) -> VortexResult<Self> {
bool::try_from(&value)
}
}

impl From<bool> for ScalarValue {
fn from(value: bool) -> Self {
ScalarValue::Bool(value)
Expand Down
24 changes: 21 additions & 3 deletions vortex-scalar/src/primitive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,19 +154,37 @@ macro_rules! primitive_scalar {
impl TryFrom<&ScalarValue> for $T {
type Error = VortexError;

fn try_from(value: &ScalarValue) -> Result<Self, Self::Error> {
Option::<$T>::try_from(value)?
.ok_or_else(|| vortex_err!("Can't extract present value from null scalar"))
}
}

impl TryFrom<ScalarValue> for $T {
type Error = VortexError;

fn try_from(value: ScalarValue) -> Result<Self, Self::Error> {
<$T>::try_from(&value)
}
}

impl TryFrom<&ScalarValue> for Option<$T> {
type Error = VortexError;

fn try_from(value: &ScalarValue) -> Result<Self, Self::Error> {
match value {
ScalarValue::Primitive(pvalue) => <$T>::try_from(*pvalue),
ScalarValue::Null => Ok(None),
ScalarValue::Primitive(pvalue) => Ok(Some(<$T>::try_from(*pvalue)?)),
_ => vortex_bail!("expected primitive"),
}
}
}

impl TryFrom<ScalarValue> for $T {
impl TryFrom<ScalarValue> for Option<$T> {
type Error = VortexError;

fn try_from(value: ScalarValue) -> Result<Self, Self::Error> {
<$T>::try_from(&value)
Option::<$T>::try_from(&value)
}
}
};
Expand Down
33 changes: 33 additions & 0 deletions vortex-scalar/src/utf8.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,36 @@ impl From<&str> for Scalar {
}
}
}

impl TryFrom<&ScalarValue> for BufferString {
type Error = VortexError;

fn try_from(value: &ScalarValue) -> Result<Self, Self::Error> {
Option::<BufferString>::try_from(value)?
.ok_or_else(|| vortex_err!("Can't extract present value from null scalar"))
}
}

impl TryFrom<ScalarValue> for BufferString {
type Error = VortexError;

fn try_from(value: ScalarValue) -> Result<Self, Self::Error> {
BufferString::try_from(&value)
}
}

impl TryFrom<&ScalarValue> for Option<BufferString> {
type Error = VortexError;

fn try_from(value: &ScalarValue) -> Result<Self, Self::Error> {
value.as_buffer_string()
}
}

impl TryFrom<ScalarValue> for Option<BufferString> {
type Error = VortexError;

fn try_from(value: ScalarValue) -> Result<Self, Self::Error> {
Option::<BufferString>::try_from(&value)
}
}
27 changes: 26 additions & 1 deletion vortex-scalar/src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::sync::Arc;
use half::f16;
use vortex_buffer::{Buffer, BufferString};
use vortex_dtype::DType;
use vortex_error::{vortex_err, VortexResult};
use vortex_error::{vortex_err, VortexError, VortexResult};

use crate::pvalue::PValue;

Expand Down Expand Up @@ -94,6 +94,13 @@ impl ScalarValue {
}
}

pub fn as_null(&self) -> VortexResult<Option<Empty>> {
match self {
Self::Null => Ok(None),
_ => Err(vortex_err!("Expected a Null scalar, found {:?}", self)),
}
}

pub fn as_bool(&self) -> VortexResult<Option<bool>> {
match self {
Self::Null => Ok(None),
Expand Down Expand Up @@ -211,6 +218,24 @@ from_vec_for_scalar_value!(BufferString);
from_vec_for_scalar_value!(bytes::Bytes);
from_vec_for_scalar_value!(Buffer);

pub enum Empty {}

impl TryFrom<&ScalarValue> for Option<Empty> {
type Error = VortexError;

fn try_from(value: &ScalarValue) -> Result<Self, Self::Error> {
value.as_null()
}
}

impl TryFrom<ScalarValue> for Option<Empty> {
type Error = VortexError;

fn try_from(value: ScalarValue) -> Result<Self, Self::Error> {
Option::<Empty>::try_from(&value)
}
}

#[cfg(test)]
mod test {
use vortex_dtype::{DType, Nullability, PType, StructDType};
Expand Down
Loading

0 comments on commit ae65a37

Please sign in to comment.