-
Notifications
You must be signed in to change notification settings - Fork 924
Add Array::with_nulls #6659
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
Closed
Closed
Add Array::with_nulls #6659
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -103,6 +103,8 @@ pub trait Array: std::fmt::Debug + Send + Sync { | |
fn as_any(&self) -> &dyn Any; | ||
|
||
/// Returns the underlying data of this array | ||
/// | ||
/// See [`Self::into_data`] for a version that consumes self | ||
fn to_data(&self) -> ArrayData; | ||
|
||
/// Returns the underlying data of this array | ||
|
@@ -196,6 +198,28 @@ pub trait Array: std::fmt::Debug + Send + Sync { | |
/// use the slower [`Array::logical_nulls`] to obtain a computed mask. | ||
fn nulls(&self) -> Option<&NullBuffer>; | ||
|
||
/// Replaces the nulls of this array. | ||
/// | ||
/// # Panics | ||
/// Panics if the length of the null buffer is not equal to the length of the array. | ||
/// | ||
/// # Example: | ||
/// ``` | ||
/// # use arrow_array::{Array, Int32Array}; | ||
/// # use arrow_array::cast::AsArray; | ||
/// # use arrow_buffer::NullBuffer; | ||
/// // Create an array with values [1, null, 3, 4, 5] | ||
/// let array = Int32Array::from(vec![Some(1), None, Some(3), Some(4), Some(5)]); | ||
/// // Set the first, third, and fifth elements to null, others to valid | ||
/// let nulls = Some(NullBuffer::from(vec![false, true, false, true, false])); | ||
/// let array_with_nulls = array.with_nulls(nulls); | ||
/// assert_eq!( | ||
/// array_with_nulls.as_primitive(), | ||
/// &Int32Array::from(vec![None, Some(0), None, Some(4), None]), | ||
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. the fact that this is now |
||
/// ); | ||
/// ``` | ||
fn with_nulls(self, nulls: Option<NullBuffer>) -> ArrayRef; | ||
|
||
/// Returns a potentially computed [`NullBuffer`] that represents the logical | ||
/// null values of this array, if any. | ||
/// | ||
|
@@ -372,6 +396,10 @@ impl Array for ArrayRef { | |
self.as_ref().nulls() | ||
} | ||
|
||
fn with_nulls(self, nulls: Option<NullBuffer>) -> ArrayRef { | ||
replace_nulls(self.to_data(), nulls) | ||
} | ||
|
||
fn logical_nulls(&self) -> Option<NullBuffer> { | ||
self.as_ref().logical_nulls() | ||
} | ||
|
@@ -442,6 +470,10 @@ impl<T: Array> Array for &T { | |
T::nulls(self) | ||
} | ||
|
||
fn with_nulls(self, nulls: Option<NullBuffer>) -> ArrayRef { | ||
replace_nulls(self.to_data(), nulls) | ||
} | ||
|
||
fn logical_nulls(&self) -> Option<NullBuffer> { | ||
T::logical_nulls(self) | ||
} | ||
|
@@ -843,6 +875,30 @@ where | |
Ok(()) | ||
} | ||
|
||
/// Helper function to replace the nulls in an array with a new null buffer | ||
/// | ||
/// See [`Array::with_nulls`] for more information | ||
pub(crate) fn replace_nulls(array_data: ArrayData, nulls: Option<NullBuffer>) -> ArrayRef { | ||
let Some(nulls) = nulls else { | ||
return make_array(array_data); | ||
}; | ||
|
||
if nulls.len() != array_data.len() { | ||
panic!( | ||
"Null buffer length must be equal to the array length. \ | ||
Expected: {}, got: {}", | ||
array_data.len(), | ||
nulls.len() | ||
); | ||
} | ||
|
||
let data = array_data.into_builder().nulls(Some(nulls)); | ||
|
||
// SAFETY: | ||
// Checked that the null buffer has the same length as the array | ||
make_array(unsafe { data.build_unchecked() }) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Here is the new API and an example. I am not sure this is a good idea due to @tustvold 's comment here: #6528 (comment) but figured I would make a PR for discussion (and only now do I really understand what he is talking about)