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
use crate::{error::Error, prelude::Relation};
impl Relation {
/// Returns the union of two compatible relations (`⋃`).
///
/// `a`
///
/// | `value` |
/// | --- |
/// | `1` |
/// | `2` |
///
/// `b`
///
/// | `value` |
/// | --- |
/// | `2` |
/// | `3` |
///
/// Output
///
/// | `value` |
/// | --- |
/// | `1` |
/// | `2` |
/// | `3` |
///
/// # Errors
///
/// Returns [`Error::HeadingMismatch`] if the relations do not have the same
/// heading.
///
/// # Example
///
/// ```rust
/// use darwen::prelude::{AttributeName, Heading, Relation, Scalar, ScalarType, Tuple};
///
/// let a = Relation::new_from_iter(
/// Heading::try_from(vec![(AttributeName::from("value"), ScalarType::Integer)])?,
/// vec![
/// Tuple::try_from(vec![(AttributeName::from("value"), Scalar::Integer(1))])?,
/// Tuple::try_from(vec![(AttributeName::from("value"), Scalar::Integer(2))])?,
/// ],
/// )?;
/// let b = Relation::new_from_iter(
/// Heading::try_from(vec![(AttributeName::from("value"), ScalarType::Integer)])?,
/// vec![
/// Tuple::try_from(vec![(AttributeName::from("value"), Scalar::Integer(2))])?,
/// Tuple::try_from(vec![(AttributeName::from("value"), Scalar::Integer(3))])?,
/// ],
/// )?;
///
/// let union = a.union(&b)?;
///
/// assert_eq!(
/// union,
/// Relation::new_from_iter(
/// Heading::try_from(vec![(AttributeName::from("value"), ScalarType::Integer)])?,
/// vec![
/// Tuple::try_from(vec![(AttributeName::from("value"), Scalar::Integer(1))])?,
/// Tuple::try_from(vec![(AttributeName::from("value"), Scalar::Integer(2))])?,
/// Tuple::try_from(vec![(AttributeName::from("value"), Scalar::Integer(3))])?,
/// ],
/// )?
/// );
/// # Ok::<(), darwen::prelude::Error>(())
/// ```
pub fn union(&self, other: &Relation) -> Result<Relation, Error> {
if self.heading != other.heading {
return Err(Error::HeadingMismatch);
}
let mut relation = Relation::new(self.heading.clone());
for tuple in self.body.iter().chain(other.body.iter()) {
relation.insert(tuple.clone())?;
}
Ok(relation)
}
}
#[cfg(test)]
mod tests {
use crate::{
prelude::{Heading, Scalar, ScalarType, Tuple},
types::AttributeName,
};
use super::*;
#[test]
fn test_union() {
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();
let other = 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(),
Tuple::try_from(vec![(AttributeName::from("foo"), Scalar::Integer(3))]).unwrap(),
Tuple::try_from(vec![(AttributeName::from("foo"), Scalar::Integer(4))]).unwrap(),
],
)
.unwrap();
assert_eq!(
relation.union(&other).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(1))])
.unwrap(),
Tuple::try_from(vec![(AttributeName::from("foo"), Scalar::Integer(2))])
.unwrap(),
Tuple::try_from(vec![(AttributeName::from("foo"), Scalar::Integer(3))])
.unwrap(),
Tuple::try_from(vec![(AttributeName::from("foo"), Scalar::Integer(4))])
.unwrap(),
],
)
.unwrap()
);
}
#[test]
fn test_union_rejects_incompatible_headings_even_when_other_is_empty() -> Result<(), Error> {
let lhs = 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()],
)?;
let rhs = Relation::new_from_iter(
Heading::try_from(vec![(AttributeName::from("bar"), ScalarType::Integer)]).unwrap(),
Vec::new(),
)?;
assert_eq!(lhs.union(&rhs), Err(Error::HeadingMismatch));
Ok(())
}
#[test]
fn test_union_with_empty_relation_is_identity() -> Result<(), Error> {
let lhs = 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()],
)?;
let rhs = Relation::new_from_iter(
Heading::try_from(vec![(AttributeName::from("foo"), ScalarType::Integer)]).unwrap(),
Vec::new(),
)?;
assert_eq!(lhs.union(&rhs)?, lhs);
Ok(())
}
}