pub trait OptimizerRule: Debug {
// Required method
fn name(&self) -> &str;
// Provided methods
fn apply_order(&self) -> Option<ApplyOrder> { ... }
fn supports_rewrite(&self) -> bool { ... }
fn rewrite(
&self,
_plan: LogicalPlan,
_config: &dyn OptimizerConfig,
) -> Result<Transformed<LogicalPlan>, DataFusionError> { ... }
}Expand description
Transforms one LogicalPlan into another which computes the same results,
but in a potentially more efficient way.
See notes on Self::rewrite for details on how to implement an OptimizerRule.
To change the semantics of a LogicalPlan, see AnalyzerRule.
Use SessionState::add_optimizer_rule to register additional
OptimizerRules.
Required Methods§
Provided Methods§
Sourcefn apply_order(&self) -> Option<ApplyOrder>
fn apply_order(&self) -> Option<ApplyOrder>
How should the rule be applied by the optimizer? See comments on
ApplyOrder for details.
If returns None, the default, the rule must handle recursion itself
Sourcefn supports_rewrite(&self) -> bool
👎Deprecated since 47.0.0: This method is no longer used
fn supports_rewrite(&self) -> bool
This method is no longer used
Does this rule support rewriting owned plans (rather than by reference)?
Sourcefn rewrite(
&self,
_plan: LogicalPlan,
_config: &dyn OptimizerConfig,
) -> Result<Transformed<LogicalPlan>, DataFusionError>
fn rewrite( &self, _plan: LogicalPlan, _config: &dyn OptimizerConfig, ) -> Result<Transformed<LogicalPlan>, DataFusionError>
Try to rewrite plan to an optimized form, returning Transformed::yes
if the plan was rewritten and Transformed::no if it was not.
§Notes for implementations:
§Return the same plan if no changes were made
If there are no suitable transformations for the input plan, the optimizer should simply return it unmodified.
The optimizer will call rewrite several times until a fixed point is
reached, so it is important that rewrite return Transformed::no if
the output is the same.
§Matching on functions
The rule should avoid function-specific transformations, and instead use
methods on ScalarUDFImpl and AggregateUDFImpl. Specifically, the
rule should not check function names as functions can be overridden, and
may not have the same semantics as the functions provided with
DataFusion.
For example, if a rule rewrites a function based on the check
func.name() == "sum", it may rewrite the plan incorrectly if the
registered sum function has different semantics (for example, the
sum function from the datafusion-spark crate).
There are still several cases that rely on function name checking in the rules included with DataFusion. Please see #18643 for more details and to help remove these cases.
Implementors§
impl OptimizerRule for CommonSubexprEliminate
impl OptimizerRule for DecorrelateLateralJoin
impl OptimizerRule for DecorrelatePredicateSubquery
impl OptimizerRule for EliminateCrossJoin
Eliminate cross joins by rewriting them to inner joins when possible.
§Example
The initial plan for this query:
select ... from a, b where a.x = b.y and b.xx = 100;Looks like this:
Filter(a.x = b.y AND b.xx = 100)
Cross Join
TableScan a
TableScan bAfter the rule is applied, the plan will look like this:
Filter(b.xx = 100)
InnerJoin(a.x = b.y)
TableScan a
TableScan b§Other Examples
- ‘select … from a, b where a.x = b.y and b.xx = 100;’
- ‘select … from a, b where (a.x = b.y and b.xx = 100) or (a.x = b.y and b.xx = 200);’
- ’select … from a, b, c where (a.x = b.y and b.xx = 100 and a.z = c.z)
- or (a.x = b.y and b.xx = 200 and a.z=c.z);’
- ‘select … from a, b where a.x > b.y’
For above queries, the join predicate is available in filters and they are moved to join nodes appropriately
This fix helps to improve the performance of TPCH Q19. issue#78
impl OptimizerRule for EliminateDuplicatedExpr
impl OptimizerRule for EliminateFilter
impl OptimizerRule for EliminateGroupByConstant
impl OptimizerRule for EliminateJoin
impl OptimizerRule for EliminateLimit
impl OptimizerRule for EliminateOuterJoin
Attempt to eliminate outer joins.
impl OptimizerRule for ExtractEquijoinPredicate
impl OptimizerRule for ExtractLeafExpressions
impl OptimizerRule for PushDownLeafProjections
impl OptimizerRule for FilterNullJoinKeys
impl OptimizerRule for OptimizeProjections
impl OptimizerRule for OptimizeUnions
impl OptimizerRule for PropagateEmptyRelation
impl OptimizerRule for PushDownFilter
impl OptimizerRule for PushDownLimit
Push down Limit.