use crate::fx::FxHashMap;
use crate::statement_facts::SqlViewKeyCondition;
use std::collections::hash_map::{IntoValues, Values};
use std::iter::Flatten;
const MAX_ALTERNATIVES_PER_CANDIDATE: usize = 32;
#[derive(Clone, Debug, Default)]
pub(in super::super::super::super) struct RowDependencies {
by_candidate: FxHashMap<usize, Vec<RowDependency>>,
}
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub(in super::super::super::super) struct RowDependency {
pub(in super::super::super) index: usize,
pub(in super::super::super) conditions: Vec<SqlViewKeyCondition>,
overflowed: bool,
}
impl From<usize> for RowDependency {
fn from(index: usize) -> Self {
Self {
index,
conditions: Vec::new(),
overflowed: false,
}
}
}
impl RowDependency {
pub(super) fn overflowed(&self) -> bool {
self.overflowed
}
pub(in super::super::super) fn guarded(mut self, conditions: &[SqlViewKeyCondition]) -> Self {
if !self.overflowed && !conditions.is_empty() {
self.conditions.extend_from_slice(conditions);
self.conditions.sort_unstable();
self.conditions.dedup();
}
self
}
}
impl RowDependencies {
pub(in super::super::super) fn insert(&mut self, dependency: RowDependency) {
if dependency
.conditions
.windows(2)
.any(|pair| contradictory(&pair[0], &pair[1]))
{
return;
}
let alternatives = self.by_candidate.entry(dependency.index).or_default();
if alternatives.contains(&dependency)
|| (!dependency.conditions.is_empty()
&& alternatives
.iter()
.any(|alternative| alternative.overflowed))
{
return;
}
if dependency.overflowed || alternatives.len() == MAX_ALTERNATIVES_PER_CANDIDATE {
alternatives.retain(|alternative| alternative.conditions.is_empty());
alternatives.push(RowDependency {
index: dependency.index,
conditions: vec![SqlViewKeyCondition::Unknown],
overflowed: true,
});
if !dependency.conditions.is_empty() {
return;
}
}
alternatives.push(dependency);
}
pub(in super::super::super) fn iter(&self) -> impl Iterator<Item = &RowDependency> {
self.into_iter()
}
pub(in super::super::super) fn contains(&self, dependency: &RowDependency) -> bool {
self.by_candidate
.get(&dependency.index)
.is_some_and(|alternatives| alternatives.contains(dependency))
}
pub(in super::super::super) fn is_empty(&self) -> bool {
self.by_candidate.is_empty()
}
pub(in super::super::super) fn clear(&mut self) {
self.by_candidate.clear();
}
}
impl IntoIterator for RowDependencies {
type Item = RowDependency;
type IntoIter = Flatten<IntoValues<usize, Vec<RowDependency>>>;
fn into_iter(self) -> Self::IntoIter {
self.by_candidate.into_values().flatten()
}
}
impl<'a> IntoIterator for &'a RowDependencies {
type Item = &'a RowDependency;
type IntoIter = Flatten<Values<'a, usize, Vec<RowDependency>>>;
fn into_iter(self) -> Self::IntoIter {
self.by_candidate.values().flatten()
}
}
impl Extend<RowDependency> for RowDependencies {
fn extend<T: IntoIterator<Item = RowDependency>>(&mut self, iter: T) {
for dependency in iter {
self.insert(dependency);
}
}
}
impl FromIterator<RowDependency> for RowDependencies {
fn from_iter<T: IntoIterator<Item = RowDependency>>(iter: T) -> Self {
let mut dependencies = Self::default();
dependencies.extend(iter);
dependencies
}
}
fn contradictory(left: &SqlViewKeyCondition, right: &SqlViewKeyCondition) -> bool {
matches!(
(left, right),
(
SqlViewKeyCondition::Column { peer, column, present: false },
SqlViewKeyCondition::Column { peer: other_peer, column: other_column, present: true },
) if peer == other_peer && column == other_column
) || matches!(
(left, right),
(SqlViewKeyCondition::Schema(left), SqlViewKeyCondition::Schema(right))
if left.relation == right.relation
&& left.matches_default_schema != right.matches_default_schema
)
}
#[cfg(test)]
mod tests;