-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
117 additions
and
478 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
use crate::array::primitive::PrimitiveArray; | ||
use crate::compute::search_sorted::{SearchSortedFn, SearchSortedSide}; | ||
use crate::error::VortexResult; | ||
use crate::match_each_native_ptype; | ||
use crate::ptype::NativePType; | ||
use crate::scalar::Scalar; | ||
|
||
impl SearchSortedFn for PrimitiveArray { | ||
fn search_sorted(&self, value: &dyn Scalar, side: SearchSortedSide) -> VortexResult<usize> { | ||
match_each_native_ptype!(self.ptype(), |$T| { | ||
let pvalue: $T = value.try_into()?; | ||
Ok(search_sorted(self.typed_data::<$T>(), pvalue, side)) | ||
}) | ||
} | ||
} | ||
|
||
fn search_sorted<T: NativePType>(arr: &[T], target: T, side: SearchSortedSide) -> usize { | ||
match side { | ||
SearchSortedSide::Left => search_sorted_cmp(arr, target, |a, b| a < b), | ||
SearchSortedSide::Right => search_sorted_cmp(arr, target, |a, b| a <= b), | ||
} | ||
} | ||
|
||
fn search_sorted_cmp<T: NativePType, Cmp>(arr: &[T], target: T, cmp: Cmp) -> usize | ||
where | ||
Cmp: Fn(T, T) -> bool + 'static, | ||
{ | ||
let mut low = 0; | ||
let mut high = arr.len(); | ||
|
||
while low < high { | ||
let mid = low + (high - low) / 2; | ||
|
||
if cmp(arr[mid], target) { | ||
low = mid + 1; | ||
} else { | ||
high = mid; | ||
} | ||
} | ||
|
||
low | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_searchsorted_primitive() { | ||
let values = vec![1u16, 2, 3]; | ||
|
||
assert_eq!(search_sorted(&values, 0, SearchSortedSide::Left), 0); | ||
assert_eq!(search_sorted(&values, 1, SearchSortedSide::Left), 0); | ||
assert_eq!(search_sorted(&values, 1, SearchSortedSide::Right), 1); | ||
assert_eq!(search_sorted(&values, 4, SearchSortedSide::Left), 3); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,29 @@ | ||
use crate::array::Array; | ||
use crate::error::VortexResult; | ||
use crate::polars::IntoPolarsSeries; | ||
use crate::polars::IntoPolarsValue; | ||
use crate::scalar::ScalarRef; | ||
use polars_core::prelude::*; | ||
use polars_ops::prelude::*; | ||
use crate::error::{VortexError, VortexResult}; | ||
use crate::scalar::{Scalar, ScalarRef}; | ||
|
||
pub enum SearchSortedSide { | ||
Left, | ||
Right, | ||
} | ||
|
||
impl From<SearchSortedSide> for polars_ops::prelude::SearchSortedSide { | ||
fn from(side: SearchSortedSide) -> Self { | ||
match side { | ||
SearchSortedSide::Left => polars_ops::prelude::SearchSortedSide::Left, | ||
SearchSortedSide::Right => polars_ops::prelude::SearchSortedSide::Right, | ||
} | ||
} | ||
pub trait SearchSortedFn { | ||
fn search_sorted(&self, value: &dyn Scalar, side: SearchSortedSide) -> VortexResult<usize>; | ||
} | ||
|
||
pub fn search_sorted_usize( | ||
indices: &dyn Array, | ||
index: usize, | ||
pub fn search_sorted<T: Into<ScalarRef>>( | ||
array: &dyn Array, | ||
target: T, | ||
side: SearchSortedSide, | ||
) -> VortexResult<usize> { | ||
let enc_scalar: ScalarRef = index.into(); | ||
// Convert index into correctly typed Arrow scalar. | ||
let enc_scalar = enc_scalar.cast(indices.dtype())?; | ||
|
||
let series: Series = indices.iter_arrow().into_polars(); | ||
Ok(search_sorted( | ||
&series, | ||
&Series::from_any_values("needle", &[enc_scalar.into_polars()], true)?, | ||
side.into(), | ||
false, | ||
)? | ||
.get(0) | ||
.unwrap() as usize) | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use super::*; | ||
use crate::array::ArrayRef; | ||
|
||
#[test] | ||
fn test_searchsorted_scalar() { | ||
let haystack: ArrayRef = vec![1, 2, 3].into(); | ||
|
||
assert_eq!( | ||
search_sorted_usize(haystack.as_ref(), 0, SearchSortedSide::Left).unwrap(), | ||
0 | ||
); | ||
assert_eq!( | ||
search_sorted_usize(haystack.as_ref(), 1, SearchSortedSide::Left).unwrap(), | ||
0 | ||
); | ||
assert_eq!( | ||
search_sorted_usize(haystack.as_ref(), 1, SearchSortedSide::Right).unwrap(), | ||
1 | ||
); | ||
assert_eq!( | ||
search_sorted_usize(haystack.as_ref(), 4, SearchSortedSide::Left).unwrap(), | ||
3 | ||
); | ||
} | ||
let scalar = target.into().cast(array.dtype())?; | ||
array | ||
.search_sorted() | ||
.map(|f| f.search_sorted(scalar.as_ref(), side)) | ||
.unwrap_or_else(|| { | ||
Err(VortexError::NotImplemented( | ||
"search_sorted", | ||
array.encoding().id(), | ||
)) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.