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

Clean up runend metadata & stats #1011

Merged
merged 5 commits into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
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
70 changes: 50 additions & 20 deletions encodings/runend/src/compress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ use std::cmp::min;
use itertools::Itertools;
use num_traits::{AsPrimitive, FromPrimitive};
use vortex::array::PrimitiveArray;
use vortex::compute::unary::scalar_at;
use vortex::stats::{ArrayStatistics, Stat};
use vortex::validity::Validity;
use vortex::ArrayDType;
use vortex_dtype::{match_each_integer_ptype, match_each_native_ptype, NativePType, Nullability};
use vortex_error::{vortex_panic, VortexResult};
use vortex_error::{vortex_panic, VortexResult, VortexUnwrap as _};

pub fn runend_encode(array: &PrimitiveArray) -> (PrimitiveArray, PrimitiveArray) {
let validity = if array.dtype().nullability() == Nullability::NonNullable {
Expand All @@ -16,27 +17,56 @@ pub fn runend_encode(array: &PrimitiveArray) -> (PrimitiveArray, PrimitiveArray)
Validity::AllValid
};

match_each_native_ptype!(array.ptype(), |$P| {
let (compressed_ends, compressed_values) = match_each_native_ptype!(array.ptype(), |$P| {
let (ends, values) = runend_encode_primitive(array.maybe_null_slice::<$P>());
(PrimitiveArray::from_vec(ends, Validity::NonNullable), PrimitiveArray::from_vec(values, validity))
});

let mut compressed_values = PrimitiveArray::from_vec(values, validity);
compressed_values.statistics().set(Stat::IsConstant, false.into());
compressed_values.statistics().set(Stat::RunCount, compressed_values.len().into());
array.statistics().get(Stat::Min).map(|s| compressed_values.statistics().set(Stat::Min, s));
array.statistics().get(Stat::Max).map(|s| compressed_values.statistics().set(Stat::Max, s));
array.statistics().get(Stat::IsSorted).map(|s| compressed_values.statistics().set(Stat::IsSorted, s));
array.statistics().get(Stat::IsStrictSorted).map(|s| compressed_values.statistics().set(Stat::IsStrictSorted, s));

let compressed_ends = PrimitiveArray::from(ends);
compressed_ends.statistics().set(Stat::IsSorted, true.into());
compressed_ends.statistics().set(Stat::IsStrictSorted, true.into());
compressed_ends.statistics().set(Stat::IsConstant, false.into());
compressed_ends.statistics().set(Stat::Max, array.len().into());
compressed_ends.statistics().set(Stat::RunCount, compressed_ends.len().into());

assert_eq!(array.dtype(), compressed_values.dtype());
(compressed_ends, compressed_values)
})
// the values array stats are trivially derived
compressed_values
.statistics()
.set(Stat::RunCount, compressed_values.len().into());
compressed_values
.statistics()
.set(Stat::IsConstant, (compressed_values.len() == 1).into());
if let Some(min) = array.statistics().get(Stat::Min) {
compressed_values.statistics().set(Stat::Min, min);
}
if let Some(max) = array.statistics().get(Stat::Max) {
compressed_values.statistics().set(Stat::Max, max);
}
if let Some(is_sorted) = array.statistics().get(Stat::IsSorted) {
compressed_values
.statistics()
.set(Stat::IsSorted, is_sorted);
}
if let Some(is_strict_sorted) = array.statistics().get(Stat::IsStrictSorted) {
compressed_values
.statistics()
.set(Stat::IsStrictSorted, is_strict_sorted);
}

compressed_ends
.statistics()
.set(Stat::IsConstant, (compressed_ends.len() == 1).into());
compressed_ends
.statistics()
.set(Stat::IsSorted, true.into());
compressed_ends
.statistics()
.set(Stat::IsStrictSorted, true.into());
if !compressed_ends.is_empty() {
compressed_ends
.statistics()
.set(Stat::Min, scalar_at(&compressed_ends, 0).vortex_unwrap());
lwwmanning marked this conversation as resolved.
Show resolved Hide resolved
compressed_ends.statistics().set(
Stat::Max,
scalar_at(&compressed_ends, compressed_ends.len() - 1).vortex_unwrap(),
lwwmanning marked this conversation as resolved.
Show resolved Hide resolved
);
}

assert_eq!(array.dtype(), compressed_values.dtype());
(compressed_ends, compressed_values)
}

fn runend_encode_primitive<T: NativePType>(elements: &[T]) -> (Vec<u64>, Vec<T>) {
Expand Down
6 changes: 3 additions & 3 deletions encodings/runend/src/compute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,12 @@ impl SliceFn for RunEndArray {
let slice_begin = self.find_physical_index(start)?;
let slice_end = self.find_physical_index(stop)?;

Ok(Self::with_offset_and_size(
Ok(Self::with_offset_and_length(
slice(self.ends(), slice_begin, slice_end + 1)?,
slice(self.values(), slice_begin, slice_end + 1)?,
self.validity().slice(start, stop)?,
stop - start,
start + self.offset(),
stop - start,
)?
.into_array())
}
Expand Down Expand Up @@ -187,7 +187,7 @@ mod test {
#[test]
fn slice_with_nulls() {
let array = RunEndArray::try_new(
PrimitiveArray::from(vec![3, 6, 8, 12]).into_array(),
PrimitiveArray::from(vec![3u32, 6, 8, 12]).into_array(),
PrimitiveArray::from_vec(vec![1, 4, 2, 5], Validity::AllValid).into_array(),
Validity::from(vec![
false, false, false, false, true, true, false, false, false, false, true, true,
Expand Down
36 changes: 23 additions & 13 deletions encodings/runend/src/runend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use vortex::{
impl_encoding, Array, ArrayDType, ArrayTrait, Canonical, IntoArray, IntoArrayVariant,
IntoCanonical,
};
use vortex_dtype::DType;
use vortex_dtype::PType;
use vortex_error::{vortex_bail, VortexExpect as _, VortexResult};

use crate::compress::{runend_decode, runend_encode};
Expand All @@ -23,10 +23,9 @@ impl_encoding!("vortex.runend", ids::RUN_END, RunEnd);
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RunEndMetadata {
validity: ValidityMetadata,
ends_dtype: DType,
ends_ptype: PType,
num_runs: usize,
offset: usize,
length: usize,
}

impl Display for RunEndMetadata {
Expand All @@ -37,20 +36,24 @@ impl Display for RunEndMetadata {

impl RunEndArray {
pub fn try_new(ends: Array, values: Array, validity: Validity) -> VortexResult<Self> {
let length: usize = scalar_at(&ends, ends.len() - 1)?.as_ref().try_into()?;
Self::with_offset_and_size(ends, values, validity, length, 0)
let length = if ends.is_empty() {
0
} else {
scalar_at(&ends, ends.len() - 1)?.as_ref().try_into()?
};
Self::with_offset_and_length(ends, values, validity, 0, length)
}

pub(crate) fn with_offset_and_size(
pub(crate) fn with_offset_and_length(
ends: Array,
values: Array,
validity: Validity,
length: usize,
offset: usize,
length: usize,
) -> VortexResult<Self> {
if values.dtype().is_nullable() == (validity == Validity::NonNullable) {
if values.dtype().nullability() != validity.nullability() {
vortex_bail!(
"incorrect validity {:?} for dtype {}",
"invalid validity {:?} for dtype {}",
validity,
values.dtype()
);
Expand All @@ -63,16 +66,19 @@ impl RunEndArray {
}
}

if !ends.dtype().is_unsigned_int() || ends.dtype().is_nullable() {
vortex_bail!(MismatchedTypes: "non-nullable unsigned int", ends.dtype());
}
if !ends.statistics().compute_is_strict_sorted().unwrap_or(true) {
vortex_bail!("Ends array must be strictly sorted",);
vortex_bail!("Ends array must be strictly sorted");
}

let dtype = values.dtype().clone();
let metadata = RunEndMetadata {
validity: validity.to_metadata(length)?,
ends_dtype: ends.dtype().clone(),
ends_ptype: PType::try_from(ends.dtype())?,
num_runs: ends.len(),
offset,
length,
};

let mut children = Vec::with_capacity(3);
Expand Down Expand Up @@ -142,7 +148,11 @@ impl RunEndArray {
#[inline]
pub fn ends(&self) -> Array {
self.as_ref()
.child(0, &self.metadata().ends_dtype, self.metadata().num_runs)
.child(
0,
&self.metadata().ends_ptype.into(),
self.metadata().num_runs,
)
.vortex_expect("RunEndArray is missing its run ends")
}

Expand Down
Loading