Skip to content

Commit

Permalink
Add iterator for FixedSizeBinaryArray (#2531)
Browse files Browse the repository at this point in the history
  • Loading branch information
tustvold committed Aug 19, 2022
1 parent c20bb8a commit 0f45932
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
6 changes: 2 additions & 4 deletions arrow/src/array/array_binary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,12 +225,10 @@ impl<OffsetSize: OffsetSizeTrait> GenericBinaryArray<OffsetSize> {
) -> impl Iterator<Item = Option<&[u8]>> + 'a {
indexes.map(|opt_index| opt_index.map(|index| self.value_unchecked(index)))
}
}

impl<'a, T: OffsetSizeTrait> GenericBinaryArray<T> {
/// constructs a new iterator
pub fn iter(&'a self) -> GenericBinaryIter<'a, T> {
GenericBinaryIter::<'a, T>::new(self)
pub fn iter(&self) -> GenericBinaryIter<'_, OffsetSize> {
GenericBinaryIter::<'_, OffsetSize>::new(self)
}
}

Expand Down
27 changes: 27 additions & 0 deletions arrow/src/array/array_fixed_size_binary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use std::fmt;
use super::{
array::print_long_array, raw_pointer::RawPtrBox, Array, ArrayData, FixedSizeListArray,
};
use crate::array::{ArrayAccessor, FixedSizeBinaryIter};
use crate::buffer::Buffer;
use crate::error::{ArrowError, Result};
use crate::util::bit_util;
Expand Down Expand Up @@ -259,6 +260,11 @@ impl FixedSizeBinaryArray {
fn value_offset_at(&self, i: usize) -> i32 {
self.length * i as i32
}

/// constructs a new iterator
pub fn iter(&self) -> FixedSizeBinaryIter<'_> {
FixedSizeBinaryIter::new(self)
}
}

impl From<ArrayData> for FixedSizeBinaryArray {
Expand Down Expand Up @@ -362,6 +368,27 @@ impl Array for FixedSizeBinaryArray {
}
}

impl<'a> ArrayAccessor for &'a FixedSizeBinaryArray {
type Item = &'a [u8];

fn value(&self, index: usize) -> Self::Item {
FixedSizeBinaryArray::value(self, index)
}

unsafe fn value_unchecked(&self, index: usize) -> Self::Item {
FixedSizeBinaryArray::value_unchecked(self, index)
}
}

impl<'a> IntoIterator for &'a FixedSizeBinaryArray {
type Item = Option<&'a [u8]>;
type IntoIter = FixedSizeBinaryIter<'a>;

fn into_iter(self) -> Self::IntoIter {
FixedSizeBinaryIter::<'a>::new(self)
}
}

#[cfg(test)]
mod tests {
use std::sync::Arc;
Expand Down
3 changes: 2 additions & 1 deletion arrow/src/array/iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
// under the License.

use crate::array::array::ArrayAccessor;
use crate::array::DecimalArray;
use crate::array::{DecimalArray, FixedSizeBinaryArray};
use crate::datatypes::{Decimal128Type, Decimal256Type};

use super::{
Expand Down Expand Up @@ -103,6 +103,7 @@ pub type PrimitiveIter<'a, T> = ArrayIter<&'a PrimitiveArray<T>>;
pub type BooleanIter<'a> = ArrayIter<&'a BooleanArray>;
pub type GenericStringIter<'a, T> = ArrayIter<&'a GenericStringArray<T>>;
pub type GenericBinaryIter<'a, T> = ArrayIter<&'a GenericBinaryArray<T>>;
pub type FixedSizeBinaryIter<'a> = ArrayIter<&'a FixedSizeBinaryArray>;
pub type GenericListArrayIter<'a, O> = ArrayIter<&'a GenericListArray<O>>;

pub type DecimalIter<'a, T> = ArrayIter<&'a DecimalArray<T>>;
Expand Down

0 comments on commit 0f45932

Please sign in to comment.