Skip to content

Commit cb371d9

Browse files
committed
(fix) return exact precision, rename var, and nullable_chunks in tests
The sortedness returned precission only when the sortedness was true. This was due to how I implemented the fn first, but now it can return precision also for `false` values. The other changes are just to improve legibility. Signed-off-by: Joaquin Colacci <[email protected]>
1 parent 8871d74 commit cb371d9

1 file changed

Lines changed: 33 additions & 65 deletions

File tree

‎vortex-layout/src/layouts/file_stats.rs‎

Lines changed: 33 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ use vortex_array::expr::stats::Precision;
3030
use vortex_array::expr::stats::Stat;
3131
use vortex_array::scalar::Scalar;
3232
use vortex_array::scalar::ScalarTruncation;
33+
use vortex_array::scalar::ScalarValue;
3334
use vortex_array::scalar::lower_bound;
3435
use vortex_array::scalar::upper_bound;
3536
use vortex_array::stats::StatsSet;
@@ -123,11 +124,11 @@ impl StatsAccumulator {
123124
// So, we need to keep each chunk's first and last values so [`StatsAccumulator::as_stats_set`]
124125
// can check where the chunks meet. Others stats only need each chunk's result.
125126
Stat::IsSorted | Stat::IsStrictSorted => {
126-
let are_values_sorted =
127+
let is_chunk_sorted =
127128
value.is_some_and(|value| value.as_bool().value() == Some(true));
128129
Some(IsSorted::make_partial(
129130
array,
130-
are_values_sorted,
131+
is_chunk_sorted,
131132
sortedness_stat_into_options(stat).strict,
132133
ctx,
133134
)?)
@@ -219,11 +220,8 @@ impl StatsAccumulator {
219220
}
220221
}
221222
Stat::IsSorted | Stat::IsStrictSorted => {
222-
let merged = merge_sortedness_partials(values, stat, ctx)?;
223-
224-
if merged.get(stat) == Precision::exact(true) {
225-
stats_set.set(stat, Precision::exact(true));
226-
}
223+
let precision = merge_sortedness_partials(values, stat, ctx)?;
224+
stats_set.set(stat, precision);
227225
}
228226
Stat::IsConstant => {}
229227
}
@@ -327,13 +325,12 @@ fn sortedness_stat_into_options(stat: Stat) -> IsSortedOptions {
327325
/// Merge sortedness partials built, while checking where consecutive
328326
/// chunks meet.
329327
///
330-
/// Returns `true` only when the merged are sorted. Otherwise, leaves
331-
/// the statistic absent.
328+
/// Returns an exact sortedness verdict when available.
332329
fn merge_sortedness_partials(
333330
partials: &ArrayRef,
334331
sortedness_stat: Stat,
335332
ctx: &mut ExecutionCtx,
336-
) -> VortexResult<StatsSet> {
333+
) -> VortexResult<Precision<ScalarValue>> {
337334
let dtype = partials
338335
.dtype()
339336
.as_struct_fields()
@@ -355,10 +352,10 @@ fn merge_sortedness_partials(
355352

356353
let result = accumulator.finish()?;
357354

358-
Ok(match result.as_bool().value() {
359-
Some(true) => StatsSet::of(sortedness_stat, Precision::exact(true)),
360-
_ => StatsSet::default(),
361-
})
355+
Ok(result
356+
.as_bool()
357+
.value()
358+
.map_or(Precision::Absent, Precision::exact))
362359
}
363360

364361
/// Arrays with their associated names, reduced version of a `StructArray`.
@@ -769,83 +766,54 @@ mod tests {
769766
acc.push_chunk(&values, &mut ctx)?;
770767

771768
let stats = acc.as_stats_set(&[Stat::IsSorted], &mut ctx)?;
772-
let expected = if expected {
773-
Precision::exact(true)
774-
} else {
775-
Precision::Absent
776-
};
777-
assert_eq!(stats.get(Stat::IsSorted), expected);
769+
assert_eq!(stats.get(Stat::IsSorted), Precision::exact(expected));
778770

779771
Ok(())
780772
}
781773

774+
macro_rules! nullable_chunks {
775+
($([$($value:expr),* $(,)?]),* $(,)?) => {
776+
vec![$(vec![$(Option::from($value)),*]),*]
777+
};
778+
}
779+
782780
#[rstest]
783-
#[case::ordered(
784-
vec![vec![Some(1.0), Some(2.0)], vec![Some(3.0), Some(4.0)]],
785-
true
786-
)]
787-
#[case::overlapping(
788-
vec![vec![Some(1.0), Some(3.0)], vec![Some(2.0), Some(4.0)]],
789-
false
790-
)]
791-
#[case::unsorted_chunk(
792-
vec![vec![Some(2.0), Some(1.0)], vec![Some(3.0), Some(4.0)]],
793-
false
794-
)]
781+
#[case::ordered(nullable_chunks![[1.0, 2.0], [3.0, 4.0]], true)]
782+
#[case::overlapping(nullable_chunks![[1.0, 3.0], [2.0, 4.0]], false)]
783+
#[case::unsorted_chunk(nullable_chunks![[2.0, 1.0], [3.0, 4.0]], false)]
795784
#[case::leading_null_across_chunks(
796-
vec![vec![None, Some(1.0), Some(2.0)], vec![Some(3.0), Some(4.0)]],
797-
true
798-
)]
799-
#[case::singleton_null_across_chunks(
800-
vec![vec![None], vec![Some(1.0), Some(2.0)]],
801-
true
802-
)]
803-
#[case::null_after_values(
804-
vec![vec![Some(1.0)], vec![None, Some(2.0)]],
805-
false
806-
)]
807-
#[case::nan_at_end(
808-
vec![vec![Some(1.0), Some(2.0)], vec![Some(3.0), Some(f64::NAN)]],
785+
nullable_chunks![[None, 1.0, 2.0], [3.0, 4.0]],
809786
true
810787
)]
788+
#[case::singleton_null_across_chunks(nullable_chunks![[None], [1.0, 2.0]], true)]
789+
#[case::null_after_values(nullable_chunks![[1.0], [None, 2.0]], false)]
790+
#[case::nan_at_end(nullable_chunks![[1.0, 2.0], [3.0, f64::NAN]], true)]
811791
#[case::nan_before_finite_values(
812-
vec![vec![Some(1.0), Some(f64::NAN)], vec![Some(2.0), Some(3.0)]],
792+
nullable_chunks![[1.0, f64::NAN], [2.0, 3.0]],
813793
false
814794
)]
815-
#[case::empty_chunk(
816-
vec![vec![], vec![None, Some(1.0), Some(2.0)]],
817-
true
818-
)]
819-
#[case::empty_between_chunks(vec![vec![Some(2.0)], vec![], vec![Some(1.0)]], false)]
795+
#[case::empty_chunk(nullable_chunks![[], [None, 1.0, 2.0]], true)]
796+
#[case::empty_between_chunks(nullable_chunks![[2.0], [], [1.0]], false)]
820797
fn combines_chunk_sortedness(
821798
#[case] chunks: Vec<Vec<Option<f64>>>,
822799
#[case] expected: bool,
823800
#[values(Stat::IsSorted, Stat::IsStrictSorted)] stat: Stat,
824801
) -> VortexResult<()> {
825802
let stats = chunk_sortedness_stats(chunks, &[stat])?;
826-
let expected = if expected {
827-
Precision::exact(true)
828-
} else {
829-
Precision::Absent
830-
};
831-
assert_eq!(stats.get(stat), expected);
803+
assert_eq!(stats.get(stat), Precision::exact(expected));
832804

833805
Ok(())
834806
}
835807

836808
#[rstest]
837-
#[case::equal_numeric_boundary(
838-
vec![vec![Some(1.0), Some(2.0)], vec![Some(2.0), Some(3.0)]]
839-
)]
840-
#[case::duplicate_nulls_across_chunks(vec![vec![None], vec![None, Some(1.0)]])]
841-
#[case::equal_nan_boundary(
842-
vec![vec![Some(1.0), Some(f64::NAN)], vec![Some(f64::NAN)]]
843-
)]
809+
#[case::equal_numeric_boundary(nullable_chunks![[1.0, 2.0], [2.0, 3.0]])]
810+
#[case::duplicate_nulls_across_chunks(nullable_chunks![[None], [None, 1.0]])]
811+
#[case::equal_nan_boundary(nullable_chunks![[1.0, f64::NAN], [f64::NAN]])]
844812
fn strict_chunk_sortedness(#[case] chunks: Vec<Vec<Option<f64>>>) -> VortexResult<()> {
845813
let requested = [Stat::IsSorted, Stat::IsStrictSorted];
846814
let stats = chunk_sortedness_stats(chunks, &requested)?;
847815
assert_eq!(stats.get(Stat::IsSorted), Precision::exact(true));
848-
assert_eq!(stats.get(Stat::IsStrictSorted), Precision::Absent);
816+
assert_eq!(stats.get(Stat::IsStrictSorted), Precision::exact(false));
849817

850818
Ok(())
851819
}

0 commit comments

Comments
 (0)