Shifts array by defined number of items (to left or right) A positive value for `offset` shifts the array to the right a negative value shifts the array to the left. # Examples ``` # use arrow_array::Int32Array; # use arrow_select::window::shift; let a: Int32Array = vec![Some(1), None, Some(4)].into(); // shift array 1 element to the right let res = shift(&a, 1).unwrap(); let expected: Int32Array
(array: &dyn Array, offset: i64)
| 52 | /// assert_eq!(res.as_ref(), &expected); |
| 53 | /// ``` |
| 54 | pub fn shift(array: &dyn Array, offset: i64) -> Result<ArrayRef, ArrowError> { |
| 55 | let value_len = array.len() as i64; |
| 56 | if offset == 0 { |
| 57 | Ok(make_array(array.to_data())) |
| 58 | } else if offset == i64::MIN || abs(offset) >= value_len { |
| 59 | Ok(new_null_array(array.data_type(), array.len())) |
| 60 | } else { |
| 61 | // Concatenate both arrays, add nulls after if shift > 0 else before |
| 62 | if offset > 0 { |
| 63 | let length = array.len() - offset as usize; |
| 64 | let slice = array.slice(0, length); |
| 65 | |
| 66 | // Generate array with remaining `null` items |
| 67 | let null_arr = new_null_array(array.data_type(), offset as usize); |
| 68 | concat(&[null_arr.as_ref(), slice.as_ref()]) |
| 69 | } else { |
| 70 | let offset = -offset as usize; |
| 71 | let length = array.len() - offset; |
| 72 | let slice = array.slice(offset, length); |
| 73 | |
| 74 | // Generate array with remaining `null` items |
| 75 | let null_arr = new_null_array(array.data_type(), offset); |
| 76 | concat(&[slice.as_ref(), null_arr.as_ref()]) |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | #[cfg(test)] |
| 82 | mod tests { |