use mz_expr::MirRelationExpr;
use crate::attribute::arity::Arity;
use crate::attribute::subtree_size::SubtreeSize;
use crate::attribute::{Attribute, DerivedAttributes, DerivedAttributesBuilder};
#[derive(Default)]
#[allow(missing_debug_implementations)]
pub struct UniqueKeys {
pub results: Vec<Vec<Vec<usize>>>,
}
impl Attribute for UniqueKeys {
type Value = Vec<Vec<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_keys = expr.keys_with_input_keys(
offsets
.iter()
.rev()
.map(|o| deps.get_results::<Arity>()[*o]),
offsets.iter().rev().map(|o| &self.results[*o]),
);
self.results.push(subtree_keys);
}
fn add_dependencies(builder: &mut DerivedAttributesBuilder)
where
Self: Sized,
{
builder.require(Arity::default());
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
}
}