|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +// SPDX-FileCopyrightText: Copyright the Vortex contributors |
| 3 | + |
| 4 | +use vortex_buffer::Buffer; |
| 5 | +use vortex_dtype::IntegerPType; |
| 6 | +use vortex_dtype::NativePType; |
| 7 | +use vortex_dtype::match_each_integer_ptype; |
| 8 | +use vortex_dtype::match_each_native_ptype; |
| 9 | +use vortex_error::VortexResult; |
| 10 | + |
| 11 | +use crate::ArrayRef; |
| 12 | +use crate::IntoArray; |
| 13 | +use crate::arrays::PrimitiveArray; |
| 14 | +use crate::arrays::primitive::compute::take::TakeImpl; |
| 15 | +use crate::validity::Validity; |
| 16 | +use crate::vtable::ValidityHelper; |
| 17 | + |
| 18 | +#[allow(unused)] |
| 19 | +pub(super) struct TakeKernelScalar; |
| 20 | + |
| 21 | +impl TakeImpl for TakeKernelScalar { |
| 22 | + #[allow(clippy::cognitive_complexity)] |
| 23 | + fn take( |
| 24 | + &self, |
| 25 | + array: &PrimitiveArray, |
| 26 | + indices: &PrimitiveArray, |
| 27 | + validity: Validity, |
| 28 | + ) -> VortexResult<ArrayRef> { |
| 29 | + match_each_native_ptype!(array.ptype(), |T| { |
| 30 | + match_each_integer_ptype!(indices.ptype(), |I| { |
| 31 | + let indices_slice = indices.as_slice::<I>(); |
| 32 | + let indices_validity = indices.validity(); |
| 33 | + let values = if indices_validity.all_valid(indices_slice.len()) { |
| 34 | + // Fast path: indices have no nulls, safe to index directly |
| 35 | + take_primitive_scalar(array.as_slice::<T>(), indices_slice) |
| 36 | + } else { |
| 37 | + // Slow path: indices may have nulls with garbage values |
| 38 | + take_primitive_scalar_with_nulls( |
| 39 | + array.as_slice::<T>(), |
| 40 | + indices_slice, |
| 41 | + indices_validity, |
| 42 | + ) |
| 43 | + }; |
| 44 | + Ok(PrimitiveArray::new(values, validity).into_array()) |
| 45 | + }) |
| 46 | + }) |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +// Compiler may see this as unused based on enabled features |
| 51 | +#[allow(unused)] |
| 52 | +#[inline(always)] |
| 53 | +fn take_primitive_scalar<T: NativePType, I: IntegerPType>(array: &[T], indices: &[I]) -> Buffer<T> { |
| 54 | + indices.iter().map(|idx| array[idx.as_()]).collect() |
| 55 | +} |
| 56 | + |
| 57 | +/// Slow path for take when indices may contain nulls with garbage values. |
| 58 | +/// Uses 0 as a safe index for null positions (the value will be masked out by validity). |
| 59 | +#[allow(unused)] |
| 60 | +#[inline(always)] |
| 61 | +fn take_primitive_scalar_with_nulls<T: NativePType, I: IntegerPType>( |
| 62 | + array: &[T], |
| 63 | + indices: &[I], |
| 64 | + validity: &Validity, |
| 65 | +) -> Buffer<T> { |
| 66 | + indices |
| 67 | + .iter() |
| 68 | + .enumerate() |
| 69 | + .map(|(i, idx)| { |
| 70 | + if validity.is_valid(i) { |
| 71 | + array[idx.as_()] |
| 72 | + } else { |
| 73 | + T::zero() |
| 74 | + } |
| 75 | + }) |
| 76 | + .collect() |
| 77 | +} |
| 78 | + |
| 79 | +#[cfg(test)] |
| 80 | +mod tests { |
| 81 | + use vortex_buffer::buffer; |
| 82 | + |
| 83 | + use crate::IntoArray; |
| 84 | + use crate::ToCanonical; |
| 85 | + use crate::arrays::PrimitiveArray; |
| 86 | + use crate::arrays::primitive::compute::take::TakeImpl; |
| 87 | + use crate::arrays::primitive::compute::take::scalar::TakeKernelScalar; |
| 88 | + use crate::validity::Validity; |
| 89 | + |
| 90 | + #[test] |
| 91 | + fn test_scalar_basic() { |
| 92 | + let values = buffer![1, 2, 3, 4, 5].into_array().to_primitive(); |
| 93 | + let indices = buffer![0, 1, 1, 2, 2, 3, 4].into_array().to_primitive(); |
| 94 | + |
| 95 | + let result = TakeKernelScalar |
| 96 | + .take(&values, &indices, Validity::NonNullable) |
| 97 | + .unwrap() |
| 98 | + .to_primitive(); |
| 99 | + assert_eq!(result.as_slice::<i32>(), &[1, 2, 2, 3, 3, 4, 5]); |
| 100 | + } |
| 101 | + |
| 102 | + #[test] |
| 103 | + fn test_scalar_with_nulls() { |
| 104 | + let values = buffer![1, 2, 3, 4, 5].into_array().to_primitive(); |
| 105 | + let validity = Validity::from_iter([true, false, true, true, true]); |
| 106 | + let indices = PrimitiveArray::new(buffer![0, 100, 2, 3, 4], validity.clone()); |
| 107 | + |
| 108 | + let result = TakeKernelScalar |
| 109 | + .take(&values, &indices, validity.clone()) |
| 110 | + .unwrap() |
| 111 | + .to_primitive(); |
| 112 | + |
| 113 | + assert_eq!(result.as_slice::<i32>(), &[1, 0, 3, 4, 5]); |
| 114 | + assert_eq!(result.validity, validity); |
| 115 | + } |
| 116 | +} |
0 commit comments