Skip to content

Commit 7b4593f

Browse files
adriangb2010YOUY01
andauthored
Add upgrade guide for PhysicalOptimizerRule::optimize_plan (#19030)
Closes #19029 --------- Co-authored-by: Yongting You <2010youy01@gmail.com>
1 parent cab28a6 commit 7b4593f

File tree

1 file changed

+96
-0
lines changed

1 file changed

+96
-0
lines changed

docs/source/library-user-guide/upgrading.md

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,102 @@ let config = FileScanConfigBuilder::new(url, source)
332332

333333
**Handling projections in `FileSource`:**
334334

335+
### `PhysicalOptimizerRule::optimize` deprecated in favor of `optimize_plan`
336+
337+
The `PhysicalOptimizerRule` trait has been updated to provide optimizer rules with access to broader session context. A new method `optimize_plan` has been added that accepts an `OptimizerContext` parameter, and the existing `optimize` method has been deprecated.
338+
339+
**Who is affected:**
340+
341+
- Users who have implemented custom `PhysicalOptimizerRule` implementations
342+
343+
**Breaking changes:**
344+
345+
1. **New `optimize_plan` method**: This is the preferred method for implementing optimization rules. It provides access to the full `SessionConfig` through `OptimizerContext`, rather than just `ConfigOptions`.
346+
347+
2. **`optimize` method deprecated**: The old `optimize` method that takes `&ConfigOptions` is now deprecated and will be removed in DataFusion 58.0.0.
348+
349+
**Migration guide:**
350+
351+
If you have a custom `PhysicalOptimizerRule` implementation, update it to implement `optimize_plan` instead of `optimize`:
352+
353+
**Before:**
354+
355+
```rust
356+
use datafusion::physical_optimizer::PhysicalOptimizerRule;
357+
use datafusion_common::config::ConfigOptions;
358+
use datafusion_common::Result;
359+
use datafusion_physical_plan::ExecutionPlan;
360+
use std::sync::Arc;
361+
362+
#[derive(Debug)]
363+
struct MyOptimizerRule;
364+
365+
#[allow(deprecated)]
366+
impl PhysicalOptimizerRule for MyOptimizerRule {
367+
fn optimize(
368+
&self,
369+
plan: Arc<dyn ExecutionPlan>,
370+
_config: &ConfigOptions,
371+
) -> Result<Arc<dyn ExecutionPlan>> {
372+
// Use config.optimizer, config.execution, etc.
373+
// ... optimization logic ...
374+
Ok(plan)
375+
}
376+
377+
fn name(&self) -> &str {
378+
"my_optimizer_rule"
379+
}
380+
381+
fn schema_check(&self) -> bool {
382+
true
383+
}
384+
}
385+
```
386+
387+
**After:**
388+
389+
```rust
390+
use datafusion::physical_optimizer::{OptimizerContext, PhysicalOptimizerRule};
391+
use datafusion_common::Result;
392+
use datafusion_physical_plan::ExecutionPlan;
393+
use std::sync::Arc;
394+
395+
#[derive(Debug)]
396+
struct MyOptimizerRule;
397+
398+
impl PhysicalOptimizerRule for MyOptimizerRule {
399+
fn optimize_plan(
400+
&self,
401+
plan: Arc<dyn ExecutionPlan>,
402+
context: &OptimizerContext,
403+
) -> Result<Arc<dyn ExecutionPlan>> {
404+
// Access ConfigOptions through session_config
405+
let _config = context.session_config().options();
406+
// Or access extensions through session_config
407+
let _extensions = context.session_config().extensions();
408+
// ... optimization logic ...
409+
Ok(plan)
410+
}
411+
412+
fn name(&self) -> &str {
413+
"my_optimizer_rule"
414+
}
415+
416+
fn schema_check(&self) -> bool {
417+
true
418+
}
419+
}
420+
```
421+
422+
**What is `OptimizerContext`?**
423+
424+
`OptimizerContext` is a new struct that provides context during physical plan optimization, similar to how `TaskContext` provides context during execution. It wraps `SessionConfig`, giving optimizer rules access to:
425+
426+
- Configuration options via `context.session_config().options()`
427+
- Session extensions via `context.session_config().extensions()`
428+
429+
This enables optimizer rules to access custom extensions registered with the session, which was not possible with the old `&ConfigOptions` parameter.
430+
335431
## DataFusion `51.0.0`
336432

337433
### `arrow` / `parquet` updated to 57.0.0

0 commit comments

Comments
 (0)