Skip to content

Commit

Permalink
use optional scalars
Browse files Browse the repository at this point in the history
  • Loading branch information
a10y committed Dec 17, 2024
1 parent 22b9fe5 commit a0c1a30
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
2 changes: 1 addition & 1 deletion encodings/fastlanes/src/bitpacking/compress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub fn bitpack_encode(array: PrimitiveArray, bit_width: u8) -> VortexResult<BitP
// Check array contains no negative values.
if array.ptype().is_signed_int() {
let has_negative_values = match_each_integer_ptype!(array.ptype(), |$P| {
array.statistics().compute_min::<$P>().unwrap_or_default() < 0
array.statistics().compute_min::<Option<$P>>().unwrap_or_default().unwrap_or_default() < 0
});
if has_negative_values {
vortex_bail!("cannot bitpack_encode array containing negative integers")
Expand Down
14 changes: 13 additions & 1 deletion vortex-array/src/stats/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,10 @@ pub trait Statistics {
/// Clear the value of the statistic
fn clear(&self, stat: Stat);

/// Computes the value of the stat if it's not present
/// Computes the value of the stat if it's not present.
///
/// Returns the scalar if compute succeeded, or `None` if the stat is not supported
/// for this array.
fn compute(&self, stat: Stat) -> Option<Scalar>;

/// Compute all the requested statistics (if not already present)
Expand Down Expand Up @@ -244,6 +247,9 @@ impl dyn Statistics + '_ {
})
}

/// Get or calculate the provided stat, converting the `Scalar` into a typed value.
///
/// This function will panic if the conversion fails.
pub fn compute_as<U: for<'a> TryFrom<&'a Scalar, Error = VortexError>>(
&self,
stat: Stat,
Expand Down Expand Up @@ -275,10 +281,16 @@ impl dyn Statistics + '_ {
})
}

/// Get or calculate the minimum value in the array, returning as a typed value.
///
/// This function will panic if the conversion fails.
pub fn compute_min<U: for<'a> TryFrom<&'a Scalar, Error = VortexError>>(&self) -> Option<U> {
self.compute_as(Stat::Min)
}

/// Get or calculate the maximum value in the array, returning as a typed value.
///
/// This function will panic if the conversion fails.
pub fn compute_max<U: for<'a> TryFrom<&'a Scalar, Error = VortexError>>(&self) -> Option<U> {
self.compute_as(Stat::Max)
}
Expand Down
4 changes: 2 additions & 2 deletions vortex-sampling-compressor/src/compressors/bitpacked.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ impl EncodingCompressor for BitPackedCompressor {
// Only arrays with non-negative values can be bit-packed
if !parray.ptype().is_unsigned_int() {
let has_negative_elements = match_each_integer_ptype!(parray.ptype(), |$P| {
parray.statistics().compute_min::<$P>().unwrap_or_default() < 0
parray.statistics().compute_min::<Option<$P>>().unwrap_or_default().unwrap_or_default() < 0
});

if has_negative_elements {
Expand Down Expand Up @@ -94,7 +94,7 @@ impl EncodingCompressor for BitPackedCompressor {
// Only arrays with non-negative values can be bit-packed
if !parray.ptype().is_unsigned_int() {
let has_negative_elements = match_each_integer_ptype!(parray.ptype(), |$P| {
parray.statistics().compute_min::<$P>().unwrap_or_default() < 0
parray.statistics().compute_min::<Option<$P>>().unwrap_or_default().unwrap_or_default() < 0
});

if has_negative_elements {
Expand Down

0 comments on commit a0c1a30

Please sign in to comment.