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

Added Utf8Array::apply_validity #1367

Merged
merged 1 commit into from
Jan 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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));
}