MCPcopy Create free account
hub / github.com/apache/arrow-rs / shift

Function shift

arrow-select/src/window.rs:54–79  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

52/// assert_eq!(res.as_ref(), &expected);
53/// ```
54pub 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)]
82mod tests {

Callers 12

test_shift_negFunction · 0.85
test_shift_posFunction · 0.85
test_shift_neg_float64Function · 0.85
test_shift_pos_float64Function · 0.85
test_shift_nilFunction · 0.85
test_shift_boundary_posFunction · 0.85
test_shift_boundary_negFunction · 0.85
test_shift_large_posFunction · 0.85
test_shift_large_negFunction · 0.85

Calls 8

new_null_arrayFunction · 0.85
concatFunction · 0.85
make_arrayFunction · 0.50
lenMethod · 0.45
to_dataMethod · 0.45
data_typeMethod · 0.45
sliceMethod · 0.45
as_refMethod · 0.45

Tested by 12

test_shift_negFunction · 0.68
test_shift_posFunction · 0.68
test_shift_neg_float64Function · 0.68
test_shift_pos_float64Function · 0.68
test_shift_nilFunction · 0.68
test_shift_boundary_posFunction · 0.68
test_shift_boundary_negFunction · 0.68
test_shift_large_posFunction · 0.68
test_shift_large_negFunction · 0.68