|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +// SPDX-FileCopyrightText: Copyright the Vortex contributors |
| 3 | + |
| 4 | +use vortex_error::VortexResult; |
| 5 | + |
| 6 | +use crate::Array; |
| 7 | +use crate::ArrayEq; |
| 8 | +use crate::ArrayRef; |
| 9 | +use crate::IntoArray; |
| 10 | +use crate::Precision; |
| 11 | +use crate::arrays::AnyScalarFn; |
| 12 | +use crate::arrays::ConstantArray; |
| 13 | +use crate::arrays::ConstantVTable; |
| 14 | +use crate::arrays::DictArray; |
| 15 | +use crate::arrays::DictVTable; |
| 16 | +use crate::arrays::ScalarFnArray; |
| 17 | +use crate::optimizer::ArrayOptimizer; |
| 18 | +use crate::optimizer::rules::ArrayParentReduceRule; |
| 19 | +use crate::optimizer::rules::ParentRuleSet; |
| 20 | + |
| 21 | +pub(super) const PARENT_RULES: ParentRuleSet<DictVTable> = ParentRuleSet::new(&[ |
| 22 | + ParentRuleSet::lift(&DictionaryScalarFnValuesPushDownRule), |
| 23 | + ParentRuleSet::lift(&DictionaryScalarFnCodesPullUpRule), |
| 24 | +]); |
| 25 | + |
| 26 | +/// Push down a scalar function to run only over the values of a dictionary array. |
| 27 | +#[derive(Debug)] |
| 28 | +struct DictionaryScalarFnValuesPushDownRule; |
| 29 | + |
| 30 | +impl ArrayParentReduceRule<DictVTable> for DictionaryScalarFnValuesPushDownRule { |
| 31 | + type Parent = AnyScalarFn; |
| 32 | + |
| 33 | + fn parent(&self) -> Self::Parent { |
| 34 | + AnyScalarFn |
| 35 | + } |
| 36 | + |
| 37 | + fn reduce_parent( |
| 38 | + &self, |
| 39 | + array: &DictArray, |
| 40 | + parent: &ScalarFnArray, |
| 41 | + child_idx: usize, |
| 42 | + ) -> VortexResult<Option<ArrayRef>> { |
| 43 | + // Check that the scalar function can actually be pushed down. |
| 44 | + let sig = parent.scalar_fn().signature(); |
| 45 | + |
| 46 | + // If the scalar function is fallible, we cannot push it down since it may fail over a |
| 47 | + // value that isn't referenced by any code. |
| 48 | + if !array.all_values_referenced && sig.is_fallible() { |
| 49 | + tracing::trace!( |
| 50 | + "Not pushing down fallible scalar function {} over dictionary with sparse codes {}", |
| 51 | + parent.scalar_fn(), |
| 52 | + array.display_tree(), |
| 53 | + ); |
| 54 | + return Ok(None); |
| 55 | + } |
| 56 | + |
| 57 | + // Check that all siblings are constant |
| 58 | + // TODO(ngates): we can also support other dictionaries if the values are the same! |
| 59 | + if !parent |
| 60 | + .children() |
| 61 | + .iter() |
| 62 | + .enumerate() |
| 63 | + .all(|(idx, c)| idx == child_idx || c.is::<ConstantVTable>()) |
| 64 | + { |
| 65 | + return Ok(None); |
| 66 | + } |
| 67 | + |
| 68 | + // If the scalar function is null-sensitive, then we cannot push it down to values if |
| 69 | + // we have any nulls in the codes. |
| 70 | + if array.codes.dtype().is_nullable() && !array.codes.all_valid() && sig.is_null_sensitive() |
| 71 | + { |
| 72 | + tracing::trace!( |
| 73 | + "Not pushing down null-sensitive scalar function {} over dictionary with null codes {}", |
| 74 | + parent.scalar_fn(), |
| 75 | + array.display_tree(), |
| 76 | + ); |
| 77 | + return Ok(None); |
| 78 | + } |
| 79 | + |
| 80 | + // Now we push the parent scalar function into the dictionary values. |
| 81 | + let values_len = array.values().len(); |
| 82 | + let mut new_children = Vec::with_capacity(parent.children().len()); |
| 83 | + for (idx, child) in parent.children().iter().enumerate() { |
| 84 | + if idx == child_idx { |
| 85 | + new_children.push(array.values().clone()); |
| 86 | + } else { |
| 87 | + let scalar = child.as_::<ConstantVTable>().scalar().clone(); |
| 88 | + new_children.push(ConstantArray::new(scalar, values_len).into_array()); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + let new_values = |
| 93 | + ScalarFnArray::try_new(parent.scalar_fn().clone(), new_children, values_len)? |
| 94 | + .into_array() |
| 95 | + .optimize()?; |
| 96 | + |
| 97 | + let new_dict = |
| 98 | + unsafe { DictArray::new_unchecked(array.codes().clone(), new_values) }.into_array(); |
| 99 | + |
| 100 | + Ok(Some(new_dict)) |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +#[derive(Debug)] |
| 105 | +struct DictionaryScalarFnCodesPullUpRule; |
| 106 | + |
| 107 | +impl ArrayParentReduceRule<DictVTable> for DictionaryScalarFnCodesPullUpRule { |
| 108 | + type Parent = AnyScalarFn; |
| 109 | + |
| 110 | + fn parent(&self) -> Self::Parent { |
| 111 | + AnyScalarFn |
| 112 | + } |
| 113 | + |
| 114 | + fn reduce_parent( |
| 115 | + &self, |
| 116 | + array: &DictArray, |
| 117 | + parent: &ScalarFnArray, |
| 118 | + child_idx: usize, |
| 119 | + ) -> VortexResult<Option<ArrayRef>> { |
| 120 | + // Check that all siblings are dictionaries with the same codes as us. |
| 121 | + if !parent.children().iter().enumerate().all(|(idx, c)| { |
| 122 | + idx == child_idx |
| 123 | + || c.as_opt::<DictVTable>().is_some_and(|c| { |
| 124 | + c.values().len() == array.values().len() |
| 125 | + && c.codes().array_eq(array.codes(), Precision::Value) |
| 126 | + }) |
| 127 | + }) { |
| 128 | + return Ok(None); |
| 129 | + } |
| 130 | + |
| 131 | + let mut new_children = Vec::with_capacity(parent.children().len()); |
| 132 | + for (idx, child) in parent.children().iter().enumerate() { |
| 133 | + if idx == child_idx { |
| 134 | + new_children.push(array.values().clone()); |
| 135 | + } else { |
| 136 | + new_children.push(child.as_::<DictVTable>().values().clone()); |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + let new_values = |
| 141 | + ScalarFnArray::try_new(parent.scalar_fn().clone(), new_children, array.values.len())? |
| 142 | + .into_array() |
| 143 | + .optimize()?; |
| 144 | + |
| 145 | + let new_dict = |
| 146 | + unsafe { DictArray::new_unchecked(array.codes().clone(), new_values) }.into_array(); |
| 147 | + |
| 148 | + Ok(Some(new_dict)) |
| 149 | + } |
| 150 | +} |
0 commit comments