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 and set_validity to mutable utf8 array
Browse files Browse the repository at this point in the history
  • Loading branch information
artem.malyshev committed Feb 14, 2023
1 parent b457d68 commit 9628d2e
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/array/utf8/mutable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,27 @@ impl<O: Offset> MutableUtf8Array<O> {
pub fn values_iter(&self) -> MutableUtf8ValuesIter<O> {
self.values.iter()
}

/// Sets the validity.
/// # Panic
/// Panics iff the validity's len is not equal to the existing values' length.
pub fn set_validity(&mut self, validity: Option<MutableBitmap>) {
if let Some(validity) = &validity {
assert_eq!(self.values.len(), validity.len())
}
self.validity = validity;
}

/// 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(MutableBitmap) -> MutableBitmap>(&mut self, f: F) {
if let Some(validity) = std::mem::take(&mut self.validity) {
self.set_validity(Some(f(validity)))
}
}
}

impl<O: Offset> MutableUtf8Array<O> {
Expand Down
42 changes: 42 additions & 0 deletions tests/it/array/utf8/mutable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,3 +199,45 @@ fn extend_from_self() {
MutableUtf8Array::<i32>::from([Some("aa"), None, Some("aa"), None])
);
}

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

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


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

array.apply_validity(|mut mut_bitmap| {
mut_bitmap.set(1, false);
mut_bitmap.set(2, false);
mut_bitmap
});

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

#[test]
fn test_apply_validity_with_no_validity_inited() {
let mut array = MutableUtf8Array::<i32>::from([Some("Red"), Some("Green"), Some("Blue")]);

array.apply_validity(|mut mut_bitmap| {
mut_bitmap.set(1, false);
mut_bitmap.set(2, false);
mut_bitmap
});

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

0 comments on commit 9628d2e

Please sign in to comment.