|
| 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::bool::BoolVector; |
| 7 | +use vortex_vector::primitive::PVector; |
| 8 | + |
| 9 | +use crate::take::Take; |
| 10 | + |
| 11 | +impl<I: UnsignedPType> Take<PVector<I>> for &BoolVector { |
| 12 | + type Output = BoolVector; |
| 13 | + |
| 14 | + fn take(self, indices: &PVector<I>) -> BoolVector { |
| 15 | + if indices.validity().all_true() { |
| 16 | + self.take(indices.elements().as_slice()) |
| 17 | + } else { |
| 18 | + take_nullable(self, indices) |
| 19 | + } |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +impl<I: UnsignedPType> Take<[I]> for &BoolVector { |
| 24 | + type Output = BoolVector; |
| 25 | + |
| 26 | + fn take(self, indices: &[I]) -> BoolVector { |
| 27 | + let taken_bits = self.bits().take(indices); |
| 28 | + let taken_validity = self.validity().take(indices); |
| 29 | + |
| 30 | + debug_assert_eq!(taken_bits.len(), taken_validity.len()); |
| 31 | + |
| 32 | + // SAFETY: We called take on both components of the vector with the same indices, so the new |
| 33 | + // components must have the same length. |
| 34 | + unsafe { BoolVector::new_unchecked(taken_bits, taken_validity) } |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +fn take_nullable<I: UnsignedPType>(bvector: &BoolVector, indices: &PVector<I>) -> BoolVector { |
| 39 | + // We ignore nullability when taking the bits since we can let the `Mask` implementation |
| 40 | + // determine which elements are null. |
| 41 | + let taken_bits = bvector.bits().take(indices.elements().as_slice()); |
| 42 | + let taken_validity = bvector.validity().take(indices); |
| 43 | + |
| 44 | + debug_assert_eq!(taken_bits.len(), taken_validity.len()); |
| 45 | + |
| 46 | + // SAFETY: We used the same indices to take from both components, so they should still have the |
| 47 | + // same length. |
| 48 | + unsafe { BoolVector::new_unchecked(taken_bits, taken_validity) } |
| 49 | +} |
0 commit comments