Skip to content

Commit

Permalink
Expose contents of Constraints (#7603)
Browse files Browse the repository at this point in the history
Closes #7511
  • Loading branch information
tv42 authored Sep 20, 2023
1 parent 1186983 commit a2a09c7
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion datafusion/common/src/functional_dependencies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ use crate::{DFSchema, DFSchemaRef, DataFusionError, JoinType, Result};
use sqlparser::ast::TableConstraint;
use std::collections::HashSet;
use std::fmt::{Display, Formatter};
use std::ops::Deref;

/// This object defines a constraint on a table.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
enum Constraint {
pub enum Constraint {
/// Columns with the given indices form a composite primary key (they are
/// jointly unique and not nullable):
PrimaryKey(Vec<usize>),
Expand Down Expand Up @@ -124,6 +125,14 @@ impl Display for Constraints {
}
}

impl Deref for Constraints {
type Target = [Constraint];

fn deref(&self) -> &Self::Target {
self.inner.as_slice()
}
}

/// This object defines a functional dependence in the schema. A functional
/// dependence defines a relationship between determinant keys and dependent
/// columns. A determinant key is a column, or a set of columns, whose value
Expand Down Expand Up @@ -518,3 +527,20 @@ fn add_offset_to_vec<T: Copy + std::ops::Add<Output = 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(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);
}
}

0 comments on commit a2a09c7

Please sign in to comment.