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

Filter struct arrays #767

Merged
merged 3 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions vortex-array/src/array/bool/compute/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use vortex_error::{vortex_err, VortexResult};

use crate::array::BoolArray;
use crate::compute::FilterFn;
use crate::validity::filter_validity;
use crate::variants::BoolArrayTrait;
use crate::{Array, IntoArray};

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

fn filter_select_bool(arr: &BoolArray, predicate: &Array) -> VortexResult<BoolArray> {
predicate.with_dyn(|b| {
let validity = filter_validity(arr.validity(), predicate)?;
let validity = arr.validity().filter(predicate)?;
let predicate = b.as_bool_array().ok_or(vortex_err!(
NotImplemented: "as_bool_array",
predicate.encoding().id()
Expand Down
3 changes: 1 addition & 2 deletions vortex-array/src/array/primitive/compute/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use vortex_error::{vortex_err, VortexResult};

use crate::array::primitive::PrimitiveArray;
use crate::compute::FilterFn;
use crate::validity::filter_validity;
use crate::variants::BoolArrayTrait;
use crate::{Array, IntoArray};

Expand All @@ -18,7 +17,7 @@ fn filter_select_primitive(
predicate: &Array,
) -> VortexResult<PrimitiveArray> {
predicate.with_dyn(|b| {
let validity = filter_validity(arr.validity(), predicate)?;
let validity = arr.validity().filter(predicate)?;
let predicate = b.as_bool_array().ok_or_else(||vortex_err!(
NotImplemented: "as_bool_array",
predicate.encoding().id()
Expand Down
31 changes: 29 additions & 2 deletions vortex-array/src/array/struct_/compute.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use itertools::Itertools;
use vortex_error::VortexResult;
use vortex_error::{vortex_err, VortexResult};
use vortex_scalar::Scalar;

use crate::array::struct_::StructArray;
use crate::compute::unary::{scalar_at, scalar_at_unchecked, ScalarAtFn};
use crate::compute::{slice, take, ArrayCompute, SliceFn, TakeFn};
use crate::compute::{filter, slice, take, ArrayCompute, FilterFn, SliceFn, TakeFn};
use crate::stats::ArrayStatistics;
use crate::variants::StructArrayTrait;
use crate::{Array, ArrayDType, IntoArray};

Expand All @@ -20,6 +21,10 @@ impl ArrayCompute for StructArray {
fn take(&self) -> Option<&dyn TakeFn> {
Some(self)
}

fn filter(&self) -> Option<&dyn FilterFn> {
Some(self)
}
}

impl ScalarAtFn for StructArray {
Expand Down Expand Up @@ -71,3 +76,25 @@ impl SliceFn for StructArray {
.map(|a| a.into_array())
}
}

impl FilterFn for StructArray {
fn filter(&self, predicate: &Array) -> VortexResult<Array> {
let fields = self
.children()
.map(|field| filter(&field, predicate))
.try_collect()?;

let predicate_true_count = predicate
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it worth doing this only if you have no fields? fields.first().map(|a| a.len()).or_else(...)

Copy link
Member

Choose a reason for hiding this comment

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

yes, I noticed this is what Arrow does as well

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 thought I pushed that, I'll add that to another PR

.statistics()
.compute_true_count()
.ok_or_else(|| vortex_err!("Predicate should always be a boolean array"))?;

Self::try_new(
self.names().clone(),
fields,
predicate_true_count,
self.validity().filter(predicate)?,
)
.map(|a| a.into_array())
}
}
16 changes: 9 additions & 7 deletions vortex-array/src/validity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,15 @@ impl Validity {
}
}

pub fn filter(&self, predicate: &Array) -> VortexResult<Self> {
match self {
v @ (Validity::NonNullable | Validity::AllValid | Validity::AllInvalid) => {
Ok(v.clone())
}
Validity::Array(arr) => Ok(Validity::Array(filter(arr, predicate)?)),
}
}

pub fn to_logical(&self, length: usize) -> LogicalValidity {
match self {
Self::NonNullable => LogicalValidity::AllValid(length),
Expand Down Expand Up @@ -328,10 +337,3 @@ impl IntoArray for LogicalValidity {
}
}
}

pub fn filter_validity(validity: Validity, predicate: &Array) -> VortexResult<Validity> {
match validity {
v @ (Validity::NonNullable | Validity::AllValid | Validity::AllInvalid) => Ok(v),
Validity::Array(arr) => Ok(Validity::Array(filter(&arr, predicate)?)),
}
}
Loading