Skip to content

Commit

Permalink
Return ScalarBuffer from PrimitiveArray::values (#3879) (#3896)
Browse files Browse the repository at this point in the history
* Return ScalarBuffer from PrimitiveArray::values (#3879)

* Fix docs

* Review feedback
  • Loading branch information
tustvold authored Mar 22, 2023
1 parent 2730da1 commit a498a03
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 5 deletions.
4 changes: 2 additions & 2 deletions arrow-array/src/array/primitive_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,9 +275,9 @@ impl<T: ArrowPrimitiveType> PrimitiveArray<T> {
self.data.is_empty()
}

/// Returns a slice of the values of this array
/// Returns the values of this array
#[inline]
pub fn values(&self) -> &[T::Native] {
pub fn values(&self) -> &ScalarBuffer<T::Native> {
&self.raw_values
}

Expand Down
33 changes: 33 additions & 0 deletions arrow-buffer/src/buffer/scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,39 @@ impl<T: ArrowNativeType> From<Vec<T>> for ScalarBuffer<T> {
}
}

impl<'a, T: ArrowNativeType> IntoIterator for &'a ScalarBuffer<T> {
type Item = &'a T;
type IntoIter = std::slice::Iter<'a, T>;

fn into_iter(self) -> Self::IntoIter {
self.as_ref().iter()
}
}

impl<T: ArrowNativeType, S: AsRef<[T]> + ?Sized> PartialEq<S> for ScalarBuffer<T> {
fn eq(&self, other: &S) -> bool {
self.as_ref().eq(other.as_ref())
}
}

impl<T: ArrowNativeType, const N: usize> PartialEq<ScalarBuffer<T>> for [T; N] {
fn eq(&self, other: &ScalarBuffer<T>) -> bool {
self.as_ref().eq(other.as_ref())
}
}

impl<T: ArrowNativeType> PartialEq<ScalarBuffer<T>> for [T] {
fn eq(&self, other: &ScalarBuffer<T>) -> bool {
self.as_ref().eq(other.as_ref())
}
}

impl<T: ArrowNativeType> PartialEq<ScalarBuffer<T>> for Vec<T> {
fn eq(&self, other: &ScalarBuffer<T>) -> bool {
self.as_slice().eq(other.as_ref())
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
6 changes: 3 additions & 3 deletions arrow/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
//!
//! let collected: Vec<_> = array.iter().collect();
//! assert_eq!(collected, vec![Some(1), None, Some(3)]);
//! assert_eq!(array.values(), [1, 0, 3])
//! assert_eq!(array.values(), &[1, 0, 3])
//! ```
//!
//! It is also possible to write generic code. For example, the following is generic over
Expand Down Expand Up @@ -168,7 +168,7 @@
//!
//! let array = parse_strings(["1", "2", "3"], DataType::Int32);
//! let integers = array.as_any().downcast_ref::<Int32Array>().unwrap();
//! assert_eq!(integers.values(), [1, 2, 3])
//! assert_eq!(integers.values(), &[1, 2, 3])
//! ```
//!
//! # Compute Kernels
Expand All @@ -192,7 +192,7 @@
//!
//! let array = parse_strings(["1", "2", "3"], &DataType::UInt32).unwrap();
//! let integers = array.as_any().downcast_ref::<UInt32Array>().unwrap();
//! assert_eq!(integers.values(), [1, 2, 3])
//! assert_eq!(integers.values(), &[1, 2, 3])
//! ```
//!
//! This module also implements many common vertical operations:
Expand Down

0 comments on commit a498a03

Please sign in to comment.