Skip to content

Commit ef5ad29

Browse files
authored
style(graph): add HepGraph::parent_id to replace HepNode to OptExprNode (#47)
1 parent eee925d commit ef5ad29

2 files changed

Lines changed: 25 additions & 44 deletions

File tree

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,5 @@ print_batches(&vec_batch_full_join)?;
6868
- CreateTable
6969

7070
### Thanks For
71-
- [sqlrs](https://github.com/Fedomn/sqlrs): 主要参考资料,Optimizer、Executor均参考自sqlrs的设计
71+
- [Fedomn/sqlrs](https://github.com/Fedomn/sqlrs): 主要参考资料,Optimizer、Executor均参考自sqlrs的设计
72+
- [systemxlabs/tinysql](https://github.com/systemxlabs/tinysql)

src/optimizer/heuristic/graph.rs

Lines changed: 23 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -10,44 +10,34 @@ use crate::planner::operator::Operator;
1010
/// HepNodeId is used in optimizer to identify a node.
1111
pub type HepNodeId = NodeIndex<OptExprNodeId>;
1212

13-
#[derive(Debug, PartialEq, Clone)]
14-
pub struct HepNode {
15-
node: OptExprNode,
16-
parent_id: Option<HepNodeId>,
17-
}
18-
1913
#[derive(Debug)]
2014
pub struct HepGraph {
21-
graph: StableDiGraph<HepNode, usize, usize>,
15+
graph: StableDiGraph<OptExprNode, usize, usize>,
2216
root_index: HepNodeId,
2317
pub version: usize,
2418
}
2519

2620
impl HepGraph {
2721
pub fn new(root: LogicalPlan) -> Self {
2822
fn graph_filling(
29-
graph: &mut StableDiGraph<HepNode, usize, usize>,
23+
graph: &mut StableDiGraph<OptExprNode, usize, usize>,
3024
LogicalPlan{ operator, childrens }: LogicalPlan,
31-
source_id: Option<HepNodeId>
3225
) -> HepNodeId {
33-
let index = graph.add_node(HepNode {
34-
node: OptExprNode::OperatorRef(operator),
35-
parent_id: source_id,
36-
});
26+
let index = graph.add_node(OptExprNode::OperatorRef(operator));
27+
3728
for (order, child) in childrens.into_iter().enumerate() {
38-
let child_index = graph_filling(graph, child, Some(index));
29+
let child_index = graph_filling(graph, child);
3930
let _ = graph.add_edge(index, child_index, order);
4031
}
4132

4233
index
4334
}
4435

45-
let mut graph = StableDiGraph::<HepNode, usize, usize>::default();
36+
let mut graph = StableDiGraph::<OptExprNode, usize, usize>::default();
4637

4738
let root_index = graph_filling(
4839
&mut graph,
4940
root,
50-
None
5141
);
5242

5343
HepGraph {
@@ -57,27 +47,24 @@ impl HepGraph {
5747
}
5848
}
5949

50+
pub fn parent_id(&self, node_id: HepNodeId) -> Option<HepNodeId> {
51+
self.graph
52+
.neighbors_directed(node_id, petgraph::Direction::Incoming)
53+
.next()
54+
}
55+
6056
pub fn add_root(&mut self, new_node: OptExprNode) {
6157
let old_root_id = mem::replace(
6258
&mut self.root_index,
63-
self.graph.add_node(HepNode {
64-
node: new_node,
65-
parent_id: None,
66-
})
59+
self.graph.add_node(new_node)
6760
);
68-
let old_root = self.graph.node_weight_mut(old_root_id).unwrap();
69-
70-
old_root.parent_id = Some(self.root_index);
7161

7262
self.graph.add_edge(self.root_index, old_root_id, 0);
7363
self.version += 1;
7464
}
7565

7666
pub fn add_node(&mut self, source_id: HepNodeId, children_option: Option<HepNodeId>, new_node: OptExprNode) {
77-
let new_index = self.graph.add_node(HepNode {
78-
node: new_node,
79-
parent_id: Some(source_id),
80-
});
67+
let new_index = self.graph.add_node(new_node);
8168

8269
let mut order = self.graph
8370
.edges(source_id)
@@ -90,7 +77,6 @@ impl HepGraph {
9077
.remove_edge(old_edge_id)
9178
.unwrap_or(0);
9279

93-
self.graph[children_id].parent_id = Some(new_index);
9480
self.graph.add_edge(new_index, children_id, 0);
9581
});
9682
}
@@ -100,20 +86,15 @@ impl HepGraph {
10086
}
10187

10288
pub fn replace_node(&mut self, source_id: HepNodeId, new_node: OptExprNode) {
103-
let node = &self.graph[source_id];
104-
105-
self.graph[source_id] = HepNode {
106-
node: new_node,
107-
parent_id: node.parent_id,
108-
};
89+
self.graph[source_id] = new_node;
10990
self.version += 1;
11091
}
11192

11293
pub fn swap_node(&mut self, a: HepNodeId, b: HepNodeId) {
113-
let tmp = self.graph[a].node.clone();
94+
let tmp = self.graph[a].clone();
11495

115-
self.graph[a].node = mem::replace(
116-
&mut self.graph[b].node,
96+
self.graph[a] = mem::replace(
97+
&mut self.graph[b],
11798
tmp
11899
);
119100
self.version += 1;
@@ -127,7 +108,7 @@ impl HepGraph {
127108
.map(|edge_ref| edge_ref.target())
128109
.collect_vec();
129110

130-
if let Some(parent_id) = source_node.parent_id {
111+
if let Some(parent_id) = self.parent_id(source_id) {
131112
if let Some(edge) = self.graph.find_edge(parent_id, source_id) {
132113
let weight = *self.graph.edge_weight(edge)
133114
.unwrap_or(&0);
@@ -145,7 +126,6 @@ impl HepGraph {
145126

146127
self.version += 1;
147128
self.graph.remove_node(source_id)
148-
.map(|node| node.node)
149129
}
150130

151131
/// Traverse the graph in BFS order.
@@ -167,12 +147,12 @@ impl HepGraph {
167147
}
168148
}
169149

170-
pub fn node(&self, node_id: HepNodeId) -> Option<&HepNode> {
150+
pub fn node(&self, node_id: HepNodeId) -> Option<&OptExprNode> {
171151
self.graph.node_weight(node_id)
172152
}
173153

174154
pub fn operator(&self, node_id: HepNodeId) -> &Operator {
175-
match &self.graph[node_id].node {
155+
match &self.graph[node_id] {
176156
OptExprNode::OperatorRef(op) => op,
177157
OptExprNode::OptExpr(node_id) => self.operator(HepNodeId::new(*node_id)),
178158
}
@@ -238,7 +218,7 @@ mod tests {
238218
use petgraph::stable_graph::{EdgeIndex, IndexType, NodeIndex};
239219
use crate::binder::test::select_sql_run;
240220
use crate::optimizer::core::opt_expr::{OptExprNode, OptExprNodeId};
241-
use crate::optimizer::heuristic::graph::{HepGraph, HepNode, HepNodeId};
221+
use crate::optimizer::heuristic::graph::{HepGraph, HepNodeId};
242222
use crate::planner::operator::Operator;
243223

244224
#[test]
@@ -282,7 +262,7 @@ mod tests {
282262
assert_eq!(graph.graph.edge_weight(EdgeIndex::new(5)), Some(&1));
283263
assert_eq!(
284264
graph.graph.node_weight(NodeIndex::new(5)),
285-
Some(&HepNode { node: OptExprNode::OptExpr(5), parent_id: Some(NodeIndex::new(1)) })
265+
Some(&OptExprNode::OptExpr(5))
286266
);
287267
assert_eq!(graph.root_index, NodeIndex::new(0));
288268

0 commit comments

Comments
 (0)