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
use std::collections::HashMap;
use crate::plan::join::JoinBuildState;
use crate::plan::join::JoinClosure;
use crate::plan::AvailableCollections;
use expr::MapFilterProject;
use expr::join_permutations;
use expr::permutation_for_arrangement;
use expr::MirScalarExpr;
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct LinearJoinPlan {
pub source_relation: usize,
pub source_key: Option<Vec<MirScalarExpr>>,
pub initial_closure: Option<JoinClosure>,
pub stage_plans: Vec<LinearStagePlan>,
pub final_closure: Option<JoinClosure>,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct LinearStagePlan {
pub lookup_relation: usize,
pub stream_key: Vec<MirScalarExpr>,
pub stream_thinning: Vec<usize>,
pub lookup_key: Vec<MirScalarExpr>,
pub closure: JoinClosure,
}
impl LinearJoinPlan {
pub fn create_from(
source_relation: usize,
source_arrangement: Option<&(Vec<MirScalarExpr>, HashMap<usize, usize>, Vec<usize>)>,
equivalences: &[Vec<MirScalarExpr>],
join_order: &[(usize, Vec<MirScalarExpr>)],
input_mapper: expr::JoinInputMapper,
mfp_above: &mut MapFilterProject,
available: &[AvailableCollections],
) -> (Self, Vec<AvailableCollections>) {
let mut requested: Vec<AvailableCollections> =
vec![Default::default(); input_mapper.total_inputs()];
let temporal_mfp = mfp_above.extract_temporal();
let mut join_build_state = JoinBuildState::new(
input_mapper.global_columns(source_relation),
&equivalences,
&mfp_above,
);
let unthinned_source_arity = input_mapper.input_arity(source_relation);
let (initial_closure, source_key) =
if let Some((key, permutation, thinning)) = source_arrangement {
let mut mfp = MapFilterProject::new(unthinned_source_arity);
mfp.permute(permutation.clone(), key.len() + thinning.len());
let mfp = mfp.into_plan().unwrap().into_nontemporal().unwrap();
(
Some(JoinClosure {
ready_equivalences: vec![],
before: mfp,
}),
Some(key.clone()),
)
} else {
(None, None)
};
let mut unthinned_stream_arity = initial_closure
.as_ref()
.map(|closure| closure.before.projection.len())
.unwrap_or(unthinned_source_arity);
let mut stage_plans = Vec::with_capacity(join_order.len());
let mut bound_inputs = vec![source_relation];
for (lookup_relation, lookup_key) in join_order.iter() {
let available = &available[*lookup_relation];
let (lookup_permutation, lookup_thinning) = available
.arranged
.iter()
.find_map(|(key, permutation, thinning)| {
if key == lookup_key {
Some((permutation.clone(), thinning.clone()))
} else {
None
}
})
.unwrap_or_else(|| {
let (permutation, thinning) = permutation_for_arrangement::<HashMap<_, _>>(
lookup_key,
input_mapper.input_arity(*lookup_relation),
);
requested[*lookup_relation].arranged.push((
lookup_key.clone(),
permutation.clone(),
thinning.clone(),
));
(permutation, thinning)
});
let lookup_key_rebased = lookup_key
.iter()
.map(|k| input_mapper.map_expr_to_global(k.clone(), *lookup_relation))
.collect::<Vec<_>>();
let stream_key = lookup_key_rebased
.iter()
.map(|expr| {
let mut bound_expr = input_mapper
.find_bound_expr(expr, &bound_inputs, &join_build_state.equivalences)
.expect("Expression in join plan is not bound at time of use");
bound_expr.permute_map(&join_build_state.column_map);
bound_expr
})
.collect::<Vec<_>>();
let (stream_permutation, stream_thinning) =
permutation_for_arrangement::<HashMap<_, _>>(&stream_key, unthinned_stream_arity);
let key_arity = stream_key.len();
let permutation = join_permutations(
key_arity,
stream_permutation.clone(),
stream_thinning.len(),
lookup_permutation.clone(),
);
let closure = join_build_state.add_columns(
input_mapper.global_columns(*lookup_relation),
&lookup_key_rebased,
key_arity + stream_thinning.len() + lookup_thinning.len(),
permutation,
);
let new_unthinned_stream_arity = closure.before.projection.len();
bound_inputs.push(*lookup_relation);
stage_plans.push(LinearStagePlan {
lookup_relation: *lookup_relation,
stream_key,
stream_thinning,
lookup_key: lookup_key.clone(),
closure,
});
unthinned_stream_arity = new_unthinned_stream_arity;
}
let final_closure = join_build_state.complete();
let final_closure = if final_closure.is_identity() {
None
} else {
Some(final_closure)
};
*mfp_above = temporal_mfp;
let plan = LinearJoinPlan {
source_relation,
source_key,
initial_closure,
stage_plans,
final_closure,
};
(plan, requested)
}
}