Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use new search-sorted for finding chunk index #342

Merged
merged 1 commit into from
Jun 12, 2024
Merged
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
19 changes: 7 additions & 12 deletions vortex-array/src/array/chunked/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::array::primitive::PrimitiveArray;
use crate::compute::as_contiguous::as_contiguous;
use crate::compute::scalar_at::scalar_at;
use crate::compute::scalar_subtract::{subtract_scalar, SubtractScalarFn};
use crate::compute::search_sorted::{search_sorted, SearchSortedSide};
use crate::compute::search_sorted::{search_sorted, SearchResult, SearchSortedSide};
use crate::iter::{ArrayIterator, ArrayIteratorAdapter};
use crate::stream::{ArrayStream, ArrayStreamAdapter};
use crate::validity::Validity::NonNullable;
Expand Down Expand Up @@ -73,19 +73,14 @@ impl ChunkedArray {
pub fn find_chunk_idx(&self, index: usize) -> (usize, usize) {
assert!(index <= self.len(), "Index out of bounds of the array");

// TODO(ngates): migrate to the new search_sorted API to subtract 1 if not exact match.
let mut index_chunk = search_sorted(&self.chunk_ends(), index, SearchSortedSide::Left)
.unwrap()
.to_index();
let mut chunk_start =
let index_chunk =
match search_sorted(&self.chunk_ends(), index, SearchSortedSide::Left).unwrap() {
SearchResult::Found(i) => i,
SearchResult::NotFound(i) => i - 1,
};
let chunk_start =
usize::try_from(&scalar_at(&self.chunk_ends(), index_chunk).unwrap()).unwrap();

if chunk_start != index {
index_chunk -= 1;
chunk_start =
usize::try_from(&scalar_at(&self.chunk_ends(), index_chunk).unwrap()).unwrap();
}

let index_in_chunk = index - chunk_start;
(index_chunk, index_in_chunk)
}
Expand Down