Class LogicalPlanOptimizer


public class LogicalPlanOptimizer extends ParameterizedRuleExecutor<LogicalPlan,LogicalOptimizerContext>

This class is part of the planner

Global optimizations based strictly on the structure of the query (i.e. not factoring in information about the backing indices). The bulk of query transformations happen in this step.

Global optimizations based strictly on the structure of the query (i.e. not factoring in information about the backing indices). The bulk of query transformations happen in this step. This has three important sub-phases:

  • The substitutions() phase rewrites things to expand out shorthand in the syntax. For example, a nested expression embedded in a stats gets replaced with an eval followed by a stats, followed by another eval. This phase also applies surrogates, such as replacing an average with a sum divided by a count.
  • operators(boolean) (NB: The word "operator" is extremely overloaded and referrers to many different things.) transform the tree in various different ways. This includes folding (i.e. computing constant expressions at parse time), combining expressions, dropping redundant clauses, and some normalization such as putting literals on the right whenever possible. These rules are run in a loop until none of the rules make any changes to the plan (there is also a safety shut off after many iterations, although hitting that is considered a bug)
  • cleanup() Which can replace sorts+limit with a TopN

Note that the operators(boolean) and cleanup() steps are reapplied at the LocalLogicalPlanOptimizer layer.