Skip to content
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
50 changes: 50 additions & 0 deletions datafusion/core/src/execution/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,11 @@ impl SessionContext {
self.state.write().append_optimizer_rule(optimizer_rule);
}

/// Removes an optimizer rule by name, returning `true` if it existed.
pub fn remove_optimizer_rule(&self, name: &str) -> bool {
self.state.write().remove_optimizer_rule(name)
}

/// Adds an analyzer rule to the end of the existing rules.
///
/// See [`SessionState`] for more control of when the rule is applied.
Expand Down Expand Up @@ -2609,4 +2614,49 @@ mod tests {
}
}
}

#[tokio::test]
async fn remove_optimizer_rule() -> Result<()> {
let get_optimizer_rules = |ctx: &SessionContext| {
ctx.state()
.optimizer()
.rules
.iter()
.map(|r| r.name().to_owned())
.collect::<HashSet<_>>()
};

let ctx = SessionContext::new();
assert!(get_optimizer_rules(&ctx).contains("simplify_expressions"));

// default plan
let plan = ctx
.sql("select 1 + 1")
.await?
.into_optimized_plan()?
.to_string();
assert_snapshot!(plan, @r"
Projection: Int64(2) AS Int64(1) + Int64(1)
EmptyRelation: rows=1
");

assert!(ctx.remove_optimizer_rule("simplify_expressions"));
assert!(!get_optimizer_rules(&ctx).contains("simplify_expressions"));

// plan without the simplify_expressions rule
let plan = ctx
.sql("select 1 + 1")
.await?
.into_optimized_plan()?
.to_string();
assert_snapshot!(plan, @r"
Projection: Int64(1) + Int64(1)
EmptyRelation: rows=1
");

// attempting to remove a non-existing rule returns false
assert!(!ctx.remove_optimizer_rule("simplify_expressions"));

Ok(())
}
}
7 changes: 7 additions & 0 deletions datafusion/core/src/execution/session_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,13 @@ impl SessionState {
self.optimizer.rules.push(optimizer_rule);
}

/// Removes an optimizer rule by name, returning `true` if it existed.
pub(crate) fn remove_optimizer_rule(&mut self, name: &str) -> bool {
let original_len = self.optimizer.rules.len();
self.optimizer.rules.retain(|r| r.name() != name);
self.optimizer.rules.len() < original_len
}

/// Registers a [`FunctionFactory`] to handle `CREATE FUNCTION` statements
pub fn set_function_factory(&mut self, function_factory: Arc<dyn FunctionFactory>) {
self.function_factory = Some(function_factory);
Expand Down