Skip to content

Commit

Permalink
Added Utf8Array::apply_validity (jorgecarleitao#1367)
Browse files Browse the repository at this point in the history
  • Loading branch information
Arty-Maly authored and ritchie46 committed Mar 29, 2023
1 parent 3e617a1 commit c69ce87
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
11 changes: 11 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
17 changes: 17 additions & 0 deletions tests/it/array/utf8/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,3 +216,20 @@ fn iter_nth() {
assert_eq!(array.iter().nth(1), Some(Some(" ")));
assert_eq!(array.iter().nth(10), None);
}

#[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 c69ce87

Please sign in to comment.