Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add BooleanArray::new_from_packed and BooleanArray::new_from_u8 #6127

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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
42 changes: 42 additions & 0 deletions arrow-array/src/array/boolean_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,22 @@ use std::sync::Arc;
/// assert_eq!(&values, &[Some(true), None, Some(false), None, Some(false)])
/// ```
///
/// # Example: From `&[u8]`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you.

///
/// ```
/// # use arrow_array::{Array, BooleanArray};
/// let v: Vec<u8> = vec![1];
/// let slice = &v[..];
/// let arr = BooleanArray::from(slice);
/// assert_eq!(8, arr.len());
/// assert!(!arr.is_null(0));
/// assert!(arr.value(0));
/// ```
/// convert each bit in `&[u8]` to boolean and use it to build [`BooleanArray`].
/// using this method will make the following two points self-evident:
/// * there is no `null` in the constructed [`BooleanArray`];
/// * the length of the constructed [`BooleanArray`] is always a multiple of 8;
///
/// # Example: Using Builder
///
/// ```
Expand Down Expand Up @@ -349,6 +365,12 @@ impl From<Vec<Option<bool>>> for BooleanArray {
}
}

impl From<&[u8]> for BooleanArray {
alamb marked this conversation as resolved.
Show resolved Hide resolved
fn from(value: &[u8]) -> Self {
Self::from(BooleanBuffer::from(value))
}
}

impl From<ArrayData> for BooleanArray {
fn from(data: ArrayData) -> Self {
assert_eq!(
Expand Down Expand Up @@ -509,6 +531,26 @@ mod tests {
}
}

#[test]
fn test_boolean_array_from_slice_u8() {
let v: Vec<u8> = vec![1, 2, 3];
let slice = &v[..];
let arr = BooleanArray::from(slice);
assert_eq!(24, arr.len());
assert_eq!(0, arr.offset());
assert_eq!(0, arr.null_count());
assert!(arr.nulls().is_none());
for i in 0..24 {
assert!(!arr.is_null(i));
assert!(arr.is_valid(i));
assert_eq!(
i == 0 || i == 9 || i == 16 || i == 17,
arr.value(i),
"failed t {i}"
)
}
}

#[test]
fn test_boolean_array_from_iter() {
let v = vec![Some(false), Some(true), Some(false), Some(true)];
Expand Down
30 changes: 30 additions & 0 deletions arrow-buffer/src/buffer/boolean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use crate::{
bit_util, buffer_bin_and, buffer_bin_or, buffer_bin_xor, buffer_unary_not,
BooleanBufferBuilder, Buffer, MutableBuffer,
};

use std::ops::{BitAnd, BitOr, BitXor, Not};

/// A slice-able [`Buffer`] containing bit-packed booleans
Expand Down Expand Up @@ -274,6 +275,12 @@ impl From<&[bool]> for BooleanBuffer {
}
}

impl From<&[u8]> for BooleanBuffer {
fn from(slice: &[u8]) -> Self {
BooleanBuffer::new(Buffer::from(slice), 0, slice.len() * 8)
}
}

impl From<Vec<bool>> for BooleanBuffer {
fn from(value: Vec<bool>) -> Self {
value.as_slice().into()
Expand Down Expand Up @@ -414,4 +421,27 @@ mod tests {
let expected = BooleanBuffer::new(Buffer::from(&[255, 254, 254, 255, 255]), offset, len);
assert_eq!(!boolean_buf, expected);
}

#[test]
fn test_boolean_from_slice_bool() {
let v = [true, false, false];
let buf = BooleanBuffer::from(&v[..]);
assert_eq!(buf.offset(), 0);
assert_eq!(buf.len(), 3);
assert_eq!(buf.values().len(), 1);
assert!(buf.value(0));
}

#[test]
fn test_boolean_from_slice_u8() {
let v = [1_u8, 2, 3];
let buf = BooleanBuffer::from(&v[..]);
assert_eq!(buf.offset(), 0);
assert_eq!(buf.len(), 24);
assert_eq!(buf.values().len(), 3);
assert!(buf.value(0));
assert!(buf.value(9));
assert!(buf.value(16));
assert!(buf.value(17));
}
}
Loading