Skip to content

Commit

Permalink
comments
Browse files Browse the repository at this point in the history
  • Loading branch information
robert3005 committed Sep 4, 2024
1 parent a14bc3e commit 95255a0
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 17 deletions.
4 changes: 3 additions & 1 deletion encodings/fastlanes/src/bitpacking/compute/search_sorted.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ where
{
let unwrapped_value: T = value.cast(array.dtype())?.try_into()?;
if let Some(patches_array) = array.patches() {
if unwrapped_value.as_() >= array.max_packed_value() {
// If patches exist they must be the last elements in the array, if the value we're looking for is greater than
// max packed value just search the patches
if unwrapped_value.as_() > array.max_packed_value() {
search_sorted(&patches_array, value.clone(), side)
} else {
Ok(SearchSorted::search_sorted(
Expand Down
4 changes: 2 additions & 2 deletions encodings/fastlanes/src/bitpacking/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use ::serde::{Deserialize, Serialize};
pub use compress::*;
use ::serde::{Deserialize, Serialize};
use vortex::array::{Primitive, PrimitiveArray};
use vortex::stats::{ArrayStatisticsCompute, StatsSet};
use vortex::validity::{ArrayValidity, LogicalValidity, Validity, ValidityMetadata};
Expand Down Expand Up @@ -164,7 +164,7 @@ impl BitPackedArray {

#[inline]
pub fn max_packed_value(&self) -> usize {
1 << self.bit_width()
1 << self.bit_width() - 1
}
}

Expand Down
5 changes: 2 additions & 3 deletions encodings/fastlanes/src/for/compress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,11 @@ fn compress_primitive<T: NativePType + WrappingSub + PrimInt>(
) -> PrimitiveArray {
assert!(shift < T::PTYPE.bit_width() as u8);
let values = if shift > 0 {
let shifted_min = min >> shift as usize;
parray
.maybe_null_slice::<T>()
.iter()
.map(|&v| v >> shift as usize)
.map(|v| v.wrapping_sub(&shifted_min))
.map(|&v| v.wrapping_sub(&min))
.map(|v| v >> shift as usize)
.collect_vec()
} else {
parray
Expand Down
10 changes: 4 additions & 6 deletions encodings/fastlanes/src/for/compute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,9 @@ where
+ Into<PValue>,
{
let min: T = array.reference().try_into()?;
let shifted_min = min >> array.shift();
let unwrapped_value: T = value.cast(array.dtype())?.as_ref().try_into()?;
let shifted_value: T = unwrapped_value >> array.shift();
let primitive_value: T = value.cast(array.dtype())?.as_ref().try_into()?;
// Make sure that smaller values are still smaller and not larger than (which they would be after wrapping_sub)
if shifted_value < shifted_min {
if primitive_value < min {
return Ok(SearchResult::NotFound(0));
}

Expand All @@ -110,9 +108,9 @@ where
//
// For values that are lossy compressed we know they wouldn't be found in the array
// in order to find index they would be inserted at we search for next value in the compressed space
let mut translated_value = shifted_value.wrapping_sub(&shifted_min);
let mut translated_value = primitive_value.wrapping_sub(&min) >> array.shift();
let mut lossy_compressed = false;
if (translated_value << array.shift()).wrapping_add(&min) != unwrapped_value {
if (translated_value << array.shift()).wrapping_add(&min) != primitive_value {
translated_value += T::from(1).unwrap();
lossy_compressed = true;
}
Expand Down
10 changes: 5 additions & 5 deletions vortex-array/src/array/primitive/compute/search_sorted.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,19 @@ impl SearchSortedFn for PrimitiveArray {
Validity::AllInvalid => Ok(SearchResult::NotFound(0)),
Validity::Array(_) => {
let pvalue: $T = value.try_into()?;
Ok(NullableSearchSorted::new(self).search_sorted(&pvalue, side))
Ok(SearchSortedNullsLast::new(self).search_sorted(&pvalue, side))
}
}
})
}
}

struct NullableSearchSorted<'a, T> {
struct SearchSortedNullsLast<'a, T> {
values: &'a [T],
validity: Validity,
}

impl<'a, T: NativePType> NullableSearchSorted<'a, T> {
impl<'a, T: NativePType> SearchSortedNullsLast<'a, T> {
pub fn new(array: &'a PrimitiveArray) -> Self {
Self {
values: array.maybe_null_slice(),
Expand All @@ -41,7 +41,7 @@ impl<'a, T: NativePType> NullableSearchSorted<'a, T> {
}
}

impl<'a, T: NativePType> IndexOrd<T> for NullableSearchSorted<'a, T> {
impl<'a, T: NativePType> IndexOrd<T> for SearchSortedNullsLast<'a, T> {
fn index_cmp(&self, idx: usize, elem: &T) -> Option<Ordering> {
if self.validity.is_null(idx) {
return Some(Greater);
Expand All @@ -51,7 +51,7 @@ impl<'a, T: NativePType> IndexOrd<T> for NullableSearchSorted<'a, T> {
}
}

impl<'a, T> Len for NullableSearchSorted<'a, T> {
impl<'a, T> Len for SearchSortedNullsLast<'a, T> {
fn len(&self) -> usize {
self.values.len()
}
Expand Down
3 changes: 3 additions & 0 deletions vortex-array/src/compute/search_sorted.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ impl SearchResult {
}
}

/// Searches for value assuming the array is sorted.
///
/// For nullable arrays we assume that the nulls are sorted last, i.e. they're the greatest value
pub trait SearchSortedFn {
fn search_sorted(&self, value: &Scalar, side: SearchSortedSide) -> VortexResult<SearchResult>;
}
Expand Down

0 comments on commit 95255a0

Please sign in to comment.