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 NullBuffer and BooleanBuffer From conversions #4380

Merged
merged 2 commits into from
Jun 8, 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
20 changes: 18 additions & 2 deletions arrow-array/src/array/primitive_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,16 +294,32 @@ impl<T: ArrowPrimitiveType> Clone for PrimitiveArray<T> {
}

impl<T: ArrowPrimitiveType> PrimitiveArray<T> {
/// Create a new [`PrimitiveArray`] from the provided data_type, values, nulls
/// Create a new [`PrimitiveArray`] from the provided values and nulls
///
/// # Panics
///
/// Panics if [`Self::try_new`] returns an error
///
/// # Example
///
/// Creating a [`PrimitiveArray`] directly from a [`ScalarBuffer`] and [`NullBuffer`] using
/// this constructor is the most performant approach, avoiding any additional allocations
///
/// ```
Comment on lines +307 to +308
Copy link
Contributor

Choose a reason for hiding this comment

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

Giving a little context about when to use this constructor I think might help

Suggested change
///
/// ```
///
/// # Example
///
/// Creating a [`PrimitiveArray`] directly from a [`ScalarBuffer`] and [`NullBuffer`] ls generally
/// the most performant approach and avoids any additional allocations.
///
/// ```

Copy link
Member

Choose a reason for hiding this comment

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

+1

/// # use arrow_array::Int32Array;
/// # use arrow_array::types::Int32Type;
/// # use arrow_buffer::NullBuffer;
/// // [1, 2, 3, 4]
/// let array = Int32Array::new(vec![1, 2, 3, 4].into(), None);
/// // [1, null, 3, 4]
/// let nulls = NullBuffer::from(vec![true, false, true, true]);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

It is unfortunate that arrow went with the name null buffer, and then made true mean not null. But hey ho

Copy link
Contributor

Choose a reason for hiding this comment

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

I agree -- it should be called the ValidBuffer or else have defined 1 to mean null. As you say 🤷

Copy link
Contributor

Choose a reason for hiding this comment

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

BTW could this be

Suggested change
/// let nulls = NullBuffer::from(vec![true, false, true, true]);
/// let nulls = NullBuffer::from(&[true, false, true, true]);

And potentially avoid the allocation? I see you added that From impl 🤔

Copy link
Member

Choose a reason for hiding this comment

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

For From<&[bool]>, looks like it still does allocation.

Copy link
Contributor Author

@tustvold tustvold Jun 8, 2023

Choose a reason for hiding this comment

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

It would eliminate the allocation for the Vec, although the compiler might be smart enough to do that anyway, will update.

Edit this actually causes issues because it infers the type as &[bool; 4], will leave as vec

/// let array = Int32Array::new(vec![1, 2, 3, 4].into(), Some(nulls));
/// ```
pub fn new(values: ScalarBuffer<T::Native>, nulls: Option<NullBuffer>) -> Self {
Self::try_new(values, nulls).unwrap()
}

/// Create a new [`PrimitiveArray`] from the provided data_type, values, nulls
/// Create a new [`PrimitiveArray`] from the provided values and nulls
///
/// # Errors
///
Expand Down
28 changes: 26 additions & 2 deletions arrow-buffer/src/buffer/boolean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
use crate::bit_chunk_iterator::BitChunks;
use crate::bit_iterator::{BitIndexIterator, BitIterator, BitSliceIterator};
use crate::{
bit_util, buffer_bin_and, buffer_bin_or, buffer_bin_xor, buffer_unary_not, Buffer,
MutableBuffer,
bit_util, buffer_bin_and, buffer_bin_or, buffer_bin_xor, buffer_unary_not,
BooleanBufferBuilder, Buffer, MutableBuffer,
};
use std::ops::{BitAnd, BitOr, BitXor, Not};

Expand Down Expand Up @@ -265,6 +265,30 @@ impl<'a> IntoIterator for &'a BooleanBuffer {
}
}

impl From<&[bool]> for BooleanBuffer {
fn from(value: &[bool]) -> Self {
let mut builder = BooleanBufferBuilder::new(value.len());
builder.append_slice(value);
builder.finish()
}
}

impl From<Vec<bool>> for BooleanBuffer {
fn from(value: Vec<bool>) -> Self {
value.as_slice().into()
}
}

impl FromIterator<bool> for BooleanBuffer {
fn from_iter<T: IntoIterator<Item = bool>>(iter: T) -> Self {
let iter = iter.into_iter();
let (hint, _) = iter.size_hint();
alamb marked this conversation as resolved.
Show resolved Hide resolved
let mut builder = BooleanBufferBuilder::new(hint);
iter.for_each(|b| builder.append(b));
builder.finish()
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
24 changes: 24 additions & 0 deletions arrow-buffer/src/buffer/null.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,30 @@ impl<'a> IntoIterator for &'a NullBuffer {
}
}

impl From<BooleanBuffer> for NullBuffer {
fn from(value: BooleanBuffer) -> Self {
Self::new(value)
}
}

impl From<&[bool]> for NullBuffer {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I debated adding custom conversion that would count the nulls, but I wasn't entirely convinced it would be faster - as the bit counting is very fast, so I left it for now. We can easily optimise later

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I debated adding custom conversion that would count the nulls, but I wasn't entirely convinced it would be faster - as the bit counting is very fast, so I left it for now. We can easily optimise later

fn from(value: &[bool]) -> Self {
BooleanBuffer::from(value).into()
}
}

impl From<Vec<bool>> for NullBuffer {
fn from(value: Vec<bool>) -> Self {
BooleanBuffer::from(value).into()
}
}

impl FromIterator<bool> for NullBuffer {
fn from_iter<T: IntoIterator<Item = bool>>(iter: T) -> Self {
BooleanBuffer::from_iter(iter).into()
}
}

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