use std::fmt::{Display, Formatter};
use std::ops::Deref;
use std::vec::IntoIter;
use crate::utils::{merge_and_order_indices, set_difference};
use crate::{DFSchema, HashSet, JoinType, NullEquality};
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Hash)]
pub enum Constraint {
PrimaryKey(Vec<usize>),
Unique(Vec<usize>),
}
#[derive(Clone, Debug, Default, Eq, Hash, PartialEq, PartialOrd)]
pub struct Constraints {
inner: Vec<Constraint>,
}
impl Constraints {
pub fn new_unverified(constraints: Vec<Constraint>) -> Self {
Self { inner: constraints }
}
pub fn extend(&mut self, other: Constraints) {
self.inner.extend(other.inner);
}
pub fn project(&self, proj_indices: &[usize]) -> Option<Self> {
let projected = self
.inner
.iter()
.filter_map(|constraint| {
match constraint {
Constraint::PrimaryKey(indices) => {
let new_indices =
update_elements_with_matching_indices(indices, proj_indices);
(new_indices.len() == indices.len())
.then_some(Constraint::PrimaryKey(new_indices))
}
Constraint::Unique(indices) => {
let new_indices =
update_elements_with_matching_indices(indices, proj_indices);
(new_indices.len() == indices.len())
.then_some(Constraint::Unique(new_indices))
}
}
})
.collect::<Vec<_>>();
(!projected.is_empty()).then_some(Constraints::new_unverified(projected))
}
}
impl IntoIterator for Constraints {
type Item = Constraint;
type IntoIter = IntoIter<Self::Item>;
fn into_iter(self) -> Self::IntoIter {
self.inner.into_iter()
}
}
impl Display for Constraints {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
let pk = self
.inner
.iter()
.map(|c| format!("{c:?}"))
.collect::<Vec<_>>();
let pk = pk.join(", ");
write!(f, "constraints=[{pk}]")
}
}
impl Deref for Constraints {
type Target = [Constraint];
fn deref(&self) -> &Self::Target {
self.inner.as_slice()
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FunctionalDependence {
pub source_indices: Vec<usize>,
pub target_indices: Vec<usize>,
pub nullable: bool,
pub null_equality: NullEquality,
pub mode: Dependency,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Dependency {
Single,
Multi,
}
impl FunctionalDependence {
pub fn new(
source_indices: Vec<usize>,
target_indices: Vec<usize>,
nullable: bool,
) -> Self {
Self {
source_indices,
target_indices,
nullable,
null_equality: NullEquality::NullEqualsNothing,
mode: Dependency::Multi,
}
}
pub fn with_mode(mut self, mode: Dependency) -> Self {
self.mode = mode;
self
}
pub fn with_null_equality(mut self, null_equality: NullEquality) -> Self {
self.null_equality = null_equality;
self
}
pub fn is_valid_across_nulls(&self, schema: &DFSchema) -> bool {
self.null_equality == NullEquality::NullEqualsNull
|| !self.nullable
|| self
.source_indices
.iter()
.all(|&source_idx| !schema.field(source_idx).is_nullable())
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FunctionalDependencies {
deps: Vec<FunctionalDependence>,
}
impl FunctionalDependencies {
pub fn empty() -> Self {
Self { deps: vec![] }
}
pub fn new(dependencies: Vec<FunctionalDependence>) -> Self {
Self { deps: dependencies }
}
pub fn new_from_constraints(
constraints: Option<&Constraints>,
n_field: usize,
) -> Self {
if let Some(Constraints { inner: constraints }) = constraints {
let dependencies = constraints
.iter()
.map(|constraint| {
let dependency = match constraint {
Constraint::PrimaryKey(indices) => FunctionalDependence::new(
indices.to_vec(),
(0..n_field).collect::<Vec<_>>(),
false,
),
Constraint::Unique(indices) => FunctionalDependence::new(
indices.to_vec(),
(0..n_field).collect::<Vec<_>>(),
true,
),
};
dependency.with_mode(Dependency::Single)
})
.collect::<Vec<_>>();
Self::new(dependencies)
} else {
Self::empty()
}
}
pub fn with_dependency(mut self, mode: Dependency) -> Self {
self.deps.iter_mut().for_each(|item| item.mode = mode);
self
}
pub fn extend(&mut self, other: FunctionalDependencies) {
self.deps.extend(other.deps);
}
pub fn is_valid(&self, n_field: usize) -> bool {
self.deps.iter().all(
|FunctionalDependence {
source_indices,
target_indices,
..
}| {
source_indices
.iter()
.max()
.map(|&max_index| max_index < n_field)
.unwrap_or(true)
&& target_indices
.iter()
.max()
.map(|&max_index| max_index < n_field)
.unwrap_or(true)
},
)
}
pub fn add_offset(&mut self, offset: usize) {
self.deps.iter_mut().for_each(
|FunctionalDependence {
source_indices,
target_indices,
..
}| {
*source_indices = add_offset_to_vec(source_indices, offset);
*target_indices = add_offset_to_vec(target_indices, offset);
},
)
}
pub fn project_functional_dependencies(
&self,
proj_indices: &[usize],
n_out: usize,
) -> FunctionalDependencies {
let mut projected_func_dependencies = Vec::with_capacity(self.deps.len());
for FunctionalDependence {
source_indices,
target_indices,
nullable,
null_equality,
mode,
} in &self.deps
{
let new_source_indices =
update_elements_with_matching_indices(source_indices, proj_indices);
let new_target_indices = if *mode == Dependency::Single {
(0..n_out).collect()
} else {
update_elements_with_matching_indices(target_indices, proj_indices)
};
if new_source_indices.len() == source_indices.len() {
let new_func_dependence = FunctionalDependence::new(
new_source_indices,
new_target_indices,
*nullable,
)
.with_mode(*mode)
.with_null_equality(*null_equality);
projected_func_dependencies.push(new_func_dependence);
}
}
FunctionalDependencies::new(projected_func_dependencies)
}
pub fn join(
&self,
other: &FunctionalDependencies,
join_type: &JoinType,
left_cols_len: usize,
) -> FunctionalDependencies {
let mut right_func_dependencies = other.clone();
let mut left_func_dependencies = self.clone();
match join_type {
JoinType::Inner | JoinType::Left | JoinType::Right => {
right_func_dependencies.add_offset(left_cols_len);
left_func_dependencies =
left_func_dependencies.with_dependency(Dependency::Multi);
right_func_dependencies =
right_func_dependencies.with_dependency(Dependency::Multi);
if *join_type == JoinType::Left {
right_func_dependencies.downgrade_dependencies();
} else if *join_type == JoinType::Right {
left_func_dependencies.downgrade_dependencies();
}
left_func_dependencies.extend(right_func_dependencies);
left_func_dependencies
}
JoinType::LeftSemi | JoinType::LeftAnti | JoinType::LeftMark => {
left_func_dependencies
}
JoinType::RightSemi | JoinType::RightAnti | JoinType::RightMark => {
right_func_dependencies
}
JoinType::Full => {
FunctionalDependencies::empty()
}
}
}
fn downgrade_dependencies(&mut self) {
self.deps.retain(|item| !item.nullable);
self.deps.iter_mut().for_each(|item| {
item.nullable = true;
item.null_equality = NullEquality::NullEqualsNull;
});
}
pub fn extend_target_indices(&mut self, n_out: usize) {
self.deps.iter_mut().for_each(
|FunctionalDependence {
mode,
target_indices,
..
}| {
if *mode == Dependency::Single {
*target_indices = (0..n_out).collect::<Vec<_>>();
}
},
)
}
}
impl Deref for FunctionalDependencies {
type Target = [FunctionalDependence];
fn deref(&self) -> &Self::Target {
self.deps.as_slice()
}
}
pub fn aggregate_functional_dependencies(
aggr_input_schema: &DFSchema,
group_by_input_indices: &[Option<usize>],
aggr_schema: &DFSchema,
) -> FunctionalDependencies {
let mut aggregate_func_dependencies = vec![];
let aggr_fields = aggr_schema.fields();
let target_indices = (0..aggr_schema.fields().len()).collect::<Vec<_>>();
let func_dependencies = aggr_input_schema.functional_dependencies();
if !func_dependencies.is_empty() {
let existing_target_indices = get_target_indices_from_dependencies(
func_dependencies.iter(),
group_by_input_indices,
);
for FunctionalDependence {
source_indices,
nullable,
null_equality,
mode,
..
} in &func_dependencies.deps
{
let mut new_source_indices = vec![];
let mut new_source_input_indices = vec![];
for (idx, input_idx) in group_by_input_indices.iter().enumerate() {
if let Some(input_idx) = input_idx
&& source_indices.contains(input_idx)
&& !new_source_input_indices.contains(&Some(*input_idx))
{
new_source_indices.push(idx);
new_source_input_indices.push(Some(*input_idx));
}
}
let new_target_indices = get_target_indices_from_dependencies(
func_dependencies.iter(),
&new_source_input_indices,
);
let mode = if existing_target_indices == new_target_indices
&& new_target_indices.is_some()
{
Dependency::Single
} else {
*mode
};
if new_source_indices.len() == source_indices.len() {
let output_null_equality =
if new_source_indices.len() == group_by_input_indices.len() {
NullEquality::NullEqualsNull
} else {
*null_equality
};
aggregate_func_dependencies.push(
FunctionalDependence::new(
new_source_indices,
target_indices.clone(),
*nullable,
)
.with_mode(mode)
.with_null_equality(output_null_equality),
);
}
}
}
if !group_by_input_indices.is_empty() {
let count = group_by_input_indices.len();
let source_indices = (0..count).collect::<Vec<_>>();
let nullable = source_indices
.iter()
.any(|idx| aggr_fields[*idx].is_nullable());
if !aggregate_func_dependencies.iter().any(|item| {
item.source_indices.iter().all(|idx| idx < &count)
&& item.is_valid_across_nulls(aggr_schema)
}) {
aggregate_func_dependencies.push(
FunctionalDependence::new(source_indices, target_indices, nullable)
.with_mode(Dependency::Single)
.with_null_equality(NullEquality::NullEqualsNull),
);
}
}
FunctionalDependencies::new(aggregate_func_dependencies)
}
pub fn get_target_functional_dependencies(
schema: &DFSchema,
group_by_input_indices: &[Option<usize>],
) -> Option<Vec<usize>> {
let dependencies = schema.functional_dependencies();
if dependencies.is_empty() {
return None;
}
let dependencies = dependencies
.iter()
.filter(|dependency| dependency.is_valid_across_nulls(schema));
get_target_indices_from_dependencies(dependencies, group_by_input_indices)
}
fn get_target_indices_from_dependencies<'a>(
dependencies: impl IntoIterator<Item = &'a FunctionalDependence>,
group_by_input_indices: &[Option<usize>],
) -> Option<Vec<usize>> {
let mut combined_target_indices = HashSet::new();
for dependency in dependencies {
if dependency
.source_indices
.iter()
.all(|source_idx| group_by_input_indices.contains(&Some(*source_idx)))
{
combined_target_indices.extend(dependency.target_indices.iter());
}
}
(!combined_target_indices.is_empty()).then_some({
let mut result = combined_target_indices.into_iter().collect::<Vec<_>>();
result.sort_unstable();
result
})
}
pub fn get_required_group_by_exprs_indices(
schema: &DFSchema,
group_by_input_indices: &[Option<usize>],
) -> Option<Vec<usize>> {
let dependencies = schema.functional_dependencies();
let mut groupby_expr_indices = group_by_input_indices
.iter()
.copied()
.collect::<Option<Vec<_>>>()?;
groupby_expr_indices.sort_unstable();
for dependency in &dependencies.deps {
if dependency
.source_indices
.iter()
.all(|source_idx| groupby_expr_indices.contains(source_idx))
&& dependency.is_valid_across_nulls(schema)
{
groupby_expr_indices =
set_difference(&groupby_expr_indices, &dependency.target_indices);
groupby_expr_indices =
merge_and_order_indices(groupby_expr_indices, &dependency.source_indices);
}
}
groupby_expr_indices
.iter()
.map(|idx| {
group_by_input_indices
.iter()
.position(|input_idx| *input_idx == Some(*idx))
})
.collect()
}
pub fn get_required_sort_exprs_indices(
schema: &DFSchema,
sort_input_indices: &[Option<usize>],
) -> Vec<usize> {
let dependencies = schema.functional_dependencies();
let mut known_field_indices = HashSet::with_capacity(sort_input_indices.len());
let mut required_sort_expr_indices = Vec::with_capacity(sort_input_indices.len());
for (sort_expr_idx, field_idx) in sort_input_indices.iter().enumerate() {
let Some(field_idx) = *field_idx else {
required_sort_expr_indices.push(sort_expr_idx);
continue;
};
let removable = dependencies.deps.iter().any(|dependency| {
dependency.target_indices.contains(&field_idx)
&& dependency
.source_indices
.iter()
.all(|source_idx| known_field_indices.contains(source_idx))
&& dependency.is_valid_across_nulls(schema)
});
if removable {
continue;
}
known_field_indices.insert(field_idx);
required_sort_expr_indices.push(sort_expr_idx);
}
required_sort_expr_indices
}
fn update_elements_with_matching_indices(
entries: &[usize],
proj_indices: &[usize],
) -> Vec<usize> {
entries
.iter()
.filter_map(|val| proj_indices.iter().position(|proj_idx| proj_idx == val))
.collect()
}
fn add_offset_to_vec<T: Copy + std::ops::Add<Output = T>>(
in_data: &[T],
offset: T,
) -> Vec<T> {
in_data.iter().map(|&item| item + offset).collect()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn constraints_iter() {
let constraints = Constraints::new_unverified(vec![
Constraint::PrimaryKey(vec![10]),
Constraint::Unique(vec![20]),
]);
let mut iter = constraints.iter();
assert_eq!(iter.next(), Some(&Constraint::PrimaryKey(vec![10])));
assert_eq!(iter.next(), Some(&Constraint::Unique(vec![20])));
assert_eq!(iter.next(), None);
}
#[test]
fn test_project_constraints() {
let constraints = Constraints::new_unverified(vec![
Constraint::PrimaryKey(vec![1, 2]),
Constraint::Unique(vec![0, 3]),
]);
let projected = constraints.project(&[1, 2, 3]).unwrap();
assert_eq!(
projected,
Constraints::new_unverified(vec![Constraint::PrimaryKey(vec![0, 1])])
);
assert!(constraints.project(&[0]).is_none());
}
#[test]
fn test_get_updated_id_keys() {
let fund_dependencies =
FunctionalDependencies::new(vec![FunctionalDependence::new(
vec![1],
vec![0, 1, 2],
true,
)]);
let res = fund_dependencies.project_functional_dependencies(&[1, 2], 2);
let expected = FunctionalDependencies::new(vec![FunctionalDependence::new(
vec![0],
vec![0, 1],
true,
)]);
assert_eq!(res, expected);
}
}