|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +// SPDX-FileCopyrightText: Copyright the Vortex contributors |
| 3 | + |
| 4 | +use vortex_array::ArrayRef; |
| 5 | +use vortex_array::IntoArray; |
| 6 | +use vortex_array::arrays::ConstantArray; |
| 7 | +use vortex_array::arrays::ConstantVTable; |
| 8 | +use vortex_array::arrays::FilterArray; |
| 9 | +use vortex_array::arrays::FilterVTable; |
| 10 | +use vortex_array::matchers::Exact; |
| 11 | +use vortex_array::optimizer::ArrayOptimizer; |
| 12 | +use vortex_array::optimizer::rules::ArrayParentReduceRule; |
| 13 | +use vortex_array::optimizer::rules::ParentRuleSet; |
| 14 | +use vortex_error::VortexExpect; |
| 15 | +use vortex_error::VortexResult; |
| 16 | + |
| 17 | +use crate::DateTimePartsArray; |
| 18 | +use crate::DateTimePartsVTable; |
| 19 | + |
| 20 | +pub(crate) const PARENT_RULES: ParentRuleSet<DateTimePartsVTable> = |
| 21 | + ParentRuleSet::new(&[ParentRuleSet::lift(&DTPFilterPushDownRule)]); |
| 22 | + |
| 23 | +/// Push the filter into the days column of a date time parts, we could extend this to other fields |
| 24 | +/// but its less clear if that is beneficial. |
| 25 | +#[derive(Debug)] |
| 26 | +struct DTPFilterPushDownRule; |
| 27 | + |
| 28 | +impl ArrayParentReduceRule<DateTimePartsVTable> for DTPFilterPushDownRule { |
| 29 | + type Parent = Exact<FilterVTable>; |
| 30 | + |
| 31 | + fn parent(&self) -> Self::Parent { |
| 32 | + Exact::from(&FilterVTable) |
| 33 | + } |
| 34 | + |
| 35 | + fn reduce_parent( |
| 36 | + &self, |
| 37 | + child: &DateTimePartsArray, |
| 38 | + parent: &FilterArray, |
| 39 | + child_idx: usize, |
| 40 | + ) -> VortexResult<Option<ArrayRef>> { |
| 41 | + if child_idx != 0 { |
| 42 | + return Ok(None); |
| 43 | + } |
| 44 | + |
| 45 | + if !child.seconds().is::<ConstantVTable>() || !child.subseconds().is::<ConstantVTable>() { |
| 46 | + return Ok(None); |
| 47 | + } |
| 48 | + |
| 49 | + DateTimePartsArray::try_new( |
| 50 | + child.dtype().clone(), |
| 51 | + FilterArray::new(child.days().clone(), parent.filter_mask().clone()) |
| 52 | + .into_array() |
| 53 | + .optimize()?, |
| 54 | + ConstantArray::new( |
| 55 | + child.seconds().as_constant().vortex_expect("constant"), |
| 56 | + parent.filter_mask().true_count(), |
| 57 | + ) |
| 58 | + .into_array(), |
| 59 | + ConstantArray::new( |
| 60 | + child.subseconds().as_constant().vortex_expect("constant"), |
| 61 | + parent.filter_mask().true_count(), |
| 62 | + ) |
| 63 | + .into_array(), |
| 64 | + ) |
| 65 | + .map(|x| Some(x.into_array())) |
| 66 | + } |
| 67 | +} |
0 commit comments