Skip to content

Commit 26e4d38

Browse files
committed
rename join graph
1 parent c1e0300 commit 26e4d38

2 files changed

Lines changed: 51 additions & 51 deletions

File tree

datafusion/optimizer/src/reorder_join/query_graph.rs renamed to datafusion/optimizer/src/reorder_join/join_graph.rs

Lines changed: 50 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -38,18 +38,18 @@ impl Node {
3838
pub(crate) fn connection_with<'graph>(
3939
&self,
4040
node_id: NodeId,
41-
query_graph: &'graph QueryGraph,
41+
join_graph: &'graph JoinGraph,
4242
) -> Option<&'graph Edge> {
4343
self.connections
4444
.iter()
45-
.filter_map(|edge_id| query_graph.get_edge(*edge_id))
45+
.filter_map(|edge_id| join_graph.get_edge(*edge_id))
4646
.find(move |x| x.nodes.contains(&node_id))
4747
}
4848

49-
pub fn neighbours(&self, node_id: NodeId, query_graph: &QueryGraph) -> Vec<NodeId> {
49+
pub fn neighbours(&self, node_id: NodeId, join_graph: &JoinGraph) -> Vec<NodeId> {
5050
self.connections
5151
.iter()
52-
.filter_map(|edge_id| query_graph.get_edge(*edge_id))
52+
.filter_map(|edge_id| join_graph.get_edge(*edge_id))
5353
.flat_map(|edge| edge.nodes)
5454
.filter(|&id| id != node_id)
5555
.collect()
@@ -65,7 +65,7 @@ pub struct Edge {
6565
pub null_equality: NullEquality,
6666
}
6767

68-
pub struct QueryGraph {
68+
pub struct JoinGraph {
6969
pub(crate) nodes: VecMap<Node>,
7070
edges: VecMap<Edge>,
7171
/// Non-equi predicates hoisted out of decomposed `Join.filter` clauses
@@ -74,17 +74,17 @@ pub struct QueryGraph {
7474
filters: Vec<Expr>,
7575
}
7676

77-
impl QueryGraph {
77+
impl JoinGraph {
7878
pub fn try_from_logical_plan(
7979
value: LogicalPlan,
80-
) -> Result<(QueryGraph, Vec<LogicalPlan>), DataFusionError> {
80+
) -> Result<(JoinGraph, Vec<LogicalPlan>), DataFusionError> {
8181
// First, extract the join subtree from any wrapper operators
8282
let (join_subtree, wrappers) = extract_join_subtree(value)?;
8383

8484
// Now convert only the join subtree to a query graph
85-
let mut query_graph = QueryGraph::new();
86-
flatten_joins_recursive(join_subtree, &mut query_graph)?;
87-
Ok((query_graph, wrappers))
85+
let mut join_graph = JoinGraph::new();
86+
flatten_joins_recursive(join_subtree, &mut join_graph)?;
87+
Ok((join_graph, wrappers))
8888
}
8989

9090
pub(crate) fn new() -> Self {
@@ -300,7 +300,7 @@ pub fn reconstruct_plan(
300300

301301
fn flatten_joins_recursive(
302302
plan: LogicalPlan,
303-
query_graph: &mut QueryGraph,
303+
join_graph: &mut JoinGraph,
304304
) -> Result<()> {
305305
match plan {
306306
// Inner joins decompose into the graph. (Cross joins are encoded as
@@ -312,17 +312,17 @@ fn flatten_joins_recursive(
312312
LogicalPlan::Join(join) if join.join_type == JoinType::Inner => {
313313
if let Some(filter) = join.filter.clone() {
314314
for conj in split_conjunction_owned(filter) {
315-
query_graph.add_filter(conj);
315+
join_graph.add_filter(conj);
316316
}
317317
}
318318

319319
flatten_joins_recursive(
320320
Arc::unwrap_or_clone(Arc::clone(&join.left)),
321-
query_graph,
321+
join_graph,
322322
)?;
323323
flatten_joins_recursive(
324324
Arc::unwrap_or_clone(Arc::clone(&join.right)),
325-
query_graph,
325+
join_graph,
326326
)?;
327327

328328
// Process each equijoin predicate to find which nodes it connects
@@ -332,7 +332,7 @@ fn flatten_joins_recursive(
332332
let right_columns = right_key.column_refs();
333333

334334
// Filter nodes by checking which ones contain the columns from each expression
335-
let matching_nodes: Vec<NodeId> = query_graph
335+
let matching_nodes: Vec<NodeId> = join_graph
336336
.nodes()
337337
.filter_map(|(node_id, node)| {
338338
let schema = node.plan.schema();
@@ -369,10 +369,10 @@ fn flatten_joins_recursive(
369369
let node_id_b = matching_nodes[1];
370370

371371
// Add an edge if one doesn't exist yet
372-
if let Some(node_a) = query_graph.get_node(node_id_a)
373-
&& node_a.connection_with(node_id_b, query_graph).is_none()
372+
if let Some(node_a) = join_graph.get_node(node_id_a)
373+
&& node_a.connection_with(node_id_b, join_graph).is_none()
374374
{
375-
query_graph.add_edge(
375+
join_graph.add_edge(
376376
node_id_a,
377377
node_id_b,
378378
join.on.clone(),
@@ -387,7 +387,7 @@ fn flatten_joins_recursive(
387387
// Non-inner joins (Left/Right/Full/Semi/Anti/Mark) are not freely
388388
// reorderable, so the entire join subtree becomes one opaque leaf.
389389
LogicalPlan::Join(join) => {
390-
query_graph.add_node(Arc::new(LogicalPlan::Join(join)));
390+
join_graph.add_node(Arc::new(LogicalPlan::Join(join)));
391391
Ok(())
392392
}
393393
// A `Filter` directly above a decomposable join is part of the join
@@ -400,17 +400,17 @@ fn flatten_joins_recursive(
400400
) =>
401401
{
402402
for conj in split_conjunction_owned(filter.predicate) {
403-
query_graph.add_filter(conj);
403+
join_graph.add_filter(conj);
404404
}
405405
let inner = Arc::unwrap_or_clone(filter.input);
406-
flatten_joins_recursive(inner, query_graph)
406+
flatten_joins_recursive(inner, join_graph)
407407
}
408408
// Anything else (Aggregate, Projection, Sort, Limit, Window, Filter
409409
// not over a decomposable join, base scans, ...) is absorbed as an
410410
// opaque leaf. Joins nested inside such a wrapper are intentionally
411411
// hidden from the enumerator (matches Databend's dphyp behavior).
412412
other => {
413-
query_graph.add_node(Arc::new(other));
413+
join_graph.add_node(Arc::new(other));
414414
Ok(())
415415
}
416416
}
@@ -470,7 +470,7 @@ mod tests {
470470
})
471471
}
472472

473-
/// Test converting a three-way join with filter into a QueryGraph
473+
/// Test converting a three-way join with filter into a JoinGraph
474474
#[test]
475475
fn test_try_from_three_way_join_with_filter() -> Result<(), DataFusionError> {
476476
// Create three-way join: customer JOIN orders JOIN lineitem
@@ -497,23 +497,23 @@ mod tests {
497497
.build()
498498
.unwrap();
499499

500-
// Convert to QueryGraph
501-
let query_graph = QueryGraph::try_from_logical_plan(plan)?.0;
500+
// Convert to JoinGraph
501+
let join_graph = JoinGraph::try_from_logical_plan(plan)?.0;
502502

503503
// Verify structure: 3 nodes, 2 edges
504-
assert_eq!(query_graph.nodes().count(), 3);
505-
assert_eq!(query_graph.edges.iter().count(), 2);
504+
assert_eq!(join_graph.nodes().count(), 3);
505+
assert_eq!(join_graph.edges.iter().count(), 2);
506506

507507
// Verify connectivity: one node has 2 connections (orders), two nodes have 1
508-
let mut connections: Vec<usize> = query_graph
508+
let mut connections: Vec<usize> = join_graph
509509
.nodes()
510510
.map(|(_, node)| node.connections().len())
511511
.collect();
512512
connections.sort();
513513
assert_eq!(connections, vec![1, 1, 2]);
514514

515515
// Verify edges have correct join predicates
516-
let edges: Vec<&Edge> = query_graph.edges.iter().map(|(_, e)| e).collect();
516+
let edges: Vec<&Edge> = join_graph.edges.iter().map(|(_, e)| e).collect();
517517
assert!(
518518
edges
519519
.iter()
@@ -528,8 +528,8 @@ mod tests {
528528
);
529529

530530
// The non-equi join.filter should now live in the side-channel
531-
assert_eq!(query_graph.filters().len(), 1);
532-
let f = format!("{}", query_graph.filters()[0]);
531+
assert_eq!(join_graph.filters().len(), 1);
532+
let f = format!("{}", join_graph.filters()[0]);
533533
assert!(
534534
f.contains("l_quantity"),
535535
"expected l_quantity in side-channel filter, got: {f}"
@@ -566,12 +566,12 @@ mod tests {
566566
.build()
567567
.unwrap();
568568

569-
let query_graph = QueryGraph::try_from_logical_plan(plan)?.0;
569+
let join_graph = JoinGraph::try_from_logical_plan(plan)?.0;
570570

571-
assert_eq!(query_graph.nodes().count(), 3);
572-
assert_eq!(query_graph.edges.iter().count(), 2);
573-
assert_eq!(query_graph.filters().len(), 1);
574-
let f = format!("{}", query_graph.filters()[0]);
571+
assert_eq!(join_graph.nodes().count(), 3);
572+
assert_eq!(join_graph.edges.iter().count(), 2);
573+
assert_eq!(join_graph.filters().len(), 1);
574+
let f = format!("{}", join_graph.filters()[0]);
575575
assert!(
576576
f.contains("o_totalprice"),
577577
"expected o_totalprice in side-channel filter, got: {f}"
@@ -610,15 +610,15 @@ mod tests {
610610
.build()
611611
.unwrap();
612612

613-
let query_graph = QueryGraph::try_from_logical_plan(plan)?.0;
613+
let join_graph = JoinGraph::try_from_logical_plan(plan)?.0;
614614

615615
// Two leaves: the aggregated subtree (opaque) and lineitem.
616-
assert_eq!(query_graph.nodes().count(), 2);
617-
assert_eq!(query_graph.edges.iter().count(), 1);
618-
assert_eq!(query_graph.filters().len(), 0);
616+
assert_eq!(join_graph.nodes().count(), 2);
617+
assert_eq!(join_graph.edges.iter().count(), 1);
618+
assert_eq!(join_graph.filters().len(), 0);
619619

620620
// One leaf must be a LogicalPlan::Aggregate.
621-
let has_aggregate_leaf = query_graph
621+
let has_aggregate_leaf = join_graph
622622
.nodes()
623623
.any(|(_, n)| matches!(n.plan.as_ref(), LogicalPlan::Aggregate(_)));
624624
assert!(
@@ -657,14 +657,14 @@ mod tests {
657657
.build()
658658
.unwrap();
659659

660-
let query_graph = QueryGraph::try_from_logical_plan(plan)?.0;
660+
let join_graph = JoinGraph::try_from_logical_plan(plan)?.0;
661661

662662
// Two leaves: the LEFT-join subtree (opaque) and lineitem.
663-
assert_eq!(query_graph.nodes().count(), 2);
664-
assert_eq!(query_graph.edges.iter().count(), 1);
663+
assert_eq!(join_graph.nodes().count(), 2);
664+
assert_eq!(join_graph.edges.iter().count(), 1);
665665

666666
// The opaque-leaf node's plan should be a LogicalPlan::Join with Left type.
667-
let has_left_join_leaf = query_graph.nodes().any(|(_, n)| {
667+
let has_left_join_leaf = join_graph.nodes().any(|(_, n)| {
668668
matches!(
669669
n.plan.as_ref(),
670670
LogicalPlan::Join(j) if j.join_type == JoinType::Left
@@ -696,13 +696,13 @@ mod tests {
696696
.build()
697697
.unwrap();
698698

699-
let query_graph = QueryGraph::try_from_logical_plan(plan)?.0;
699+
let join_graph = JoinGraph::try_from_logical_plan(plan)?.0;
700700

701-
assert_eq!(query_graph.nodes().count(), 1);
702-
assert_eq!(query_graph.edges.iter().count(), 0);
703-
assert_eq!(query_graph.filters().len(), 0);
701+
assert_eq!(join_graph.nodes().count(), 1);
702+
assert_eq!(join_graph.edges.iter().count(), 0);
703+
assert_eq!(join_graph.filters().len(), 0);
704704

705-
let only_node = query_graph.nodes().next().unwrap().1;
705+
let only_node = join_graph.nodes().next().unwrap().1;
706706
assert!(matches!(
707707
only_node.plan.as_ref(),
708708
LogicalPlan::Join(j) if j.join_type == JoinType::Left

datafusion/optimizer/src/reorder_join/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@
1717

1818
//! Optimizer rule for reordering joins to minimize query execution cost
1919
20-
pub mod query_graph;
20+
pub mod join_graph;

0 commit comments

Comments
 (0)