-
Notifications
You must be signed in to change notification settings - Fork 867
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -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 | ||||||
/// | ||||||
/// ``` | ||||||
/// # 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]); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree -- it should be called the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. BTW could this be
Suggested change
And potentially avoid the allocation? I see you added that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||||||
/// 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 | ||||||
/// | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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::*; | ||
|
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1