Skip to content

Commit de47414

Browse files
authored
Port IsSortedFn to IsSortedKernel (#3203)
1 parent fc3196c commit de47414

File tree

20 files changed

+314
-192
lines changed

20 files changed

+314
-192
lines changed
Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
use vortex_array::compute::{IsSortedFn, is_sorted, is_strict_sorted};
1+
use vortex_array::compute::{IsSortedKernel, IsSortedKernelAdapter, is_sorted, is_strict_sorted};
2+
use vortex_array::register_kernel;
23
use vortex_error::VortexResult;
34

45
use crate::{DictArray, DictEncoding};
56

6-
impl IsSortedFn<&DictArray> for DictEncoding {
7+
impl IsSortedKernel for DictEncoding {
78
fn is_sorted(&self, array: &DictArray) -> VortexResult<bool> {
9+
// TODO(ngates): we should change these kernels to return Option<bool> to allow for "unknown"
810
let is_sorted = is_sorted(array.values())? && is_sorted(array.codes())?;
911
Ok(is_sorted)
1012
}
@@ -14,3 +16,5 @@ impl IsSortedFn<&DictArray> for DictEncoding {
1416
Ok(is_sorted)
1517
}
1618
}
19+
20+
register_kernel!(IsSortedKernelAdapter(DictEncoding).lift());

encodings/dict/src/compute/mod.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ mod like;
77
mod min_max;
88

99
use vortex_array::compute::{
10-
FillNullFn, FilterKernel, FilterKernelAdapter, IsSortedFn, MinMaxFn, ScalarAtFn, TakeFn,
11-
filter, scalar_at, take,
10+
FillNullFn, FilterKernel, FilterKernelAdapter, MinMaxFn, ScalarAtFn, TakeFn, filter, scalar_at,
11+
take,
1212
};
1313
use vortex_array::vtable::ComputeVTable;
1414
use vortex_array::{Array, ArrayRef, register_kernel};
@@ -23,10 +23,6 @@ impl ComputeVTable for DictEncoding {
2323
Some(self)
2424
}
2525

26-
fn is_sorted_fn(&self) -> Option<&dyn IsSortedFn<&dyn Array>> {
27-
Some(self)
28-
}
29-
3026
fn scalar_at_fn(&self) -> Option<&dyn ScalarAtFn<&dyn Array>> {
3127
Some(self)
3228
}
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
use vortex_array::Array;
2-
use vortex_array::compute::{IsSortedFn, is_sorted, is_strict_sorted};
1+
use vortex_array::compute::{IsSortedKernel, IsSortedKernelAdapter, is_sorted, is_strict_sorted};
2+
use vortex_array::{Array, register_kernel};
33

44
use crate::{RunEndArray, RunEndEncoding};
55

6-
impl IsSortedFn<&RunEndArray> for RunEndEncoding {
6+
impl IsSortedKernel for RunEndEncoding {
77
fn is_sorted(&self, array: &RunEndArray) -> vortex_error::VortexResult<bool> {
88
is_sorted(array.values())
99
}
@@ -12,3 +12,5 @@ impl IsSortedFn<&RunEndArray> for RunEndEncoding {
1212
is_strict_sorted(array.to_canonical()?.as_ref())
1313
}
1414
}
15+
16+
register_kernel!(IsSortedKernelAdapter(RunEndEncoding).lift());

encodings/runend/src/compute/mod.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ pub(crate) mod take;
1010
mod take_from;
1111

1212
use vortex_array::Array;
13-
use vortex_array::compute::{FillNullFn, IsSortedFn, MinMaxFn, ScalarAtFn, TakeFn, TakeFromFn};
13+
use vortex_array::compute::{FillNullFn, MinMaxFn, ScalarAtFn, TakeFn, TakeFromFn};
1414
use vortex_array::vtable::ComputeVTable;
1515

1616
use crate::RunEndEncoding;
@@ -20,10 +20,6 @@ impl ComputeVTable for RunEndEncoding {
2020
Some(self)
2121
}
2222

23-
fn is_sorted_fn(&self) -> Option<&dyn IsSortedFn<&dyn Array>> {
24-
Some(self)
25-
}
26-
2723
fn scalar_at_fn(&self) -> Option<&dyn ScalarAtFn<&dyn Array>> {
2824
Some(self)
2925
}

vortex-array/src/arrays/bool/compute/is_sorted.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
use vortex_error::VortexResult;
22
use vortex_mask::Mask;
33

4-
use crate::Array;
54
use crate::arrays::{BoolArray, BoolEncoding};
6-
use crate::compute::{IsSortedFn, IsSortedIteratorExt};
5+
use crate::compute::{IsSortedIteratorExt, IsSortedKernel, IsSortedKernelAdapter};
6+
use crate::{Array, register_kernel};
77

8-
impl IsSortedFn<&BoolArray> for BoolEncoding {
8+
impl IsSortedKernel for BoolEncoding {
99
fn is_sorted(&self, array: &BoolArray) -> VortexResult<bool> {
1010
match array.validity_mask()? {
1111
Mask::AllFalse(_) => Ok(true),
@@ -42,3 +42,5 @@ impl IsSortedFn<&BoolArray> for BoolEncoding {
4242
}
4343
}
4444
}
45+
46+
register_kernel!(IsSortedKernelAdapter(BoolEncoding).lift());

vortex-array/src/arrays/bool/compute/mod.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::Array;
22
use crate::arrays::BoolEncoding;
3-
use crate::compute::{FillNullFn, IsSortedFn, MinMaxFn, ScalarAtFn, TakeFn, UncompressedSizeFn};
3+
use crate::compute::{FillNullFn, MinMaxFn, ScalarAtFn, TakeFn, UncompressedSizeFn};
44
use crate::vtable::ComputeVTable;
55

66
mod cast;
@@ -22,10 +22,6 @@ impl ComputeVTable for BoolEncoding {
2222
Some(self)
2323
}
2424

25-
fn is_sorted_fn(&self) -> Option<&dyn IsSortedFn<&dyn Array>> {
26-
Some(self)
27-
}
28-
2925
fn min_max_fn(&self) -> Option<&dyn MinMaxFn<&dyn Array>> {
3026
Some(self)
3127
}

vortex-array/src/arrays/chunked/compute/is_sorted.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
use vortex_error::VortexResult;
22

3-
use crate::Array;
43
use crate::arrays::{ChunkedArray, ChunkedEncoding};
5-
use crate::compute::{IsSortedFn, is_sorted, is_strict_sorted, scalar_at};
4+
use crate::compute::{
5+
IsSortedKernel, IsSortedKernelAdapter, is_sorted, is_strict_sorted, scalar_at,
6+
};
7+
use crate::{Array, register_kernel};
68

7-
impl IsSortedFn<&ChunkedArray> for ChunkedEncoding {
9+
impl IsSortedKernel for ChunkedEncoding {
810
fn is_sorted(&self, array: &ChunkedArray) -> VortexResult<bool> {
911
is_sorted_impl(array, false, is_sorted)
1012
}
@@ -14,6 +16,8 @@ impl IsSortedFn<&ChunkedArray> for ChunkedEncoding {
1416
}
1517
}
1618

19+
register_kernel!(IsSortedKernelAdapter(ChunkedEncoding).lift());
20+
1721
fn is_sorted_impl(
1822
array: &ChunkedArray,
1923
strict: bool,

vortex-array/src/arrays/chunked/compute/mod.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::Array;
22
use crate::arrays::ChunkedEncoding;
3-
use crate::compute::{FillNullFn, IsSortedFn, MinMaxFn, ScalarAtFn, TakeFn, UncompressedSizeFn};
3+
use crate::compute::{FillNullFn, MinMaxFn, ScalarAtFn, TakeFn, UncompressedSizeFn};
44
use crate::vtable::ComputeVTable;
55

66
mod cast;
@@ -23,10 +23,6 @@ impl ComputeVTable for ChunkedEncoding {
2323
Some(self)
2424
}
2525

26-
fn is_sorted_fn(&self) -> Option<&dyn IsSortedFn<&dyn Array>> {
27-
Some(self)
28-
}
29-
3026
fn scalar_at_fn(&self) -> Option<&dyn ScalarAtFn<&dyn Array>> {
3127
Some(self)
3228
}

vortex-array/src/arrays/decimal/compute/is_sorted.rs

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1+
use itertools::Itertools;
12
use vortex_error::VortexResult;
23
use vortex_mask::Mask;
34

45
use crate::arrays::{DecimalArray, DecimalEncoding, NativeDecimalType};
5-
use crate::compute::{IsSortedFn, IsSortedIteratorExt};
6-
use crate::{Array, match_each_decimal_value_type};
6+
use crate::compute::{IsSortedIteratorExt, IsSortedKernel, IsSortedKernelAdapter};
7+
use crate::{Array, match_each_decimal_value_type, register_kernel};
78

8-
impl IsSortedFn<&DecimalArray> for DecimalEncoding {
9+
impl IsSortedKernel for DecimalEncoding {
910
fn is_sorted(&self, array: &DecimalArray) -> VortexResult<bool> {
1011
is_decimal_sorted(array, false)
1112
}
@@ -15,6 +16,8 @@ impl IsSortedFn<&DecimalArray> for DecimalEncoding {
1516
}
1617
}
1718

19+
register_kernel!(IsSortedKernelAdapter(DecimalEncoding).lift());
20+
1821
fn is_decimal_sorted(array: &DecimalArray, strict: bool) -> VortexResult<bool> {
1922
match_each_decimal_value_type!(array.values_type, |$S| {
2023
compute_is_sorted::<$S>(array, strict)
@@ -38,12 +41,12 @@ where
3841
})
3942
}
4043
Mask::Values(mask_values) => {
41-
let buf = array.buffer::<T>();
42-
44+
let values = array.buffer::<T>();
4345
let iter = mask_values
4446
.boolean_buffer()
45-
.set_indices()
46-
.map(|idx| buf[idx]);
47+
.iter()
48+
.zip_eq(values)
49+
.map(|(is_valid, v)| is_valid.then_some(v));
4750

4851
Ok(if strict {
4952
IsSortedIteratorExt::is_strict_sorted(iter)
@@ -56,6 +59,8 @@ where
5659

5760
#[cfg(test)]
5861
mod tests {
62+
use arrow_array::types::Decimal128Type;
63+
use arrow_cast::parse::parse_decimal;
5964
use vortex_buffer::buffer;
6065
use vortex_dtype::DecimalDType;
6166

@@ -65,10 +70,14 @@ mod tests {
6570

6671
#[test]
6772
fn test_is_sorted() {
68-
let sorted = buffer![100i128, 200i128, 200i128];
69-
let unsorted = buffer![200i128, 100i128, 200i128];
70-
7173
let dtype = DecimalDType::new(19, 2);
74+
let i100 =
75+
parse_decimal::<Decimal128Type>("100.00", dtype.precision(), dtype.scale()).unwrap();
76+
let i200 =
77+
parse_decimal::<Decimal128Type>("200.00", dtype.precision(), dtype.scale()).unwrap();
78+
79+
let sorted = buffer![i100, i200, i200];
80+
let unsorted = buffer![i200, i100, i200];
7281

7382
let sorted_array = DecimalArray::new(sorted, dtype, Validity::NonNullable);
7483
let unsorted_array = DecimalArray::new(unsorted, dtype, Validity::NonNullable);
@@ -79,8 +88,16 @@ mod tests {
7988

8089
#[test]
8190
fn test_is_strict_sorted() {
82-
let strict_sorted = buffer![100i128, 200i128, 300i128];
83-
let sorted = buffer![100i128, 200i128, 200i128];
91+
let dtype = DecimalDType::new(19, 2);
92+
let i100 =
93+
parse_decimal::<Decimal128Type>("100.00", dtype.precision(), dtype.scale()).unwrap();
94+
let i200 =
95+
parse_decimal::<Decimal128Type>("200.00", dtype.precision(), dtype.scale()).unwrap();
96+
let i300 =
97+
parse_decimal::<Decimal128Type>("300.00", dtype.precision(), dtype.scale()).unwrap();
98+
99+
let strict_sorted = buffer![i100, i200, i300];
100+
let sorted = buffer![i100, i200, i200];
84101

85102
let dtype = DecimalDType::new(19, 2);
86103

vortex-array/src/arrays/decimal/compute/mod.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,10 @@ mod uncompressed_size;
1010

1111
use crate::Array;
1212
use crate::arrays::DecimalEncoding;
13-
use crate::compute::{IsSortedFn, MinMaxFn, ScalarAtFn, TakeFn, UncompressedSizeFn};
13+
use crate::compute::{MinMaxFn, ScalarAtFn, TakeFn, UncompressedSizeFn};
1414
use crate::vtable::ComputeVTable;
1515

1616
impl ComputeVTable for DecimalEncoding {
17-
fn is_sorted_fn(&self) -> Option<&dyn IsSortedFn<&dyn Array>> {
18-
Some(self)
19-
}
20-
2117
fn scalar_at_fn(&self) -> Option<&dyn ScalarAtFn<&dyn Array>> {
2218
Some(self)
2319
}

0 commit comments

Comments
 (0)