Skip to content

feat: user-defined optimizer and analyzer rules#1557

Draft
timsaucer wants to merge 1 commit into
apache:mainfrom
timsaucer:feat/df54-followups-wave4
Draft

feat: user-defined optimizer and analyzer rules#1557
timsaucer wants to merge 1 commit into
apache:mainfrom
timsaucer:feat/df54-followups-wave4

Conversation

@timsaucer
Copy link
Copy Markdown
Member

Which issue does this PR close?

Related to #1533

Rationale for this change

This PR allows for adding user defined optimizer and analyzer rules. Support for these exist upstream but has not been exposed in Python.

What changes are included in this PR?

  • Update SessionContext to take user provided analyzer or optimizer rules.
  • Add unit tests
  • Add documentation

Are there any user-facing changes?

Pure addition.

Expose `SessionContext.add_optimizer_rule` and
`SessionContext.add_analyzer_rule` symmetric with the existing
`remove_optimizer_rule`. Each accepts a Python subclass of the new
`datafusion.optimizer.OptimizerRule` / `AnalyzerRule` ABCs.

Implementation:

* New `crates/core/src/optimizer_rules.rs` wraps user Python instances
  in `PyOptimizerRuleAdapter` / `PyAnalyzerRuleAdapter`, which
  implement the upstream `OptimizerRule` / `AnalyzerRule` traits.
* `OptimizerRule.rewrite(plan)` returns `None` for "no change" or a
  new `LogicalPlan`. The adapter maps that to
  `Transformed::no` / `Transformed::yes` so the upstream optimizer's
  fixed-point loop terminates correctly.
* `AnalyzerRule.analyze(plan)` must always return a `LogicalPlan`;
  returning `None` surfaces a `DataFusionError::Execution` naming the
  offending rule.
* The upstream `&dyn OptimizerConfig` / `&ConfigOptions` arguments are
  not surfaced to Python in this MVP; rules that need configuration
  should capture it at construction time (for example by holding a
  `SessionContext` reference) or be implemented in Rust.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant