-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
elide bound checks in boolean take_unchecked (#4274)
- Loading branch information
Showing
8 changed files
with
99 additions
and
8 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
pub mod cast; | ||
pub mod take; |
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 |
---|---|---|
@@ -0,0 +1,88 @@ | ||
use crate::index::IdxSize; | ||
use arrow::{ | ||
array::{Array, BooleanArray, PrimitiveArray}, | ||
bitmap::{Bitmap, MutableBitmap}, | ||
}; | ||
|
||
unsafe fn take_values(values: &Bitmap, indices: &[IdxSize]) -> Bitmap { | ||
let values = indices.iter().map(|&index| { | ||
debug_assert!((index as usize) < values.len()); | ||
values.get_bit_unchecked(index as usize) | ||
}); | ||
Bitmap::from_trusted_len_iter(values) | ||
} | ||
|
||
// take implementation when neither values nor indices contain nulls | ||
unsafe fn take_no_validity(values: &Bitmap, indices: &[IdxSize]) -> (Bitmap, Option<Bitmap>) { | ||
(take_values(values, indices), None) | ||
} | ||
|
||
// take implementation when only values contain nulls | ||
unsafe fn take_values_validity( | ||
values: &BooleanArray, | ||
indices: &[IdxSize], | ||
) -> (Bitmap, Option<Bitmap>) { | ||
let validity_values = values.validity().unwrap(); | ||
let validity = take_values(validity_values, indices); | ||
|
||
let values_values = values.values(); | ||
let buffer = take_values(values_values, indices); | ||
|
||
(buffer, validity.into()) | ||
} | ||
|
||
// take implementation when only indices contain nulls | ||
unsafe fn take_indices_validity( | ||
values: &Bitmap, | ||
indices: &PrimitiveArray<IdxSize>, | ||
) -> (Bitmap, Option<Bitmap>) { | ||
// simply take all and copy the bitmap | ||
let buffer = take_values(values, indices.values()); | ||
|
||
(buffer, indices.validity().cloned()) | ||
} | ||
|
||
// take implementation when both values and indices contain nulls | ||
unsafe fn take_values_indices_validity( | ||
values: &BooleanArray, | ||
indices: &PrimitiveArray<IdxSize>, | ||
) -> (Bitmap, Option<Bitmap>) { | ||
let mut validity = MutableBitmap::with_capacity(indices.len()); | ||
|
||
let values_validity = values.validity().unwrap(); | ||
|
||
let values_values = values.values(); | ||
let values = indices.iter().map(|index| match index { | ||
Some(&index) => { | ||
let index = index as usize; | ||
debug_assert!(index < values.len()); | ||
validity.push(values_validity.get_bit_unchecked(index)); | ||
values_values.get_bit_unchecked(index) | ||
} | ||
None => { | ||
validity.push(false); | ||
false | ||
} | ||
}); | ||
let values = Bitmap::from_trusted_len_iter(values); | ||
(values, validity.into()) | ||
} | ||
|
||
/// `take` implementation for boolean arrays | ||
pub unsafe fn take_unchecked( | ||
values: &BooleanArray, | ||
indices: &PrimitiveArray<IdxSize>, | ||
) -> BooleanArray { | ||
let data_type = values.data_type().clone(); | ||
let indices_has_validity = indices.null_count() > 0; | ||
let values_has_validity = values.null_count() > 0; | ||
|
||
let (values, validity) = match (values_has_validity, indices_has_validity) { | ||
(false, false) => take_no_validity(values.values(), indices.values()), | ||
(true, false) => take_values_validity(values, indices.values()), | ||
(false, true) => take_indices_validity(values.values(), indices), | ||
(true, true) => take_values_indices_validity(values, indices), | ||
}; | ||
|
||
BooleanArray::new(data_type, values, validity) | ||
} |
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
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
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
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
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
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