Jefffrey commented on code in PR #5573:
URL: https://github.com/apache/arrow-rs/pull/5573#discussion_r1545521930
##########
arrow-select/src/filter.rs:
##########
@@ -844,6 +888,72 @@ mod tests {
assert_eq!(9, d.value(1));
}
+ #[test]
+ fn test_filter_run_end_encoding_array() {
+ let run_ends = Int64Array::from(vec![2, 3, 8]);
+ let values = Int64Array::from(vec![7, -2, 9]);
+ let a = RunArray::try_new(&run_ends, &values).expect("Failed to create
RunArray");
+ let b = BooleanArray::from(vec![true, false, true, false, true, false,
true, false]);
+ let c = filter(&a, &b).unwrap();
+ let actual = c
+ .as_ref()
+ .as_any()
+ .downcast_ref::<RunArray<Int64Type>>()
+ .unwrap();
Review Comment:
Could use this
https://github.com/apache/arrow-rs/blob/9f36c883459405ecd9a5f4fdfa9a3317ab52302c/arrow-array/src/cast.rs#L514
##########
arrow-select/src/filter.rs:
##########
@@ -336,6 +336,13 @@ fn filter_array(values: &dyn Array, predicate:
&FilterPredicate) -> Result<Array
DataType::LargeBinary => {
Ok(Arc::new(filter_bytes(values.as_binary::<i64>(),
predicate)))
}
+ DataType::RunEndEncoded(_, _) => {
+ if let Some(ree_array) =
values.as_any().downcast_ref::<RunArray<Int64Type>>() {
+ Ok(Arc::new(filter_run_end_array(ree_array, predicate)?))
+ } else {
+ unimplemented!("Filter not supported for RunEndEncoded
type {:?}", values.data_type())
+ }
+ }
Review Comment:
Could use `downcast_run_array`:
https://github.com/apache/arrow-rs/blob/9f36c883459405ecd9a5f4fdfa9a3317ab52302c/arrow-array/src/cast.rs#L532-L577
Akin to how dictionary handles it below? This way don't need to limit the
index type to `Int64Type`
##########
arrow-select/src/filter.rs:
##########
@@ -368,6 +375,43 @@ fn filter_array(values: &dyn Array, predicate:
&FilterPredicate) -> Result<Array
}
}
+/// Filter a [`RunArray`] based on a [`FilterPredicate`]
+fn filter_run_end_array(
+ re_arr: &RunArray<Int64Type>,
+ pred: &FilterPredicate,
+) -> Result<RunArray<Int64Type>, ArrowError> {
+ let run_ends: &RunEndBuffer<i64> = re_arr.run_ends();
+ let mut values_filter = BooleanBufferBuilder::new(run_ends.len());
+ let mut new_run_ends = vec![0i64; run_ends.len()];
+
+ let mut start = 0i64;
+ let mut i = 0;
+ let filter_values = pred.filter.values();
+ let mut count = 0;
+ for end in run_ends.inner() {
+ let mut keep = false;
+ for pred in (start..*end).map(|i| unsafe {
filter_values.value_unchecked(i as usize) }) {
+ count += pred as i64;
+ keep |= pred
+ }
+ new_run_ends[i] = count;
+ i += keep as usize;
+ values_filter.append(keep);
+ start = *end;
+ }
+
+ new_run_ends.truncate(i);
+
+ if values_filter.is_empty() {
+ new_run_ends.clear();
+ }
Review Comment:
If you allocate the `new_run_ends` as just an empty Vec but with
`run_ends.len()` capacity and push to it, you probably won't need this part
##########
arrow-select/src/filter.rs:
##########
@@ -368,6 +375,43 @@ fn filter_array(values: &dyn Array, predicate:
&FilterPredicate) -> Result<Array
}
}
+/// Filter a [`RunArray`] based on a [`FilterPredicate`]
+fn filter_run_end_array(
+ re_arr: &RunArray<Int64Type>,
+ pred: &FilterPredicate,
+) -> Result<RunArray<Int64Type>, ArrowError> {
+ let run_ends: &RunEndBuffer<i64> = re_arr.run_ends();
+ let mut values_filter = BooleanBufferBuilder::new(run_ends.len());
+ let mut new_run_ends = vec![0i64; run_ends.len()];
+
+ let mut start = 0i64;
+ let mut i = 0;
+ let filter_values = pred.filter.values();
+ let mut count = 0;
+ for end in run_ends.inner() {
+ let mut keep = false;
+ for pred in (start..*end).map(|i| unsafe {
filter_values.value_unchecked(i as usize) }) {
Review Comment:
Maybe add a note about safety here for this unsafe block?
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]