From fdcd643fc9fdeefae0e8c0b9beed269ce9909903 Mon Sep 17 00:00:00 2001 From: Raphael Taylor-Davies Date: Tue, 21 Mar 2023 19:16:18 +0000 Subject: [PATCH] Return ScalarBuffer from PrimitiveArray::values (#3879) --- arrow-array/src/array/dictionary_array.rs | 2 +- arrow-array/src/array/primitive_array.rs | 2 +- arrow-buffer/src/buffer/scalar.rs | 33 +++++++++++++++++++++++ 3 files changed, 35 insertions(+), 2 deletions(-) diff --git a/arrow-array/src/array/dictionary_array.rs b/arrow-array/src/array/dictionary_array.rs index ee58a485c71c..2fd562daa771 100644 --- a/arrow-array/src/array/dictionary_array.rs +++ b/arrow-array/src/array/dictionary_array.rs @@ -877,7 +877,7 @@ mod tests { let keys = array.keys(); assert_eq!(&DataType::Int8, keys.data_type()); assert_eq!(0, keys.null_count()); - assert_eq!(&[0, 1, 2, 0], keys.values()); + assert_eq!(keys.values(), &[0, 1, 2, 0]); } #[test] diff --git a/arrow-array/src/array/primitive_array.rs b/arrow-array/src/array/primitive_array.rs index 78859bd5956f..9e1bf5b7db8e 100644 --- a/arrow-array/src/array/primitive_array.rs +++ b/arrow-array/src/array/primitive_array.rs @@ -277,7 +277,7 @@ impl PrimitiveArray { /// Returns a slice of the values of this array #[inline] - pub fn values(&self) -> &[T::Native] { + pub fn values(&self) -> &ScalarBuffer { &self.raw_values } diff --git a/arrow-buffer/src/buffer/scalar.rs b/arrow-buffer/src/buffer/scalar.rs index 9b3a47785098..04c6d9dcc7ac 100644 --- a/arrow-buffer/src/buffer/scalar.rs +++ b/arrow-buffer/src/buffer/scalar.rs @@ -114,6 +114,39 @@ impl From> for ScalarBuffer { } } +impl<'a, T: ArrowNativeType> IntoIterator for &'a ScalarBuffer { + type Item = &'a T; + type IntoIter = std::slice::Iter<'a, T>; + + fn into_iter(self) -> Self::IntoIter { + self.as_ref().iter() + } +} + +impl + ?Sized> PartialEq for ScalarBuffer { + fn eq(&self, other: &S) -> bool { + self.as_ref().eq(other.as_ref()) + } +} + +impl PartialEq> for [T; N] { + fn eq(&self, other: &ScalarBuffer) -> bool { + self.as_ref().eq(other.as_ref()) + } +} + +impl PartialEq> for [T] { + fn eq(&self, other: &ScalarBuffer) -> bool { + self.as_ref().eq(other.as_ref()) + } +} + +impl PartialEq> for Vec { + fn eq(&self, other: &ScalarBuffer) -> bool { + self.as_slice().eq(other.as_ref()) + } +} + #[cfg(test)] mod tests { use super::*;