Repository navigation
Expand file tree
/
Copy pathjoin_pruning.rs
More file actions
114 lines (106 loc) · 4.27 KB
/
Copy pathjoin_pruning.rs
File metadata and controls
114 lines (106 loc) · 4.27 KB
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
use std::collections::HashSet;
use itertools::Itertools;
use crate::{
query_graph::{
optimizer::{
utils::{
apply_map_to_parents_and_replace_input, required_columns_from_parents,
required_columns_to_column_map,
},
OptRuleType, Rule,
},
properties::num_columns,
NodeId, QueryGraph, QueryNode,
},
scalar_expr::{
rewrite::{apply_column_map, rewrite_expr_vec},
visitor::store_input_dependencies,
ScalarExpr,
},
};
/// Rule that given a shared join where all its parents are pruning projections, computes
/// the superset of columns required by all its parents, and prunes the columns not used
/// by any of them, replacing the parents of the join with projections over the pruned
/// join.
pub struct JoinPruningRule {}
impl Rule for JoinPruningRule {
fn rule_type(&self) -> OptRuleType {
OptRuleType::TopDown
}
fn apply(
&self,
query_graph: &mut QueryGraph,
node_id: NodeId,
) -> Option<Vec<(NodeId, NodeId)>> {
if let QueryNode::Join {
join_type,
left,
right,
conditions,
} = query_graph.node(node_id)
{
if let Some(required_columns) = required_columns_from_parents(query_graph, node_id) {
// Rewrite conditions
let column_map = required_columns_to_column_map(&required_columns);
let mut required_columns_including_join = column_map
.iter()
.map(|(col, _)| *col)
.collect::<HashSet<_>>();
for condition in conditions.iter() {
store_input_dependencies(condition, &mut required_columns_including_join);
}
if required_columns_including_join.len() == num_columns(&query_graph, node_id) {
return None;
}
let join_column_map =
required_columns_to_column_map(&required_columns_including_join);
let new_conditions = rewrite_expr_vec(conditions, &mut |e| {
apply_column_map(e, &join_column_map).unwrap()
});
// Prune the branches
let left_num_columns = num_columns(query_graph, *left);
let (left_columns, right_columns): (Vec<usize>, Vec<usize>) =
required_columns_including_join
.iter()
.sorted()
.partition(|col| **col < left_num_columns);
let left_outputs = left_columns
.iter()
.map(|i| ScalarExpr::InputRef { index: *i }.into())
.collect::<Vec<_>>();
let right_outputs = right_columns
.iter()
.map(|i| {
ScalarExpr::InputRef {
index: *i - left_num_columns,
}
.into()
})
.collect::<Vec<_>>();
let left = *left;
let right = *right;
let join_type = *join_type;
let new_left = query_graph.project(left, left_outputs);
let new_right = query_graph.project(right, right_outputs);
let new_join = query_graph.join(join_type, new_left, new_right, new_conditions);
// Prune the columns used by the join conditions but not by the parents
let pruning_proj_outputs = required_columns_including_join
.iter()
.sorted()
.enumerate()
.filter(|(_, orig_col)| required_columns.contains(&orig_col))
.map(|(i, _)| ScalarExpr::input_ref(i).into())
.collect();
let pruning_proj = query_graph.project(new_join, pruning_proj_outputs);
// Rewrite the parent projections
return Some(apply_map_to_parents_and_replace_input(
query_graph,
node_id,
&column_map,
pruning_proj,
));
}
}
None
}
}