Skip to content

Commit 55a38d4

Browse files
nuno-fariaalamb
andauthored
feat: Add remove_optimizer_rule to SessionContext (#19209)
## Which issue does this PR close? <!-- We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes #123` indicates that this PR will close issue #123. --> - Closes #19116. ## Rationale for this change <!-- Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed. Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes. --> Adds the ability to remove logical optimizer rules from the `SessionContext` on the fly, without having to create a new one. Together with the existing `add_optimizer_rule` we can now freely add and remove rules. ## What changes are included in this PR? <!-- There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR. --> - Added `remove_optimizer_rule` to `SessionContext`. - Added `remove_optimizer_rule` to `SessionState`. - Added a unit test. ## Are these changes tested? <!-- We typically require tests for all PRs in order to: 1. Prevent the code from being accidentally broken by subsequent changes 2. Serve as another way to document the expected behavior of the code If tests are not included in your PR, please explain why (for example, are they covered by existing tests)? --> Yes. ## Are there any user-facing changes? <!-- If there are user-facing changes then we may require documentation to be updated before approving the PR. --> New optional method. <!-- If there are any breaking changes to public APIs, please add the `api change` label. --> Co-authored-by: Andrew Lamb <andrew@nerdnetworks.org>
1 parent 71a6982 commit 55a38d4

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

datafusion/core/src/execution/context/mod.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,11 @@ impl SessionContext {
482482
self.state.write().append_optimizer_rule(optimizer_rule);
483483
}
484484

485+
/// Removes an optimizer rule by name, returning `true` if it existed.
486+
pub fn remove_optimizer_rule(&self, name: &str) -> bool {
487+
self.state.write().remove_optimizer_rule(name)
488+
}
489+
485490
/// Adds an analyzer rule to the end of the existing rules.
486491
///
487492
/// See [`SessionState`] for more control of when the rule is applied.
@@ -2609,4 +2614,49 @@ mod tests {
26092614
}
26102615
}
26112616
}
2617+
2618+
#[tokio::test]
2619+
async fn remove_optimizer_rule() -> Result<()> {
2620+
let get_optimizer_rules = |ctx: &SessionContext| {
2621+
ctx.state()
2622+
.optimizer()
2623+
.rules
2624+
.iter()
2625+
.map(|r| r.name().to_owned())
2626+
.collect::<HashSet<_>>()
2627+
};
2628+
2629+
let ctx = SessionContext::new();
2630+
assert!(get_optimizer_rules(&ctx).contains("simplify_expressions"));
2631+
2632+
// default plan
2633+
let plan = ctx
2634+
.sql("select 1 + 1")
2635+
.await?
2636+
.into_optimized_plan()?
2637+
.to_string();
2638+
assert_snapshot!(plan, @r"
2639+
Projection: Int64(2) AS Int64(1) + Int64(1)
2640+
EmptyRelation: rows=1
2641+
");
2642+
2643+
assert!(ctx.remove_optimizer_rule("simplify_expressions"));
2644+
assert!(!get_optimizer_rules(&ctx).contains("simplify_expressions"));
2645+
2646+
// plan without the simplify_expressions rule
2647+
let plan = ctx
2648+
.sql("select 1 + 1")
2649+
.await?
2650+
.into_optimized_plan()?
2651+
.to_string();
2652+
assert_snapshot!(plan, @r"
2653+
Projection: Int64(1) + Int64(1)
2654+
EmptyRelation: rows=1
2655+
");
2656+
2657+
// attempting to remove a non-existing rule returns false
2658+
assert!(!ctx.remove_optimizer_rule("simplify_expressions"));
2659+
2660+
Ok(())
2661+
}
26122662
}

datafusion/core/src/execution/session_state.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,13 @@ impl SessionState {
347347
self.optimizer.rules.push(optimizer_rule);
348348
}
349349

350+
/// Removes an optimizer rule by name, returning `true` if it existed.
351+
pub(crate) fn remove_optimizer_rule(&mut self, name: &str) -> bool {
352+
let original_len = self.optimizer.rules.len();
353+
self.optimizer.rules.retain(|r| r.name() != name);
354+
self.optimizer.rules.len() < original_len
355+
}
356+
350357
/// Registers a [`FunctionFactory`] to handle `CREATE FUNCTION` statements
351358
pub fn set_function_factory(&mut self, function_factory: Arc<dyn FunctionFactory>) {
352359
self.function_factory = Some(function_factory);

0 commit comments

Comments
 (0)