Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make OptimizerConfig a builder style API #3525

Merged
merged 1 commit into from
Sep 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions datafusion/core/src/execution/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1564,14 +1564,16 @@ impl SessionState {

/// Optimizes the logical plan by applying optimizer rules.
pub fn optimize(&self, plan: &LogicalPlan) -> Result<LogicalPlan> {
let mut optimizer_config = OptimizerConfig::new().with_skip_failing_rules(
self.config
.config_options
.read()
.get_bool(OPT_OPTIMIZER_SKIP_FAILED_RULES),
);
optimizer_config.query_execution_start_time =
self.execution_props.query_execution_start_time;
let mut optimizer_config = OptimizerConfig::new()
.with_skip_failing_rules(
self.config
.config_options
.read()
.get_bool(OPT_OPTIMIZER_SKIP_FAILED_RULES),
)
.with_query_execution_start_time(
self.execution_props.query_execution_start_time,
);

if let LogicalPlan::Explain(e) = plan {
let mut stringified_plans = e.stringified_plans.clone();
Expand Down
31 changes: 26 additions & 5 deletions datafusion/optimizer/src/optimizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,15 @@ pub trait OptimizerRule {
fn name(&self) -> &str;
}

/// Placeholder for optimizer configuration options
/// Options to control the DataFusion Optimizer.
#[derive(Debug)]
pub struct OptimizerConfig {
/// Query execution start time that can be used to rewrite expressions such as `now()`
/// to use a literal value instead
pub query_execution_start_time: DateTime<Utc>,
/// Query execution start time that can be used to rewrite
/// expressions such as `now()` to use a literal value instead
query_execution_start_time: DateTime<Utc>,
/// id generator for optimizer passes
// TODO this should not be on the config,
// it should be its own 'OptimizerState' or something)
next_id: usize,
/// Option to skip rules that produce errors
skip_failing_rules: bool,
Expand All @@ -59,16 +62,34 @@ impl OptimizerConfig {
}
}

/// Specify whether the optimizer should skip rules that produce errors, or fail the query
/// Specify whether the optimizer should skip rules that produce
/// errors, or fail the query
pub fn with_query_execution_start_time(
mut self,
query_execution_tart_time: DateTime<Utc>,
) -> Self {
self.query_execution_start_time = query_execution_tart_time;
self
}

/// Specify whether the optimizer should skip rules that produce
/// errors, or fail the query
pub fn with_skip_failing_rules(mut self, b: bool) -> Self {
self.skip_failing_rules = b;
self
}

/// Generate the next ID needed
pub fn next_id(&mut self) -> usize {
self.next_id += 1;
self.next_id
}

/// Return the time at which the query execution started. This
/// time is used as the value for now()
pub fn query_execution_start_time(&self) -> DateTime<Utc> {
self.query_execution_start_time
}
}

impl Default for OptimizerConfig {
Expand Down
10 changes: 5 additions & 5 deletions datafusion/optimizer/src/simplify_expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ impl OptimizerRule for SimplifyExpressions {
) -> Result<LogicalPlan> {
let mut execution_props = ExecutionProps::new();
execution_props.query_execution_start_time =
optimizer_config.query_execution_start_time;
optimizer_config.query_execution_start_time();
self.optimize_internal(plan, &execution_props)
}
}
Expand Down Expand Up @@ -1887,8 +1887,8 @@ mod tests {

// expect optimizing will result in an error, returning the error string
fn get_optimized_plan_err(plan: &LogicalPlan, date_time: &DateTime<Utc>) -> String {
let mut config = OptimizerConfig::new();
config.query_execution_start_time = *date_time;
let mut config =
OptimizerConfig::new().with_query_execution_start_time(*date_time);
let rule = SimplifyExpressions::new();

let err = rule
Expand All @@ -1902,8 +1902,8 @@ mod tests {
plan: &LogicalPlan,
date_time: &DateTime<Utc>,
) -> String {
let mut config = OptimizerConfig::new();
config.query_execution_start_time = *date_time;
let mut config =
OptimizerConfig::new().with_query_execution_start_time(*date_time);
let rule = SimplifyExpressions::new();

let optimized_plan = rule
Expand Down
2 changes: 1 addition & 1 deletion datafusion/optimizer/src/type_coercion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ impl OptimizerRule for TypeCoercion {

let mut execution_props = ExecutionProps::new();
execution_props.query_execution_start_time =
optimizer_config.query_execution_start_time;
optimizer_config.query_execution_start_time();
let const_evaluator = ConstEvaluator::try_new(&execution_props)?;

let mut expr_rewrite = TypeCoercionRewriter {
Expand Down