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

Use Option<T> rather than Option<&T> for copy types in substring kernel #1576

Merged
merged 1 commit into from
Apr 17, 2022
Merged
Show file tree
Hide file tree
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
7 changes: 1 addition & 6 deletions arrow/benches/string_kernels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,7 @@ use arrow::compute::kernels::substring::substring;
use arrow::util::bench_util::*;

fn bench_substring(arr: &StringArray, start: i64, length: usize) {
substring(
criterion::black_box(arr),
start,
Some(length as u64).as_ref(),
)
.unwrap();
substring(criterion::black_box(arr), start, Some(length as u64)).unwrap();
}

fn add_benchmark(c: &mut Criterion) {
Expand Down
20 changes: 8 additions & 12 deletions arrow/src/compute/kernels/substring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use crate::{
fn generic_substring<OffsetSize: StringOffsetSizeTrait>(
array: &GenericStringArray<OffsetSize>,
start: OffsetSize,
length: Option<&OffsetSize>,
length: Option<OffsetSize>,
) -> Result<ArrayRef> {
let offsets = array.value_offsets();
let null_bit_buffer = array.data_ref().null_buffer().cloned();
Expand All @@ -47,9 +47,9 @@ fn generic_substring<OffsetSize: StringOffsetSizeTrait>(

let cal_new_length: Box<dyn Fn(OffsetSize, OffsetSize) -> OffsetSize> =
if let Some(length) = length {
Box::new(|start: OffsetSize, end: OffsetSize| (*length).min(end - start))
Box::new(move |start: OffsetSize, end: OffsetSize| length.min(end - start))
} else {
Box::new(|start: OffsetSize, end: OffsetSize| end - start)
Box::new(move |start: OffsetSize, end: OffsetSize| end - start)
};

// start and end offsets for each substring
Expand Down Expand Up @@ -130,27 +130,23 @@ fn generic_substring<OffsetSize: StringOffsetSizeTrait>(
///
/// # Error
/// this function errors when the passed array is not a \[Large\]String array.
pub fn substring(
array: &dyn Array,
start: i64,
length: Option<&u64>,
) -> Result<ArrayRef> {
pub fn substring(array: &dyn Array, start: i64, length: Option<u64>) -> Result<ArrayRef> {
match array.data_type() {
DataType::LargeUtf8 => generic_substring(
array
.as_any()
.downcast_ref::<LargeStringArray>()
.expect("A large string is expected"),
start,
length.map(|e| *e as i64).as_ref(),
length.map(|e| e as i64),
),
DataType::Utf8 => generic_substring(
array
.as_any()
.downcast_ref::<StringArray>()
.expect("A string is expected"),
start as i32,
length.map(|e| *e as i32).as_ref(),
length.map(|e| e as i32),
),
_ => Err(ArrowError::ComputeError(format!(
"substring does not support type {:?}",
Expand Down Expand Up @@ -206,7 +202,7 @@ mod tests {
cases.into_iter().try_for_each::<_, Result<()>>(
|(array, start, length, expected)| {
let array = T::from(array);
let result: ArrayRef = substring(&array, start, length.as_ref())?;
let result: ArrayRef = substring(&array, start, length)?;
assert_eq!(array.len(), result.len());

let result = result.as_any().downcast_ref::<T>().unwrap();
Expand Down Expand Up @@ -287,7 +283,7 @@ mod tests {
cases.into_iter().try_for_each::<_, Result<()>>(
|(array, start, length, expected)| {
let array = StringArray::from(array);
let result = substring(&array, start, length.as_ref())?;
let result = substring(&array, start, length)?;
assert_eq!(array.len(), result.len());
let result = result.as_any().downcast_ref::<StringArray>().unwrap();
let expected = StringArray::from(expected);
Expand Down