|
| 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::AnyScalarFn; |
| 7 | +use vortex_array::arrays::ConstantArray; |
| 8 | +use vortex_array::arrays::ConstantVTable; |
| 9 | +use vortex_array::arrays::ScalarFnArray; |
| 10 | +use vortex_array::optimizer::rules::ArrayParentReduceRule; |
| 11 | +use vortex_array::optimizer::rules::Exact; |
| 12 | +use vortex_dtype::DType; |
| 13 | +use vortex_error::VortexResult; |
| 14 | + |
| 15 | +use crate::RunEndArray; |
| 16 | +use crate::RunEndVTable; |
| 17 | + |
| 18 | +/// A rule to push down scalar functions through run-end encoding into the values array. |
| 19 | +/// |
| 20 | +/// This only works if all other children of the scalar function array are constants. |
| 21 | +#[derive(Debug)] |
| 22 | +pub(crate) struct RunEndScalarFnRule; |
| 23 | + |
| 24 | +impl ArrayParentReduceRule<Exact<RunEndVTable>, AnyScalarFn> for RunEndScalarFnRule { |
| 25 | + fn child(&self) -> Exact<RunEndVTable> { |
| 26 | + Exact::from(&RunEndVTable) |
| 27 | + } |
| 28 | + |
| 29 | + fn parent(&self) -> AnyScalarFn { |
| 30 | + AnyScalarFn |
| 31 | + } |
| 32 | + |
| 33 | + fn reduce_parent( |
| 34 | + &self, |
| 35 | + run_end: &RunEndArray, |
| 36 | + parent: &ScalarFnArray, |
| 37 | + child_idx: usize, |
| 38 | + ) -> VortexResult<Option<ArrayRef>> { |
| 39 | + for (idx, child) in parent.children().iter().enumerate() { |
| 40 | + if idx == child_idx { |
| 41 | + // Skip ourselves |
| 42 | + continue; |
| 43 | + } |
| 44 | + |
| 45 | + if !child.is::<ConstantVTable>() { |
| 46 | + // We can only push down if all other children are constants |
| 47 | + return Ok(None); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + // TODO(ngates): relax this constraint and implement run-end decoding for all vector types. |
| 52 | + if !matches!(parent.dtype(), DType::Bool(_) | DType::Primitive(..)) { |
| 53 | + return Ok(None); |
| 54 | + } |
| 55 | + |
| 56 | + let values_len = run_end.values().len(); |
| 57 | + let mut new_children = parent.children(); |
| 58 | + for (idx, child) in new_children.iter_mut().enumerate() { |
| 59 | + if idx == child_idx { |
| 60 | + // Replace ourselves with run end values |
| 61 | + *child = run_end.values().clone(); |
| 62 | + continue; |
| 63 | + } |
| 64 | + |
| 65 | + // Replace other children with their constant scalar value with length adjusted |
| 66 | + // to the length of the run end values. |
| 67 | + let constant = child.as_::<ConstantVTable>(); |
| 68 | + *child = ConstantArray::new(constant.scalar().clone(), values_len).into_array(); |
| 69 | + } |
| 70 | + |
| 71 | + let new_values = |
| 72 | + ScalarFnArray::try_new(parent.scalar_fn().clone(), new_children, values_len)? |
| 73 | + .into_array(); |
| 74 | + |
| 75 | + Ok(Some( |
| 76 | + RunEndArray::try_new_offset_length( |
| 77 | + run_end.ends().clone(), |
| 78 | + new_values, |
| 79 | + run_end.offset(), |
| 80 | + run_end.len(), |
| 81 | + )? |
| 82 | + .into_array(), |
| 83 | + )) |
| 84 | + } |
| 85 | +} |
0 commit comments