Skip to content

Commit 2b8a0fb

Browse files
authored
Implement OptimizerConfig for SessionState (#4775)
1 parent 21169b9 commit 2b8a0fb

File tree

1 file changed

+28
-15
lines changed

1 file changed

+28
-15
lines changed

datafusion/core/src/execution/context.rs

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ use crate::logical_expr::{
5959
CreateView, DropTable, DropView, Explain, LogicalPlan, LogicalPlanBuilder,
6060
SetVariable, TableSource, TableType, UNNAMED_TABLE,
6161
};
62-
use crate::optimizer::{OptimizerContext, OptimizerRule};
62+
use crate::optimizer::OptimizerRule;
6363
use datafusion_sql::{ResolvedTableReference, TableReference};
6464

6565
use crate::physical_optimizer::coalesce_batches::CoalesceBatches;
@@ -93,6 +93,7 @@ use crate::physical_optimizer::global_sort_selection::GlobalSortSelection;
9393
use crate::physical_optimizer::optimize_sorts::OptimizeSorts;
9494
use crate::physical_optimizer::pipeline_checker::PipelineChecker;
9595
use crate::physical_optimizer::pipeline_fixer::PipelineFixer;
96+
use datafusion_optimizer::OptimizerConfig;
9697
use uuid::Uuid;
9798

9899
use super::options::{
@@ -1644,25 +1645,13 @@ impl SessionState {
16441645

16451646
/// Optimizes the logical plan by applying optimizer rules.
16461647
pub fn optimize(&self, plan: &LogicalPlan) -> Result<LogicalPlan> {
1647-
// TODO: Implement OptimizerContext directly on DataFrame (#4631) (#4626)
1648-
let config = {
1649-
let config = &self.config_options().optimizer;
1650-
OptimizerContext::new()
1651-
.with_skip_failing_rules(config.skip_failed_rules)
1652-
.with_max_passes(config.max_passes as u8)
1653-
.with_query_execution_start_time(
1654-
self.execution_props.query_execution_start_time,
1655-
)
1656-
.filter_null_keys(config.filter_null_join_keys)
1657-
};
1658-
16591648
if let LogicalPlan::Explain(e) = plan {
16601649
let mut stringified_plans = e.stringified_plans.clone();
16611650

16621651
// optimize the child plan, capturing the output of each optimizer
16631652
let plan = self.optimizer.optimize(
16641653
e.plan.as_ref(),
1665-
&config,
1654+
self,
16661655
|optimized_plan, optimizer| {
16671656
let optimizer_name = optimizer.name().to_string();
16681657
let plan_type = PlanType::OptimizedLogicalPlan { optimizer_name };
@@ -1677,7 +1666,7 @@ impl SessionState {
16771666
schema: e.schema.clone(),
16781667
}))
16791668
} else {
1680-
self.optimizer.optimize(plan, &config, |_, _| {})
1669+
self.optimizer.optimize(plan, self, |_, _| {})
16811670
}
16821671
}
16831672

@@ -1811,6 +1800,30 @@ impl FunctionRegistry for SessionState {
18111800
}
18121801
}
18131802

1803+
impl OptimizerConfig for SessionState {
1804+
fn query_execution_start_time(&self) -> DateTime<Utc> {
1805+
self.execution_props.query_execution_start_time
1806+
}
1807+
1808+
fn rule_enabled(&self, name: &str) -> bool {
1809+
use datafusion_optimizer::filter_null_join_keys::FilterNullJoinKeys;
1810+
match name {
1811+
FilterNullJoinKeys::NAME => {
1812+
self.config_options().optimizer.filter_null_join_keys
1813+
}
1814+
_ => true,
1815+
}
1816+
}
1817+
1818+
fn skip_failing_rules(&self) -> bool {
1819+
self.config_options().optimizer.skip_failed_rules
1820+
}
1821+
1822+
fn max_passes(&self) -> u8 {
1823+
self.config_options().optimizer.max_passes as _
1824+
}
1825+
}
1826+
18141827
/// Task Execution Context
18151828
pub struct TaskContext {
18161829
/// Session Id

0 commit comments

Comments
 (0)