-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
refactor(rust): Refactor compute kernels in polars-arrow to avoid using gather #19669
Draft
nameexhaustion
wants to merge
10
commits into
pola-rs:main
Choose a base branch
from
nameexhaustion:arrow-compute-no-take
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+246
−203
Draft
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
b8d4b60
refactor(rust): Refactor compute kernels in polars-arrow to avoid usi…
nameexhaustion 3223228
c
nameexhaustion 20d0b1d
c
nameexhaustion e869def
c
nameexhaustion 8971c10
c
nameexhaustion 1630aa2
c
nameexhaustion 4ca943b
c
nameexhaustion 2815c94
c
nameexhaustion 5dff00a
c
nameexhaustion 5cb0ae4
c
nameexhaustion 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 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
193 changes: 141 additions & 52 deletions
193
crates/polars-arrow/src/legacy/kernels/fixed_size_list.rs
This file contains 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 |
---|---|---|
@@ -1,73 +1,162 @@ | ||
use polars_error::{polars_bail, PolarsResult}; | ||
use polars_utils::index::NullCount; | ||
use polars_utils::IdxSize; | ||
|
||
use crate::array::{ArrayRef, FixedSizeListArray, PrimitiveArray}; | ||
use crate::compute::take::take_unchecked; | ||
use crate::legacy::prelude::*; | ||
use crate::legacy::utils::CustomIterTools; | ||
|
||
fn sub_fixed_size_list_get_indexes_literal(width: usize, len: usize, index: i64) -> IdxArr { | ||
(0..len) | ||
.map(|i| { | ||
if index >= width as i64 { | ||
return None; | ||
} | ||
|
||
index | ||
.negative_to_usize(width) | ||
.map(|idx| (idx + i * width) as IdxSize) | ||
}) | ||
.collect_trusted() | ||
} | ||
use polars_error::{polars_bail, PolarsError, PolarsResult}; | ||
|
||
fn sub_fixed_size_list_get_indexes(width: usize, index: &PrimitiveArray<i64>) -> IdxArr { | ||
index | ||
.iter() | ||
.enumerate() | ||
.map(|(i, idx)| { | ||
if let Some(idx) = idx { | ||
if *idx >= width as i64 { | ||
return None; | ||
} | ||
|
||
idx.negative_to_usize(width) | ||
.map(|idx| (idx + i * width) as IdxSize) | ||
} else { | ||
None | ||
} | ||
}) | ||
.collect_trusted() | ||
} | ||
use crate::array::growable::make_growable; | ||
use crate::array::{Array, ArrayRef, FixedSizeListArray, PrimitiveArray}; | ||
use crate::bitmap::BitmapBuilder; | ||
use crate::compute::utils::combine_validities_and3; | ||
use crate::datatypes::ArrowDataType; | ||
|
||
pub fn sub_fixed_size_list_get_literal( | ||
arr: &FixedSizeListArray, | ||
index: i64, | ||
null_on_oob: bool, | ||
) -> PolarsResult<ArrayRef> { | ||
let take_by = sub_fixed_size_list_get_indexes_literal(arr.size(), arr.len(), index); | ||
if !null_on_oob && take_by.null_count() > 0 { | ||
polars_bail!(ComputeError: "get index is out of bounds"); | ||
let ArrowDataType::FixedSizeList(_, width) = arr.dtype() else { | ||
unreachable!(); | ||
}; | ||
|
||
let width = *width; | ||
|
||
let orig_index = index; | ||
|
||
let index = if index < 0 { | ||
if index.unsigned_abs() as usize > width { | ||
width | ||
} else { | ||
(width as i64 + index) as usize | ||
} | ||
} else { | ||
usize::try_from(index).unwrap() | ||
}; | ||
|
||
let index_is_oob = index >= width; | ||
|
||
if !null_on_oob && index >= width { | ||
polars_bail!( | ||
ComputeError: | ||
"get index {} is out of bounds for array(width={})", | ||
orig_index, | ||
width | ||
); | ||
} | ||
|
||
let values = arr.values(); | ||
// SAFETY: | ||
// the indices we generate are in bounds | ||
unsafe { Ok(take_unchecked(&**values, &take_by)) } | ||
|
||
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.
|
||
let mut growable = make_growable( | ||
&[values.as_ref()], | ||
values.validity().is_some() | arr.validity().is_some() | index_is_oob, | ||
arr.len(), | ||
); | ||
|
||
if index_is_oob { | ||
unsafe { growable.extend_validity(arr.len()) } | ||
let out = growable.as_box(); | ||
return Ok(out); | ||
} | ||
|
||
if let Some(arr_validity) = arr.validity() { | ||
for i in 0..arr.len() { | ||
unsafe { | ||
if arr_validity.get_bit_unchecked(i) { | ||
growable.extend(0, i * width + index, 1) | ||
} else { | ||
growable.extend_validity(1) | ||
} | ||
} | ||
} | ||
} else { | ||
for i in 0..arr.len() { | ||
unsafe { growable.extend(0, i * width + index, 1) } | ||
} | ||
} | ||
|
||
Ok(growable.as_box()) | ||
} | ||
|
||
pub fn sub_fixed_size_list_get( | ||
arr: &FixedSizeListArray, | ||
index: &PrimitiveArray<i64>, | ||
null_on_oob: bool, | ||
) -> PolarsResult<ArrayRef> { | ||
let take_by = sub_fixed_size_list_get_indexes(arr.size(), index); | ||
if !null_on_oob && take_by.null_count() > 0 { | ||
polars_bail!(ComputeError: "get index is out of bounds"); | ||
assert_eq!(arr.len(), index.len()); | ||
|
||
fn idx_oob_err(index: i64, width: usize) -> PolarsError { | ||
PolarsError::ComputeError( | ||
format!( | ||
"get index {} is out of bounds for array(width={})", | ||
index, width | ||
) | ||
.into(), | ||
) | ||
} | ||
|
||
let ArrowDataType::FixedSizeList(_, width) = arr.dtype() else { | ||
unreachable!(); | ||
}; | ||
|
||
let width = *width; | ||
|
||
if arr.is_empty() { | ||
let values = arr.values(); | ||
assert!(values.is_empty()); | ||
return Ok(values.clone()); | ||
} | ||
|
||
if !null_on_oob && width == 0 { | ||
if let Some(i) = index.non_null_values_iter().next() { | ||
return Err(idx_oob_err(i, width)); | ||
} | ||
} | ||
|
||
// Array is non-empty and has non-zero width at this point | ||
let values = arr.values(); | ||
// SAFETY: | ||
// the indices we generate are in bounds | ||
unsafe { Ok(take_unchecked(&**values, &take_by)) } | ||
|
||
let mut growable = make_growable(&[values.as_ref()], values.validity().is_some(), arr.len()); | ||
let mut idx_oob_validity = BitmapBuilder::with_capacity(arr.len()); | ||
let opt_index_validity = index.validity(); | ||
let mut exceeded_width_idx = 0; | ||
let mut current_index_i64 = 0; | ||
|
||
for i in 0..arr.len() { | ||
let index = index.value(i); | ||
current_index_i64 = index; | ||
|
||
let idx = if index < 0 { | ||
if index.unsigned_abs() as usize > width { | ||
width | ||
} else { | ||
(width as i64 + index) as usize | ||
} | ||
} else { | ||
usize::try_from(index).unwrap() | ||
}; | ||
|
||
let idx_is_oob = idx >= width; | ||
let idx_is_valid = opt_index_validity.map_or(true, |x| unsafe { x.get_bit_unchecked(i) }); | ||
|
||
if idx_is_oob && idx_is_valid && exceeded_width_idx < width { | ||
exceeded_width_idx = idx; | ||
} | ||
|
||
let idx = if idx_is_oob { 0 } else { idx }; | ||
|
||
unsafe { | ||
growable.extend(0, i * width + idx, 1); | ||
let output_is_valid = idx_is_valid & !idx_is_oob; | ||
idx_oob_validity.push_unchecked(output_is_valid); | ||
} | ||
} | ||
|
||
if !null_on_oob && exceeded_width_idx >= width { | ||
return Err(idx_oob_err(current_index_i64, width)); | ||
} | ||
|
||
let output = growable.as_box(); | ||
let output_validity = combine_validities_and3( | ||
output.validity(), // inner validity | ||
Some(&idx_oob_validity.freeze()), // validity for OOB idx | ||
arr.validity(), // outer validity | ||
); | ||
|
||
Ok(output.with_validity(output_validity)) | ||
} |
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.
Casting nullable List -> FixedSizeList, used a gather to ensure the width of the null slots - have updated this to use
Growable
instead.