Skip to content

Commit

Permalink
Rolling window improvement (#1442)
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 committed Sep 26, 2021
1 parent 92f2801 commit e2c0d5e
Show file tree
Hide file tree
Showing 22 changed files with 2,739 additions and 1,242 deletions.
1 change: 1 addition & 0 deletions polars/polars-arrow/src/kernels/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use arrow::array::{Array, BooleanArray};
use arrow::bitmap::utils::BitChunks;
use std::iter::Enumerate;
pub mod rolling;
pub mod set;

/// Internal state of [SlicesIterator]
Expand Down
59 changes: 59 additions & 0 deletions polars/polars-arrow/src/kernels/rolling/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
pub mod no_nulls;
pub mod nulls;
use arrow::bitmap::{Bitmap, MutableBitmap};

type Start = usize;
type End = usize;
type Idx = usize;
type WindowSize = usize;
type Len = usize;

fn det_offsets(i: Idx, window_size: WindowSize, _len: Len) -> (usize, usize) {
(i.saturating_sub(window_size - 1), i + 1)
}
fn det_offsets_center(i: Idx, window_size: WindowSize, len: Len) -> (usize, usize) {
(
i.saturating_sub(window_size / 2),
std::cmp::min(len, i + window_size / 2),
)
}

fn create_validity<Fo>(
min_periods: usize,
len: usize,
window_size: usize,
det_offsets_fn: Fo,
) -> Option<MutableBitmap>
where
Fo: Fn(Idx, WindowSize, Len) -> (Start, End),
{
if min_periods > 1 {
let mut validity = MutableBitmap::with_capacity(len);
validity.extend_constant(len, true);

// set the null values at the boundaries

// head
for i in 0..len {
let (start, end) = det_offsets_fn(i, window_size, len);
if (end - start) < min_periods {
validity.set(i, false)
} else {
break;
}
}
// tail
for i in (0..len).rev() {
let (start, end) = det_offsets_fn(i, window_size, len);
if (end - start) < min_periods {
validity.set(i, false)
} else {
break;
}
}

Some(validity)
} else {
None
}
}

0 comments on commit e2c0d5e

Please sign in to comment.