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
use std::cell::RefCell;
use std::collections::HashSet;
use crate::query_model::attribute::core::Attribute;
use crate::query_model::attribute::propagated_nulls::PropagatedNulls;
use crate::query_model::attribute::rejected_nulls::RejectedNulls;
use crate::query_model::model::{
BoundRef, BoxType, ColumnReference, QuantifierType, QueryBox, Select,
};
use crate::query_model::rewrite::ApplyStrategy;
use crate::query_model::rewrite::Rule;
use crate::query_model::rewrite::VisitOrder;
use crate::query_model::{BoxId, Model, QuantifierId};
pub(crate) struct SimplifyOuterJoins;
impl Rule for SimplifyOuterJoins {
type Match = (BoxId, Vec<QuantifierId>);
fn name(&self) -> &'static str {
"SimplifyOuterJoins"
}
fn strategy(&self) -> ApplyStrategy {
ApplyStrategy::AllBoxes(VisitOrder::Pre)
}
fn required_attributes(&self) -> std::collections::HashSet<Box<dyn Attribute>> {
let attributes: [Box<dyn Attribute>; 2] =
[Box::new(PropagatedNulls), Box::new(RejectedNulls)];
HashSet::from(attributes)
}
fn check(
&self,
model: &crate::query_model::Model,
box_id_to_check: crate::query_model::BoxId,
) -> Option<Self::Match> {
let mut quantifiers_to_change = vec![];
let box_to_check = model.get_box(box_id_to_check);
if let BoxType::OuterJoin(..) = box_to_check.box_type {
assert_eq!(box_to_check.input_quantifiers().count(), 2);
let unique_ancestor_chain = unique_ancestor_chain(model, box_id_to_check);
if let Some(ancestor_chain) = unique_ancestor_chain {
let mut ancestral_rejected_nulls = HashSet::new();
for ancestor_id in ancestor_chain {
let ancestor_box = model.get_box(ancestor_id);
map_nulls_to_inputs(&mut ancestral_rejected_nulls, &ancestor_box);
ancestral_rejected_nulls.extend(
ancestor_box
.attributes
.get::<RejectedNulls>()
.into_iter()
.cloned(),
);
}
map_nulls_to_inputs(&mut ancestral_rejected_nulls, &box_to_check);
let mut quantifiers = box_to_check.input_quantifiers();
let lhs = quantifiers.next().unwrap();
let rhs = quantifiers.next().unwrap();
let rej_lhs = ancestral_rejected_nulls
.iter()
.any(|c| c.quantifier_id == lhs.id);
let rej_rhs = ancestral_rejected_nulls
.iter()
.any(|c| c.quantifier_id == rhs.id);
if rej_lhs && rhs.quantifier_type == QuantifierType::PRESERVED_FOREACH {
quantifiers_to_change.push(rhs.id);
}
if rej_rhs && lhs.quantifier_type == QuantifierType::PRESERVED_FOREACH {
quantifiers_to_change.push(lhs.id);
}
}
}
if quantifiers_to_change.len() > 0 {
Some((box_id_to_check, quantifiers_to_change))
} else {
None
}
}
fn rewrite(&self, model: &mut Model, mat: Self::Match) {
let (box_id, q_ids) = (mat.0, mat.1);
for q_id in q_ids {
let mut q = model.get_mut_quantifier(q_id);
q.quantifier_type = QuantifierType::FOREACH;
}
let mut r#box = model.get_mut_box(box_id);
if r#box
.input_quantifiers()
.all(|q| q.quantifier_type == QuantifierType::FOREACH)
{
r#box.box_type = match &mut r#box.box_type {
BoxType::OuterJoin(outer_join) => {
Select::new(outer_join.predicates.split_off(0)).into()
}
_ => Select::default().into(),
};
}
}
}
fn unique_ancestor_chain(model: &Model, target: BoxId) -> Option<Vec<BoxId>> {
let ancestor_chain = RefCell::new(vec![]);
let unique_ancestor_chain_found = RefCell::new(false);
let _: Result<(), ()> = mz_ore::graph::try_nonrecursive_dft(
model,
model.top_box,
&mut |model, box_id| {
let r#box = model.get_box(*box_id);
if *unique_ancestor_chain_found.borrow() {
Ok(vec![])
} else {
ancestor_chain.borrow_mut().push(*box_id);
if r#box.ranging_quantifiers().count() > 1 {
Ok(vec![])
} else if *box_id == target {
*unique_ancestor_chain_found.borrow_mut() = true;
Ok(vec![])
} else {
Ok(r#box.input_quantifiers().map(|q| q.input_box).collect())
}
}
},
&mut |_, _| {
if !*unique_ancestor_chain_found.borrow() {
ancestor_chain.borrow_mut().pop();
}
Ok(())
},
);
if *unique_ancestor_chain_found.borrow() {
let mut ancestor_chain = ancestor_chain.take();
ancestor_chain.pop();
Some(ancestor_chain)
} else {
None
}
}
fn map_nulls_to_inputs(
ancestral_rejected_nulls: &mut HashSet<ColumnReference>,
r#box: &BoundRef<'_, QueryBox>,
) {
if !ancestral_rejected_nulls.is_empty() {
let quantifier_id = r#box.ranging_quantifiers().nth(0).unwrap().id;
ancestral_rejected_nulls.retain(|c| c.quantifier_id == quantifier_id);
let propagated_nulls = r#box.attributes.get::<PropagatedNulls>();
*ancestral_rejected_nulls = ancestral_rejected_nulls
.iter()
.flat_map(|c| match propagated_nulls.get(c.position) {
Some(set) => set.clone(),
None => HashSet::new(),
})
.collect();
}
}