use std::hash::BuildHasher;
use num_traits::ToPrimitive;
use num_traits::WrappingSub;
use rustc_hash::FxBuildHasher;
use vortex_array::ArrayRef;
use vortex_array::ExecutionCtx;
use vortex_array::IntoArray;
use vortex_array::RecursiveCanonical;
use vortex_array::arrays::Chunked;
use vortex_array::arrays::DecimalArray;
use vortex_array::arrays::Primitive;
use vortex_array::arrays::PrimitiveArray;
use vortex_array::arrays::Slice;
use vortex_array::arrays::VarBinViewArray;
use vortex_array::arrays::chunked::ChunkedArrayExt;
use vortex_array::arrays::primitive::PrimitiveArrayExt;
use vortex_array::arrays::slice::SliceArraySlotsExt;
use vortex_array::arrays::varbinview::BinaryView;
use vortex_array::dtype::DType;
use vortex_array::dtype::NativeDecimalType;
use vortex_array::dtype::NativePType;
use vortex_array::dtype::PType;
use vortex_array::dtype::i256;
use vortex_array::match_each_decimal_value_type;
use vortex_array::match_each_integer_ptype;
use vortex_array::optimizer::ArrayOptimizer;
use vortex_array::scalar_fn::builtin::compare::build_row_comparator;
use vortex_array::validity::Validity;
use vortex_buffer::BitBuffer;
use vortex_buffer::BitBufferMut;
use vortex_buffer::Buffer;
use vortex_buffer::BufferAllocatorRef;
use vortex_buffer::BufferMut;
use vortex_compute::lane_kernels::collect_bits;
use vortex_error::VortexExpect;
use vortex_error::VortexResult;
use vortex_utils::aliases::hash_map::HashTable;
use vortex_utils::aliases::hash_map::HashTableEntry;
const BITMAP_BITS_PER_ELEMENT: u128 = 64;
const L1_BITMAP_MIN_BITS: u128 = 1 << 12;
const BITMAP_MIN_BITS: u128 = 1 << 20;
const L2_BITMAP_MIN_ELEMENTS: usize = 16;
const NEEDLE_BATCH_LEN: usize = 8192;
pub(crate) struct ArrayHaystack {
state: Box<dyn HaystackState>,
}
impl ArrayHaystack {
pub(crate) fn try_new(array: &ArrayRef, ctx: &mut ExecutionCtx) -> VortexResult<Self> {
let valid = array.validity()?.execute_mask(array.len(), ctx)?;
let members = if valid.all_true() {
array.clone()
} else {
array.filter(valid)?
};
Ok(Self {
state: new_state(members, ctx)?,
})
}
pub(crate) fn contains(
&self,
needles: &ArrayRef,
ctx: &mut ExecutionCtx,
) -> VortexResult<(BitBuffer, Validity)> {
self.state.contains(needles, ctx)
}
}
trait HaystackState: Send + Sync {
fn contains(
&self,
needles: &ArrayRef,
ctx: &mut ExecutionCtx,
) -> VortexResult<(BitBuffer, Validity)>;
}
fn new_state(elements: ArrayRef, ctx: &mut ExecutionCtx) -> VortexResult<Box<dyn HaystackState>> {
Ok(match elements.dtype() {
DType::Primitive(ptype, _) => {
let ptype = bit_pattern_ptype(*ptype);
let elements = elements
.execute::<PrimitiveArray>(ctx)?
.reinterpret_cast(ptype);
match_each_integer_ptype!(ptype, |T| {
let set = IntegerSet::<T>::new(elements.into_buffer(), ctx.allocator());
Box::new(PrimitiveSet(set)) as Box<dyn HaystackState>
})
}
DType::Decimal(..) => {
let elements = elements.execute::<DecimalArray>(ctx)?;
match_each_decimal_value_type!(elements.values_type(), |T| {
let set = IntegerSet::new(elements.buffer::<T>(), ctx.allocator());
Box::new(DecimalSet(set)) as Box<dyn HaystackState>
})
}
DType::Utf8(_) | DType::Binary(_) => {
Box::new(BytesSet::new(elements.execute::<VarBinViewArray>(ctx)?))
}
_ => Box::new(RowSet::try_new(elements, ctx)?),
})
}
enum IntegerSet<T> {
Bitmap { min: T, bitmap: BitBuffer },
Sorted(Buffer<T>),
}
impl<T: SetInteger> IntegerSet<T> {
fn new(values: Buffer<T>, allocator: &BufferAllocatorRef) -> Self {
if values.len() <= 4 {
return Self::Sorted(sorted_values(values));
}
let (Some(&min), Some(&max)) = (values.iter().min(), values.iter().max()) else {
return Self::Sorted(values);
};
let Some(span) = max.offset_from(min) else {
return Self::Sorted(sorted_values(values));
};
if span == usize::MAX {
return Self::Sorted(sorted_values(values));
}
let span_u128 = span as u128;
if span_u128 < L1_BITMAP_MIN_BITS
|| (span_u128 < BITMAP_MIN_BITS
&& has_at_least_distinct(&values, L2_BITMAP_MIN_ELEMENTS))
{
return Self::Bitmap {
min,
bitmap: build_integer_bitmap(min, span, &values, allocator),
};
}
if span_u128 >= values.len() as u128 * BITMAP_BITS_PER_ELEMENT {
return Self::Sorted(sorted_values(values));
}
let distinct = sorted_values(values);
if span_u128 >= distinct.len() as u128 * BITMAP_BITS_PER_ELEMENT {
return Self::Sorted(distinct);
}
Self::Bitmap {
min,
bitmap: build_integer_bitmap(min, span, &distinct, allocator),
}
}
fn contains(&self, needles: &[T], allocator: &BufferAllocatorRef) -> BitBuffer {
match self {
Self::Bitmap { min, bitmap } => {
let bytes = bitmap.inner().as_slice();
let (bit_offset, len) = (bitmap.offset(), bitmap.len());
collect_bits(
needles,
|needle| {
let offset = needle.offset_from(*min).unwrap_or(usize::MAX);
let in_span = offset < len;
let bit = if in_span { offset } else { 0 } + bit_offset;
let byte = unsafe { *bytes.get_unchecked(bit / 8) };
in_span & ((byte >> (bit % 8)) & 1 != 0)
},
allocator,
)
}
Self::Sorted(sorted) => match sorted.as_slice() {
[] => BitBuffer::full_in(false, needles.len(), allocator.clone()),
&[m0] => collect_bits(needles, |needle| needle == m0, allocator),
&[m0, m1] => {
collect_bits(needles, |needle| (needle == m0) | (needle == m1), allocator)
}
&[m0, m1, m2] => collect_bits(
needles,
|needle| (needle == m0) | (needle == m1) | (needle == m2),
allocator,
),
&[m0, m1, m2, m3] => collect_bits(
needles,
|needle| (needle == m0) | (needle == m1) | (needle == m2) | (needle == m3),
allocator,
),
sorted => collect_bits(
needles,
|needle| sorted.binary_search(&needle).is_ok(),
allocator,
),
},
}
}
}
struct PrimitiveSet<T>(IntegerSet<T>);
impl<T: NativePType + SetInteger> HaystackState for PrimitiveSet<T> {
fn contains(
&self,
needles: &ArrayRef,
ctx: &mut ExecutionCtx,
) -> VortexResult<(BitBuffer, Validity)> {
if needles.len() > NEEDLE_BATCH_LEN && !needles.is::<Primitive>() {
let first = needles.slice(0..NEEDLE_BATCH_LEN)?.optimize_ctx(ctx)?;
if slices_without_full_decode(&first, ctx)? {
let len = needles.len();
let mut bits = BitBufferMut::with_capacity_in(len, ctx.allocator().clone());
let batch = first
.execute::<PrimitiveArray>(ctx)?
.reinterpret_cast(T::PTYPE);
bits.append_buffer(&self.0.contains(batch.as_slice::<T>(), ctx.allocator()));
for start in (NEEDLE_BATCH_LEN..len).step_by(NEEDLE_BATCH_LEN) {
let end = (start + NEEDLE_BATCH_LEN).min(len);
let batch = needles
.slice(start..end)?
.execute::<PrimitiveArray>(ctx)?
.reinterpret_cast(T::PTYPE);
bits.append_buffer(&self.0.contains(batch.as_slice::<T>(), ctx.allocator()));
}
return Ok((bits.freeze(), needles.validity()?));
}
}
let needles = needles
.clone()
.execute::<PrimitiveArray>(ctx)?
.reinterpret_cast(T::PTYPE);
let bits = self.0.contains(needles.as_slice::<T>(), ctx.allocator());
Ok((bits, needles.validity()?))
}
}
fn slices_without_full_decode(array: &ArrayRef, ctx: &mut ExecutionCtx) -> VortexResult<bool> {
if let Some(slice) = array.as_opt::<Slice>() {
let child = slice.child();
if let Some(chunked) = child.as_opt::<Chunked>() {
for chunk in chunked.iter_chunks() {
let sliced = chunk
.slice(0..chunk.len().saturating_sub(1))?
.optimize_ctx(ctx)?;
if !slices_without_full_decode(&sliced, ctx)? {
return Ok(false);
}
}
return Ok(true);
}
let Some(rewritten) = ctx.try_execute_parent(array)? else {
return Ok(false);
};
let rewritten = rewritten.optimize_ctx(ctx)?;
return slices_without_full_decode(&rewritten, ctx);
}
for child in array.children().iter() {
if !slices_without_full_decode(child, ctx)? {
return Ok(false);
}
}
Ok(true)
}
struct DecimalSet<T>(IntegerSet<T>);
impl<T: NativeDecimalType + SetInteger> HaystackState for DecimalSet<T> {
fn contains(
&self,
needles: &ArrayRef,
ctx: &mut ExecutionCtx,
) -> VortexResult<(BitBuffer, Validity)> {
let needles = needles.clone().execute::<DecimalArray>(ctx)?;
let bits = self.0.contains(&needles.buffer::<T>(), ctx.allocator());
Ok((bits, needles.validity()?))
}
}
struct BytesSet {
hasher: FxBuildHasher,
short: HashTable<u128>,
elements: VarBinViewArray,
long: HashTable<u32>,
}
impl BytesSet {
fn new(elements: VarBinViewArray) -> Self {
let hasher = FxBuildHasher;
let views = elements.views();
let buffers = data_buffers(&elements);
let short_len = views.iter().filter(|view| view.is_inlined()).count();
let mut short = HashTable::with_capacity(short_len);
let mut long = HashTable::with_capacity(views.len() - short_len);
for (idx, view) in views.iter().enumerate() {
if view.is_inlined() {
let whole = view.as_u128();
if let HashTableEntry::Vacant(vacant) = short.entry(
hasher.hash_one(whole),
|&other| other == whole,
|&other| hasher.hash_one(other),
) {
vacant.insert(whole);
}
continue;
}
let value = view.bytes(&buffers);
let bytes = |other: u32| views[other as usize].bytes(&buffers);
if let HashTableEntry::Vacant(vacant) = long.entry(
hasher.hash_one(value),
|&other| bytes(other) == value,
|&other| hasher.hash_one(bytes(other)),
) {
vacant.insert(
u32::try_from(idx).vortex_expect("a list holds fewer than 2^32 elements"),
);
}
}
Self {
hasher,
short,
elements,
long,
}
}
#[inline]
fn contains_view(
&self,
view: &BinaryView,
buffers: &[&[u8]],
element_views: &[BinaryView],
element_buffers: &[&[u8]],
) -> bool {
if view.is_inlined() {
let whole = view.as_u128();
return self
.short
.find(self.hasher.hash_one(whole), |&other| other == whole)
.is_some();
}
let head = view_head(view);
let value = view.bytes(buffers);
self.long
.find(self.hasher.hash_one(value), |&idx| {
let element = &element_views[idx as usize];
view_head(element) == head && element.bytes(element_buffers)[4..] == value[4..]
})
.is_some()
}
}
impl HaystackState for BytesSet {
fn contains(
&self,
needles: &ArrayRef,
ctx: &mut ExecutionCtx,
) -> VortexResult<(BitBuffer, Validity)> {
let element_views = self.elements.views();
let element_buffers = data_buffers(&self.elements);
let array = needles.clone().execute::<VarBinViewArray>(ctx)?;
let buffers = data_buffers(&array);
let bits = collect_bits(
array.views(),
|view: BinaryView| self.contains_view(&view, &buffers, element_views, &element_buffers),
ctx.allocator(),
);
Ok((bits, array.validity()?))
}
}
#[inline]
#[expect(
clippy::cast_possible_truncation,
reason = "the head is the low 8 bytes"
)]
fn view_head(view: &BinaryView) -> u64 {
view.as_u128() as u64
}
struct RowSet {
elements: ArrayRef,
indices: Vec<usize>,
}
impl RowSet {
fn try_new(elements: ArrayRef, ctx: &mut ExecutionCtx) -> VortexResult<Self> {
let elements = elements.execute::<RecursiveCanonical>(ctx)?.0.into_array();
let mut indices: Vec<usize> = (0..elements.len()).collect();
if !indices.is_empty() {
let compare = build_row_comparator(&elements, &elements, ctx)?;
if !indices.is_sorted_by(|&lhs, &rhs| compare(lhs, rhs).is_le()) {
indices.sort_unstable_by(|&lhs, &rhs| compare(lhs, rhs));
}
indices.dedup_by(|lhs, rhs| compare(*lhs, *rhs).is_eq());
}
Ok(Self { elements, indices })
}
}
impl HaystackState for RowSet {
fn contains(
&self,
needles: &ArrayRef,
ctx: &mut ExecutionCtx,
) -> VortexResult<(BitBuffer, Validity)> {
if self.indices.is_empty() {
return Ok((
BitBuffer::full_in(false, needles.len(), ctx.allocator().clone()),
needles.validity()?,
));
}
let needles = needles
.clone()
.execute::<RecursiveCanonical>(ctx)?
.0
.into_array();
let compare = build_row_comparator(&self.elements, &needles, ctx)?;
let bits = BitBuffer::collect_bool_in(
needles.len(),
|row| {
self.indices
.binary_search_by(|&element| compare(element, row))
.is_ok()
},
ctx.allocator().clone(),
);
Ok((bits, needles.validity()?))
}
}
fn data_buffers(array: &VarBinViewArray) -> Vec<&[u8]> {
(0..array.data_buffers().len())
.map(|idx| array.buffer(idx).as_slice())
.collect()
}
fn bit_pattern_ptype(ptype: PType) -> PType {
match ptype {
PType::F16 => PType::U16,
PType::F32 => PType::U32,
PType::F64 => PType::U64,
_ => ptype,
}
}
trait SetInteger: Copy + Ord + Send + Sync + 'static {
fn offset_from(self, min: Self) -> Option<usize>;
}
macro_rules! impl_set_integer {
($($signed:ty => $unsigned:ty),* $(,)?) => {
$(impl SetInteger for $signed {
fn offset_from(self, min: Self) -> Option<usize> {
usize::try_from((self as $unsigned).wrapping_sub(min as $unsigned)).ok()
}
})*
};
}
impl_set_integer!(
u8 => u8, u16 => u16, u32 => u32, u64 => u64,
i8 => u8, i16 => u16, i32 => u32, i64 => u64, i128 => u128,
);
impl SetInteger for i256 {
fn offset_from(self, min: Self) -> Option<usize> {
self.wrapping_sub(&min).to_usize()
}
}
fn has_at_least_distinct<T: Copy + PartialEq>(values: &[T], min_distinct: usize) -> bool {
debug_assert!(min_distinct <= L2_BITMAP_MIN_ELEMENTS);
if values.len() < min_distinct {
return false;
}
let Some(&first) = values.first() else {
return min_distinct == 0;
};
let mut seen = [first; L2_BITMAP_MIN_ELEMENTS];
let mut count = 1;
for &value in &values[1..] {
if !seen[..count].contains(&value) {
if count + 1 >= min_distinct {
return true;
}
seen[count] = value;
count += 1;
}
}
count >= min_distinct
}
fn build_integer_bitmap<T: SetInteger>(
min: T,
span: usize,
values: &[T],
allocator: &BufferAllocatorRef,
) -> BitBuffer {
let mut bitmap = BitBufferMut::from_buffer(
BufferMut::zeroed_in((span + 1).div_ceil(8), allocator.clone()),
0,
span + 1,
);
for &value in values {
bitmap.set(
value
.offset_from(min)
.vortex_expect("value within bitmap span"),
);
}
bitmap.freeze()
}
fn sorted_values<T: Copy + Ord + Send + Sync + 'static>(values: Buffer<T>) -> Buffer<T> {
if values.is_sorted_by(|a, b| a < b) {
return values;
}
let mut sorted = values.to_vec();
sorted.sort_unstable();
sorted.dedup();
Buffer::from(sorted)
}
#[cfg(test)]
mod tests {
use std::fmt;
use std::hash::Hash;
use std::hash::Hasher;
use std::ops::Range;
use std::sync::Arc;
use std::sync::atomic::AtomicUsize;
use std::sync::atomic::Ordering;
use vortex_array::Array;
use vortex_array::ArrayEq;
use vortex_array::ArrayHash;
use vortex_array::ArrayId;
use vortex_array::ArrayParts;
use vortex_array::ArraySlots;
use vortex_array::ArrayView;
use vortex_array::EqMode;
use vortex_array::ExecutionResult;
use vortex_array::OperationsVTable;
use vortex_array::VTable;
use vortex_array::ValidityVTable;
use vortex_array::VortexSessionExecute;
use vortex_array::arrays::ChunkedArray;
use vortex_array::arrays::WidenArray;
use vortex_array::arrays::slice::SliceExecuteAdaptor;
use vortex_array::arrays::slice::SliceKernel;
use vortex_array::buffer::BufferHandle;
use vortex_array::dtype::Nullability;
use vortex_array::optimizer::kernels::ArrayKernelsExt;
use vortex_array::scalar::Scalar;
use vortex_array::session::ArraySession;
use vortex_buffer::Buffer;
use vortex_error::VortexResult;
use vortex_error::vortex_panic;
use vortex_session::VortexSession;
use vortex_session::registry::CachedId;
use super::*;
#[derive(Clone, Debug)]
struct CountingEncoding;
#[derive(Clone, Debug)]
struct CountingData {
values: PrimitiveArray,
decodes: Arc<AtomicUsize>,
}
impl fmt::Display for CountingData {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("CountingData")
}
}
impl ArrayHash for CountingData {
fn array_hash<H: Hasher>(&self, state: &mut H, _eq_mode: EqMode) {
self.values.len().hash(state);
}
}
impl ArrayEq for CountingData {
fn array_eq(&self, other: &Self, _eq_mode: EqMode) -> bool {
Arc::ptr_eq(&self.decodes, &other.decodes)
}
}
impl VTable for CountingEncoding {
type TypedArrayData = CountingData;
type OperationsVTable = Self;
type ValidityVTable = Self;
fn id(&self) -> ArrayId {
static ID: CachedId = CachedId::new("vortex.test.counting_haystack_needles");
*ID
}
fn validate(
&self,
_data: &Self::TypedArrayData,
_dtype: &DType,
_len: usize,
_slots: &[Option<ArrayRef>],
) -> VortexResult<()> {
Ok(())
}
fn nbuffers(_array: ArrayView<'_, Self>) -> usize {
0
}
fn buffer(_array: ArrayView<'_, Self>, _idx: usize) -> BufferHandle {
vortex_panic!("CountingEncoding has no buffers")
}
fn buffer_name(_array: ArrayView<'_, Self>, _idx: usize) -> Option<String> {
None
}
fn with_buffers(
&self,
_array: ArrayView<'_, Self>,
_buffers: &[BufferHandle],
) -> VortexResult<ArrayParts<Self>> {
vortex_panic!("CountingEncoding has no buffers")
}
fn slot_name(_array: ArrayView<'_, Self>, _idx: usize) -> String {
vortex_panic!("CountingEncoding has no slots")
}
fn execute(array: Array<Self>, _ctx: &mut ExecutionCtx) -> VortexResult<ExecutionResult> {
array.decodes.fetch_add(1, Ordering::Relaxed);
Ok(ExecutionResult::done(array.values.clone().into_array()))
}
}
impl OperationsVTable<CountingEncoding> for CountingEncoding {
type ProbeState = ();
fn scalar_at(
array: ArrayView<'_, CountingEncoding>,
index: usize,
ctx: &mut ExecutionCtx,
) -> VortexResult<Scalar> {
array.values.execute_scalar(index, ctx)
}
}
impl ValidityVTable<CountingEncoding> for CountingEncoding {
fn validity(array: ArrayView<'_, CountingEncoding>) -> VortexResult<Validity> {
array.values.validity()
}
}
impl SliceKernel for CountingEncoding {
fn slice(
_array: ArrayView<'_, Self>,
_range: Range<usize>,
_ctx: &mut ExecutionCtx,
) -> VortexResult<Option<ArrayRef>> {
Ok(None)
}
}
fn counting_array(
values: PrimitiveArray,
decodes: &Arc<AtomicUsize>,
) -> VortexResult<ArrayRef> {
Ok(Array::try_from_parts(ArrayParts::new(
CountingEncoding,
values.dtype().clone(),
values.len(),
CountingData {
values,
decodes: Arc::clone(decodes),
},
ArraySlots::new(),
))?
.into_array())
}
#[test]
fn small_wide_span_uses_sorted_large_uses_bitmap() {
let session = VortexSession::empty().with::<ArraySession>();
let ctx = session.create_execution_ctx();
let small_wide = IntegerSet::new(Buffer::from(vec![0i64, 1, 2, 1 << 19]), ctx.allocator());
assert!(matches!(small_wide, IntegerSet::Sorted(_)));
let small_narrow =
IntegerSet::new(Buffer::from(vec![0i64, 1, 2, 3, 1000]), ctx.allocator());
assert!(matches!(small_narrow, IntegerSet::Bitmap { .. }));
let tiny_narrow = IntegerSet::new(Buffer::from(vec![0i64, 1, 2, 1000]), ctx.allocator());
assert!(matches!(tiny_narrow, IntegerSet::Sorted(_)));
let large_wide: Vec<i64> = (0..256).map(|i| i * 4000).collect();
let large_wide = IntegerSet::new(Buffer::from(large_wide), ctx.allocator());
assert!(matches!(large_wide, IntegerSet::Bitmap { .. }));
let mut dup_wide = vec![0i64; L2_BITMAP_MIN_ELEMENTS];
dup_wide.push(1 << 19);
let dup_wide = IntegerSet::new(Buffer::from(dup_wide), ctx.allocator());
assert!(matches!(dup_wide, IntegerSet::Sorted(_)));
}
#[test]
fn non_sliceable_needles_decode_once_instead_of_per_batch() -> VortexResult<()> {
let len = NEEDLE_BATCH_LEN * 3;
let narrow = PrimitiveArray::from_iter(0..i32::try_from(len).vortex_expect("fits in i32"));
let widened = WidenArray::try_new(
narrow.clone().into_array(),
DType::Primitive(PType::I64, Nullability::NonNullable),
)?
.into_array();
let chunked = ChunkedArray::try_new(
vec![narrow.clone().into_array(), narrow.clone().into_array()],
DType::Primitive(PType::I32, Nullability::NonNullable),
)?
.into_array();
let session = VortexSession::empty().with::<ArraySession>();
let mut ctx = session.create_execution_ctx();
let widened_slice = widened.slice(0..NEEDLE_BATCH_LEN)?.optimize_ctx(&ctx)?;
assert!(slices_without_full_decode(&widened_slice, &mut ctx)?);
let chunked_slice = chunked.slice(0..NEEDLE_BATCH_LEN)?.optimize_ctx(&ctx)?;
assert!(slices_without_full_decode(&chunked_slice, &mut ctx)?);
let decodes = Arc::new(AtomicUsize::new(0));
let unsliced_needles = counting_array(narrow, &decodes)?;
let elements = PrimitiveArray::from_iter([5i32, 9_000, 20_000]).into_array();
let haystack = ArrayHaystack::try_new(&elements, &mut ctx)?;
let (bits, _) = haystack.contains(&unsliced_needles, &mut ctx)?;
assert_eq!(decodes.load(Ordering::Relaxed), 1);
assert!(bits.value(5));
assert!(bits.value(9_000));
assert!(bits.value(20_000));
assert!(!bits.value(0));
assert_eq!(bits.true_count(), 3);
Ok(())
}
#[test]
fn declining_slice_kernel_decodes_once_instead_of_per_batch() -> VortexResult<()> {
let len = NEEDLE_BATCH_LEN * 3;
let values = PrimitiveArray::from_iter(0..i32::try_from(len).vortex_expect("fits in i32"));
let decodes = Arc::new(AtomicUsize::new(0));
let needles = counting_array(values, &decodes)?;
let session = VortexSession::empty().with::<ArraySession>();
session.kernels().register_execute_parent_kernel(
Slice.id(),
CountingEncoding,
SliceExecuteAdaptor(CountingEncoding),
);
let mut ctx = session.create_execution_ctx();
let first = needles.slice(0..NEEDLE_BATCH_LEN)?.optimize_ctx(&ctx)?;
assert!(!slices_without_full_decode(&first, &mut ctx)?);
let elements = PrimitiveArray::from_iter([5i32, 9_000, 20_000]).into_array();
let haystack = ArrayHaystack::try_new(&elements, &mut ctx)?;
let (bits, _) = haystack.contains(&needles, &mut ctx)?;
assert_eq!(decodes.load(Ordering::Relaxed), 1);
assert_eq!(bits.true_count(), 3);
Ok(())
}
#[test]
fn chunks_without_slice_kernel_decode_once_instead_of_per_batch() -> VortexResult<()> {
let len = NEEDLE_BATCH_LEN * 3;
let values = PrimitiveArray::from_iter(0..i32::try_from(len).vortex_expect("fits in i32"));
let decodes = Arc::new(AtomicUsize::new(0));
let sliced_chunk = counting_array(values.clone(), &decodes)?.slice(1..len)?;
let chunk = counting_array(values, &decodes)?;
let needles = ChunkedArray::try_new(
vec![sliced_chunk, chunk],
DType::Primitive(PType::I32, Nullability::NonNullable),
)?
.into_array();
let session = VortexSession::empty().with::<ArraySession>();
let mut ctx = session.create_execution_ctx();
let first = needles.slice(0..NEEDLE_BATCH_LEN)?.optimize_ctx(&ctx)?;
assert!(!slices_without_full_decode(&first, &mut ctx)?);
let elements = PrimitiveArray::from_iter([5i32, 9_000, 20_000]).into_array();
let haystack = ArrayHaystack::try_new(&elements, &mut ctx)?;
let (bits, _) = haystack.contains(&needles, &mut ctx)?;
assert_eq!(decodes.load(Ordering::Relaxed), 2);
assert_eq!(bits.true_count(), 6);
Ok(())
}
}