pub struct GroupedRelation<T: Ord> { /* private fields */ }Expand description
Deterministic exact grouping of an n-ary relation. A deterministic exact grouping of an n-ary relation by named key columns.
GroupedRelation<T> is the first G2 grouping surface for exact n-ary
relations. Groups are keyed by projected key rows and stored in a
BTreeMap, so group iteration order is deterministic.
In this first exact grouping slice, each grouped member relation keeps the original relation schema rather than removing key columns. This avoids introducing zero-ary relation semantics while keeping the grouped output relation-first and easy to inspect.
The first exact aggregate on grouped output is Self::counts, which
returns the number of stored rows in each group.
§Examples
use relmath::NaryRelation;
let completed = NaryRelation::from_rows(
["student", "course", "term"],
[
["Alice", "Math", "Fall"],
["Alice", "Physics", "Fall"],
["Bob", "Math", "Spring"],
],
)?;
let grouped = completed.group_by(["term", "student"])?;
assert_eq!(
grouped.key_schema(),
&["term".to_string(), "student".to_string()]
);
assert_eq!(
grouped.counts(),
vec![(vec!["Fall", "Alice"], 2), (vec!["Spring", "Bob"], 1)]
);Implementations§
Source§impl<T: Ord> GroupedRelation<T>
impl<T: Ord> GroupedRelation<T>
Sourcepub fn key_schema(&self) -> &[String]
pub fn key_schema(&self) -> &[String]
Returns the grouping key schema in order.
Examples found in repository?
7fn main() -> Result<(), relmath::NaryRelationError> {
8 let progress = NaryRelation::from_named_rows(
9 ["student", "course", "status"],
10 [
11 BTreeMap::from([
12 ("course", "Math"),
13 ("status", "passed"),
14 ("student", "Alice"),
15 ]),
16 BTreeMap::from([
17 ("course", "Physics"),
18 ("status", "passed"),
19 ("student", "Alice"),
20 ]),
21 BTreeMap::from([("course", "Math"), ("status", "passed"), ("student", "Bob")]),
22 BTreeMap::from([
23 ("course", "Physics"),
24 ("status", "in_progress"),
25 ("student", "Bob"),
26 ]),
27 BTreeMap::from([
28 ("course", "Math"),
29 ("status", "passed"),
30 ("student", "Cara"),
31 ]),
32 ],
33 )?;
34 let course_rooms = NaryRelation::from_named_rows(
35 ["course", "room"],
36 [
37 BTreeMap::from([("course", "Math"), ("room", "R101")]),
38 BTreeMap::from([("course", "Physics"), ("room", "R201")]),
39 BTreeMap::from([("course", "History"), ("room", "R301")]),
40 ],
41 )?;
42
43 let completed = progress
44 .select(|row| row[2] == "passed")
45 .project(["student", "course"])?;
46 let scheduled = completed.natural_join(&course_rooms);
47 let by_room = scheduled.group_by(["room"])?;
48 let room_r101 = by_room.group(&["R101"]).expect("expected R101 group");
49 let room_assignments = scheduled.project(["room", "student"])?;
50 let exported_assignments = room_assignments.to_named_rows();
51
52 assert!(completed.contains_row(&["Alice", "Math"]));
53 assert_eq!(
54 scheduled.schema(),
55 &[
56 "student".to_string(),
57 "course".to_string(),
58 "room".to_string(),
59 ]
60 );
61 assert_eq!(
62 scheduled.to_rows(),
63 vec![
64 vec!["Alice", "Math", "R101"],
65 vec!["Alice", "Physics", "R201"],
66 vec!["Bob", "Math", "R101"],
67 vec!["Cara", "Math", "R101"],
68 ]
69 );
70 assert_eq!(by_room.key_schema(), &["room".to_string()]);
71 assert_eq!(by_room.member_schema(), scheduled.schema());
72 assert_eq!(by_room.counts(), vec![(vec!["R101"], 3), (vec!["R201"], 1)]);
73 assert_eq!(
74 room_r101.to_rows(),
75 vec![
76 vec!["Alice", "Math", "R101"],
77 vec!["Bob", "Math", "R101"],
78 vec!["Cara", "Math", "R101"],
79 ]
80 );
81 assert_eq!(
82 room_assignments.to_rows(),
83 vec![
84 vec!["R101", "Alice"],
85 vec!["R101", "Bob"],
86 vec!["R101", "Cara"],
87 vec!["R201", "Alice"],
88 ]
89 );
90 assert_eq!(
91 exported_assignments[0],
92 BTreeMap::from([
93 ("room".to_string(), "R101"),
94 ("student".to_string(), "Alice"),
95 ])
96 );
97
98 println!("scheduled course completions: {:?}", scheduled.to_rows());
99
100 Ok(())
101}Sourcepub fn member_schema(&self) -> &[String]
pub fn member_schema(&self) -> &[String]
Returns the schema of each member relation.
In the current exact grouping slice this is the full original relation schema.
Examples found in repository?
7fn main() -> Result<(), relmath::NaryRelationError> {
8 let progress = NaryRelation::from_named_rows(
9 ["student", "course", "status"],
10 [
11 BTreeMap::from([
12 ("course", "Math"),
13 ("status", "passed"),
14 ("student", "Alice"),
15 ]),
16 BTreeMap::from([
17 ("course", "Physics"),
18 ("status", "passed"),
19 ("student", "Alice"),
20 ]),
21 BTreeMap::from([("course", "Math"), ("status", "passed"), ("student", "Bob")]),
22 BTreeMap::from([
23 ("course", "Physics"),
24 ("status", "in_progress"),
25 ("student", "Bob"),
26 ]),
27 BTreeMap::from([
28 ("course", "Math"),
29 ("status", "passed"),
30 ("student", "Cara"),
31 ]),
32 ],
33 )?;
34 let course_rooms = NaryRelation::from_named_rows(
35 ["course", "room"],
36 [
37 BTreeMap::from([("course", "Math"), ("room", "R101")]),
38 BTreeMap::from([("course", "Physics"), ("room", "R201")]),
39 BTreeMap::from([("course", "History"), ("room", "R301")]),
40 ],
41 )?;
42
43 let completed = progress
44 .select(|row| row[2] == "passed")
45 .project(["student", "course"])?;
46 let scheduled = completed.natural_join(&course_rooms);
47 let by_room = scheduled.group_by(["room"])?;
48 let room_r101 = by_room.group(&["R101"]).expect("expected R101 group");
49 let room_assignments = scheduled.project(["room", "student"])?;
50 let exported_assignments = room_assignments.to_named_rows();
51
52 assert!(completed.contains_row(&["Alice", "Math"]));
53 assert_eq!(
54 scheduled.schema(),
55 &[
56 "student".to_string(),
57 "course".to_string(),
58 "room".to_string(),
59 ]
60 );
61 assert_eq!(
62 scheduled.to_rows(),
63 vec![
64 vec!["Alice", "Math", "R101"],
65 vec!["Alice", "Physics", "R201"],
66 vec!["Bob", "Math", "R101"],
67 vec!["Cara", "Math", "R101"],
68 ]
69 );
70 assert_eq!(by_room.key_schema(), &["room".to_string()]);
71 assert_eq!(by_room.member_schema(), scheduled.schema());
72 assert_eq!(by_room.counts(), vec![(vec!["R101"], 3), (vec!["R201"], 1)]);
73 assert_eq!(
74 room_r101.to_rows(),
75 vec![
76 vec!["Alice", "Math", "R101"],
77 vec!["Bob", "Math", "R101"],
78 vec!["Cara", "Math", "R101"],
79 ]
80 );
81 assert_eq!(
82 room_assignments.to_rows(),
83 vec![
84 vec!["R101", "Alice"],
85 vec!["R101", "Bob"],
86 vec!["R101", "Cara"],
87 vec!["R201", "Alice"],
88 ]
89 );
90 assert_eq!(
91 exported_assignments[0],
92 BTreeMap::from([
93 ("room".to_string(), "R101"),
94 ("student".to_string(), "Alice"),
95 ])
96 );
97
98 println!("scheduled course completions: {:?}", scheduled.to_rows());
99
100 Ok(())
101}Sourcepub fn group(&self, key: &[T]) -> Option<&NaryRelation<T>>
pub fn group(&self, key: &[T]) -> Option<&NaryRelation<T>>
Returns the member relation for the given grouping key.
Examples found in repository?
7fn main() -> Result<(), relmath::NaryRelationError> {
8 let progress = NaryRelation::from_named_rows(
9 ["student", "course", "status"],
10 [
11 BTreeMap::from([
12 ("course", "Math"),
13 ("status", "passed"),
14 ("student", "Alice"),
15 ]),
16 BTreeMap::from([
17 ("course", "Physics"),
18 ("status", "passed"),
19 ("student", "Alice"),
20 ]),
21 BTreeMap::from([("course", "Math"), ("status", "passed"), ("student", "Bob")]),
22 BTreeMap::from([
23 ("course", "Physics"),
24 ("status", "in_progress"),
25 ("student", "Bob"),
26 ]),
27 BTreeMap::from([
28 ("course", "Math"),
29 ("status", "passed"),
30 ("student", "Cara"),
31 ]),
32 ],
33 )?;
34 let course_rooms = NaryRelation::from_named_rows(
35 ["course", "room"],
36 [
37 BTreeMap::from([("course", "Math"), ("room", "R101")]),
38 BTreeMap::from([("course", "Physics"), ("room", "R201")]),
39 BTreeMap::from([("course", "History"), ("room", "R301")]),
40 ],
41 )?;
42
43 let completed = progress
44 .select(|row| row[2] == "passed")
45 .project(["student", "course"])?;
46 let scheduled = completed.natural_join(&course_rooms);
47 let by_room = scheduled.group_by(["room"])?;
48 let room_r101 = by_room.group(&["R101"]).expect("expected R101 group");
49 let room_assignments = scheduled.project(["room", "student"])?;
50 let exported_assignments = room_assignments.to_named_rows();
51
52 assert!(completed.contains_row(&["Alice", "Math"]));
53 assert_eq!(
54 scheduled.schema(),
55 &[
56 "student".to_string(),
57 "course".to_string(),
58 "room".to_string(),
59 ]
60 );
61 assert_eq!(
62 scheduled.to_rows(),
63 vec![
64 vec!["Alice", "Math", "R101"],
65 vec!["Alice", "Physics", "R201"],
66 vec!["Bob", "Math", "R101"],
67 vec!["Cara", "Math", "R101"],
68 ]
69 );
70 assert_eq!(by_room.key_schema(), &["room".to_string()]);
71 assert_eq!(by_room.member_schema(), scheduled.schema());
72 assert_eq!(by_room.counts(), vec![(vec!["R101"], 3), (vec!["R201"], 1)]);
73 assert_eq!(
74 room_r101.to_rows(),
75 vec![
76 vec!["Alice", "Math", "R101"],
77 vec!["Bob", "Math", "R101"],
78 vec!["Cara", "Math", "R101"],
79 ]
80 );
81 assert_eq!(
82 room_assignments.to_rows(),
83 vec![
84 vec!["R101", "Alice"],
85 vec!["R101", "Bob"],
86 vec!["R101", "Cara"],
87 vec!["R201", "Alice"],
88 ]
89 );
90 assert_eq!(
91 exported_assignments[0],
92 BTreeMap::from([
93 ("room".to_string(), "R101"),
94 ("student".to_string(), "Alice"),
95 ])
96 );
97
98 println!("scheduled course completions: {:?}", scheduled.to_rows());
99
100 Ok(())
101}Sourcepub fn iter(&self) -> impl Iterator<Item = (&[T], &NaryRelation<T>)>
pub fn iter(&self) -> impl Iterator<Item = (&[T], &NaryRelation<T>)>
Returns an iterator over groups in deterministic key order.
Sourcepub fn counts(&self) -> Vec<(Vec<T>, usize)>where
T: Clone,
pub fn counts(&self) -> Vec<(Vec<T>, usize)>where
T: Clone,
Returns the first exact aggregate over the grouped relation: row counts.
Each count is the number of stored rows in the corresponding group after the base relation’s set semantics have already deduplicated equal rows. Results follow the deterministic key order of the grouping.
Examples found in repository?
7fn main() -> Result<(), relmath::NaryRelationError> {
8 let progress = NaryRelation::from_named_rows(
9 ["student", "course", "status"],
10 [
11 BTreeMap::from([
12 ("course", "Math"),
13 ("status", "passed"),
14 ("student", "Alice"),
15 ]),
16 BTreeMap::from([
17 ("course", "Physics"),
18 ("status", "passed"),
19 ("student", "Alice"),
20 ]),
21 BTreeMap::from([("course", "Math"), ("status", "passed"), ("student", "Bob")]),
22 BTreeMap::from([
23 ("course", "Physics"),
24 ("status", "in_progress"),
25 ("student", "Bob"),
26 ]),
27 BTreeMap::from([
28 ("course", "Math"),
29 ("status", "passed"),
30 ("student", "Cara"),
31 ]),
32 ],
33 )?;
34 let course_rooms = NaryRelation::from_named_rows(
35 ["course", "room"],
36 [
37 BTreeMap::from([("course", "Math"), ("room", "R101")]),
38 BTreeMap::from([("course", "Physics"), ("room", "R201")]),
39 BTreeMap::from([("course", "History"), ("room", "R301")]),
40 ],
41 )?;
42
43 let completed = progress
44 .select(|row| row[2] == "passed")
45 .project(["student", "course"])?;
46 let scheduled = completed.natural_join(&course_rooms);
47 let by_room = scheduled.group_by(["room"])?;
48 let room_r101 = by_room.group(&["R101"]).expect("expected R101 group");
49 let room_assignments = scheduled.project(["room", "student"])?;
50 let exported_assignments = room_assignments.to_named_rows();
51
52 assert!(completed.contains_row(&["Alice", "Math"]));
53 assert_eq!(
54 scheduled.schema(),
55 &[
56 "student".to_string(),
57 "course".to_string(),
58 "room".to_string(),
59 ]
60 );
61 assert_eq!(
62 scheduled.to_rows(),
63 vec![
64 vec!["Alice", "Math", "R101"],
65 vec!["Alice", "Physics", "R201"],
66 vec!["Bob", "Math", "R101"],
67 vec!["Cara", "Math", "R101"],
68 ]
69 );
70 assert_eq!(by_room.key_schema(), &["room".to_string()]);
71 assert_eq!(by_room.member_schema(), scheduled.schema());
72 assert_eq!(by_room.counts(), vec![(vec!["R101"], 3), (vec!["R201"], 1)]);
73 assert_eq!(
74 room_r101.to_rows(),
75 vec![
76 vec!["Alice", "Math", "R101"],
77 vec!["Bob", "Math", "R101"],
78 vec!["Cara", "Math", "R101"],
79 ]
80 );
81 assert_eq!(
82 room_assignments.to_rows(),
83 vec![
84 vec!["R101", "Alice"],
85 vec!["R101", "Bob"],
86 vec!["R101", "Cara"],
87 vec!["R201", "Alice"],
88 ]
89 );
90 assert_eq!(
91 exported_assignments[0],
92 BTreeMap::from([
93 ("room".to_string(), "R101"),
94 ("student".to_string(), "Alice"),
95 ])
96 );
97
98 println!("scheduled course completions: {:?}", scheduled.to_rows());
99
100 Ok(())
101}Trait Implementations§
Source§impl<T: Clone + Ord> Clone for GroupedRelation<T>
impl<T: Clone + Ord> Clone for GroupedRelation<T>
Source§fn clone(&self) -> GroupedRelation<T>
fn clone(&self) -> GroupedRelation<T>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more