You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by "askoa (via GitHub)" <gi...@apache.org> on 2023/01/25 15:18:47 UTC
[GitHub] [arrow-rs] askoa opened a new pull request, #3603: Add ArrayAccessor, Iterator, Extend and benchmarks for RunArray
askoa opened a new pull request, #3603:
URL: https://github.com/apache/arrow-rs/pull/3603
# Which issue does this PR close?
Part of #3520
# Rationale for this change
See issue description.
# What changes are included in this PR?
- Implement `Extend` for the `PrimitiveRunBuilder` and `GenericByteRunBuilder`
- Add `TypedRunArray`
- Add `RunArrayIter` and `ArrayAccessor` for `TypedRunArray`
- Add benchmarks for `StringRunBuilder`
# Are there any user-facing changes?
Users will get new features for RunArray.
No breaking changes.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow-rs] askoa commented on a diff in pull request #3603: Add ArrayAccessor, Iterator, Extend and benchmarks for RunArray
Posted by "askoa (via GitHub)" <gi...@apache.org>.
askoa commented on code in PR #3603:
URL: https://github.com/apache/arrow-rs/pull/3603#discussion_r1092863125
##########
arrow-array/src/array/run_array.rs:
##########
@@ -274,15 +296,191 @@ pub type Int32RunArray = RunArray<Int32Type>;
/// ```
pub type Int64RunArray = RunArray<Int64Type>;
+/// A strongly-typed wrapper around a [`RunArray`] that implements [`ArrayAccessor`]
+/// and [`IntoIterator`] allowing fast access to its elements
+///
+/// ```
+/// use arrow_array::{RunArray, StringArray, types::Int32Type};
+///
+/// let orig = ["a", "b", "a", "b"];
+/// let ree_array = RunArray::<Int32Type>::from_iter(orig);
+///
+/// // `TypedRunArray` allows you to access the values directly
+/// let typed = ree_array.downcast_ref::<StringArray>().unwrap();
+///
+/// for (maybe_val, orig) in typed.into_iter().zip(orig) {
+/// assert_eq!(maybe_val.unwrap(), orig)
+/// }
+/// ```
+pub struct TypedRunArray<'a, R: RunEndIndexType, V> {
+ /// The run array
+ run_array: &'a RunArray<R>,
+
+ /// The values of the run_array
+ values: &'a V,
+}
+
+// Manually implement `Clone` to avoid `V: Clone` type constraint
+impl<'a, R: RunEndIndexType, V> Clone for TypedRunArray<'a, R, V> {
+ fn clone(&self) -> Self {
+ Self {
+ run_array: self.run_array,
+ values: self.values,
+ }
+ }
+}
+
+impl<'a, R: RunEndIndexType, V> Copy for TypedRunArray<'a, R, V> {}
+
+impl<'a, R: RunEndIndexType, V> std::fmt::Debug for TypedRunArray<'a, R, V> {
+ fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
+ writeln!(f, "TypedRunArray({:?})", self.run_array)
+ }
+}
+
+impl<'a, R: RunEndIndexType, V> TypedRunArray<'a, R, V> {
+ /// Returns the run_ends of this [`TypedRunArray`]
+ pub fn run_ends(&self) -> &'a PrimitiveArray<R> {
+ self.run_array.run_ends()
+ }
+
+ /// Returns the values of this [`TypedRunArray`]
+ pub fn values(&self) -> &'a V {
+ self.values
+ }
+
+ /// Returns index to the physcial array for the given index to the logical array.
+ /// Performs a binary search on the run_ends array for the input index.
+ #[inline]
+ pub fn get_physical_index(&self, logical_index: usize) -> Option<usize> {
+ if logical_index >= self.run_array.len() {
+ return None;
+ }
+ let mut st: usize = 0;
Review Comment:
To use `binary_search_by` a custom `Ordering` has to be implemented. Two values (`i` and `i-1`) has to be accessed to determine `std::cmp::Ordering::Equal`. I am not sure what'll happen if `Equal` was never returned in custom `Ordering`. I did not spend lot of time on it because did not see the benefits.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow-rs] viirya commented on a diff in pull request #3603: Add ArrayAccessor, Iterator, Extend and benchmarks for RunArray
Posted by "viirya (via GitHub)" <gi...@apache.org>.
viirya commented on code in PR #3603:
URL: https://github.com/apache/arrow-rs/pull/3603#discussion_r1089825200
##########
arrow-array/src/array/run_array.rs:
##########
@@ -504,4 +693,30 @@ mod tests {
let a = RunArray::<Int32Type>::from_iter(["32"]);
let _ = RunArray::<Int64Type>::from(a.into_data());
}
+
+ #[test]
+ fn test_ree_array_accessor() {
+ let input_array = build_input_array(256);
+
+ // Encode the input_array to ree_array
+ let mut builder =
+ PrimitiveRunBuilder::<Int16Type, Int32Type>::with_capacity(input_array.len());
+ builder.extend(input_array.clone().into_iter());
+ let run_array = builder.finish();
+ let typed = run_array
+ .downcast_ref::<PrimitiveArray<Int32Type>>()
+ .unwrap();
+
+ let len = input_array.len();
+ for i in 0..len {
+ let actual = typed.value(i);
+ match input_array[i] {
+ Some(val) => assert_eq!(val, actual),
+ None => {
Review Comment:
I'm not sure if the spec has defined how to return nullability for RunArray. Another similar one is `UnionArray` which also doesn't have validity buffer and its `is_null` simply returns false and just asks users to check underlying array in `is_null` doc.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow-rs] askoa commented on a diff in pull request #3603: Add ArrayAccessor, Iterator, Extend and benchmarks for RunArray
Posted by "askoa (via GitHub)" <gi...@apache.org>.
askoa commented on code in PR #3603:
URL: https://github.com/apache/arrow-rs/pull/3603#discussion_r1089825081
##########
arrow-array/src/array/run_array.rs:
##########
@@ -274,15 +296,191 @@ pub type Int32RunArray = RunArray<Int32Type>;
/// ```
pub type Int64RunArray = RunArray<Int64Type>;
+/// A strongly-typed wrapper around a [`RunArray`] that implements [`ArrayAccessor`]
+/// and [`IntoIterator`] allowing fast access to its elements
+///
+/// ```
+/// use arrow_array::{RunArray, StringArray, types::Int32Type};
+///
+/// let orig = ["a", "b", "a", "b"];
+/// let ree_array = RunArray::<Int32Type>::from_iter(orig);
+///
+/// // `TypedRunArray` allows you to access the values directly
+/// let typed = ree_array.downcast_ref::<StringArray>().unwrap();
+///
+/// for (maybe_val, orig) in typed.into_iter().zip(orig) {
+/// assert_eq!(maybe_val.unwrap(), orig)
+/// }
+/// ```
+pub struct TypedRunArray<'a, R: RunEndIndexType, V> {
+ /// The run array
+ run_array: &'a RunArray<R>,
+
+ /// The values of the run_array
+ values: &'a V,
+}
+
+// Manually implement `Clone` to avoid `V: Clone` type constraint
+impl<'a, R: RunEndIndexType, V> Clone for TypedRunArray<'a, R, V> {
+ fn clone(&self) -> Self {
+ Self {
+ run_array: self.run_array,
+ values: self.values,
+ }
+ }
+}
+
+impl<'a, R: RunEndIndexType, V> Copy for TypedRunArray<'a, R, V> {}
+
+impl<'a, R: RunEndIndexType, V> std::fmt::Debug for TypedRunArray<'a, R, V> {
+ fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
+ writeln!(f, "TypedRunArray({:?})", self.run_array)
+ }
+}
+
+impl<'a, R: RunEndIndexType, V> TypedRunArray<'a, R, V> {
+ /// Returns the run_ends of this [`TypedRunArray`]
+ pub fn run_ends(&self) -> &'a PrimitiveArray<R> {
+ self.run_array.run_ends()
+ }
+
+ /// Returns the values of this [`TypedRunArray`]
+ pub fn values(&self) -> &'a V {
+ self.values
+ }
+
+ /// Returns index to the physcial array for the given index to the logical array.
+ /// Performs a binary search on the run_ends array for the input index.
+ #[inline]
+ pub fn get_physical_index(&self, logical_index: usize) -> Option<usize> {
Review Comment:
Good catch. I have moved it to `RunArray` in recently queued PR #3622. I am not planning to fix it here.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow-rs] askoa commented on a diff in pull request #3603: Add ArrayAccessor, Iterator, Extend and benchmarks for RunArray
Posted by "askoa (via GitHub)" <gi...@apache.org>.
askoa commented on code in PR #3603:
URL: https://github.com/apache/arrow-rs/pull/3603#discussion_r1086778086
##########
arrow-array/src/array/run_array.rs:
##########
@@ -504,4 +693,30 @@ mod tests {
let a = RunArray::<Int32Type>::from_iter(["32"]);
let _ = RunArray::<Int64Type>::from(a.into_data());
}
+
+ #[test]
+ fn test_ree_array_accessor() {
+ let input_array = build_input_array(256);
+
+ // Encode the input_array to ree_array
+ let mut builder =
+ PrimitiveRunBuilder::<Int16Type, Int32Type>::with_capacity(input_array.len());
+ builder.extend(input_array.clone().into_iter());
+ let run_array = builder.finish();
+ let typed = run_array
+ .downcast_ref::<PrimitiveArray<Int32Type>>()
+ .unwrap();
+
+ let len = input_array.len();
+ for i in 0..len {
+ let actual = typed.value(i);
+ match input_array[i] {
+ Some(val) => assert_eq!(val, actual),
+ None => {
Review Comment:
I am wondering how users will check if a value in logical index in null. Should `array.is_null` method be overridden to return the nullability of logical index?
cc: @viirya, @tustvold
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow-rs] askoa commented on pull request #3603: Add ArrayAccessor, Iterator, Extend and benchmarks for RunArray
Posted by "askoa (via GitHub)" <gi...@apache.org>.
askoa commented on PR #3603:
URL: https://github.com/apache/arrow-rs/pull/3603#issuecomment-1403835531
Benchmark result for `string_run_builder`
```
Benchmarking string_run_builder/(run_array_len:1000, physical_array_len:20, string_len: 5): Collecting 100 samples in estimated 5.0413 s (333k it
string_run_builder/(run_array_len:1000, physical_array_len:20, string_len: 5)
time: [14.727 µs 14.761 µs 14.803 µs]
Found 9 outliers among 100 measurements (9.00%)
3 (3.00%) high mild
6 (6.00%) high severe
Benchmarking string_run_builder/(run_array_len:1000, physical_array_len:100, string_len: 5): Collecting 100 samples in estimated 5.0525 s (303k i
string_run_builder/(run_array_len:1000, physical_array_len:100, string_len: 5)
time: [16.507 µs 16.544 µs 16.586 µs]
Found 11 outliers among 100 measurements (11.00%)
5 (5.00%) high mild
6 (6.00%) high severe
Benchmarking string_run_builder/(run_array_len:1000, physical_array_len:100, string_len: 10): Collecting 100 samples in estimated 5.0012 s (419k
string_run_builder/(run_array_len:1000, physical_array_len:100, string_len: 10)
time: [11.861 µs 11.920 µs 12.002 µs]
Found 11 outliers among 100 measurements (11.00%)
4 (4.00%) high mild
7 (7.00%) high severe
Benchmarking string_run_builder/(run_array_len:10000, physical_array_len:100, string_len: 10): Collecting 100 samples in estimated 5.2888 s (81k
string_run_builder/(run_array_len:10000, physical_array_len:100, string_len: 10)
time: [64.982 µs 65.272 µs 65.737 µs]
Found 10 outliers among 100 measurements (10.00%)
3 (3.00%) high mild
7 (7.00%) high severe
Benchmarking string_run_builder/(run_array_len:10000, physical_array_len:100, string_len: 100): Collecting 100 samples in estimated 6.1245 s (25k
string_run_builder/(run_array_len:10000, physical_array_len:100, string_len: 100)
time: [239.52 µs 240.42 µs 241.57 µs]
Found 16 outliers among 100 measurements (16.00%)
6 (6.00%) high mild
10 (10.00%) high severe
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow-rs] tustvold commented on pull request #3603: Add ArrayAccessor, Iterator, Extend and benchmarks for RunArray
Posted by "tustvold (via GitHub)" <gi...@apache.org>.
tustvold commented on PR #3603:
URL: https://github.com/apache/arrow-rs/pull/3603#issuecomment-1407500801
I intend to review this early next week, apologies for the delay, it is hard for me to find time to review large PRs
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow-rs] ursabot commented on pull request #3603: Add ArrayAccessor, Iterator, Extend and benchmarks for RunArray
Posted by "ursabot (via GitHub)" <gi...@apache.org>.
ursabot commented on PR #3603:
URL: https://github.com/apache/arrow-rs/pull/3603#issuecomment-1416707803
Benchmark runs are scheduled for baseline = 79bda7d361579cda88fec2eb9b8793ad7f653442 and contender = 25e10ddf0b8b65f0d73142717e7c22ab150d6870. 25e10ddf0b8b65f0d73142717e7c22ab150d6870 is a master commit associated with this PR. Results will be available as each benchmark for each run completes.
Conbench compare runs links:
[Skipped :warning: Benchmarking of arrow-rs-commits is not supported on ec2-t3-xlarge-us-east-2] [ec2-t3-xlarge-us-east-2](https://conbench.ursa.dev/compare/runs/d4c62388d49b4adaae0502b0366c5509...360b433bcd8f426ea1c32558edd2d1e5/)
[Skipped :warning: Benchmarking of arrow-rs-commits is not supported on test-mac-arm] [test-mac-arm](https://conbench.ursa.dev/compare/runs/ce2e8c3e59b843e7b172745662ed65c8...49b99d042df24c1299d930476e9be58d/)
[Skipped :warning: Benchmarking of arrow-rs-commits is not supported on ursa-i9-9960x] [ursa-i9-9960x](https://conbench.ursa.dev/compare/runs/30323b3c925744cca69be819d4e3650b...0e99825aa86c4f2fa497ce250e6d56ac/)
[Skipped :warning: Benchmarking of arrow-rs-commits is not supported on ursa-thinkcentre-m75q] [ursa-thinkcentre-m75q](https://conbench.ursa.dev/compare/runs/c435990c4b5046e28f278af49b6d8bf5...eb920f9a3ec6446daa060abe82697fb6/)
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
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow-rs] tustvold commented on a diff in pull request #3603: Add ArrayAccessor, Iterator, Extend and benchmarks for RunArray
Posted by "tustvold (via GitHub)" <gi...@apache.org>.
tustvold commented on code in PR #3603:
URL: https://github.com/apache/arrow-rs/pull/3603#discussion_r1089827550
##########
arrow-array/src/array/run_array.rs:
##########
@@ -504,4 +693,30 @@ mod tests {
let a = RunArray::<Int32Type>::from_iter(["32"]);
let _ = RunArray::<Int64Type>::from(a.into_data());
}
+
+ #[test]
+ fn test_ree_array_accessor() {
+ let input_array = build_input_array(256);
+
+ // Encode the input_array to ree_array
+ let mut builder =
+ PrimitiveRunBuilder::<Int16Type, Int32Type>::with_capacity(input_array.len());
+ builder.extend(input_array.clone().into_iter());
+ let run_array = builder.finish();
+ let typed = run_array
+ .downcast_ref::<PrimitiveArray<Int32Type>>()
+ .unwrap();
+
+ let len = input_array.len();
+ for i in 0..len {
+ let actual = typed.value(i);
+ match input_array[i] {
+ Some(val) => assert_eq!(val, actual),
+ None => {
Review Comment:
I think given there is no null mask `is_null` should always return false, we could add a `is_logical_null` but I think it might cause confusion and I'm not sure how it would be used in practice.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow-rs] viirya commented on a diff in pull request #3603: Add ArrayAccessor, Iterator, Extend and benchmarks for RunArray
Posted by "viirya (via GitHub)" <gi...@apache.org>.
viirya commented on code in PR #3603:
URL: https://github.com/apache/arrow-rs/pull/3603#discussion_r1089824290
##########
arrow-array/src/array/run_array.rs:
##########
@@ -121,6 +122,27 @@ impl<R: RunEndIndexType> RunArray<R> {
pub fn values(&self) -> &ArrayRef {
&self.values
}
+
+ /// Downcast this dictionary to a [`TypedRunArray`]
Review Comment:
```suggestion
/// Downcast this [`RunArray`] to a [`TypedRunArray`]
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow-rs] askoa commented on a diff in pull request #3603: Add ArrayAccessor, Iterator, Extend and benchmarks for RunArray
Posted by "askoa (via GitHub)" <gi...@apache.org>.
askoa commented on code in PR #3603:
URL: https://github.com/apache/arrow-rs/pull/3603#discussion_r1089825670
##########
arrow-array/src/array/run_array.rs:
##########
@@ -504,4 +693,30 @@ mod tests {
let a = RunArray::<Int32Type>::from_iter(["32"]);
let _ = RunArray::<Int64Type>::from(a.into_data());
}
+
+ #[test]
+ fn test_ree_array_accessor() {
+ let input_array = build_input_array(256);
+
+ // Encode the input_array to ree_array
+ let mut builder =
+ PrimitiveRunBuilder::<Int16Type, Int32Type>::with_capacity(input_array.len());
+ builder.extend(input_array.clone().into_iter());
+ let run_array = builder.finish();
+ let typed = run_array
+ .downcast_ref::<PrimitiveArray<Int32Type>>()
+ .unwrap();
+
+ let len = input_array.len();
+ for i in 0..len {
+ let actual = typed.value(i);
+ match input_array[i] {
+ Some(val) => assert_eq!(val, actual),
+ None => {
Review Comment:
The issue here is we do not have any api that checks nullability based on logical index. The child arrays can be used to check for nullability on physical index. May be a custom function `is_value_null` in `RunArray`?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow-rs] tustvold commented on a diff in pull request #3603: Add ArrayAccessor, Iterator, Extend and benchmarks for RunArray
Posted by "tustvold (via GitHub)" <gi...@apache.org>.
tustvold commented on code in PR #3603:
URL: https://github.com/apache/arrow-rs/pull/3603#discussion_r1092258209
##########
arrow-array/src/array/run_array.rs:
##########
@@ -504,4 +702,30 @@ mod tests {
let a = RunArray::<Int32Type>::from_iter(["32"]);
let _ = RunArray::<Int64Type>::from(a.into_data());
}
+
+ #[test]
+ fn test_ree_array_accessor() {
+ let input_array = build_input_array(256);
+
+ // Encode the input_array to ree_array
+ let mut builder =
+ PrimitiveRunBuilder::<Int16Type, Int32Type>::with_capacity(input_array.len());
+ builder.extend(input_array.clone().into_iter());
Review Comment:
```suggestion
builder.extend(input_array.iter().copied());
```
Perhaps
##########
arrow-array/src/array/run_array.rs:
##########
@@ -121,6 +122,27 @@ impl<R: RunEndIndexType> RunArray<R> {
pub fn values(&self) -> &ArrayRef {
&self.values
}
+
+ /// Downcast this dictionary to a [`TypedRunArray`]
+ ///
+ /// ```
+ /// use arrow_array::{Array, ArrayAccessor, RunArray, StringArray, types::Int32Type};
+ ///
+ /// let orig = [Some("a"), Some("b"), None];
+ /// let run_array = RunArray::<Int32Type>::from_iter(orig);
+ /// let typed = run_array.downcast_ref::<StringArray>().unwrap();
+ /// assert_eq!(typed.value(0), "a");
+ /// assert_eq!(typed.value(1), "b");
+ /// assert!(typed.values().is_null(2));
+ /// ```
+ ///
+ pub fn downcast_ref<V: 'static>(&self) -> Option<TypedRunArray<'_, R, V>> {
Review Comment:
```suggestion
pub fn downcast<V: 'static>(&self) -> Option<TypedRunArray<'_, R, V>> {
```
Or something so as not to overload `dyn Any`
##########
arrow-array/src/array/run_array.rs:
##########
@@ -274,15 +296,191 @@ pub type Int32RunArray = RunArray<Int32Type>;
/// ```
pub type Int64RunArray = RunArray<Int64Type>;
+/// A strongly-typed wrapper around a [`RunArray`] that implements [`ArrayAccessor`]
+/// and [`IntoIterator`] allowing fast access to its elements
+///
+/// ```
+/// use arrow_array::{RunArray, StringArray, types::Int32Type};
+///
+/// let orig = ["a", "b", "a", "b"];
+/// let ree_array = RunArray::<Int32Type>::from_iter(orig);
+///
+/// // `TypedRunArray` allows you to access the values directly
+/// let typed = ree_array.downcast_ref::<StringArray>().unwrap();
+///
+/// for (maybe_val, orig) in typed.into_iter().zip(orig) {
+/// assert_eq!(maybe_val.unwrap(), orig)
+/// }
+/// ```
+pub struct TypedRunArray<'a, R: RunEndIndexType, V> {
+ /// The run array
+ run_array: &'a RunArray<R>,
+
+ /// The values of the run_array
+ values: &'a V,
+}
+
+// Manually implement `Clone` to avoid `V: Clone` type constraint
+impl<'a, R: RunEndIndexType, V> Clone for TypedRunArray<'a, R, V> {
+ fn clone(&self) -> Self {
+ Self {
+ run_array: self.run_array,
+ values: self.values,
+ }
+ }
+}
+
+impl<'a, R: RunEndIndexType, V> Copy for TypedRunArray<'a, R, V> {}
+
+impl<'a, R: RunEndIndexType, V> std::fmt::Debug for TypedRunArray<'a, R, V> {
+ fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
+ writeln!(f, "TypedRunArray({:?})", self.run_array)
+ }
+}
+
+impl<'a, R: RunEndIndexType, V> TypedRunArray<'a, R, V> {
+ /// Returns the run_ends of this [`TypedRunArray`]
+ pub fn run_ends(&self) -> &'a PrimitiveArray<R> {
+ self.run_array.run_ends()
+ }
+
+ /// Returns the values of this [`TypedRunArray`]
+ pub fn values(&self) -> &'a V {
+ self.values
+ }
+
+ /// Returns index to the physcial array for the given index to the logical array.
+ /// Performs a binary search on the run_ends array for the input index.
+ #[inline]
+ pub fn get_physical_index(&self, logical_index: usize) -> Option<usize> {
+ if logical_index >= self.run_array.len() {
+ return None;
+ }
+ let mut st: usize = 0;
Review Comment:
I wonder if we could use [`binary_search`](https://doc.rust-lang.org/std/primitive.slice.html#method.binary_search) on `self.run_array.values()`
##########
arrow-array/src/run_iterator.rs:
##########
@@ -0,0 +1,271 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+//! Idiomatic iterator for [`RunArray`](crate::Array)
+
+use arrow_buffer::ArrowNativeType;
+
+use crate::{array::ArrayAccessor, types::RunEndIndexType, Array, TypedRunArray};
+
+/// The [`RunArrayIter`] provides an idiomatic way to iterate over the run array.
+/// It returns Some(T) if there is a value or None if the value is null.
+///
+/// The iterator comes with a cost as it has to iterate over three arrays to determine
+/// the value to be returned. The run_ends array is used to determine the index of the value.
+/// The nulls array is used to determine if the value is null and the values array is used to
+/// get the value.
+///
+/// Unlike other iterators in this crate, [`RunArrayIter`] does not use [`ArrayAccessor`]
+/// because the run array accessor does binary search to access each value which is too slow.
+/// The run array iterator can determine the next value in constant time.
+///
+#[derive(Debug)]
+pub struct RunArrayIter<'a, R, V>
+where
+ R: RunEndIndexType,
+ V: Sync + Send,
+ &'a V: ArrayAccessor,
+ <&'a V as ArrayAccessor>::Item: Default,
+{
+ array: TypedRunArray<'a, R, V>,
+ current_logical: usize,
+ current_physical: usize,
+ current_end_logical: usize,
+ current_end_physical: usize,
+}
+
+impl<'a, R, V> RunArrayIter<'a, R, V>
+where
+ R: RunEndIndexType,
+ V: Sync + Send,
+ &'a V: ArrayAccessor,
+ <&'a V as ArrayAccessor>::Item: Default,
+{
+ /// create a new iterator
+ pub fn new(array: TypedRunArray<'a, R, V>) -> Self {
+ let logical_len = array.len();
+ let physical_len: usize = array.values().len();
+ RunArrayIter {
+ array,
+ current_logical: 0,
+ current_physical: 0,
+ current_end_logical: logical_len,
+ current_end_physical: physical_len,
+ }
+ }
+}
+
+impl<'a, R, V> Iterator for RunArrayIter<'a, R, V>
+where
+ R: RunEndIndexType,
+ V: Sync + Send,
+ &'a V: ArrayAccessor,
+ <&'a V as ArrayAccessor>::Item: Default,
+{
+ type Item = Option<<&'a V as ArrayAccessor>::Item>;
+
+ #[inline]
+ fn next(&mut self) -> Option<Self::Item> {
+ if self.current_logical == self.current_end_logical {
+ return None;
+ }
+ // If current logical index is greater than current run end index then increment
+ // the physical index.
+ if self.current_logical
+ >= self
+ .array
+ .run_ends()
+ .value(self.current_physical)
+ .as_usize()
+ {
+ // As the run_ends is expected to be strictly increasing, there
+ // should be at least one logical entry in one physical entry. Because of this
+ // reason the next value can be accessed by incrementing physical index once.
+ self.current_physical += 1;
+ }
+ if self.array.values().is_null(self.current_physical) {
+ self.current_logical += 1;
+ Some(None)
+ } else {
+ self.current_logical += 1;
+ // Safety:
+ // The self.current_physical is kept within bounds of self.current_logical.
+ // The self.current_logical will not go out of bounds because of the check
+ // `self.current_logical = self.current_end_logical` above.
+ unsafe {
+ Some(Some(
+ self.array.values().value_unchecked(self.current_physical),
+ ))
+ }
+ }
+ }
+
+ fn size_hint(&self) -> (usize, Option<usize>) {
+ (
+ self.current_end_logical - self.current_logical,
+ Some(self.current_end_logical - self.current_logical),
+ )
+ }
+}
+
+impl<'a, R, V> DoubleEndedIterator for RunArrayIter<'a, R, V>
+where
+ R: RunEndIndexType,
+ V: Sync + Send,
+ &'a V: ArrayAccessor,
+ <&'a V as ArrayAccessor>::Item: Default,
+{
+ fn next_back(&mut self) -> Option<Self::Item> {
+ if self.current_end_logical == self.current_logical {
Review Comment:
FWIW it might be clear to reduce the nesting with some early `return`, don't feel strongly
##########
arrow-array/src/array/run_array.rs:
##########
@@ -274,15 +296,191 @@ pub type Int32RunArray = RunArray<Int32Type>;
/// ```
pub type Int64RunArray = RunArray<Int64Type>;
+/// A strongly-typed wrapper around a [`RunArray`] that implements [`ArrayAccessor`]
+/// and [`IntoIterator`] allowing fast access to its elements
+///
+/// ```
+/// use arrow_array::{RunArray, StringArray, types::Int32Type};
+///
+/// let orig = ["a", "b", "a", "b"];
+/// let ree_array = RunArray::<Int32Type>::from_iter(orig);
+///
+/// // `TypedRunArray` allows you to access the values directly
+/// let typed = ree_array.downcast_ref::<StringArray>().unwrap();
+///
+/// for (maybe_val, orig) in typed.into_iter().zip(orig) {
+/// assert_eq!(maybe_val.unwrap(), orig)
+/// }
+/// ```
+pub struct TypedRunArray<'a, R: RunEndIndexType, V> {
+ /// The run array
+ run_array: &'a RunArray<R>,
+
+ /// The values of the run_array
+ values: &'a V,
+}
+
+// Manually implement `Clone` to avoid `V: Clone` type constraint
+impl<'a, R: RunEndIndexType, V> Clone for TypedRunArray<'a, R, V> {
+ fn clone(&self) -> Self {
+ Self {
+ run_array: self.run_array,
+ values: self.values,
+ }
+ }
+}
+
+impl<'a, R: RunEndIndexType, V> Copy for TypedRunArray<'a, R, V> {}
+
+impl<'a, R: RunEndIndexType, V> std::fmt::Debug for TypedRunArray<'a, R, V> {
+ fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
+ writeln!(f, "TypedRunArray({:?})", self.run_array)
+ }
+}
+
+impl<'a, R: RunEndIndexType, V> TypedRunArray<'a, R, V> {
+ /// Returns the run_ends of this [`TypedRunArray`]
+ pub fn run_ends(&self) -> &'a PrimitiveArray<R> {
+ self.run_array.run_ends()
+ }
+
+ /// Returns the values of this [`TypedRunArray`]
+ pub fn values(&self) -> &'a V {
+ self.values
+ }
+
+ /// Returns index to the physcial array for the given index to the logical array.
+ /// Performs a binary search on the run_ends array for the input index.
+ #[inline]
+ pub fn get_physical_index(&self, logical_index: usize) -> Option<usize> {
+ if logical_index >= self.run_array.len() {
+ return None;
+ }
+ let mut st: usize = 0;
+ let mut en: usize = self.run_ends().len();
+ while st + 1 < en {
+ let mid: usize = (st + en) / 2;
+ if logical_index
+ < unsafe {
+ // Safety:
+ // The value of mid will always be between 1 and len - 1,
+ // where len is length of run ends array.
+ // This is based on the fact that `st` starts with 0 and
+ // `en` starts with len. The condition `st + 1 < en` ensures
+ // `st` and `en` differs atleast by two. So the value of `mid`
+ // will never be either `st` or `en`
+ self.run_ends().value_unchecked(mid - 1).as_usize()
+ }
+ {
+ en = mid
+ } else {
+ st = mid
+ }
+ }
+ Some(st)
+ }
+}
+
+impl<'a, R: RunEndIndexType, V: Sync> Array for TypedRunArray<'a, R, V> {
+ fn as_any(&self) -> &dyn Any {
+ self.run_array
+ }
+
+ fn data(&self) -> &ArrayData {
+ &self.run_array.data
+ }
+
+ fn into_data(self) -> ArrayData {
+ self.run_array.into_data()
+ }
+}
+
+// Array accessor converts the index of logical array to the index of the physical array
+// using binary search. The time complexity is O(log N) where N is number of runs.
+impl<'a, R, V> ArrayAccessor for TypedRunArray<'a, R, V>
+where
+ R: RunEndIndexType,
+ V: Sync + Send,
+ &'a V: ArrayAccessor,
+ <&'a V as ArrayAccessor>::Item: Default,
+{
+ type Item = <&'a V as ArrayAccessor>::Item;
+
+ fn value(&self, logical_index: usize) -> Self::Item {
+ assert!(
+ logical_index < self.len(),
+ "Trying to access an element at index {} from a TypedRunArray of length {}",
+ logical_index,
+ self.len()
+ );
+ unsafe { self.value_unchecked(logical_index) }
+ }
+
+ unsafe fn value_unchecked(&self, logical_index: usize) -> Self::Item {
+ let physical_index = self.get_physical_index(logical_index).unwrap();
+ self.values().value_unchecked(physical_index)
+ }
+}
+
+impl<'a, R, V> IntoIterator for TypedRunArray<'a, R, V>
+where
+ R: RunEndIndexType,
+ V: Sync + Send,
+ &'a V: ArrayAccessor,
+ <&'a V as ArrayAccessor>::Item: Default,
+{
+ type Item = Option<<&'a V as ArrayAccessor>::Item>;
+ type IntoIter = RunArrayIter<'a, R, V>;
+
+ fn into_iter(self) -> Self::IntoIter {
+ RunArrayIter::new(self)
+ }
+}
+
#[cfg(test)]
mod tests {
use std::sync::Arc;
+ use rand::seq::SliceRandom;
+ use rand::thread_rng;
+ use rand::Rng;
+
use super::*;
use crate::builder::PrimitiveRunBuilder;
use crate::types::{Int16Type, Int32Type, Int8Type, UInt32Type};
use crate::{Array, Int16Array, Int32Array, StringArray};
+ fn build_input_array(approx_size: usize) -> Vec<Option<i32>> {
+ let mut seed: Vec<Option<i32>> = vec![
Review Comment:
Some comments might help explain what this is doing, i.e. creating runs of the same value sourced from `seed`
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow-rs] tustvold merged pull request #3603: Add ArrayAccessor, Iterator, Extend and benchmarks for RunArray
Posted by "tustvold (via GitHub)" <gi...@apache.org>.
tustvold merged PR #3603:
URL: https://github.com/apache/arrow-rs/pull/3603
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow-rs] tustvold commented on pull request #3603: Add ArrayAccessor, Iterator, Extend and benchmarks for RunArray
Posted by "tustvold (via GitHub)" <gi...@apache.org>.
tustvold commented on PR #3603:
URL: https://github.com/apache/arrow-rs/pull/3603#issuecomment-1416706779
Thank you again
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow-rs] viirya commented on a diff in pull request #3603: Add ArrayAccessor, Iterator, Extend and benchmarks for RunArray
Posted by "viirya (via GitHub)" <gi...@apache.org>.
viirya commented on code in PR #3603:
URL: https://github.com/apache/arrow-rs/pull/3603#discussion_r1089824552
##########
arrow-array/src/array/run_array.rs:
##########
@@ -274,15 +296,191 @@ pub type Int32RunArray = RunArray<Int32Type>;
/// ```
pub type Int64RunArray = RunArray<Int64Type>;
+/// A strongly-typed wrapper around a [`RunArray`] that implements [`ArrayAccessor`]
+/// and [`IntoIterator`] allowing fast access to its elements
+///
+/// ```
+/// use arrow_array::{RunArray, StringArray, types::Int32Type};
+///
+/// let orig = ["a", "b", "a", "b"];
+/// let ree_array = RunArray::<Int32Type>::from_iter(orig);
+///
+/// // `TypedRunArray` allows you to access the values directly
+/// let typed = ree_array.downcast_ref::<StringArray>().unwrap();
+///
+/// for (maybe_val, orig) in typed.into_iter().zip(orig) {
+/// assert_eq!(maybe_val.unwrap(), orig)
+/// }
+/// ```
+pub struct TypedRunArray<'a, R: RunEndIndexType, V> {
+ /// The run array
+ run_array: &'a RunArray<R>,
+
+ /// The values of the run_array
+ values: &'a V,
+}
+
+// Manually implement `Clone` to avoid `V: Clone` type constraint
+impl<'a, R: RunEndIndexType, V> Clone for TypedRunArray<'a, R, V> {
+ fn clone(&self) -> Self {
+ Self {
+ run_array: self.run_array,
+ values: self.values,
+ }
+ }
+}
+
+impl<'a, R: RunEndIndexType, V> Copy for TypedRunArray<'a, R, V> {}
+
+impl<'a, R: RunEndIndexType, V> std::fmt::Debug for TypedRunArray<'a, R, V> {
+ fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
+ writeln!(f, "TypedRunArray({:?})", self.run_array)
+ }
+}
+
+impl<'a, R: RunEndIndexType, V> TypedRunArray<'a, R, V> {
+ /// Returns the run_ends of this [`TypedRunArray`]
+ pub fn run_ends(&self) -> &'a PrimitiveArray<R> {
+ self.run_array.run_ends()
+ }
+
+ /// Returns the values of this [`TypedRunArray`]
+ pub fn values(&self) -> &'a V {
+ self.values
+ }
+
+ /// Returns index to the physcial array for the given index to the logical array.
+ /// Performs a binary search on the run_ends array for the input index.
+ #[inline]
+ pub fn get_physical_index(&self, logical_index: usize) -> Option<usize> {
Review Comment:
Is this for `TypedRunArray`? It doesn't need any typed information.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow-rs] askoa commented on a diff in pull request #3603: Add ArrayAccessor, Iterator, Extend and benchmarks for RunArray
Posted by "askoa (via GitHub)" <gi...@apache.org>.
askoa commented on code in PR #3603:
URL: https://github.com/apache/arrow-rs/pull/3603#discussion_r1089827707
##########
arrow-array/src/array/run_array.rs:
##########
@@ -504,4 +693,30 @@ mod tests {
let a = RunArray::<Int32Type>::from_iter(["32"]);
let _ = RunArray::<Int64Type>::from(a.into_data());
}
+
+ #[test]
+ fn test_ree_array_accessor() {
+ let input_array = build_input_array(256);
+
+ // Encode the input_array to ree_array
+ let mut builder =
+ PrimitiveRunBuilder::<Int16Type, Int32Type>::with_capacity(input_array.len());
+ builder.extend(input_array.clone().into_iter());
+ let run_array = builder.finish();
+ let typed = run_array
+ .downcast_ref::<PrimitiveArray<Int32Type>>()
+ .unwrap();
+
+ let len = input_array.len();
+ for i in 0..len {
+ let actual = typed.value(i);
+ match input_array[i] {
+ Some(val) => assert_eq!(val, actual),
+ None => {
Review Comment:
cool! I'll leave it as is for now.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow-rs] askoa commented on pull request #3603: Add ArrayAccessor, Iterator, Extend and benchmarks for RunArray
Posted by "askoa (via GitHub)" <gi...@apache.org>.
askoa commented on PR #3603:
URL: https://github.com/apache/arrow-rs/pull/3603#issuecomment-1404477861
Benchmark result for binary search based `ArrayAccessor`
The benchmark test definition is in https://github.com/askoa/arrow-rs/blob/run-extra-bench/arrow/benches/primitive_run_accessor.rs
```
primitive_run_accessor/(run_array_len:1000, physical_array_len:20)
time: [4.3275 µs 4.3478 µs 4.3688 µs]
Found 4 outliers among 100 measurements (4.00%)
3 (3.00%) high mild
1 (1.00%) high severe
primitive_run_accessor/(run_array_len:1000, physical_array_len:100)
time: [6.7064 µs 6.7404 µs 6.7796 µs]
Found 11 outliers among 100 measurements (11.00%)
1 (1.00%) low severe
3 (3.00%) low mild
4 (4.00%) high mild
3 (3.00%) high severe
primitive_run_accessor/(run_array_len:5000, physical_array_len:500)
time: [84.471 µs 84.617 µs 84.775 µs]
Found 4 outliers among 100 measurements (4.00%)
1 (1.00%) high mild
3 (3.00%) high severe
primitive_run_accessor/(run_array_len:10000, physical_array_len:1000)
time: [187.83 µs 189.35 µs 191.37 µs]
Found 12 outliers among 100 measurements (12.00%)
6 (6.00%) high mild
6 (6.00%) high severe
primitive_run_accessor/(run_array_len:50000, physical_array_len:5000)
time: [876.08 µs 883.37 µs 894.42 µs]
Found 3 outliers among 100 measurements (3.00%)
2 (2.00%) high mild
1 (1.00%) high severe
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org