forked from apache/datafusion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathequivalence.rs
321 lines (284 loc) · 11.3 KB
/
equivalence.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
use crate::expressions::Column;
use arrow::datatypes::SchemaRef;
use std::collections::HashMap;
use std::collections::HashSet;
/// Equivalence Properties is a vec of EquivalentClass.
#[derive(Debug, Clone)]
pub struct EquivalenceProperties {
classes: Vec<EquivalentClass>,
schema: SchemaRef,
}
impl EquivalenceProperties {
pub fn new(schema: SchemaRef) -> Self {
EquivalenceProperties {
classes: vec![],
schema,
}
}
pub fn classes(&self) -> &[EquivalentClass] {
&self.classes
}
pub fn schema(&self) -> SchemaRef {
self.schema.clone()
}
pub fn extend<I: IntoIterator<Item = EquivalentClass>>(&mut self, iter: I) {
for ec in iter {
for column in ec.iter() {
assert_eq!(column.name(), self.schema.fields()[column.index()].name());
}
self.classes.push(ec)
}
}
/// Add new equal conditions into the EquivalenceProperties, the new equal conditions are usually comming from the
/// equality predicates in Join or Filter
pub fn add_equal_conditions(&mut self, new_conditions: (&Column, &Column)) {
assert_eq!(
new_conditions.0.name(),
self.schema.fields()[new_conditions.0.index()].name()
);
assert_eq!(
new_conditions.1.name(),
self.schema.fields()[new_conditions.1.index()].name()
);
let mut idx1: Option<usize> = None;
let mut idx2: Option<usize> = None;
for (idx, class) in self.classes.iter_mut().enumerate() {
let contains_first = class.contains(new_conditions.0);
let contains_second = class.contains(new_conditions.1);
match (contains_first, contains_second) {
(true, false) => {
class.insert(new_conditions.1.clone());
idx1 = Some(idx);
}
(false, true) => {
class.insert(new_conditions.0.clone());
idx2 = Some(idx);
}
(true, true) => {
idx1 = Some(idx);
idx2 = Some(idx);
break;
}
(false, false) => {}
}
}
match (idx1, idx2) {
(Some(idx_1), Some(idx_2)) if idx_1 != idx_2 => {
// need to merge the two existing EquivalentClasses
let second_eq_class = self.classes.get(idx_2).unwrap().clone();
let first_eq_class = self.classes.get_mut(idx_1).unwrap();
for prop in second_eq_class.iter() {
if !first_eq_class.contains(prop) {
first_eq_class.insert(prop.clone());
}
}
self.classes.remove(idx_2);
}
(None, None) => {
// adding new pairs
self.classes.push(EquivalentClass::new(
new_conditions.0.clone(),
vec![new_conditions.1.clone()],
));
}
_ => {}
}
}
}
/// Equivalent Class is a set of Columns that are known to have the same value in all tuples in a relation
/// Equivalent Class is generated by equality predicates, typically equijoin conditions and equality conditions in filters.
#[derive(Debug, Clone)]
pub struct EquivalentClass {
/// First element in the EquivalentClass
head: Column,
/// Other equal columns
others: HashSet<Column>,
}
impl EquivalentClass {
pub fn new(head: Column, others: Vec<Column>) -> Self {
EquivalentClass {
head,
others: HashSet::from_iter(others),
}
}
pub fn head(&self) -> &Column {
&self.head
}
pub fn others(&self) -> &HashSet<Column> {
&self.others
}
pub fn contains(&self, col: &Column) -> bool {
self.head == *col || self.others.contains(col)
}
pub fn insert(&mut self, col: Column) -> bool {
self.others.insert(col)
}
pub fn remove(&mut self, col: &Column) -> bool {
let removed = self.others.remove(col);
if !removed && *col == self.head {
let one_col = self.others.iter().next().cloned();
if let Some(col) = one_col {
let removed = self.others.remove(&col);
self.head = col;
removed
} else {
false
}
} else {
true
}
}
pub fn iter(&self) -> impl Iterator<Item = &'_ Column> {
std::iter::once(&self.head).chain(self.others.iter())
}
pub fn len(&self) -> usize {
self.others.len() + 1
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
}
/// Project Equivalence Properties.
/// 1) Add Alias, Alias can introduce additional equivalence properties,
/// For example: Projection(a, a as a1, a as a2)
/// 2) Truncate the EquivalentClasses that are not in the output schema
pub fn project_equivalence_properties(
input_eq: EquivalenceProperties,
alias_map: &HashMap<Column, Vec<Column>>,
output_eq: &mut EquivalenceProperties,
) {
let mut ec_classes = input_eq.classes().to_vec();
for (column, columns) in alias_map {
let mut find_match = false;
for class in ec_classes.iter_mut() {
if class.contains(column) {
for col in columns {
class.insert(col.clone());
}
find_match = true;
break;
}
}
if !find_match {
ec_classes.push(EquivalentClass::new(column.clone(), columns.clone()));
}
}
let schema = output_eq.schema();
for class in ec_classes.iter_mut() {
let mut columns_to_remove = vec![];
for column in class.iter() {
if column.index() >= schema.fields().len()
|| schema.fields()[column.index()].name() != column.name()
{
columns_to_remove.push(column.clone());
}
}
for column in columns_to_remove {
class.remove(&column);
}
}
ec_classes.retain(|props| props.len() > 1);
output_eq.extend(ec_classes);
}
#[cfg(test)]
mod tests {
use super::*;
use crate::expressions::Column;
use arrow::datatypes::{DataType, Field, Schema};
use datafusion_common::Result;
use std::sync::Arc;
#[test]
fn add_equal_conditions_test() -> Result<()> {
let schema = Arc::new(Schema::new(vec![
Field::new("a", DataType::Int64, true),
Field::new("b", DataType::Int64, true),
Field::new("c", DataType::Int64, true),
Field::new("x", DataType::Int64, true),
Field::new("y", DataType::Int64, true),
]));
let mut eq_properties = EquivalenceProperties::new(schema);
let new_condition = (&Column::new("a", 0), &Column::new("b", 1));
eq_properties.add_equal_conditions(new_condition);
assert_eq!(eq_properties.classes().len(), 1);
let new_condition = (&Column::new("b", 1), &Column::new("a", 0));
eq_properties.add_equal_conditions(new_condition);
assert_eq!(eq_properties.classes().len(), 1);
assert_eq!(eq_properties.classes()[0].len(), 2);
assert!(eq_properties.classes()[0].contains(&Column::new("a", 0)));
assert!(eq_properties.classes()[0].contains(&Column::new("b", 1)));
let new_condition = (&Column::new("b", 1), &Column::new("c", 2));
eq_properties.add_equal_conditions(new_condition);
assert_eq!(eq_properties.classes().len(), 1);
assert_eq!(eq_properties.classes()[0].len(), 3);
assert!(eq_properties.classes()[0].contains(&Column::new("a", 0)));
assert!(eq_properties.classes()[0].contains(&Column::new("b", 1)));
assert!(eq_properties.classes()[0].contains(&Column::new("c", 2)));
let new_condition = (&Column::new("x", 3), &Column::new("y", 4));
eq_properties.add_equal_conditions(new_condition);
assert_eq!(eq_properties.classes().len(), 2);
let new_condition = (&Column::new("x", 3), &Column::new("a", 0));
eq_properties.add_equal_conditions(new_condition);
assert_eq!(eq_properties.classes().len(), 1);
assert_eq!(eq_properties.classes()[0].len(), 5);
assert!(eq_properties.classes()[0].contains(&Column::new("a", 0)));
assert!(eq_properties.classes()[0].contains(&Column::new("b", 1)));
assert!(eq_properties.classes()[0].contains(&Column::new("c", 2)));
assert!(eq_properties.classes()[0].contains(&Column::new("x", 3)));
assert!(eq_properties.classes()[0].contains(&Column::new("y", 4)));
Ok(())
}
#[test]
fn project_equivalence_properties_test() -> Result<()> {
let input_schema = Arc::new(Schema::new(vec![
Field::new("a", DataType::Int64, true),
Field::new("b", DataType::Int64, true),
Field::new("c", DataType::Int64, true),
]));
let mut input_properties = EquivalenceProperties::new(input_schema);
let new_condition = (&Column::new("a", 0), &Column::new("b", 1));
input_properties.add_equal_conditions(new_condition);
let new_condition = (&Column::new("b", 1), &Column::new("c", 2));
input_properties.add_equal_conditions(new_condition);
let out_schema = Arc::new(Schema::new(vec![
Field::new("a1", DataType::Int64, true),
Field::new("a2", DataType::Int64, true),
Field::new("a3", DataType::Int64, true),
Field::new("a4", DataType::Int64, true),
]));
let mut alias_map = HashMap::new();
alias_map.insert(
Column::new("a", 0),
vec![
Column::new("a1", 0),
Column::new("a2", 1),
Column::new("a3", 2),
Column::new("a4", 3),
],
);
let mut out_properties = EquivalenceProperties::new(out_schema);
project_equivalence_properties(input_properties, &alias_map, &mut out_properties);
assert_eq!(out_properties.classes().len(), 1);
assert_eq!(out_properties.classes()[0].len(), 4);
assert!(out_properties.classes()[0].contains(&Column::new("a1", 0)));
assert!(out_properties.classes()[0].contains(&Column::new("a2", 1)));
assert!(out_properties.classes()[0].contains(&Column::new("a3", 2)));
assert!(out_properties.classes()[0].contains(&Column::new("a4", 3)));
Ok(())
}
}