Skip to content

Commit a5e1ad0

Browse files
committed
add stubs for take on vector + impl FSL, null, and struct
Signed-off-by: Connor Tsui <[email protected]>
1 parent a8ad74f commit a5e1ad0

File tree

12 files changed

+781
-59
lines changed

12 files changed

+781
-59
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vortex-compute/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ arrow-array = { workspace = true, optional = true }
3030
arrow-buffer = { workspace = true, optional = true }
3131
arrow-schema = { workspace = true, optional = true }
3232
half = { workspace = true }
33+
itertools = { workspace = true }
3334
log = { workspace = true }
3435
multiversion = { workspace = true }
3536
num-traits = { workspace = true }

vortex-compute/src/take/bit_buffer.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,29 @@ fn take_bool<I: UnsignedPType>(bools: &BitBuffer, indices: &[I]) -> BitBuffer {
5858
get_bit(buffer, offset + bool_idx)
5959
})
6060
}
61+
62+
#[cfg(test)]
63+
mod tests {
64+
use crate::take::Take;
65+
66+
#[test]
67+
fn test_bit_buffer_take_small_and_large() {
68+
use vortex_buffer::BitBuffer;
69+
70+
// Small buffer (uses take_byte_bool path).
71+
let small: BitBuffer = [true, false, true, true, false, true, false, false]
72+
.into_iter()
73+
.collect();
74+
let result = (&small).take(&[7u32, 0, 2, 5, 1][..]);
75+
76+
let values: Vec<bool> = (0..result.len()).map(|i| result.value(i)).collect();
77+
assert_eq!(values, vec![false, true, true, true, false]);
78+
79+
// Large buffer (uses take_bool path, len > 4096).
80+
let large: BitBuffer = (0..5000).map(|i| i % 3 == 0).collect();
81+
let result = (&large).take(&[4999u32, 0, 1, 2, 3, 4998][..]);
82+
83+
let values: Vec<bool> = (0..result.len()).map(|i| result.value(i)).collect();
84+
assert_eq!(values, vec![false, true, false, false, true, true]);
85+
}
86+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
// SPDX-FileCopyrightText: Copyright the Vortex contributors
3+
4+
// use std::ops::Deref;
5+
6+
// use num_traits::AsPrimitive;
7+
// use vortex_buffer::Buffer;
8+
use vortex_dtype::UnsignedPType;
9+
use vortex_vector::VectorOps;
10+
// use vortex_vector::binaryview::BinaryView;
11+
use vortex_vector::binaryview::BinaryViewType;
12+
use vortex_vector::binaryview::BinaryViewVector;
13+
use vortex_vector::primitive::PVector;
14+
15+
use crate::take::Take;
16+
17+
impl<T: BinaryViewType, I: UnsignedPType> Take<PVector<I>> for &BinaryViewVector<T> {
18+
type Output = BinaryViewVector<T>;
19+
20+
fn take(self, indices: &PVector<I>) -> BinaryViewVector<T> {
21+
if indices.validity().all_true() {
22+
self.take(indices.elements().as_slice())
23+
} else {
24+
take_nullable(self, indices)
25+
}
26+
}
27+
}
28+
29+
impl<T: BinaryViewType, I: UnsignedPType> Take<[I]> for &BinaryViewVector<T> {
30+
type Output = BinaryViewVector<T>;
31+
32+
fn take(self, _indices: &[I]) -> BinaryViewVector<T> {
33+
todo!("TODO(connor): Implement `take` for `BinaryViewVector` and figure out rebuilding");
34+
35+
/*
36+
37+
let taken_views = take_views(self.views(), indices);
38+
let taken_validity = self.validity().take(indices);
39+
40+
debug_assert_eq!(taken_views.len(), taken_validity.len());
41+
42+
// SAFETY: We called take on views and validity with the same indices, so the new components
43+
// must have the same length. The views still point into the same buffers which we clone via
44+
// Arc, so all view references remain valid.
45+
unsafe {
46+
BinaryViewVector::new_unchecked(taken_views, self.buffers().clone(), taken_validity)
47+
}
48+
49+
*/
50+
}
51+
}
52+
53+
fn take_nullable<T: BinaryViewType, I: UnsignedPType>(
54+
_bvector: &BinaryViewVector<T>,
55+
_indices: &PVector<I>,
56+
) -> BinaryViewVector<T> {
57+
todo!("TODO(connor): Implement `take` for `BinaryViewVector` and figure out rebuilding");
58+
59+
/*
60+
61+
// We ignore nullability when taking the views since we can let the `Mask` implementation
62+
// determine which elements are null.
63+
let taken_views = take_views(bvector.views(), indices.elements().as_slice());
64+
let taken_validity = bvector.validity().take(indices);
65+
66+
debug_assert_eq!(taken_views.len(), taken_validity.len());
67+
68+
// SAFETY: We used the same indices to take from both components, so they should still have the
69+
// same length. The views still point into the same buffers which we clone via Arc, so all view
70+
// references remain valid.
71+
unsafe {
72+
BinaryViewVector::new_unchecked(taken_views, bvector.buffers().clone(), taken_validity)
73+
}
74+
75+
*/
76+
}
77+
78+
/*
79+
80+
/// Takes views at the given indices.
81+
fn take_views<I: AsPrimitive<usize>>(
82+
views: &Buffer<BinaryView>,
83+
indices: &[I],
84+
) -> Buffer<BinaryView> {
85+
let views_ref = views.deref();
86+
Buffer::<BinaryView>::from_trusted_len_iter(indices.iter().map(|i| views_ref[(*i).as_()]))
87+
}
88+
89+
*/
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
// SPDX-FileCopyrightText: Copyright the Vortex contributors
3+
4+
use vortex_dtype::UnsignedPType;
5+
use vortex_vector::VectorOps;
6+
use vortex_vector::decimal::DecimalVector;
7+
use vortex_vector::match_each_dvector;
8+
use vortex_vector::primitive::PVector;
9+
10+
use crate::take::Take;
11+
12+
impl<I: UnsignedPType> Take<PVector<I>> for &DecimalVector {
13+
type Output = DecimalVector;
14+
15+
fn take(self, indices: &PVector<I>) -> DecimalVector {
16+
// If all the indices are valid, we can delegate to the slice indices implementation.
17+
if indices.validity().all_true() {
18+
return self.take(indices.elements().as_slice());
19+
}
20+
21+
match_each_dvector!(self, |v| { v.take(indices).into() })
22+
}
23+
}
24+
25+
impl<I: UnsignedPType> Take<[I]> for &DecimalVector {
26+
type Output = DecimalVector;
27+
28+
fn take(self, indices: &[I]) -> DecimalVector {
29+
match_each_dvector!(self, |v| { v.take(indices).into() })
30+
}
31+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
// SPDX-FileCopyrightText: Copyright the Vortex contributors
3+
4+
use vortex_dtype::NativeDecimalType;
5+
use vortex_dtype::UnsignedPType;
6+
use vortex_vector::VectorOps;
7+
use vortex_vector::decimal::DVector;
8+
use vortex_vector::primitive::PVector;
9+
10+
use crate::take::Take;
11+
12+
impl<D: NativeDecimalType, I: UnsignedPType> Take<PVector<I>> for &DVector<D> {
13+
type Output = DVector<D>;
14+
15+
fn take(self, indices: &PVector<I>) -> DVector<D> {
16+
if indices.validity().all_true() {
17+
self.take(indices.elements().as_slice())
18+
} else {
19+
take_nullable(self, indices)
20+
}
21+
}
22+
}
23+
24+
impl<D: NativeDecimalType, I: UnsignedPType> Take<[I]> for &DVector<D> {
25+
type Output = DVector<D>;
26+
27+
fn take(self, _indices: &[I]) -> DVector<D> {
28+
todo!("TODO(connor): Implement `take` for `DVector` and figure out trait bounds");
29+
30+
/*
31+
32+
let taken_elements = self.elements().take(indices);
33+
let taken_validity = self.validity().take(indices);
34+
35+
debug_assert_eq!(taken_elements.len(), taken_validity.len());
36+
37+
// SAFETY: We called take on both components of the vector with the same indices, so the new
38+
// components must have the same length. The elements are unchanged, so they must still be
39+
// within the precision/scale bounds.
40+
unsafe { DVector::new_unchecked(self.precision_scale(), taken_elements, taken_validity) }
41+
42+
*/
43+
}
44+
}
45+
46+
fn take_nullable<D: NativeDecimalType, I: UnsignedPType>(
47+
_dvector: &DVector<D>,
48+
_indices: &PVector<I>,
49+
) -> DVector<D> {
50+
todo!("TODO(connor): Implement `take` for `DVector` and figure out trait bounds");
51+
52+
/*
53+
54+
// We ignore nullability when taking the elements since we can let the `Mask` implementation
55+
// determine which elements are null.
56+
let taken_elements = dvector.elements().take(indices.elements().as_slice());
57+
let taken_validity = dvector.validity().take(indices);
58+
59+
debug_assert_eq!(taken_elements.len(), taken_validity.len());
60+
61+
// SAFETY: We used the same indices to take from both components, so they should still have the
62+
// same length. The elements are unchanged, so they must still be within the precision/scale
63+
// bounds.
64+
unsafe { DVector::new_unchecked(dvector.precision_scale(), taken_elements, taken_validity) }
65+
66+
*/
67+
}

0 commit comments

Comments
 (0)