From 4a02dba3f7604c700942c8588a8db7f1d152bd87 Mon Sep 17 00:00:00 2001 From: Robert Kruszewski Date: Mon, 16 Sep 2024 15:05:53 +0100 Subject: [PATCH] Handle filtering empty struct arrays (#827) --- vortex-array/src/array/struct_/compute.rs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/vortex-array/src/array/struct_/compute.rs b/vortex-array/src/array/struct_/compute.rs index 9772c8e185..516252f388 100644 --- a/vortex-array/src/array/struct_/compute.rs +++ b/vortex-array/src/array/struct_/compute.rs @@ -5,6 +5,7 @@ use vortex_scalar::Scalar; use crate::array::struct_::StructArray; use crate::compute::unary::{scalar_at, scalar_at_unchecked, ScalarAtFn}; use crate::compute::{filter, slice, take, ArrayCompute, FilterFn, SliceFn, TakeFn}; +use crate::stats::ArrayStatistics; use crate::variants::StructArrayTrait; use crate::{Array, ArrayDType, IntoArray}; @@ -85,6 +86,7 @@ impl FilterFn for StructArray { let length = fields .first() .map(|a| a.len()) + .or_else(|| predicate.statistics().compute_true_count()) .ok_or_else(|| vortex_err!("Struct arrays should have at least one field"))?; Self::try_new( @@ -96,3 +98,22 @@ impl FilterFn for StructArray { .map(|a| a.into_array()) } } + +#[cfg(test)] +mod tests { + use crate::array::{BoolArray, StructArray}; + use crate::compute::filter; + use crate::validity::Validity; + use crate::IntoArray; + + #[test] + fn filter_empty_struct() { + let struct_arr = + StructArray::try_new(vec![].into(), vec![], 10, Validity::NonNullable).unwrap(); + let mask = vec![ + false, true, false, true, false, true, false, true, false, true, + ]; + let filtered = filter(struct_arr.as_ref(), &BoolArray::from(mask).into_array()).unwrap(); + assert_eq!(filtered.len(), 5); + } +}