Skip to content

Commit c5b4ab1

Browse files
authored
enhancement: remove redundant if/clamp_min/abs (#1428)
1 parent 486b084 commit c5b4ab1

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

arrow/src/compute/kernels/window.rs

+11-9
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ use crate::{
2424
compute::concat,
2525
};
2626
use num::abs;
27-
use num::traits::clamp_min;
2827

2928
/// Shifts array by defined number of items (to left or right)
3029
/// A positive value for `offset` shifts the array to the right
@@ -64,18 +63,21 @@ pub fn shift(array: &dyn Array, offset: i64) -> Result<ArrayRef> {
6463
} else if offset == i64::MIN || abs(offset) >= value_len {
6564
Ok(new_null_array(array.data_type(), array.len()))
6665
} else {
67-
let slice_offset = clamp_min(-offset, 0) as usize;
68-
let length = array.len() - abs(offset) as usize;
69-
let slice = array.slice(slice_offset, length);
70-
71-
// Generate array with remaining `null` items
72-
let nulls = abs(offset) as usize;
73-
let null_arr = new_null_array(array.data_type(), nulls);
74-
7566
// Concatenate both arrays, add nulls after if shift > 0 else before
7667
if offset > 0 {
68+
let length = array.len() - offset as usize;
69+
let slice = array.slice(0, length);
70+
71+
// Generate array with remaining `null` items
72+
let null_arr = new_null_array(array.data_type(), offset as usize);
7773
concat(&[null_arr.as_ref(), slice.as_ref()])
7874
} else {
75+
let offset = -offset as usize;
76+
let length = array.len() - offset;
77+
let slice = array.slice(offset, length);
78+
79+
// Generate array with remaining `null` items
80+
let null_arr = new_null_array(array.data_type(), offset);
7981
concat(&[slice.as_ref(), null_arr.as_ref()])
8082
}
8183
}

0 commit comments

Comments
 (0)