Skip to main content

OptimizerRule

Trait OptimizerRule 

Source
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§

Source

fn name(&self) -> &str

A human readable name for this optimizer rule

Provided Methods§

Source

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

Source

fn supports_rewrite(&self) -> bool

👎Deprecated since 47.0.0:

This method is no longer used

Does this rule support rewriting owned plans (rather than by reference)?

Source

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§

Source§

impl OptimizerRule for CommonSubexprEliminate

Source§

impl OptimizerRule for DecorrelateLateralJoin

Source§

impl OptimizerRule for DecorrelatePredicateSubquery

Source§

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 b

After 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

Source§

impl OptimizerRule for EliminateDuplicatedExpr

Source§

impl OptimizerRule for EliminateFilter

Source§

impl OptimizerRule for EliminateGroupByConstant

Source§

impl OptimizerRule for EliminateJoin

Source§

impl OptimizerRule for EliminateLimit

Source§

impl OptimizerRule for EliminateOuterJoin

Attempt to eliminate outer joins.

Source§

impl OptimizerRule for ExtractEquijoinPredicate

Source§

impl OptimizerRule for ExtractLeafExpressions

Source§

impl OptimizerRule for PushDownLeafProjections

Source§

impl OptimizerRule for FilterNullJoinKeys

Source§

impl OptimizerRule for OptimizeProjections

Source§

impl OptimizerRule for OptimizeUnions

Source§

impl OptimizerRule for PropagateEmptyRelation

Source§

impl OptimizerRule for PushDownFilter

Source§

impl OptimizerRule for PushDownLimit

Push down Limit.

Source§

impl OptimizerRule for ReplaceDistinctWithAggregate

Source§

impl OptimizerRule for RewriteSetComparison

Source§

impl OptimizerRule for ScalarSubqueryToJoin

Source§

impl OptimizerRule for SimplifyExpressions

Source§

impl OptimizerRule for SingleDistinctToGroupBy