Skip to content

Commit 915f01b

Browse files
authored
style(graph): split test (#48)
1 parent ef5ad29 commit 915f01b

1 file changed

Lines changed: 81 additions & 90 deletions

File tree

src/optimizer/heuristic/graph.rs

Lines changed: 81 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -172,18 +172,6 @@ impl HepGraph {
172172
.collect_vec()
173173
}
174174

175-
pub fn to_opt_expr(&self, start: HepNodeId) -> OptExpr {
176-
let children = self
177-
.children_at(start)
178-
.iter()
179-
.map(|&id| self.to_opt_expr(id))
180-
.collect::<Vec<_>>();
181-
OptExpr::new(
182-
OptExprNode::OperatorRef(self.operator(start).clone()),
183-
children,
184-
)
185-
}
186-
187175
pub fn to_plan_with_index(&self, start_index: HepNodeId) -> LogicalPlan {
188176
let mut root_plan = LogicalPlan {
189177
operator: self.operator(start_index).clone(),
@@ -222,9 +210,25 @@ mod tests {
222210
use crate::planner::operator::Operator;
223211

224212
#[test]
225-
fn test_graph() -> Result<()> {
213+
fn test_graph_for_plan() -> Result<()> {
226214
let plan = select_sql_run("select * from t1 left join t2 on c1 = c3")?;
227-
let mut graph = HepGraph::new(plan.clone());
215+
let graph = HepGraph::new(plan);
216+
217+
assert!(graph.graph.contains_edge(NodeIndex::new(1), NodeIndex::new(2)));
218+
assert!(graph.graph.contains_edge(NodeIndex::new(1), NodeIndex::new(3)));
219+
assert!(graph.graph.contains_edge(NodeIndex::new(0), NodeIndex::new(1)));
220+
221+
assert_eq!(graph.graph.edge_weight(EdgeIndex::new(0)), Some(&0));
222+
assert_eq!(graph.graph.edge_weight(EdgeIndex::new(1)), Some(&1));
223+
assert_eq!(graph.graph.edge_weight(EdgeIndex::new(2)), Some(&0));
224+
225+
Ok(())
226+
}
227+
228+
#[test]
229+
fn test_graph_add_node() -> Result<()> {
230+
let plan = select_sql_run("select * from t1 left join t2 on c1 = c3")?;
231+
let mut graph = HepGraph::new(plan);
228232

229233
graph.add_node(
230234
HepNodeId::new(1),
@@ -244,114 +248,101 @@ mod tests {
244248
OptExprNode::OperatorRef(Operator::Dummy)
245249
);
246250

247-
graph.replace_node(HepNodeId::new(5), OptExprNode::OptExpr(OptExprNodeId::new(5)));
248-
249-
assert_eq!(graph.version, 4);
250-
251-
assert!(graph.graph.contains_edge(NodeIndex::new(1), NodeIndex::new(2)));
252-
assert!(graph.graph.contains_edge(NodeIndex::new(1), NodeIndex::new(3)));
253-
assert!(graph.graph.contains_edge(NodeIndex::new(0), NodeIndex::new(1)));
254251
assert!(graph.graph.contains_edge(NodeIndex::new(5), NodeIndex::new(4)));
255252
assert!(graph.graph.contains_edge(NodeIndex::new(1), NodeIndex::new(5)));
256253
assert!(graph.graph.contains_edge(NodeIndex::new(5), NodeIndex::new(6)));
257-
assert_eq!(graph.graph.edge_weight(EdgeIndex::new(0)), Some(&0));
258-
assert_eq!(graph.graph.edge_weight(EdgeIndex::new(1)), Some(&1));
259-
assert_eq!(graph.graph.edge_weight(EdgeIndex::new(2)), Some(&0));
254+
260255
assert_eq!(graph.graph.edge_weight(EdgeIndex::new(3)), Some(&0));
261256
assert_eq!(graph.graph.edge_weight(EdgeIndex::new(4)), Some(&2));
262257
assert_eq!(graph.graph.edge_weight(EdgeIndex::new(5)), Some(&1));
263-
assert_eq!(
264-
graph.graph.node_weight(NodeIndex::new(5)),
265-
Some(&OptExprNode::OptExpr(5))
266-
);
267-
assert_eq!(graph.root_index, NodeIndex::new(0));
268258

269-
let old_root_node = graph.remove_node(
270-
HepNodeId::new(0),
271-
false
272-
).unwrap();
259+
Ok(())
260+
}
273261

274-
graph.remove_node(
275-
HepNodeId::new(5),
276-
true
277-
);
262+
#[test]
263+
fn test_graph_replace_node() -> Result<()> {
264+
let plan = select_sql_run("select * from t1 left join t2 on c1 = c3")?;
265+
let mut graph = HepGraph::new(plan);
278266

279-
assert_eq!(graph.version, 6);
267+
graph.replace_node(HepNodeId::new(1), OptExprNode::OperatorRef(Operator::Dummy));
280268

281-
assert!(graph.graph.contains_edge(NodeIndex::new(1), NodeIndex::new(2)));
282-
assert!(graph.graph.contains_edge(NodeIndex::new(1), NodeIndex::new(3)));
283-
assert_eq!(graph.graph.edge_weight(EdgeIndex::new(0)), Some(&0));
284-
assert_eq!(graph.graph.edge_weight(EdgeIndex::new(1)), Some(&1));
285-
assert_eq!(graph.root_index, NodeIndex::new(1));
269+
assert!(matches!(graph.operator(HepNodeId::new(1)), Operator::Dummy));
286270

287-
let final_plan = graph.to_plan();
271+
Ok(())
272+
}
288273

289-
match final_plan.operator {
290-
Operator::Join(_) => (),
291-
_ => unreachable!("Should be a join operator"),
292-
}
274+
#[test]
275+
fn test_graph_remove_middle_node_by_single() -> Result<()> {
276+
let plan = select_sql_run("select * from t1 left join t2 on c1 = c3")?;
277+
let mut graph = HepGraph::new(plan);
293278

294-
assert_eq!(final_plan.childrens.len(), 2);
279+
graph.remove_node(HepNodeId::new(1), false);
295280

296-
let part_plan = graph.to_plan_with_index(HepNodeId::new(3));
281+
assert_eq!(graph.graph.edge_count(), 2);
297282

298-
match part_plan.operator {
299-
Operator::Scan(_) => (),
300-
_ => unreachable!("Should be a scan operator"),
301-
}
283+
assert!(graph.graph.contains_edge(NodeIndex::new(0), NodeIndex::new(2)));
284+
assert!(graph.graph.contains_edge(NodeIndex::new(0), NodeIndex::new(3)));
302285

303-
assert_eq!(part_plan.childrens.len(), 0);
286+
Ok(())
287+
}
304288

305-
let root_expr = graph.to_opt_expr(HepNodeId::new(1));
289+
#[test]
290+
fn test_graph_remove_middle_node_with_childrens() -> Result<()> {
291+
let plan = select_sql_run("select * from t1 left join t2 on c1 = c3")?;
292+
let mut graph = HepGraph::new(plan);
306293

307-
match root_expr.root.get_operator() {
308-
Operator::Join(_) => (),
309-
_ => unreachable!("Should be a join operator"),
310-
}
294+
graph.remove_node(HepNodeId::new(1), true);
311295

312-
assert_eq!(root_expr.childrens.len(), 2);
296+
assert_eq!(graph.graph.edge_count(), 0);
313297

314-
let part_expr = graph.to_opt_expr(HepNodeId::new(3));
298+
Ok(())
299+
}
315300

316-
match part_expr.root.get_operator() {
317-
Operator::Scan(_) => (),
318-
_ => unreachable!("Should be a scan operator"),
319-
}
301+
#[test]
302+
fn test_graph_swap_node() -> Result<()> {
303+
let plan = select_sql_run("select * from t1 left join t2 on c1 = c3")?;
304+
let mut graph = HepGraph::new(plan);
320305

321-
assert_eq!(part_expr.childrens.len(), 0);
306+
let before_op_0 = graph.operator(HepNodeId::new(0)).clone();
307+
let before_op_1 = graph.operator(HepNodeId::new(1)).clone();
322308

323-
graph.add_root(old_root_node);
309+
graph.swap_node(HepNodeId::new(0), HepNodeId::new(1));
324310

325-
assert_eq!(graph.version, 7);
311+
let op_0 = graph.operator(HepNodeId::new(0));
312+
let op_1 = graph.operator(HepNodeId::new(1));
326313

327-
let re_root_plan = graph.to_plan();
314+
assert_eq!(op_0, &before_op_1);
315+
assert_eq!(op_1, &before_op_0);
328316

329-
match re_root_plan.operator {
330-
Operator::Project(_) => (),
331-
_ => unreachable!("Should be a project operator"),
332-
}
317+
Ok(())
318+
}
319+
320+
#[test]
321+
fn test_graph_add_root() -> Result<()> {
322+
let plan = select_sql_run("select * from t1 left join t2 on c1 = c3")?;
323+
let mut graph = HepGraph::new(plan);
324+
325+
graph.add_root(OptExprNode::OperatorRef(Operator::Dummy));
333326

334-
assert_eq!(re_root_plan.childrens.len(), 1);
327+
assert_eq!(graph.graph.edge_count(), 4);
328+
assert!(graph.graph.contains_edge(NodeIndex::new(4), NodeIndex::new(0)));
329+
assert_eq!(graph.graph.edge_weight(EdgeIndex::new(3)), Some(&0));
330+
331+
Ok(())
332+
}
335333

336-
graph.swap_node(HepNodeId::new(2), HepNodeId::new(3));
334+
#[test]
335+
fn test_graph_to_plan() -> Result<()> {
336+
let plan = select_sql_run("select * from t1 left join t2 on c1 = c3")?;
337+
let graph = HepGraph::new(plan.clone());
337338

338-
assert_eq!(graph.version, 8);
339+
let plan_for_graph = graph.to_plan();
339340

340-
let swap_plan = graph.to_plan();
341+
assert_eq!(plan, plan_for_graph);
341342

342-
match &swap_plan.childrens[0].childrens[0].operator {
343-
Operator::Scan(op) => {
344-
assert_eq!(op.columns[0].referenced_columns()[0].name, "c3");
345-
},
346-
_ => unreachable!("Should be a scan operator"),
347-
}
343+
let plan_by_index = graph.to_plan_with_index(HepNodeId::new(1));
348344

349-
match &swap_plan.childrens[0].childrens[1].operator {
350-
Operator::Scan(op) => {
351-
assert_eq!(op.columns[0].referenced_columns()[0].name, "c1");
352-
},
353-
_ => unreachable!("Should be a scan operator"),
354-
}
345+
assert_eq!(plan.childrens[0], plan_by_index);
355346

356347
Ok(())
357348
}

0 commit comments

Comments
 (0)