use crate::{DataFusionError, Result, ScalarValue};
use arrow::array::ArrayRef;
use arrow::compute::SortOptions;
use std::cmp::Ordering;
fn compare(
x: &[ScalarValue],
y: &[ScalarValue],
sort_options: &[SortOptions],
) -> Result<Ordering> {
let zip_it = x.iter().zip(y.iter()).zip(sort_options.iter());
for ((lhs, rhs), sort_options) in zip_it {
let result = match (lhs.is_null(), rhs.is_null(), sort_options.nulls_first) {
(true, false, false) | (false, true, true) => Ordering::Greater,
(true, false, true) | (false, true, false) => Ordering::Less,
(false, false, _) => if sort_options.descending {
rhs.partial_cmp(lhs)
} else {
lhs.partial_cmp(rhs)
}
.ok_or_else(|| {
DataFusionError::Internal("Column array shouldn't be empty".to_string())
})?,
(true, true, _) => continue,
};
if result != Ordering::Equal {
return Ok(result);
}
}
Ok(Ordering::Equal)
}
pub fn bisect<const SIDE: bool>(
item_columns: &[ArrayRef],
target: &[ScalarValue],
sort_options: &[SortOptions],
) -> Result<usize> {
let mut low: usize = 0;
let mut high: usize = item_columns
.get(0)
.ok_or_else(|| {
DataFusionError::Internal("Column array shouldn't be empty".to_string())
})?
.len();
while low < high {
let mid = ((high - low) / 2) + low;
let val = item_columns
.iter()
.map(|arr| ScalarValue::try_from_array(arr, mid))
.collect::<Result<Vec<ScalarValue>>>()?;
let cmp = compare(&val, target, sort_options)?;
let flag = if SIDE { cmp.is_lt() } else { cmp.is_le() };
if flag {
low = mid + 1;
} else {
high = mid;
}
}
Ok(low)
}
#[cfg(test)]
mod tests {
use arrow::array::Float64Array;
use std::sync::Arc;
use crate::from_slice::FromSlice;
use crate::ScalarValue;
use crate::ScalarValue::Null;
use super::*;
#[test]
fn test_bisect_left_and_right() {
let arrays: Vec<ArrayRef> = vec![
Arc::new(Float64Array::from_slice(&[5.0, 7.0, 8.0, 9., 10.])),
Arc::new(Float64Array::from_slice(&[2.0, 3.0, 3.0, 4.0, 5.0])),
Arc::new(Float64Array::from_slice(&[5.0, 7.0, 8.0, 10., 11.0])),
Arc::new(Float64Array::from_slice(&[15.0, 13.0, 8.0, 5., 0.0])),
];
let search_tuple: Vec<ScalarValue> = vec![
ScalarValue::Float64(Some(8.0)),
ScalarValue::Float64(Some(3.0)),
ScalarValue::Float64(Some(8.0)),
ScalarValue::Float64(Some(8.0)),
];
let ords = [
SortOptions {
descending: false,
nulls_first: true,
},
SortOptions {
descending: false,
nulls_first: true,
},
SortOptions {
descending: false,
nulls_first: true,
},
SortOptions {
descending: true,
nulls_first: true,
},
];
let res: usize = bisect::<true>(&arrays, &search_tuple, &ords).unwrap();
assert_eq!(res, 2);
let res: usize = bisect::<false>(&arrays, &search_tuple, &ords).unwrap();
assert_eq!(res, 3);
}
#[test]
fn vector_ord() {
assert!(vec![1, 0, 0, 0, 0, 0, 0, 1] < vec![1, 0, 0, 0, 0, 0, 0, 2]);
assert!(vec![1, 0, 0, 0, 0, 0, 1, 1] > vec![1, 0, 0, 0, 0, 0, 0, 2]);
assert!(
vec![
ScalarValue::Int32(Some(2)),
Null,
ScalarValue::Int32(Some(0)),
] < vec![
ScalarValue::Int32(Some(2)),
Null,
ScalarValue::Int32(Some(1)),
]
);
assert!(
vec![
ScalarValue::Int32(Some(2)),
ScalarValue::Int32(None),
ScalarValue::Int32(Some(0)),
] < vec![
ScalarValue::Int32(Some(2)),
ScalarValue::Int32(None),
ScalarValue::Int32(Some(1)),
]
);
}
#[test]
fn ord_same_type() {
assert!((ScalarValue::Int32(Some(2)) < ScalarValue::Int32(Some(3))));
}
#[test]
fn test_bisect_left_and_right_diff_sort() {
let arrays: Vec<ArrayRef> = vec![Arc::new(Float64Array::from_slice(&[
4.0, 3.0, 2.0, 1.0, 0.0,
]))];
let search_tuple: Vec<ScalarValue> = vec![ScalarValue::Float64(Some(4.0))];
let ords = [SortOptions {
descending: true,
nulls_first: true,
}];
let res: usize = bisect::<true>(&arrays, &search_tuple, &ords).unwrap();
assert_eq!(res, 0);
let arrays: Vec<ArrayRef> = vec![Arc::new(Float64Array::from_slice(&[
4.0, 3.0, 2.0, 1.0, 0.0,
]))];
let search_tuple: Vec<ScalarValue> = vec![ScalarValue::Float64(Some(4.0))];
let ords = [SortOptions {
descending: true,
nulls_first: true,
}];
let res: usize = bisect::<false>(&arrays, &search_tuple, &ords).unwrap();
assert_eq!(res, 1);
let arrays: Vec<ArrayRef> = vec![Arc::new(Float64Array::from_slice(&[
5.0, 7.0, 8.0, 9., 10.,
]))];
let search_tuple: Vec<ScalarValue> = vec![ScalarValue::Float64(Some(7.0))];
let ords = [SortOptions {
descending: false,
nulls_first: true,
}];
let res: usize = bisect::<true>(&arrays, &search_tuple, &ords).unwrap();
assert_eq!(res, 1);
let arrays: Vec<ArrayRef> = vec![Arc::new(Float64Array::from_slice(&[
5.0, 7.0, 8.0, 9., 10.,
]))];
let search_tuple: Vec<ScalarValue> = vec![ScalarValue::Float64(Some(7.0))];
let ords = [SortOptions {
descending: false,
nulls_first: true,
}];
let res: usize = bisect::<false>(&arrays, &search_tuple, &ords).unwrap();
assert_eq!(res, 2);
let arrays: Vec<ArrayRef> = vec![
Arc::new(Float64Array::from_slice(&[5.0, 7.0, 8.0, 8.0, 9., 10.])),
Arc::new(Float64Array::from_slice(&[10.0, 9.0, 8.0, 7.5, 7., 6.])),
];
let search_tuple: Vec<ScalarValue> = vec![
ScalarValue::Float64(Some(8.0)),
ScalarValue::Float64(Some(8.0)),
];
let ords = [
SortOptions {
descending: false,
nulls_first: true,
},
SortOptions {
descending: true,
nulls_first: true,
},
];
let res: usize = bisect::<false>(&arrays, &search_tuple, &ords).unwrap();
assert_eq!(res, 3);
let res: usize = bisect::<true>(&arrays, &search_tuple, &ords).unwrap();
assert_eq!(res, 2);
}
}