use super::super::PhysicalInput;
use crate::fx::{FxHashMap, FxHashSet};
use crate::statement_facts::SqlViewKeyDependencies;
mod alternatives;
pub(in super::super::super) use alternatives::{RowDependencies, RowDependency};
#[derive(Default)]
pub(in super::super) struct LateralDependencies {
pub(in super::super) all: RowDependencies,
shared: RowDependencies,
columns: FxHashMap<String, RowDependencies>,
physical_inputs: Vec<PhysicalInput>,
}
impl LateralDependencies {
pub(in super::super) fn with_columns(
shared: RowDependencies,
columns: FxHashMap<String, RowDependencies>,
) -> Self {
let mut all = shared.clone();
all.extend(columns.values().flatten().cloned());
Self {
all,
shared,
columns,
..Self::default()
}
}
pub(in super::super) fn whole(all: RowDependencies) -> Self {
Self {
shared: all.clone(),
all,
..Self::default()
}
}
pub(in super::super) fn cross_join(scope: &super::Scope, all: RowDependencies) -> Self {
let mut recorded = FxHashSet::default();
let mut shared = scope.unbound_lateral.clone();
for output in scope.lateral_outputs.values() {
recorded.extend(output.all.iter().cloned());
shared.extend(output.shared.iter().cloned());
}
let mut columns = scope.unbound_columns.clone();
for dependencies in columns.values() {
recorded.extend(dependencies.iter().cloned());
}
shared.extend(
all.iter()
.filter(|dependency| dependency.overflowed() || !recorded.contains(*dependency))
.cloned(),
);
let derived = scope
.derived_columns
.iter()
.chain(&scope.explicit_columns)
.flat_map(|(qualifier, labels)| labels.iter().map(move |label| (qualifier, label)));
let joined = scope.join_outputs.iter().flat_map(|(qualifier, output)| {
output
.known_columns
.iter()
.map(move |label| (qualifier, label))
});
for (qualifier, label) in derived.chain(joined) {
let dependencies = columns.entry(label.clone()).or_default();
if let Some(output) = scope.lateral_outputs.get(qualifier) {
dependencies.extend(output.for_column(Some(label)));
}
}
for input in &scope.physical_inputs {
for label in &input.column_aliases {
columns.entry(label.clone()).or_default();
}
}
Self {
all,
shared,
columns,
physical_inputs: scope.visible_physical_inputs().cloned().collect(),
}
}
pub(in super::super) fn for_column(
&self,
column: Option<&str>,
) -> impl Iterator<Item = RowDependency> + '_ {
let specific = column.and_then(|column| self.columns.get(column));
let guards = column
.filter(|_| specific.is_none() && !self.physical_inputs.is_empty())
.map(|column| {
self.physical_inputs
.iter()
.map(|input| input.column_condition(column, false))
.collect::<Vec<_>>()
})
.unwrap_or_default();
let (common, extra) = if specific.is_some() {
(&self.shared, None)
} else if !guards.is_empty() {
(&self.shared, Some(&self.all))
} else {
(&self.all, None)
};
common
.iter()
.cloned()
.chain(specific.into_iter().flatten().cloned())
.chain(
extra
.into_iter()
.flatten()
.filter(move |dep| !self.shared.contains(dep))
.cloned()
.map(move |dep| dep.guarded(&guards)),
)
}
pub(in super::super) fn independence(
&self,
column: &str,
index: usize,
) -> Option<SqlViewKeyDependencies> {
let dependencies: RowDependencies = self
.for_column(Some(column))
.filter(|dependency| dependency.index == index)
.collect();
let mut conditions = Vec::new();
for dependency in dependencies {
if dependency.conditions.is_empty() {
return None;
}
conditions.push(dependency.conditions);
}
conditions.sort_unstable();
conditions.dedup();
Some(conditions)
}
}
impl super::Scope {
pub(in super::super) fn record_unbound(
&mut self,
columns: Option<Vec<String>>,
output: Option<&LateralDependencies>,
) {
self.unbound_lateral.extend(
output
.into_iter()
.flat_map(|output| output.shared.iter().cloned()),
);
if columns.is_none() {
self.opaque_lateral_columns.extend(
output
.into_iter()
.flat_map(|output| output.all.iter().cloned()),
);
}
for column in columns.into_iter().flatten() {
let dependencies = self.unbound_columns.entry(column.clone()).or_default();
dependencies.extend(
output
.into_iter()
.flat_map(|output| output.for_column(Some(&column))),
);
}
}
}
#[cfg(all(test, any(nm_shard_all, nm_shard_2)))]
mod tests;