use mz_expr::MirRelationExpr;
use crate::attribute::subtree_size::SubtreeSize;
use crate::attribute::{Attribute, DerivedAttributes, DerivedAttributesBuilder};
#[derive(Default)]
#[allow(missing_debug_implementations)]
pub struct Arity {
pub results: Vec<usize>,
}
impl Attribute for Arity {
type Value = usize;
fn derive(&mut self, expr: &MirRelationExpr, deps: &DerivedAttributes) {
let n = self.results.len();
let mut offsets = Vec::new();
let mut offset = 1;
for _ in 0..expr.num_inputs() {
offsets.push(n - offset);
offset += &deps.get_results::<SubtreeSize>()[n - offset];
}
let subtree_arity =
expr.arity_with_input_arities(offsets.into_iter().rev().map(|o| self.results[o]));
self.results.push(subtree_arity);
}
fn add_dependencies(builder: &mut DerivedAttributesBuilder)
where
Self: Sized,
{
builder.require(SubtreeSize::default());
}
fn get_results(&self) -> &Vec<Self::Value> {
&self.results
}
fn get_results_mut(&mut self) -> &mut Vec<Self::Value> {
&mut self.results
}
fn take(self) -> Vec<Self::Value> {
self.results
}
}