Apache DataFusion SQL Query Engine
feat: Add `remove_optimizer_rule` to `SessionContext` (#19209)
Nuno Faria55a38d4a6da8
diff --git a/datafusion/core/src/execution/context/mod.rs b/datafusion/core/src/execution/context/mod.rsindex 8849615927..55f92b747a 100644--- a/datafusion/core/src/execution/context/mod.rs+++ b/datafusion/core/src/execution/context/mod.rs@@ -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.@@ -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(())+ }}diff --git a/datafusion/core/src/execution/session_state.rs b/datafusion/core/src/execution/session_state.rsindex c9ab980993..c1bf670870 100644--- a/datafusion/core/src/execution/session_state.rs+++ b/datafusion/core/src/execution/session_state.rs@@ -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` statementspub fn set_function_factory(&mut self, function_factory: Arc<dyn FunctionFactory>) {self.function_factory = Some(function_factory);