Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 90e0beb

Browse files
authored
Filter struct arrays (#767)
1 parent bb21fb1 commit 90e0beb

4 files changed

Lines changed: 40 additions & 13 deletions

File tree

vortex-array/src/array/bool/compute/filter.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use vortex_error::{vortex_err, VortexResult};
33

44
use crate::array::BoolArray;
55
use crate::compute::FilterFn;
6-
use crate::validity::filter_validity;
76
use crate::variants::BoolArrayTrait;
87
use crate::{Array, IntoArray};
98

@@ -15,7 +14,7 @@ impl FilterFn for BoolArray {
1514

1615
fn filter_select_bool(arr: &BoolArray, predicate: &Array) -> VortexResult<BoolArray> {
1716
predicate.with_dyn(|b| {
18-
let validity = filter_validity(arr.validity(), predicate)?;
17+
let validity = arr.validity().filter(predicate)?;
1918
let predicate = b.as_bool_array().ok_or(vortex_err!(
2019
NotImplemented: "as_bool_array",
2120
predicate.encoding().id()

vortex-array/src/array/primitive/compute/filter.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use vortex_error::{vortex_err, VortexResult};
33

44
use crate::array::primitive::PrimitiveArray;
55
use crate::compute::FilterFn;
6-
use crate::validity::filter_validity;
76
use crate::variants::BoolArrayTrait;
87
use crate::{Array, IntoArray};
98

@@ -18,7 +17,7 @@ fn filter_select_primitive(
1817
predicate: &Array,
1918
) -> VortexResult<PrimitiveArray> {
2019
predicate.with_dyn(|b| {
21-
let validity = filter_validity(arr.validity(), predicate)?;
20+
let validity = arr.validity().filter(predicate)?;
2221
let predicate = b.as_bool_array().ok_or_else(||vortex_err!(
2322
NotImplemented: "as_bool_array",
2423
predicate.encoding().id()

vortex-array/src/array/struct_/compute.rs

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
use itertools::Itertools;
2-
use vortex_error::VortexResult;
2+
use vortex_error::{vortex_err, VortexResult};
33
use vortex_scalar::Scalar;
44

55
use crate::array::struct_::StructArray;
66
use crate::compute::unary::{scalar_at, scalar_at_unchecked, ScalarAtFn};
7-
use crate::compute::{slice, take, ArrayCompute, SliceFn, TakeFn};
7+
use crate::compute::{filter, slice, take, ArrayCompute, FilterFn, SliceFn, TakeFn};
8+
use crate::stats::ArrayStatistics;
89
use crate::variants::StructArrayTrait;
910
use crate::{Array, ArrayDType, IntoArray};
1011

@@ -20,6 +21,10 @@ impl ArrayCompute for StructArray {
2021
fn take(&self) -> Option<&dyn TakeFn> {
2122
Some(self)
2223
}
24+
25+
fn filter(&self) -> Option<&dyn FilterFn> {
26+
Some(self)
27+
}
2328
}
2429

2530
impl ScalarAtFn for StructArray {
@@ -71,3 +76,25 @@ impl SliceFn for StructArray {
7176
.map(|a| a.into_array())
7277
}
7378
}
79+
80+
impl FilterFn for StructArray {
81+
fn filter(&self, predicate: &Array) -> VortexResult<Array> {
82+
let fields = self
83+
.children()
84+
.map(|field| filter(&field, predicate))
85+
.try_collect()?;
86+
87+
let predicate_true_count = predicate
88+
.statistics()
89+
.compute_true_count()
90+
.ok_or_else(|| vortex_err!("Predicate should always be a boolean array"))?;
91+
92+
Self::try_new(
93+
self.names().clone(),
94+
fields,
95+
predicate_true_count,
96+
self.validity().filter(predicate)?,
97+
)
98+
.map(|a| a.into_array())
99+
}
100+
}

vortex-array/src/validity.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,15 @@ impl Validity {
120120
}
121121
}
122122

123+
pub fn filter(&self, predicate: &Array) -> VortexResult<Self> {
124+
match self {
125+
v @ (Validity::NonNullable | Validity::AllValid | Validity::AllInvalid) => {
126+
Ok(v.clone())
127+
}
128+
Validity::Array(arr) => Ok(Validity::Array(filter(arr, predicate)?)),
129+
}
130+
}
131+
123132
pub fn to_logical(&self, length: usize) -> LogicalValidity {
124133
match self {
125134
Self::NonNullable => LogicalValidity::AllValid(length),
@@ -328,10 +337,3 @@ impl IntoArray for LogicalValidity {
328337
}
329338
}
330339
}
331-
332-
pub fn filter_validity(validity: Validity, predicate: &Array) -> VortexResult<Validity> {
333-
match validity {
334-
v @ (Validity::NonNullable | Validity::AllValid | Validity::AllInvalid) => Ok(v),
335-
Validity::Array(arr) => Ok(Validity::Array(filter(&arr, predicate)?)),
336-
}
337-
}

0 commit comments

Comments
 (0)