-
Notifications
You must be signed in to change notification settings - Fork 819
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
Add RunEndBuffer (#1799) #3817
Add RunEndBuffer (#1799) #3817
Conversation
@@ -2157,8 +2157,7 @@ mod tests { | |||
let take_out = take_run(&run_array, &take_indices).unwrap(); | |||
|
|||
assert_eq!(take_out.len(), 7); | |||
|
|||
assert_eq!(take_out.run_ends().len(), 5); | |||
assert_eq!(take_out.run_ends().len(), 7); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
run_ends().len()
now returns the logical length
arrow-buffer/src/buffer/run.rs
Outdated
|
||
/// Performs a binary search to find the physical index for the given logical index | ||
pub fn get_physical_index(&self, logical_index: usize) -> Option<usize> { | ||
if logical_index > self.len { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if logical_index > self.len { | |
if logical_index >= self.len { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I opted to instead remove this check, as it doesn't appear to be necessary. PTAL
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure. I think the newly added comment should be fixed.
let logical_index = E::usize_as(self.offset + logical_index); | ||
let cmp = |p: &E| p.partial_cmp(&logical_index).unwrap(); | ||
|
||
match self.run_ends.binary_search_by(cmp) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍🏼
arrow-buffer/src/buffer/run.rs
Outdated
|
||
/// Performs a binary search to find the physical index for the given logical index | ||
/// | ||
/// The result is arbitrary if `logical_index > self.len()` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/// The result is arbitrary if `logical_index > self.len()` | |
/// The result is arbitrary if `logical_index >= self.len()` |
Also, I think the function should panic if the logical index is not valid for given array.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. Just one minor comment. Not a big deal.
Thank you for taking the time to review this 👍 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense to me, I think this looks great @tustvold -- thank you
@@ -347,7 +316,7 @@ impl<R: RunEndIndexType> std::fmt::Debug for RunArray<R> { | |||
/// .map(|&x| if x == "b" { None } else { Some(x) }) | |||
/// .collect(); | |||
/// assert_eq!( | |||
/// "RunArray {run_ends: PrimitiveArray<Int16>\n[\n 2,\n 3,\n 5,\n], values: StringArray\n[\n \"a\",\n null,\n \"c\",\n]}\n", | |||
/// "RunArray {run_ends: [2, 3, 5], values: StringArray\n[\n \"a\",\n null,\n \"c\",\n]}\n", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is certainly a nicer API
arrow-buffer/src/buffer/run.rs
Outdated
/// describe the value indices `1, 1, 2, 2` for a RunArray | ||
/// | ||
/// For example, a [RunEndBuffer] containing values `[6, 8, 9]` with offset `2` and length `5` | ||
/// would describe the value indices `0, 0, 0, 0, 1` for a RunArray |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
there are 4 zeros because 6 - 2 = 4
, right?
where | ||
E: ArrowNativeType, | ||
{ | ||
/// Create a new [`RunEndBuffer`] from a [`ScalarBuffer`], an `offset` and `len` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should add a note that this panic
s if the invariants are not satisfied (strictly monotonically increasing)
} | ||
} | ||
|
||
/// Returns the logical offset into the run-ends stored by this buffer |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/// Returns the logical offset into the run-ends stored by this buffer | |
/// Returns the logical offset into the run-ends stored by this buffer. | |
/// | |
/// See [`RunEndBuffer`] for interpretation of logical offset |
self.offset | ||
} | ||
|
||
/// Returns the logical length of the run-ends stored by this buffer |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/// Returns the logical length of the run-ends stored by this buffer | |
/// Returns the logical length of the run-ends stored by this buffer | |
/// | |
/// See [`RunEndBuffer`] for interpretation of logical offset |
use crate::buffer::ScalarBuffer; | ||
use crate::ArrowNativeType; | ||
|
||
/// A slice-able buffer of monotonically increasing, positive integers used to store run-ends |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it would be good to define (or link to the definition) the difference between physical and logical offsets / posititions.
Could also be done as a follow on PR as well
Benchmark runs are scheduled for baseline = 81ed334 and contender = 36f2db3. 36f2db3 is a master commit associated with this PR. Results will be available as each benchmark for each run completes. |
/// as there are `3` values, and the maximum logical index is `6`, as the maximum run end | ||
/// is `6`. The physical indices are therefore `[0, 0, 0, 1, 1, 2, 2]` | ||
/// | ||
/// ```text |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is added after I reviewed. This is not correct. The array length is 6 per run_ends and 7 for physical array. Per run_ends, the grouping should be (0,1,2) , (3) and (4,5). The physical array is defined as (0,1,2), (3,4) and (5,6).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Aah yes, I edited it to make the diagram and messed it up. Will fix tomorrow
assert!(!run_ends.is_empty(), "non-empty slice but empty run-ends"); | ||
let end = E::from_usize(offset.saturating_add(len)).unwrap(); | ||
assert!( | ||
*run_ends.first().unwrap() >= E::usize_as(0), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missed this during original review. Should be >0
and not >=0
Which issue does this PR close?
Part of #1799
Rationale for this change
As part of #1799 we need an abstraction similar to
BooleanBuffer
but forRunArray
. Much likeBooleanBuffer
this needs to store a logical offset and length, as theBuffer
cannot simply be sliced directly.What changes are included in this PR?
Are there any user-facing changes?
This changes the API of
RunArray
to move away from returningPrimitiveArray
, inline with the broader plan under #1176