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

perf: Use sorted flag for (first|last)_non_null #15050

Merged
merged 1 commit into from
Mar 14, 2024
Merged
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
38 changes: 36 additions & 2 deletions crates/polars-core/src/chunked_array/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,16 +220,50 @@ impl<T: PolarsDataType> ChunkedArray<T> {

/// Get the index of the first non null value in this [`ChunkedArray`].
pub fn first_non_null(&self) -> Option<usize> {
if self.is_empty() {
if self.null_count() == self.len() {
None
}
// We now know there is at least 1 non-null item in the array, and self.len() > 0
else if matches!(
self.is_sorted_flag(),
IsSorted::Ascending | IsSorted::Descending
) {
let out = if unsafe { self.downcast_get_unchecked(0).is_null_unchecked(0) } {
// nulls are all at the start
self.null_count()
} else {
// nulls are all at the end
0
};

Some(out)
} else {
first_non_null(self.iter_validities())
}
}

/// Get the index of the last non null value in this [`ChunkedArray`].
pub fn last_non_null(&self) -> Option<usize> {
last_non_null(self.iter_validities(), self.length as usize)
if self.null_count() == self.len() {
None
}
// We now know there is at least 1 non-null item in the array, and self.len() > 0
else if matches!(
self.is_sorted_flag(),
IsSorted::Ascending | IsSorted::Descending
) {
let out = if unsafe { self.downcast_get_unchecked(0).is_null_unchecked(0) } {
// nulls are all at the start
self.len() - 1
} else {
// nulls are all at the end
self.len() - self.null_count() - 1
};

Some(out)
} else {
last_non_null(self.iter_validities(), self.len())
}
}

/// Get the buffer of bits representing null values
Expand Down
Loading