-
Notifications
You must be signed in to change notification settings - Fork 242
/
Copy pathdistinct.rs
254 lines (206 loc) · 7.4 KB
/
distinct.rs
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
use std::cmp::Ordering;
use std::collections::HashMap;
use crate::prelude::*;
/// This will get distinct records from a set of records compared over a given set of columns
#[derive(Clone, Serialize, Deserialize)]
pub struct Distinct {
// Parent Node
src: IndexPair,
us: Option<IndexPair>,
group_by: Vec<usize>,
}
impl Distinct {
pub fn new(src: NodeIndex, group_by: Vec<usize>) -> Self {
let mut group_by = group_by;
group_by.sort();
Distinct {
src: src.into(),
us: None,
group_by,
}
}
}
impl Ingredient for Distinct {
/// Returns a clone of the node
fn take(&mut self) -> NodeOperator {
Clone::clone(self).into()
}
fn ancestors(&self) -> Vec<NodeIndex> {
vec![self.src.as_global()]
}
fn on_input(
&mut self,
_: &mut dyn Executor,
from: LocalNodeIndex,
rs: Records,
_: Option<&[usize]>,
_: &DomainNodes,
state: &StateMap,
) -> ProcessingResult {
debug_assert_eq!(from, *self.src);
// Check that the records aren't empty
if rs.is_empty() {
return ProcessingResult {
results: rs,
..Default::default()
};
}
let us = self.us.unwrap();
let db = state
.get(*us)
.expect("Distinct must have its own state initialized");
let pos_comp = |a: &Record, b: &Record| a.is_positive().cmp(&b.is_positive());
let mut rs: Vec<_> = rs.into();
// Sort by positive or negative
rs.sort_by(&pos_comp);
let group_by = &self.group_by;
let group_cmp = |a: &Record, b: &Record| {
group_by
.iter()
.map(|&col| &a[col])
.cmp(group_by.iter().map(|&col| &b[col]))
};
// First, we want to be smart about multiple added/removed rows with same group.
// For example, if we get a -, then a +, for the same group, we don't want to
// execute two queries. We'll do this by sorting the batch by our group by.
rs.sort_by(&group_cmp);
let mut output = Vec::new();
let mut prev_grp = Vec::new();
let mut prev_pos = false;
for rec in rs {
let group_by = &self.group_by[..];
let group = rec
.iter()
.enumerate()
.filter_map(|(i, v)| {
if self.group_by.iter().any(|col| col == &i) {
Some(v)
} else {
None
}
})
.cloned()
.collect::<Vec<_>>();
// Do not take overlapping elements
if prev_grp.iter().cmp(group_by.iter().map(|&col| &rec[col])) == Ordering::Equal
&& prev_pos == rec.is_positive()
{
continue;
}
// make ready for the new one
prev_grp.clear();
prev_grp.extend(group_by.iter().map(|&col| &rec[col]).cloned());
prev_pos = rec.is_positive();
let positive = rec.is_positive();
match db.lookup(group_by, &KeyType::from(&group[..])) {
LookupResult::Some(rr) => {
if positive {
//println!("record {:?}", rr);
if rr.is_empty() {
output.push(rec.clone());
}
} else if !rr.is_empty() {
output.push(rec.clone());
}
}
LookupResult::Missing => unimplemented!("Distinct does not yet support partial"),
}
}
ProcessingResult {
results: output.into(),
..Default::default()
}
}
fn description(&self, _: bool) -> String {
"Distinct".into()
}
fn on_connected(&mut self, _: &Graph) {}
fn on_commit(&mut self, us: NodeIndex, remap: &HashMap<NodeIndex, IndexPair>) {
self.src.remap(remap);
self.us = Some(remap[&us]);
}
fn parent_columns(&self, column: usize) -> Vec<(NodeIndex, Option<usize>)> {
vec![(self.src.as_global(), Some(column))]
}
fn resolve(&self, col: usize) -> Option<Vec<(NodeIndex, usize)>> {
Some(vec![(self.src.as_global(), col)])
}
fn requires_full_materialization(&self) -> bool {
true
}
fn suggest_indexes(&self, this: NodeIndex) -> HashMap<NodeIndex, Vec<usize>> {
vec![(this, self.group_by.clone())].into_iter().collect()
}
}
/// Tests for the Distinct Operator
#[cfg(test)]
mod tests {
use super::*;
use crate::ops;
fn setup(materialized: bool) -> ops::test::MockGraph {
let mut g = ops::test::MockGraph::new();
let s = g.add_base("source", &["x", "y", "z"]);
g.set_op(
"distinct",
&["x", "y", "z"],
Distinct::new(s.as_global(), vec![1, 2]),
materialized,
);
g
}
#[test]
fn simple_distinct() {
let mut g = setup(true);
let r1: Vec<DataType> = vec![1.into(), "z".into(), 1.into()];
let r2: Vec<DataType> = vec![1.into(), "z".into(), 1.into()];
let r3: Vec<DataType> = vec![1.into(), "c".into(), 2.into()];
let a = g.narrow_one_row(r1.clone(), true);
assert_eq!(a, vec![r1.clone()].into());
let a = g.narrow_one_row(r2.clone(), false);
assert_eq!(a.len(), 0);
let a = g.narrow_one_row(r3.clone(), true);
assert_eq!(a, vec![r3.clone()].into());
}
#[test]
fn distinct_neg_record() {
let mut g = setup(true);
let r1: Vec<DataType> = vec![1.into(), "z".into(), 1.into()];
let r2: Vec<DataType> = vec![2.into(), "a".into(), 2.into()];
let r3: Vec<DataType> = vec![3.into(), "c".into(), 2.into()];
let a = g.narrow_one_row(r1.clone(), true);
println!("{:?}", a);
assert_eq!(a, vec![r1.clone()].into());
let a = g.narrow_one_row(r2.clone(), true);
println!("{:?}", a);
assert_eq!(a, vec![r2.clone()].into());
let a = g.narrow_one_row(r3.clone(), true);
assert_eq!(a, vec![r3.clone()].into());
let a = g.narrow_one_row((r1.clone(), false), true);
println!("{:?}", a);
let a = g.narrow_one_row((r1.clone(), true), true);
println!("{:?}", a);
assert_eq!(a, vec![r1.clone()].into());
}
#[test]
fn multiple_records_distinct() {
let mut g = setup(true);
let r1: Vec<DataType> = vec![1.into(), "z".into(), 1.into()];
let r2: Vec<DataType> = vec![2.into(), "a".into(), 2.into()];
let r3: Vec<DataType> = vec![3.into(), "c".into(), 2.into()];
let a = g.narrow_one(
vec![
(r2.clone(), true),
(r1.clone(), true),
(r1.clone(), true),
(r3.clone(), true),
],
true,
);
assert!(a.iter().any(|r| r == &(r1.clone(), true).into()));
assert!(a.iter().any(|r| r == &(r2.clone(), true).into()));
assert!(a.iter().any(|r| r == &(r3.clone(), true).into()));
let a = g.narrow_one(vec![(r1.clone(), false), (r3.clone(), true)], true);
assert!(a.iter().any(|r| r == &(r1.clone(), false).into()));
assert!(!a.iter().any(|r| r == &(r3.clone(), true).into()));
}
}