1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
use crate::{
error::Error,
prelude::{Predicate, Relation},
};
impl Relation {
/// Restricts a relation to tuples that satisfy a predicate (`σ`).
///
/// `people`
///
/// | `id` | `age` |
/// | --- | --- |
/// | `1` | `19` |
/// | `2` | `24` |
///
/// Output
///
/// | `id` | `age` |
/// | --- | --- |
/// | `2` | `24` |
///
/// # Errors
///
/// Returns [`Error::AttributeNotFound`] if the predicate references an
/// attribute that does not exist in the relation tuples.
/// Returns [`Error::ScalarTypeMismatch`] if the predicate uses `Eq` to
/// compare values of different scalar types.
/// Returns [`Error::NonComparableTypes`] if the predicate uses `<` or `>`
/// with non-integer operands.
///
/// # Example
///
/// ```rust
/// use darwen::prelude::{
/// AttributeName, Heading, Predicate, Relation, Scalar, ScalarType, Tuple,
/// };
///
/// let people = Relation::new_from_iter(
/// Heading::try_from(vec![
/// (AttributeName::from("id"), ScalarType::Integer),
/// (AttributeName::from("age"), ScalarType::Integer),
/// ])?,
/// vec![
/// Tuple::try_from(vec![
/// (AttributeName::from("id"), Scalar::Integer(1)),
/// (AttributeName::from("age"), Scalar::Integer(19)),
/// ])?,
/// Tuple::try_from(vec![
/// (AttributeName::from("id"), Scalar::Integer(2)),
/// (AttributeName::from("age"), Scalar::Integer(24)),
/// ])?,
/// ],
/// )?;
///
/// let adults = people.restrict(&Predicate::eq(
/// AttributeName::from("age"),
/// Scalar::Integer(24),
/// ))?;
///
/// assert_eq!(
/// adults,
/// Relation::new_from_iter(
/// Heading::try_from(vec![
/// (AttributeName::from("id"), ScalarType::Integer),
/// (AttributeName::from("age"), ScalarType::Integer),
/// ])?,
/// vec![Tuple::try_from(vec![
/// (AttributeName::from("id"), Scalar::Integer(2)),
/// (AttributeName::from("age"), Scalar::Integer(24)),
/// ])?],
/// )?
/// );
/// # Ok::<(), darwen::prelude::Error>(())
/// ```
pub fn restrict(&self, predicate: &Predicate) -> Result<Relation, Error> {
let mut relation = Relation::new(self.heading.clone());
for tuple in &self.body {
if predicate.eval(tuple)? {
relation.body.insert(tuple.clone());
}
}
Ok(relation)
}
}
#[cfg(test)]
mod tests {
use crate::{
prelude::{Heading, Scalar, ScalarType, Tuple},
types::AttributeName,
};
use super::*;
#[test]
fn test_restrict() {
let relation = Relation::new_from_iter(
Heading::try_from(vec![(AttributeName::from("foo"), ScalarType::Integer)]).unwrap(),
vec![
Tuple::try_from(vec![(AttributeName::from("foo"), Scalar::Integer(1))]).unwrap(),
Tuple::try_from(vec![(AttributeName::from("foo"), Scalar::Integer(2))]).unwrap(),
Tuple::try_from(vec![(AttributeName::from("foo"), Scalar::Integer(3))]).unwrap(),
],
)
.unwrap();
assert_eq!(
relation
.restrict(&Predicate::eq(
AttributeName::from("foo"),
Scalar::Integer(2)
))
.unwrap(),
Relation::new_from_iter(
Heading::try_from(vec![(AttributeName::from("foo"), ScalarType::Integer)]).unwrap(),
vec![
Tuple::try_from(vec![(AttributeName::from("foo"), Scalar::Integer(2))])
.unwrap(),
],
)
.unwrap()
);
}
#[test]
fn test_restrict_returns_empty_relation_when_nothing_matches() -> Result<(), Error> {
let relation = Relation::new_from_iter(
Heading::try_from(vec![(AttributeName::from("foo"), ScalarType::Integer)]).unwrap(),
vec![Tuple::try_from(vec![(AttributeName::from("foo"), Scalar::Integer(1))]).unwrap()],
)?;
assert_eq!(
relation.restrict(&Predicate::eq(
AttributeName::from("foo"),
Scalar::Integer(2)
))?,
Relation::new_from_iter(
Heading::try_from(vec![(AttributeName::from("foo"), ScalarType::Integer)]).unwrap(),
Vec::new(),
)?
);
Ok(())
}
#[test]
fn test_restrict_returns_error_for_unknown_attribute() -> Result<(), Error> {
let relation = Relation::new_from_iter(
Heading::try_from(vec![(AttributeName::from("foo"), ScalarType::Integer)]).unwrap(),
vec![Tuple::try_from(vec![(AttributeName::from("foo"), Scalar::Integer(1))]).unwrap()],
)?;
assert_eq!(
relation.restrict(&Predicate::eq(
AttributeName::from("bar"),
Scalar::Integer(1)
)),
Err(Error::AttributeNotFound {
name: AttributeName::from("bar")
})
);
Ok(())
}
}