Skip to content

Commit

Permalink
Fix SearchSorted on sliced sparse array
Browse files Browse the repository at this point in the history
  • Loading branch information
robert3005 committed Jun 25, 2024
1 parent 2378cb6 commit 3caf4cc
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions vortex-array/src/array/sparse/compute/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,16 @@ impl SearchSortedFn for SparseArray {
search_sorted(&self.values(), value.clone(), side).and_then(|sr| match sr {
SearchResult::Found(i) => {
let index: usize = scalar_at(&self.indices(), i)?.as_ref().try_into().unwrap();
Ok(SearchResult::Found(index))
Ok(SearchResult::Found(index - self.indices_offset()))
}
SearchResult::NotFound(i) => {
let index: usize = scalar_at(&self.indices(), if i == 0 { 0 } else { i - 1 })?
.as_ref()
.try_into()
.unwrap();
Ok(SearchResult::NotFound(if i == 0 {
index
} else {
index + 1
}))
Ok(SearchResult::NotFound(
if i == 0 { index } else { index + 1 } - self.indices_offset(),
))
}
})
}
Expand All @@ -71,6 +69,7 @@ mod test {
use crate::array::primitive::PrimitiveArray;
use crate::array::sparse::SparseArray;
use crate::compute::search_sorted::{search_sorted, SearchResult, SearchSortedSide};
use crate::compute::slice::slice;
use crate::validity::Validity;
use crate::{Array, IntoArray};

Expand Down Expand Up @@ -102,4 +101,13 @@ mod test {
let res = search_sorted(&array(), 44, SearchSortedSide::Left).unwrap();
assert_eq!(res, SearchResult::Found(9));
}

#[test]
pub fn search_sliced() {
let array = slice(&array(), 7, 20).unwrap();
assert_eq!(
search_sorted(&array, 22, SearchSortedSide::Left).unwrap(),
SearchResult::NotFound(2)
);
}
}

0 comments on commit 3caf4cc

Please sign in to comment.