The optimizer is a program that takes a relational expression (query plan) and rewrites it with optimization rules. The output is still a relational expression and is generally called the physical plan.
The optimizer uses for that:
- a planner (rule,..)
- and metadata information (Materialization, Indexes)
They are all defined in the environment object RelOptCluster.
The Planner rules transform expression trees using mathematical identities that preserve semantics.
Calcite optimize the query plan (ie rewrite it) in the Prepare statement class. See optimize function (L133)