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
use crate::TransformArgs;
use mz_expr::visit::Visit;
use mz_expr::{MirRelationExpr, MirScalarExpr};
#[derive(Debug)]
pub struct ProjectionExtraction;
impl crate::Transform for ProjectionExtraction {
#[tracing::instrument(
target = "optimizer"
level = "trace",
skip_all,
fields(path.segment = "projection_extraction")
)]
fn transform(
&self,
relation: &mut MirRelationExpr,
_: TransformArgs,
) -> Result<(), crate::TransformError> {
let result = relation.try_visit_mut_post(&mut Self::action);
mz_repr::explain::trace_plan(&*relation);
result
}
}
impl ProjectionExtraction {
pub fn action(relation: &mut MirRelationExpr) -> Result<(), crate::TransformError> {
if let MirRelationExpr::Map { input, scalars } = relation {
if scalars
.iter()
.any(|s| matches!(s, MirScalarExpr::Column(_)))
{
let input_arity = input.arity();
let mut outputs: Vec<_> = (0..input_arity).collect();
let mut dropped = 0;
scalars.retain(|scalar| {
if let MirScalarExpr::Column(col) = scalar {
dropped += 1;
outputs.push(outputs[*col]);
false } else {
outputs.push(outputs.len() - dropped);
true }
});
if dropped > 0 {
for scalar in scalars {
scalar.permute(&outputs);
}
*relation = relation.take_dangerous().project(outputs);
}
}
} else if let MirRelationExpr::Reduce {
input: _,
group_key,
aggregates,
monotonic: _,
expected_group_size: _,
} = relation
{
let mut projection = Vec::new();
let mut finger = 0;
while finger < group_key.len() {
if let Some(position) = group_key[..finger]
.iter()
.position(|x| x == &group_key[finger])
{
projection.push(position);
group_key.remove(finger);
} else {
projection.push(finger);
finger += 1;
}
}
let mut finger = 0;
while finger < aggregates.len() {
if let Some(position) = aggregates[..finger]
.iter()
.position(|x| x == &aggregates[finger])
{
projection.push(group_key.len() + position);
aggregates.remove(finger);
} else {
projection.push(group_key.len() + finger);
finger += 1;
}
}
if projection.iter().enumerate().any(|(i, p)| i != *p) {
*relation = relation.take_dangerous().project(projection);
}
}
Ok(())
}
}