|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +// SPDX-FileCopyrightText: Copyright the Vortex contributors |
| 3 | + |
| 4 | +use std::marker::PhantomData; |
| 5 | +use std::sync::Arc; |
| 6 | + |
| 7 | +use vortex_error::VortexResult; |
| 8 | +use vortex_utils::aliases::dash_map::DashMap; |
| 9 | + |
| 10 | +use crate::EncodingId; |
| 11 | +use crate::array::ArrayRef; |
| 12 | +use crate::array::transform::context::ArrayRuleContext; |
| 13 | +use crate::array::transform::rules::{ |
| 14 | + AnyParent, ArrayParentMatcher, ArrayParentReduceRule, ArrayReduceRule, |
| 15 | +}; |
| 16 | +use crate::vtable::VTable; |
| 17 | + |
| 18 | +/// Dynamic trait for array reduce rules |
| 19 | +pub trait DynArrayReduceRule: Send + Sync { |
| 20 | + fn reduce(&self, array: &ArrayRef, ctx: &ArrayRuleContext) -> VortexResult<Option<ArrayRef>>; |
| 21 | +} |
| 22 | + |
| 23 | +/// Dynamic trait for array parent reduce rules |
| 24 | +pub trait DynArrayParentReduceRule: Send + Sync { |
| 25 | + fn reduce_parent( |
| 26 | + &self, |
| 27 | + array: &ArrayRef, |
| 28 | + parent: &ArrayRef, |
| 29 | + child_idx: usize, |
| 30 | + ctx: &ArrayRuleContext, |
| 31 | + ) -> VortexResult<Option<ArrayRef>>; |
| 32 | +} |
| 33 | + |
| 34 | +/// Adapter for ArrayReduceRule |
| 35 | +struct ArrayReduceRuleAdapter<V: VTable, R> { |
| 36 | + rule: R, |
| 37 | + _phantom: PhantomData<V>, |
| 38 | +} |
| 39 | + |
| 40 | +/// Adapter for ArrayParentReduceRule |
| 41 | +struct ArrayParentReduceRuleAdapter<Child: VTable, Parent: ArrayParentMatcher, R> { |
| 42 | + rule: R, |
| 43 | + _phantom: PhantomData<(Child, Parent)>, |
| 44 | +} |
| 45 | + |
| 46 | +impl<V, R> DynArrayReduceRule for ArrayReduceRuleAdapter<V, R> |
| 47 | +where |
| 48 | + V: VTable, |
| 49 | + R: ArrayReduceRule<V>, |
| 50 | +{ |
| 51 | + fn reduce(&self, array: &ArrayRef, ctx: &ArrayRuleContext) -> VortexResult<Option<ArrayRef>> { |
| 52 | + let Some(view) = array.as_opt::<V>() else { |
| 53 | + return Ok(None); |
| 54 | + }; |
| 55 | + self.rule.reduce(view, ctx) |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +impl<Child, Parent, R> DynArrayParentReduceRule for ArrayParentReduceRuleAdapter<Child, Parent, R> |
| 60 | +where |
| 61 | + Child: VTable, |
| 62 | + Parent: ArrayParentMatcher, |
| 63 | + R: ArrayParentReduceRule<Child, Parent>, |
| 64 | +{ |
| 65 | + fn reduce_parent( |
| 66 | + &self, |
| 67 | + array: &ArrayRef, |
| 68 | + parent: &ArrayRef, |
| 69 | + child_idx: usize, |
| 70 | + ctx: &ArrayRuleContext, |
| 71 | + ) -> VortexResult<Option<ArrayRef>> { |
| 72 | + let Some(view) = array.as_opt::<Child>() else { |
| 73 | + return Ok(None); |
| 74 | + }; |
| 75 | + let Some(parent_view) = Parent::try_match(parent) else { |
| 76 | + return Ok(None); |
| 77 | + }; |
| 78 | + self.rule.reduce_parent(view, parent_view, child_idx, ctx) |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +/// Inner struct that holds all the rule registries. |
| 83 | +/// Wrapped in a single Arc by ArrayRewriteRuleRegistry for efficient cloning. |
| 84 | +#[derive(Default)] |
| 85 | +struct ArrayRewriteRuleRegistryInner { |
| 86 | + /// Reduce rules indexed by encoding ID |
| 87 | + reduce_rules: DashMap<EncodingId, Vec<Arc<dyn DynArrayReduceRule>>>, |
| 88 | + /// Parent reduce rules for specific parent types, indexed by (child_id, parent_id) |
| 89 | + parent_rules: DashMap<(EncodingId, EncodingId), Vec<Arc<dyn DynArrayParentReduceRule>>>, |
| 90 | + /// Wildcard parent rules (match any parent), indexed by child_id only |
| 91 | + any_parent_rules: DashMap<EncodingId, Vec<Arc<dyn DynArrayParentReduceRule>>>, |
| 92 | +} |
| 93 | + |
| 94 | +impl std::fmt::Debug for ArrayRewriteRuleRegistryInner { |
| 95 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 96 | + f.debug_struct("ArrayRewriteRuleRegistryInner") |
| 97 | + .field( |
| 98 | + "reduce_rules", |
| 99 | + &format!("{} encodings", self.reduce_rules.len()), |
| 100 | + ) |
| 101 | + .field( |
| 102 | + "parent_rules", |
| 103 | + &format!("{} pairs", self.parent_rules.len()), |
| 104 | + ) |
| 105 | + .field( |
| 106 | + "any_parent_rules", |
| 107 | + &format!("{} encodings", self.any_parent_rules.len()), |
| 108 | + ) |
| 109 | + .finish() |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +/// Registry of array rewrite rules. |
| 114 | +/// |
| 115 | +/// Stores rewrite rules indexed by the encoding ID they apply to. |
| 116 | +#[derive(Clone, Debug)] |
| 117 | +pub struct ArrayRewriteRuleRegistry { |
| 118 | + inner: Arc<ArrayRewriteRuleRegistryInner>, |
| 119 | +} |
| 120 | + |
| 121 | +impl Default for ArrayRewriteRuleRegistry { |
| 122 | + fn default() -> Self { |
| 123 | + Self { |
| 124 | + inner: Arc::new(ArrayRewriteRuleRegistryInner::default()), |
| 125 | + } |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +impl ArrayRewriteRuleRegistry { |
| 130 | + pub fn new() -> Self { |
| 131 | + Self::default() |
| 132 | + } |
| 133 | + |
| 134 | + /// Register a reduce rule for a specific array encoding. |
| 135 | + pub fn register_reduce_rule<V, R>(&self, encoding: &V::Encoding, rule: R) |
| 136 | + where |
| 137 | + V: VTable, |
| 138 | + R: 'static, |
| 139 | + R: ArrayReduceRule<V>, |
| 140 | + { |
| 141 | + let adapter = ArrayReduceRuleAdapter { |
| 142 | + rule, |
| 143 | + _phantom: PhantomData, |
| 144 | + }; |
| 145 | + let encoding_id = V::id(encoding); |
| 146 | + self.inner |
| 147 | + .reduce_rules |
| 148 | + .entry(encoding_id) |
| 149 | + .or_default() |
| 150 | + .push(Arc::new(adapter)); |
| 151 | + } |
| 152 | + |
| 153 | + /// Register a parent rule for a specific parent type. |
| 154 | + pub fn register_parent_rule<Child, Parent, R>( |
| 155 | + &self, |
| 156 | + child_encoding: &Child::Encoding, |
| 157 | + parent_encoding: &Parent::Encoding, |
| 158 | + rule: R, |
| 159 | + ) where |
| 160 | + Child: VTable, |
| 161 | + Parent: VTable, |
| 162 | + R: 'static, |
| 163 | + R: ArrayParentReduceRule<Child, Parent>, |
| 164 | + { |
| 165 | + let adapter = ArrayParentReduceRuleAdapter { |
| 166 | + rule, |
| 167 | + _phantom: PhantomData, |
| 168 | + }; |
| 169 | + let child_id = Child::id(child_encoding); |
| 170 | + let parent_id = Parent::id(parent_encoding); |
| 171 | + self.inner |
| 172 | + .parent_rules |
| 173 | + .entry((child_id, parent_id)) |
| 174 | + .or_default() |
| 175 | + .push(Arc::new(adapter)); |
| 176 | + } |
| 177 | + |
| 178 | + /// Register a parent rule that matches ANY parent type (wildcard). |
| 179 | + pub fn register_any_parent_rule<Child, R>(&self, child_encoding: &Child::Encoding, rule: R) |
| 180 | + where |
| 181 | + Child: VTable, |
| 182 | + R: 'static, |
| 183 | + R: ArrayParentReduceRule<Child, AnyParent>, |
| 184 | + { |
| 185 | + let adapter = ArrayParentReduceRuleAdapter { |
| 186 | + rule, |
| 187 | + _phantom: PhantomData, |
| 188 | + }; |
| 189 | + let child_id = Child::id(child_encoding); |
| 190 | + self.inner |
| 191 | + .any_parent_rules |
| 192 | + .entry(child_id) |
| 193 | + .or_default() |
| 194 | + .push(Arc::new(adapter)); |
| 195 | + } |
| 196 | + |
| 197 | + /// Execute a callback with all reduce rules for a given encoding ID. |
| 198 | + pub(crate) fn with_reduce_rules<F, R>(&self, id: &EncodingId, f: F) -> R |
| 199 | + where |
| 200 | + F: FnOnce(&mut dyn Iterator<Item = &dyn DynArrayReduceRule>) -> R, |
| 201 | + { |
| 202 | + f(&mut self |
| 203 | + .inner |
| 204 | + .reduce_rules |
| 205 | + .get(id) |
| 206 | + .iter() |
| 207 | + .flat_map(|v| v.value()) |
| 208 | + .map(|arc| arc.as_ref())) |
| 209 | + } |
| 210 | + |
| 211 | + /// Execute a callback with all parent reduce rules for a given child and parent encoding ID. |
| 212 | + /// |
| 213 | + /// Returns rules from both specific parent rules (if parent_id provided) and "any parent" wildcard rules. |
| 214 | + pub(crate) fn with_parent_rules<F, R>( |
| 215 | + &self, |
| 216 | + child_id: &EncodingId, |
| 217 | + parent_id: Option<&EncodingId>, |
| 218 | + f: F, |
| 219 | + ) -> R |
| 220 | + where |
| 221 | + F: FnOnce(&mut dyn Iterator<Item = &dyn DynArrayParentReduceRule>) -> R, |
| 222 | + { |
| 223 | + let specific_entry = parent_id.and_then(|pid| { |
| 224 | + self.inner |
| 225 | + .parent_rules |
| 226 | + .get(&(child_id.clone(), pid.clone())) |
| 227 | + }); |
| 228 | + let wildcard_entry = self.inner.any_parent_rules.get(child_id); |
| 229 | + |
| 230 | + f(&mut specific_entry |
| 231 | + .iter() |
| 232 | + .flat_map(|v| v.value()) |
| 233 | + .chain(wildcard_entry.iter().flat_map(|v| v.value())) |
| 234 | + .map(|arc| arc.as_ref())) |
| 235 | + } |
| 236 | +} |
0 commit comments