Skip to content

Commit

Permalink
implement From<PrimitiveScalar> for Scalar
Browse files Browse the repository at this point in the history
  • Loading branch information
doki23 committed Dec 19, 2024
1 parent 530c2c0 commit 40fb896
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 12 deletions.
3 changes: 1 addition & 2 deletions vortex-array/src/array/constant/compute/binary_numeric.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ impl BinaryNumericFn<ConstantArray> for ConstantEncoding {
.scalar()
.as_primitive()
.checked_numeric_operator(rhs.as_primitive(), op)?
.ok_or_else(|| vortex_err!("numeric overflow"))?
.to_scalar(),
.ok_or_else(|| vortex_err!("numeric overflow"))?,
array.len(),
)
.into_array(),
Expand Down
2 changes: 1 addition & 1 deletion vortex-array/src/array/sparse/compute/binary_numeric.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ impl BinaryNumericFn<SparseArray> for SparseEncoding {
.as_primitive()
.checked_numeric_operator(rhs_scalar.as_primitive(), op)?
.ok_or_else(|| vortex_err!("numeric overflow"))?
.to_scalar();
.into();
SparseArray::try_new_from_patches(
new_patches,
array.len(),
Expand Down
11 changes: 11 additions & 0 deletions vortex-scalar/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,17 @@ where
}
}

impl From<PrimitiveScalar<'_>> for Scalar {
fn from(pscalar: PrimitiveScalar) -> Self {
let dtype = pscalar.dtype().clone();
let value = pscalar
.pvalue()
.map(|pvalue| ScalarValue(InnerScalarValue::Primitive(pvalue)))
.unwrap_or_else(|| ScalarValue(InnerScalarValue::Null));
Self::new(dtype, value)
}
}

macro_rules! from_vec_for_scalar {
($T:ty) => {
impl From<Vec<$T>> for Scalar {
Expand Down
14 changes: 5 additions & 9 deletions vortex-scalar/src/primitive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ impl<'a> PrimitiveScalar<'a> {
self.ptype
}

#[inline]
pub fn pvalue(&self) -> Option<PValue> {
self.pvalue
}

pub fn typed_value<T: NativePType + TryFrom<PValue, Error = VortexError>>(&self) -> Option<T> {
assert_eq!(
self.ptype,
Expand Down Expand Up @@ -109,15 +114,6 @@ impl<'a> PrimitiveScalar<'a> {
pub fn checked_sub(self, other: PrimitiveScalar<'a>) -> VortexResult<Option<Self>> {
self.checked_numeric_operator(other, BinaryNumericOperator::Sub)
}

pub fn to_scalar(self) -> Scalar {
Scalar::new(
self.dtype().clone(),
self.pvalue
.map(|pvalue| ScalarValue(InnerScalarValue::Primitive(pvalue)))
.unwrap_or_else(|| ScalarValue(InnerScalarValue::Null)),
)
}
}

pub trait FromPrimitiveOrF16: FromPrimitive {
Expand Down

0 comments on commit 40fb896

Please sign in to comment.