Skip to content

Commit cd322f1

Browse files
authored
feat: Expose public method for optimizing physical plans (#11879)
* expose public method for optimizing physical plans using the default planner * cargo fmt
1 parent 679a85f commit cd322f1

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

datafusion-examples/examples/planner_api.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
use datafusion::error::Result;
1919
use datafusion::physical_plan::displayable;
20+
use datafusion::physical_planner::DefaultPhysicalPlanner;
2021
use datafusion::prelude::*;
2122
use datafusion_expr::{LogicalPlan, PlanType};
2223

@@ -123,5 +124,20 @@ async fn to_physical_plan_step_by_step_demo(
123124
.plan
124125
);
125126

127+
// Call the physical optimizer with an existing physical plan (in this
128+
// case the plan is already optimized, but an unoptimized plan would
129+
// typically be used in this context)
130+
// Note that this is not part of the trait but a public method
131+
// on DefaultPhysicalPlanner. Not all planners will provide this feature.
132+
let planner = DefaultPhysicalPlanner::default();
133+
let physical_plan =
134+
planner.optimize_physical_plan(physical_plan, &ctx.state(), |_, _| {})?;
135+
println!(
136+
"Optimized physical plan:\n\n{}\n\n",
137+
displayable(physical_plan.as_ref())
138+
.to_stringified(false, PlanType::InitialPhysicalPlan)
139+
.plan
140+
);
141+
126142
Ok(())
127143
}

datafusion/core/src/physical_planner.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ impl PhysicalPlanner for DefaultPhysicalPlanner {
180180
.create_initial_plan(logical_plan, session_state)
181181
.await?;
182182

183-
self.optimize_internal(plan, session_state, |_, _| {})
183+
self.optimize_physical_plan(plan, session_state, |_, _| {})
184184
}
185185
}
186186
}
@@ -1732,7 +1732,7 @@ impl DefaultPhysicalPlanner {
17321732
}
17331733
}
17341734

1735-
let optimized_plan = self.optimize_internal(
1735+
let optimized_plan = self.optimize_physical_plan(
17361736
input,
17371737
session_state,
17381738
|plan, optimizer| {
@@ -1816,7 +1816,7 @@ impl DefaultPhysicalPlanner {
18161816

18171817
/// Optimize a physical plan by applying each physical optimizer,
18181818
/// calling observer(plan, optimizer after each one)
1819-
fn optimize_internal<F>(
1819+
pub fn optimize_physical_plan<F>(
18201820
&self,
18211821
plan: Arc<dyn ExecutionPlan>,
18221822
session_state: &SessionState,

0 commit comments

Comments
 (0)