Skip to content
This repository has been archived by the owner on Feb 18, 2024. It is now read-only.

Commit

Permalink
Added extend_*values to MutablePrimitiveArray (#383)
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 authored Sep 6, 2021
1 parent bd7ad8d commit 4e4ea28
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 1 deletion.
43 changes: 43 additions & 0 deletions src/array/primitive/mutable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,49 @@ impl<T: NativeType> MutablePrimitiveArray<T> {
}
}
}
/// Extends the [`MutablePrimitiveArray`] from an iterator of values of trusted len.
/// This differs from `extend_trusted_len` which accepts in iterator of optional values.
#[inline]
pub fn extend_trusted_len_values<I>(&mut self, iterator: I)
where
I: TrustedLen<Item = T>,
{
unsafe { self.extend_trusted_len_values_unchecked(iterator) }
}

/// Extends the [`MutablePrimitiveArray`] from an iterator of values of trusted len.
/// This differs from `extend_trusted_len_unchecked` which accepts in iterator of optional values.
/// # Safety
/// The iterator must be trusted len.
#[inline]
pub unsafe fn extend_trusted_len_values_unchecked<I>(&mut self, iterator: I)
where
I: Iterator<Item = T>,
{
self.values.extend_from_trusted_len_iter_unchecked(iterator);
self.update_all_valid();
}

#[inline]
/// Extends the [`MutablePrimitiveArray`] from a slice
pub fn extend_from_slice(&mut self, items: &[T]) {
self.values.extend_from_slice(items);
self.update_all_valid();
}

fn update_all_valid(&mut self) {
// get len before mutable borrow
let len = self.len();
if let Some(validity) = self.validity.as_mut() {
validity.extend_constant(len - validity.len(), true);
} else {
let mut validity = MutableBitmap::new();
validity.extend_constant(len, true);
if validity.null_count() > 0 {
self.validity = Some(validity);
}
}
}

fn init_validity(&mut self) {
let mut validity = MutableBitmap::new();
Expand Down
5 changes: 4 additions & 1 deletion src/compute/like.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,10 @@ fn a_like_binary_scalar<O: Offset, F: Fn(bool) -> bool>(
) -> Result<BooleanArray> {
let validity = lhs.validity();
let pattern = std::str::from_utf8(rhs).map_err(|e| {
ArrowError::InvalidArgumentError(format!("Unable to convert the LIKE pattern to string: {}", e))
ArrowError::InvalidArgumentError(format!(
"Unable to convert the LIKE pattern to string: {}",
e
))
})?;

let values = if !pattern.contains(is_like_pattern) {
Expand Down
32 changes: 32 additions & 0 deletions tests/it/array/primitive/mutable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,38 @@ fn extend_trusted_len() {
assert_eq!(a.values(), &MutableBuffer::<i32>::from([1, 2, 0, 4]));
}

#[test]
fn extend_trusted_len_values() {
let mut a = MutablePrimitiveArray::<i32>::new();
a.extend_trusted_len_values(vec![1, 2, 3].into_iter());
assert_eq!(a.validity(), &None);
assert_eq!(a.values(), &MutableBuffer::<i32>::from([1, 2, 3]));

let mut a = MutablePrimitiveArray::<i32>::new();
a.push(None);
a.extend_trusted_len_values(vec![1, 2].into_iter());
assert_eq!(
a.validity(),
&Some(MutableBitmap::from([false, true, true]))
);
}

#[test]
fn extend_from_slice() {
let mut a = MutablePrimitiveArray::<i32>::new();
a.extend_from_slice(&[1, 2, 3]);
assert_eq!(a.validity(), &None);
assert_eq!(a.values(), &MutableBuffer::<i32>::from([1, 2, 3]));

let mut a = MutablePrimitiveArray::<i32>::new();
a.push(None);
a.extend_from_slice(&[1, 2]);
assert_eq!(
a.validity(),
&Some(MutableBitmap::from([false, true, true]))
);
}

#[test]
fn set_validity() {
let mut a = MutablePrimitiveArray::<i32>::new();
Expand Down

0 comments on commit 4e4ea28

Please sign in to comment.