Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ use vortex_array::ArrayRef;
use vortex_array::ExecutionCtx;
use vortex_array::aggregate_fn::AggregateFnRef;
use vortex_array::aggregate_fn::fns::is_sorted::IsSorted;
use vortex_array::aggregate_fn::fns::is_sorted::is_sorted;
use vortex_array::aggregate_fn::fns::is_sorted::is_strict_sorted;
use vortex_array::aggregate_fn::fns::is_sorted::is_sorted_with_options;
use vortex_array::aggregate_fn::fns::max::Max;
use vortex_array::aggregate_fn::fns::min::Min;
use vortex_array::aggregate_fn::fns::min_max::MinMax;
Expand Down Expand Up @@ -49,9 +48,10 @@ impl DynAggregateKernel for DecimalBytePartsAggregateKernel {
}

if let Some(options) = aggregate_fn.as_opt::<IsSorted>() {
return Ok(Some(is_sorted_partial(
let sorted = is_sorted_with_options(array.msp(), *options, ctx)?;
return Ok(Some(IsSorted::make_partial(
batch,
array.msp(),
sorted,
options.strict,
ctx,
)?));
Expand All @@ -65,21 +65,6 @@ impl DynAggregateKernel for DecimalBytePartsAggregateKernel {
}
}

fn is_sorted_partial(
batch: &ArrayRef,
msp: &ArrayRef,
strict: bool,
ctx: &mut ExecutionCtx,
) -> VortexResult<Scalar> {
let sorted = if strict {
is_strict_sorted(msp, ctx)?
} else {
is_sorted(msp, ctx)?
};

IsSorted::make_partial(batch, sorted, strict, ctx)
}

/// Returns `None` to decline, so the accumulator falls back to decoding the batch.
fn sum_partial(
aggregate_fn: &AggregateFnRef,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use vortex_array::aggregate_fn::NumericalAggregateOpts;
use vortex_array::aggregate_fn::fns::first::First;
use vortex_array::aggregate_fn::fns::is_sorted::IsSorted;
use vortex_array::aggregate_fn::fns::is_sorted::IsSortedOptions;
use vortex_array::aggregate_fn::fns::is_sorted::SortDirection;
use vortex_array::aggregate_fn::fns::max::Max;
use vortex_array::aggregate_fn::fns::min::Min;
use vortex_array::aggregate_fn::fns::min_max::MinMax;
Expand Down Expand Up @@ -86,15 +87,30 @@ fn aggregates() -> Vec<AggregateFnRef> {
MinMax.bind(options),
Min.bind(options),
Max.bind(options),
IsSorted.bind(IsSortedOptions { strict: false }),
IsSorted.bind(IsSortedOptions { strict: true }),
IsSorted.bind(IsSortedOptions {
strict: false,
direction: SortDirection::Asc,
}),
IsSorted.bind(IsSortedOptions {
strict: true,
direction: SortDirection::Asc,
}),
IsSorted.bind(IsSortedOptions {
strict: false,
direction: SortDirection::Desc,
}),
IsSorted.bind(IsSortedOptions {
strict: true,
direction: SortDirection::Desc,
}),
Sum.bind(options),
]
}

#[rstest]
#[case::ascending(vec![-9, -7, -5, -3, -1, 0, 2, 4, 6, 8], Validity::NonNullable)]
#[case::duplicate(vec![-9, -7, -5, -3, -1, 0, 2, 4, 6, 6], Validity::AllValid)]
#[case::descending(vec![8, 6, 4, 2, 0, -1, -3, -5, -7, -9], Validity::NonNullable)]
#[case::unsorted(vec![8, -7, -5, -3, -1, 0, 2, 4, 6, -9], Validity::NonNullable)]
#[case::partially_valid(vec![-9, -7, -5, -3, -1, 0, 2, 4, 6, 8], Validity::from_iter([false, true, true, true, true, true, true, true, true, true]))]
#[case::all_null(vec![0; 10], Validity::AllInvalid)]
Expand Down
42 changes: 35 additions & 7 deletions encodings/fastlanes/src/for_/compute/is_sorted.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ use vortex_array::ExecutionCtx;
use vortex_array::IntoArray;
use vortex_array::aggregate_fn::AggregateFnRef;
use vortex_array::aggregate_fn::fns::is_sorted::IsSorted;
use vortex_array::aggregate_fn::fns::is_sorted::is_sorted;
use vortex_array::aggregate_fn::fns::is_sorted::is_strict_sorted;
use vortex_array::aggregate_fn::fns::is_sorted::is_sorted_with_options;
use vortex_array::aggregate_fn::kernels::DynAggregateKernel;
use vortex_array::arrays::PrimitiveArray;
use vortex_array::scalar::Scalar;
Expand Down Expand Up @@ -47,11 +46,7 @@ impl DynAggregateKernel for FoRIsSortedKernel {
)
.into_array();

let result = if options.strict {
is_strict_sorted(&unsigned_array, ctx)?
} else {
is_sorted(&unsigned_array, ctx)?
};
let result = is_sorted_with_options(&unsigned_array, *options, ctx)?;

Ok(Some(IsSorted::make_partial(
batch,
Expand All @@ -68,7 +63,10 @@ mod test {

use vortex_array::IntoArray;
use vortex_array::VortexSessionExecute;
use vortex_array::aggregate_fn::fns::is_sorted::IsSortedOptions;
use vortex_array::aggregate_fn::fns::is_sorted::SortDirection;
use vortex_array::aggregate_fn::fns::is_sorted::is_sorted;
use vortex_array::aggregate_fn::fns::is_sorted::is_sorted_with_options;
use vortex_array::arrays::PrimitiveArray;
use vortex_array::validity::Validity;
use vortex_buffer::buffer;
Expand Down Expand Up @@ -142,5 +140,35 @@ mod test {
"{}",
b.encoded().display_values(&ctx)
);

// Tests for desc sort
let desc = IsSortedOptions {
strict: false,
direction: SortDirection::Desc,
};

let a = PrimitiveArray::new(buffer![i8::MAX, 0, -1, i8::MIN], Validity::NonNullable);
let b = FoRData::encode(a, &mut ctx).unwrap();
assert!(
is_sorted_with_options(&b.clone().into_array(), desc, &mut ctx).unwrap(),
"{}",
b.encoded().display_values(&ctx)
);

let a = PrimitiveArray::new(buffer![-1, -3, -10], Validity::NonNullable);
let b = FoRData::encode(a, &mut ctx).unwrap();
assert!(
is_sorted_with_options(&b.clone().into_array(), desc, &mut ctx).unwrap(),
"{}",
b.encoded().display_values(&ctx)
);

let a = PrimitiveArray::new(buffer![i8::MIN, 0, i8::MAX], Validity::NonNullable);
let b = FoRData::encode(a, &mut ctx).unwrap();
assert!(
!is_sorted_with_options(&b.clone().into_array(), desc, &mut ctx).unwrap(),
"{}",
b.encoded().display_values(&ctx)
);
}
}
40 changes: 32 additions & 8 deletions encodings/sequence/src/array/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ use vortex_array::EqMode;
use vortex_array::ExecutionCtx;
use vortex_array::ExecutionResult;
use vortex_array::ProbeState;
use vortex_array::aggregate_fn::fns::is_sorted::IsSortedOptions;
use vortex_array::aggregate_fn::fns::is_sorted::SortDirection;
use vortex_array::buffer::BufferHandle;
use vortex_array::dtype::DType;
use vortex_array::dtype::NativePType;
Expand Down Expand Up @@ -380,24 +382,46 @@ impl Sequence {
/// Whether a sequence `A[i] = base + i * multiplier` of `length` elements is sorted, or
/// strictly sorted when `strict` is set.
///
/// With at least two elements it is sorted iff multiplier >= 0, and strictly sorted iff
/// multiplier > 0. Zero or one elements have no adjacent pair to compare, so they are
/// strictly sorted whatever the multiplier.
pub(crate) fn is_sorted(multiplier: PValue, length: usize, strict: bool) -> bool {
/// With at least two elements, the multiplier decides the order:
///
/// | Direction | Sorted iff | Strictly sorted iff |
/// |------------|-------------------|---------------------|
/// | Ascending | `multiplier >= 0` | `multiplier > 0` |
/// | Descending | `multiplier <= 0` | `multiplier < 0` |
///
/// Zero or one elements have no adjacent pair to compare, so they are strictly sorted whatever
/// the multiplier.
pub(crate) fn is_sorted(multiplier: PValue, length: usize, options: IsSortedOptions) -> bool {
if length <= 1 {
return true;
}
match_each_pvalue!(
multiplier,
uint: |v| { !strict || v > 0 },
int: |v| { if strict { v > 0 } else { v >= 0 } },
uint: |v| {
match options.direction {
SortDirection::Asc => !options.strict || v > 0,
SortDirection::Desc => !options.strict && v == 0,
}
},
int: |v| {
match (options.direction, options.strict) {
(SortDirection::Asc, false) => v >= 0,
(SortDirection::Asc, true) => v > 0,
(SortDirection::Desc, false) => v <= 0,
(SortDirection::Desc, true) => v < 0,
}
},
float: |_v| { unreachable!("float multiplier not supported") }
)
}

fn stats(multiplier: PValue, length: usize) -> StatsSet {
let is_sorted = Self::is_sorted(multiplier, length, false);
let is_strict_sorted = Self::is_sorted(multiplier, length, true);
let is_sorted = Self::is_sorted(multiplier, length, IsSortedOptions::default());
let strict = IsSortedOptions {
strict: true,
..Default::default()
};
let is_strict_sorted = Self::is_sorted(multiplier, length, strict);

// SAFETY: we don't have duplicate stats.
unsafe {
Expand Down
59 changes: 45 additions & 14 deletions encodings/sequence/src/compute/is_sorted.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ impl DynAggregateKernel for SequenceIsSortedKernel {

// `is_sorted` short-circuits on len <= 1, but the Chunked accumulator dispatches this
// kernel per chunk without that outer check, so the length has to be considered here too.
let result = Sequence::is_sorted(array.multiplier(), array.len(), options.strict);
let result = Sequence::is_sorted(array.multiplier(), array.len(), *options);

Ok(Some(IsSorted::make_partial(
batch,
Expand All @@ -50,18 +50,23 @@ mod tests {
use std::sync::LazyLock;

use rstest::rstest;
use vortex_array::Canonical;
use vortex_array::IntoArray;
use vortex_array::VortexSessionExecute;
use vortex_array::aggregate_fn::AggregateFnVTableExt;
use vortex_array::aggregate_fn::fns::is_sorted::IsSorted;
use vortex_array::aggregate_fn::fns::is_sorted::IsSortedOptions;
use vortex_array::aggregate_fn::fns::is_sorted::SortDirection;
use vortex_array::aggregate_fn::fns::is_sorted::is_sorted;
use vortex_array::aggregate_fn::fns::is_sorted::is_sorted_with_options;
use vortex_array::aggregate_fn::fns::is_sorted::is_strict_sorted;
use vortex_array::aggregate_fn::kernels::DynAggregateKernel;
use vortex_array::arrays::ChunkedArray;
use vortex_array::dtype::DType;
use vortex_array::dtype::NativePType;
use vortex_array::dtype::Nullability;
use vortex_array::dtype::PType;
use vortex_array::scalar::PValue;
use vortex_error::VortexResult;
use vortex_session::VortexSession;

Expand All @@ -74,24 +79,50 @@ mod tests {
session
});

fn assert_kernel_agrees_with_decoded<T: NativePType + Into<PValue>>(
base: T,
multiplier: T,
length: usize,
options: IsSortedOptions,
) -> VortexResult<()> {
let mut ctx = SESSION.create_execution_ctx();
let array = Sequence::try_new_typed(base, multiplier, Nullability::NonNullable, length)?
.into_array();
let decoded = array.clone().execute::<Canonical>(&mut ctx)?.into_array();
let expected = is_sorted_with_options(&decoded, options, &mut ctx)?;

let result = SequenceIsSortedKernel.aggregate(&IsSorted.bind(options), &array, &mut ctx)?;
assert_eq!(
result,
Some(IsSorted::make_partial(
&array,
expected,
options.strict,
&mut ctx
)?),
);
Ok(())
}

#[rstest]
fn singleton_is_sorted_for_any_multiplier(
fn multiplier_sets_direction(
#[values(-1i64, 0, 3)] multiplier: i64,
#[values(1, 2)] length: usize,
#[values(false, true)] strict: bool,
#[values(SortDirection::Asc, SortDirection::Desc)] direction: SortDirection,
) -> VortexResult<()> {
let mut ctx = SESSION.create_execution_ctx();
let array =
Sequence::try_new_typed(10i64, multiplier, Nullability::NonNullable, 1)?.into_array();

let result = SequenceIsSortedKernel.aggregate(
&IsSorted.bind(IsSortedOptions { strict }),
&array,
&mut ctx,
)?;
let options = IsSortedOptions { strict, direction };
assert_kernel_agrees_with_decoded(10i64, multiplier, length, options)
}

let expected = IsSorted::make_partial(&array, true, strict, &mut ctx)?;
assert_eq!(result, Some(expected));
Ok(())
#[rstest]
fn unsigned_multiplier_sets_direction(
#[values(0u64, 3)] multiplier: u64,
#[values(false, true)] strict: bool,
#[values(SortDirection::Asc, SortDirection::Desc)] direction: SortDirection,
) -> VortexResult<()> {
let options = IsSortedOptions { strict, direction };
assert_kernel_agrees_with_decoded(10u64, multiplier, 2, options)
}

#[rstest]
Expand Down
5 changes: 4 additions & 1 deletion encodings/uuid/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,10 @@ mod tests {
let column = uuid_column(leading_bytes)?;
let mut acc = Accumulator::try_new(
ExtensionStoragePushdown::new(IsSorted, Uuid),
IsSortedOptions { strict },
IsSortedOptions {
strict,
..Default::default()
},
column.dtype().clone(),
)?;
acc.accumulate(&column, &mut session.create_execution_ctx())?;
Expand Down
4 changes: 4 additions & 0 deletions vortex-array/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,10 @@ harness = false
name = "compare_sorted"
harness = false

[[bench]]
name = "is_sorted"
harness = false

[[bench]]
name = "binary_ops"
harness = false
Expand Down
Loading
Loading