Skip to content
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

Merged
merged 9 commits into from
Mar 8, 2023
Merged

Conversation

tustvold
Copy link
Contributor

@tustvold tustvold commented Mar 7, 2023

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 for RunArray. Much like BooleanBuffer this needs to store a logical offset and length, as the Buffer 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 returning PrimitiveArray, inline with the broader plan under #1176

@tustvold tustvold added the api-change Changes to the arrow API label Mar 7, 2023
@github-actions github-actions bot added the arrow Changes to the arrow crate label Mar 7, 2023
@tustvold tustvold marked this pull request as draft March 7, 2023 19:48
@tustvold tustvold marked this pull request as ready for review March 7, 2023 19:50
@@ -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);
Copy link
Contributor Author

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


/// 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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if logical_index > self.len {
if logical_index >= self.len {

Copy link
Contributor Author

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

Copy link
Contributor

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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍🏼


/// Performs a binary search to find the physical index for the given logical index
///
/// The result is arbitrary if `logical_index > self.len()`
Copy link
Contributor

@askoa askoa Mar 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// 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.

Copy link
Contributor

@askoa askoa left a 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.

@tustvold
Copy link
Contributor Author

tustvold commented Mar 8, 2023

Thank you for taking the time to review this 👍

Copy link
Contributor

@alamb alamb left a 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",
Copy link
Contributor

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

/// 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
Copy link
Contributor

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`
Copy link
Contributor

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 panics if the invariants are not satisfied (strictly monotonically increasing)

}
}

/// Returns the logical offset into the run-ends stored by this buffer
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// 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
Copy link
Contributor

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

@tustvold tustvold merged commit 36f2db3 into apache:master Mar 8, 2023
@ursabot
Copy link

ursabot commented Mar 8, 2023

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.
Conbench compare runs links:
[Skipped ⚠️ Benchmarking of arrow-rs-commits is not supported on ec2-t3-xlarge-us-east-2] ec2-t3-xlarge-us-east-2
[Skipped ⚠️ Benchmarking of arrow-rs-commits is not supported on test-mac-arm] test-mac-arm
[Skipped ⚠️ Benchmarking of arrow-rs-commits is not supported on ursa-i9-9960x] ursa-i9-9960x
[Skipped ⚠️ Benchmarking of arrow-rs-commits is not supported on ursa-thinkcentre-m75q] ursa-thinkcentre-m75q
Buildkite builds:
Supported benchmarks:
ec2-t3-xlarge-us-east-2: Supported benchmark langs: Python, R. Runs only benchmarks with cloud = True
test-mac-arm: Supported benchmark langs: C++, Python, R
ursa-i9-9960x: Supported benchmark langs: Python, R, JavaScript
ursa-thinkcentre-m75q: Supported benchmark langs: C++, Java

/// 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
Copy link
Contributor

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).

Copy link
Contributor Author

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),
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api-change Changes to the arrow API arrow Changes to the arrow crate
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants