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

Commit

Permalink
add apply_validity function to utf8array struct
Browse files Browse the repository at this point in the history
  • Loading branch information
artem.malyshev committed Jan 18, 2023
1 parent 70571d6 commit 43fc2c6
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/array/utf8/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,17 @@ impl<O: Offset> Utf8Array<O> {
{
MutableUtf8Array::<O>::try_from_trusted_len_iter(iter).map(|x| x.into())
}

/// Applies a function `f` to the validity of this array.
///
/// This is an API to leverage clone-on-write
/// # Panics
/// This function panics if the function `f` modifies the length of the [`Bitmap`].
pub fn apply_validity<F: FnOnce(Bitmap) -> Bitmap>(&mut self, f: F) {
if let Some(validity) = std::mem::take(&mut self.validity) {
self.set_validity(Some(f(validity)))
}
}
}

impl<O: Offset> Array for Utf8Array<O> {
Expand Down Expand Up @@ -579,3 +590,25 @@ impl<O: Offset> Default for Utf8Array<O> {
Utf8Array::new(data_type, Default::default(), Default::default(), None)
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_apply_validity() {
let mut array = Utf8Array::<i32>::from([Some("Red"), Some("Green"), Some("Blue")]);
array.set_validity(Some([true, true, true].into()));

array.apply_validity(|bitmap| {
let mut mut_bitmap = bitmap.into_mut().right().unwrap();
mut_bitmap.set(1, false);
mut_bitmap.set(2, false);
mut_bitmap.into()
});

assert!(array.is_valid(0));
assert!(!array.is_valid(1));
assert!(!array.is_valid(2));
}
}

0 comments on commit 43fc2c6

Please sign in to comment.