This repository was archived by the owner on Jan 7, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
feat(df-repr): add back join order enumeration #204
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,3 +15,4 @@ anyhow = "1" | |
async-recursion = "1" | ||
futures-lite = "2" | ||
futures-util = "0.3" | ||
itertools = "0.11" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,13 +16,11 @@ use datafusion::{ | |
physical_plan::{displayable, explain::ExplainExec, ExecutionPlan}, | ||
physical_planner::{DefaultPhysicalPlanner, PhysicalPlanner}, | ||
}; | ||
use itertools::Itertools; | ||
use optd_datafusion_repr::{ | ||
plan_nodes::{ | ||
ConstantType, OptRelNode, OptRelNodeRef, OptRelNodeTyp, PhysicalHashJoin, | ||
PhysicalNestedLoopJoin, PlanNode, | ||
}, | ||
plan_nodes::{ConstantType, OptRelNode, PlanNode}, | ||
properties::schema::Catalog, | ||
DatafusionOptimizer, | ||
DatafusionOptimizer, MemoExt, | ||
}; | ||
use std::{ | ||
collections::HashMap, | ||
|
@@ -89,93 +87,6 @@ pub struct OptdQueryPlanner { | |
pub optimizer: Arc<Mutex<Option<Box<DatafusionOptimizer>>>>, | ||
} | ||
|
||
#[derive(Debug, Eq, PartialEq, Hash, PartialOrd, Ord)] | ||
enum JoinOrder { | ||
Table(String), | ||
HashJoin(Box<Self>, Box<Self>), | ||
NestedLoopJoin(Box<Self>, Box<Self>), | ||
} | ||
|
||
#[allow(dead_code)] | ||
impl JoinOrder { | ||
pub fn conv_into_logical_join_order(&self) -> LogicalJoinOrder { | ||
match self { | ||
JoinOrder::Table(name) => LogicalJoinOrder::Table(name.clone()), | ||
JoinOrder::HashJoin(left, right) => LogicalJoinOrder::Join( | ||
Box::new(left.conv_into_logical_join_order()), | ||
Box::new(right.conv_into_logical_join_order()), | ||
), | ||
JoinOrder::NestedLoopJoin(left, right) => LogicalJoinOrder::Join( | ||
Box::new(left.conv_into_logical_join_order()), | ||
Box::new(right.conv_into_logical_join_order()), | ||
), | ||
} | ||
} | ||
} | ||
|
||
#[allow(unused)] | ||
#[derive(Debug, Eq, PartialEq, Hash, PartialOrd, Ord)] | ||
enum LogicalJoinOrder { | ||
Table(String), | ||
Join(Box<Self>, Box<Self>), | ||
} | ||
|
||
#[allow(dead_code)] | ||
fn get_join_order(rel_node: OptRelNodeRef) -> Option<JoinOrder> { | ||
match rel_node.typ { | ||
OptRelNodeTyp::PhysicalHashJoin(_) => { | ||
let join = PhysicalHashJoin::from_rel_node(rel_node.clone()).unwrap(); | ||
let left = get_join_order(join.left().into_rel_node())?; | ||
let right = get_join_order(join.right().into_rel_node())?; | ||
Some(JoinOrder::HashJoin(Box::new(left), Box::new(right))) | ||
} | ||
OptRelNodeTyp::PhysicalNestedLoopJoin(_) => { | ||
let join = PhysicalNestedLoopJoin::from_rel_node(rel_node.clone()).unwrap(); | ||
let left = get_join_order(join.left().into_rel_node())?; | ||
let right = get_join_order(join.right().into_rel_node())?; | ||
Some(JoinOrder::NestedLoopJoin(Box::new(left), Box::new(right))) | ||
} | ||
OptRelNodeTyp::PhysicalScan => { | ||
let scan = | ||
optd_datafusion_repr::plan_nodes::PhysicalScan::from_rel_node(rel_node).unwrap(); | ||
Some(JoinOrder::Table(scan.table().to_string())) | ||
} | ||
_ => { | ||
for child in &rel_node.children { | ||
if let Some(res) = get_join_order(child.clone()) { | ||
return Some(res); | ||
} | ||
} | ||
None | ||
} | ||
} | ||
} | ||
|
||
impl std::fmt::Display for LogicalJoinOrder { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
match self { | ||
LogicalJoinOrder::Table(name) => write!(f, "{}", name), | ||
LogicalJoinOrder::Join(left, right) => { | ||
write!(f, "(Join {} {})", left, right) | ||
} | ||
} | ||
} | ||
} | ||
|
||
impl std::fmt::Display for JoinOrder { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
match self { | ||
JoinOrder::Table(name) => write!(f, "{}", name), | ||
JoinOrder::HashJoin(left, right) => { | ||
write!(f, "(HashJoin {} {})", left, right) | ||
} | ||
JoinOrder::NestedLoopJoin(left, right) => { | ||
write!(f, "(NLJ {} {})", left, right) | ||
} | ||
} | ||
} | ||
} | ||
|
||
impl OptdQueryPlanner { | ||
pub fn enable_adaptive(&self) { | ||
self.optimizer | ||
|
@@ -247,7 +158,7 @@ impl OptdQueryPlanner { | |
} | ||
} | ||
|
||
let (_, optimized_rel, meta) = optimizer.cascades_optimize(optd_rel)?; | ||
let (group_id, optimized_rel, meta) = optimizer.cascades_optimize(optd_rel)?; | ||
|
||
if let Some(explains) = &mut explains { | ||
explains.push(StringifiedPlan::new( | ||
|
@@ -258,52 +169,17 @@ impl OptdQueryPlanner { | |
.unwrap() | ||
.explain_to_string(if verbose { Some(&meta) } else { None }), | ||
)); | ||
|
||
// const ENABLE_JOIN_ORDER: bool = false; | ||
|
||
// if ENABLE_JOIN_ORDER { | ||
// let join_order = get_join_order(optimized_rel.clone()); | ||
// explains.push(StringifiedPlan::new( | ||
// PlanType::OptimizedPhysicalPlan { | ||
// optimizer_name: "optd-join-order".to_string(), | ||
// }, | ||
// if let Some(join_order) = join_order { | ||
// join_order.to_string() | ||
// } else { | ||
// "None".to_string() | ||
// }, | ||
// )); | ||
// let bindings = optimizer | ||
// .optd_cascades_optimizer() | ||
// .get_all_group_bindings(group_id, true); | ||
// let mut join_orders = BTreeSet::new(); | ||
// let mut logical_join_orders = BTreeSet::new(); | ||
// for binding in bindings { | ||
// if let Some(join_order) = get_join_order(binding) { | ||
// logical_join_orders.insert(join_order.conv_into_logical_join_order()); | ||
// join_orders.insert(join_order); | ||
// } | ||
// } | ||
// explains.push(StringifiedPlan::new( | ||
// PlanType::OptimizedPhysicalPlan { | ||
// optimizer_name: "optd-all-join-orders".to_string(), | ||
// }, | ||
// join_orders.iter().map(|x| x.to_string()).join("\n"), | ||
// )); | ||
// explains.push(StringifiedPlan::new( | ||
// PlanType::OptimizedPhysicalPlan { | ||
// optimizer_name: "optd-all-logical-join-orders".to_string(), | ||
// }, | ||
// logical_join_orders.iter().map(|x| x.to_string()).join("\n"), | ||
// )); | ||
// } | ||
let join_orders = optimizer | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this optional or always calculated? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. always calculated, as before, unless we can find a better way of passing options through datafusion SQL... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. that's why I didn't close the issue, this is mentioned in the issue |
||
.optd_cascades_optimizer() | ||
.memo() | ||
.enumerate_join_order(group_id); | ||
explains.push(StringifiedPlan::new( | ||
PlanType::OptimizedPhysicalPlan { | ||
optimizer_name: "optd-all-logical-join-orders".to_string(), | ||
}, | ||
join_orders.iter().map(|x| x.to_string()).join("\n"), | ||
)); | ||
} | ||
// println!( | ||
// "{} cost={}", | ||
// get_join_order(optimized_rel.clone()).unwrap(), | ||
// optimizer.optd_optimizer().get_cost_of(group_id) | ||
// ); | ||
// optimizer.dump(Some(group_id)); | ||
ctx.optimizer = Some(&optimizer); | ||
let physical_plan = ctx.conv_from_optd(optimized_rel, meta).await?; | ||
if let Some(explains) = &mut explains { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Exposing the memo table publicly strikes me as a bit scary. I am worried users of the library might try to manipulate the memo table manually.
I'm hoping access is read-only (looks like it is?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes it's read only 🤪