public class VolcanoPlanner extends AbstractRelOptPlanner
RelOptPlanner.CannotPlanException, RelOptPlanner.Executor| Modifier and Type | Field and Description |
|---|---|
protected boolean |
ambitious
If true, the planner keeps applying rules as long as they continue to
reduce the cost.
|
protected static double |
COST_IMPROVEMENT |
protected boolean |
impatient
If true, and if
ambitious is true, the planner waits a finite
number of iterations for the cost to improve. |
protected RelSubset |
root |
protected Set<RelOptRule> |
ruleSet
Set of all registered rules.
|
context, costFactoryLOGGER| Constructor and Description |
|---|
VolcanoPlanner()
Creates a uninitialized
VolcanoPlanner. |
VolcanoPlanner(Context externalContext)
Creates a uninitialized
VolcanoPlanner. |
VolcanoPlanner(RelOptCostFactory costFactory,
Context externalContext)
Creates a
VolcanoPlanner with a given cost factory. |
| Modifier and Type | Method and Description |
|---|---|
void |
addLattice(RelOptLattice lattice)
Defines a lattice.
|
void |
addListener(RelOptListener newListener)
Adds a listener to this planner.
|
void |
addMaterialization(RelOptMaterialization materialization)
Defines a pair of relational expressions that are equivalent.
|
boolean |
addRelTraitDef(RelTraitDef relTraitDef)
Registers a rel trait definition.
|
boolean |
addRule(RelOptRule rule)
Registers a rule.
|
boolean |
canConvert(RelTraitSet fromTraits,
RelTraitSet toTraits) |
RelNode |
changeTraits(RelNode rel,
RelTraitSet toTraits)
Changes a relational expression to an equivalent one with a different set
of traits.
|
RelOptPlanner |
chooseDelegate()
Negotiates an appropriate planner to deal with distributed queries.
|
void |
clear()
Removes all internal state, including all rules.
|
void |
clearRelTraitDefs()
Clear all the registered RelTraitDef.
|
void |
dump(PrintWriter pw)
Dumps the internal state of this VolcanoPlanner to a writer.
|
RelTraitSet |
emptyTraitSet()
Creates an empty trait set.
|
RelSubset |
ensureRegistered(RelNode rel,
RelNode equivRel)
Registers a relational expression if it is not already registered.
|
void |
ensureRegistered(RelNode rel,
RelNode equivRel,
VolcanoRuleCall ruleCall) |
RelNode |
findBestExp()
Finds the most efficient expression to implement the query given via
RelOptPlanner.setRoot(org.apache.calcite.rel.RelNode). |
RelOptCost |
getCost(RelNode rel)
Computes the cost of a RelNode.
|
RelOptLattice |
getLattice(RelOptTable table)
Retrieves a lattice, given its star table.
|
protected VolcanoPlannerPhaseRuleMappingInitializer |
getPhaseRuleMappingInitializer() |
long |
getRelMetadataTimestamp(RelNode rel)
Gets a timestamp for a given rel's metadata.
|
List<RelTraitDef> |
getRelTraitDefs()
Returns the list of active trait types.
|
RelNode |
getRoot()
Returns the root node of this query.
|
org.apache.calcite.plan.volcano.RelSet |
getSet(RelNode rel)
Finds an expression's equivalence set.
|
RelSubset |
getSubset(RelNode rel)
Returns the subset that a relational expression belongs to.
|
RelSubset |
getSubset(RelNode rel,
RelTraitSet traits) |
RelSubset |
getSubset(RelNode rel,
RelTraitSet traits,
boolean createIfMissing) |
boolean |
isRegistered(RelNode rel)
Determines whether a relational expression has been registered.
|
static String |
normalizePlan(String plan)
Normalizes references to subsets within the string representation of a
plan.
|
protected void |
onNewClass(RelNode node)
Called when a new class of
RelNode is seen. |
RelSubset |
register(RelNode rel,
RelNode equivRel)
Registers a relational expression in the expression bank.
|
void |
registerAbstractRelationalRules() |
void |
registerMetadataProviders(List<RelMetadataProvider> list)
Gives this planner a chance to register one or more
RelMetadataProviders in the chain which will be used to answer
metadata queries. |
void |
registerSchema(RelOptSchema schema)
Tells this planner that a schema exists.
|
boolean |
removeRule(RelOptRule rule)
Removes a rule.
|
void |
setImportance(RelNode rel,
double importance)
Sets the importance of a relational expression.
|
void |
setLocked(boolean locked)
Sets whether this planner is locked.
|
void |
setRoot(RelNode rel)
Sets the root node of this query.
|
protected void |
validate()
Checks internal consistency.
|
checkCancel, fireRule, getContext, getCostFactory, getExecutor, getListener, getRuleByDescription, isRuleExcluded, mapRuleDescription, notifyChosen, notifyDiscard, notifyEquivalence, notifyTransformation, onCopy, registerClass, setCancelFlag, setExecutor, setRuleDescExclusionFilter, subClasses, unmapRuleDescriptionprotected static final double COST_IMPROVEMENT
protected RelSubset root
protected boolean ambitious
protected boolean impatient
ambitious is true, the planner waits a finite
number of iterations for the cost to improve.
The number of iterations K is equal to the number of iterations
required to get the first finite plan. After the first finite plan, it
continues to fire rules to try to improve it. The planner sets a target
cost of the current best cost multiplied by COST_IMPROVEMENT. If
it does not meet that cost target within K steps, it quits, and uses the
current best plan. If it meets the cost, it sets a new, lower target, and
has another K iterations to meet it. And so forth.
If false, the planner continues to fire rules until the rule queue is empty.
protected final Set<RelOptRule> ruleSet
public VolcanoPlanner()
VolcanoPlanner. To fully initialize
it, the caller must register the desired set of relations, rules, and
calling conventions.public VolcanoPlanner(Context externalContext)
VolcanoPlanner. To fully initialize
it, the caller must register the desired set of relations, rules, and
calling conventions.public VolcanoPlanner(RelOptCostFactory costFactory, Context externalContext)
VolcanoPlanner with a given cost factory.protected VolcanoPlannerPhaseRuleMappingInitializer getPhaseRuleMappingInitializer()
public boolean isRegistered(RelNode rel)
RelOptPlannerrel - expression to testpublic void setRoot(RelNode rel)
RelOptPlannerrel - Relational expressionpublic RelNode getRoot()
RelOptPlannerpublic void addMaterialization(RelOptMaterialization materialization)
RelOptPlannerTypically tableRel is a
LogicalTableScan representing a
table that is a materialized view and queryRel is the SQL
expression that populates that view. The intention is that
tableRel is cheaper to evaluate and therefore if the query being
optimized uses (or can be rewritten to use) queryRel as a
sub-expression then it can be optimized by using tableRel
instead.
addMaterialization in interface RelOptPlanneraddMaterialization in class AbstractRelOptPlannerpublic void addLattice(RelOptLattice lattice)
RelOptPlannerThe lattice may have materializations; it is not necessary to call
RelOptPlanner.addMaterialization(org.apache.calcite.plan.RelOptMaterialization) for these; they are registered implicitly.
addLattice in interface RelOptPlanneraddLattice in class AbstractRelOptPlannerpublic RelOptLattice getLattice(RelOptTable table)
RelOptPlannergetLattice in interface RelOptPlannergetLattice in class AbstractRelOptPlannerpublic org.apache.calcite.plan.volcano.RelSet getSet(RelNode rel)
rel - Relational expressionpublic boolean addRelTraitDef(RelTraitDef relTraitDef)
RelOptPlannerRelTraitDef has already
been registered, does nothing.addRelTraitDef in interface RelOptPlanneraddRelTraitDef in class AbstractRelOptPlannerCollection.add(E)public void clearRelTraitDefs()
RelOptPlannerclearRelTraitDefs in interface RelOptPlannerclearRelTraitDefs in class AbstractRelOptPlannerpublic List<RelTraitDef> getRelTraitDefs()
RelOptPlannergetRelTraitDefs in interface RelOptPlannergetRelTraitDefs in class AbstractRelOptPlannerpublic RelTraitSet emptyTraitSet()
RelOptPlannerThe empty trait set acts as the prototype (a kind of factory) for all subsequently created trait sets.
emptyTraitSet in interface RelOptPlanneremptyTraitSet in class AbstractRelOptPlannerpublic void clear()
RelOptPlannerclear in interface RelOptPlannerclear in class AbstractRelOptPlannerpublic boolean addRule(RelOptRule rule)
RelOptPlannerIf the rule has already been registered, does nothing.
This method determines if the given rule is a
ConverterRule and pass the
ConverterRule to all
registered RelTraitDef
instances.
Collection.add(E)public boolean removeRule(RelOptRule rule)
RelOptPlannerCollection.remove(Object)protected void onNewClass(RelNode node)
AbstractRelOptPlannerRelNode is seen.onNewClass in class AbstractRelOptPlannerpublic boolean canConvert(RelTraitSet fromTraits, RelTraitSet toTraits)
public RelNode changeTraits(RelNode rel, RelTraitSet toTraits)
RelOptPlannerrel - Relational expression (may or may not have been registered; must
not have the desired traits)toTraits - Trait set to convert the relational expression topublic RelOptPlanner chooseDelegate()
RelOptPlannerchooseDelegate in interface RelOptPlannerchooseDelegate in class AbstractRelOptPlannerpublic RelNode findBestExp()
RelOptPlanner.setRoot(org.apache.calcite.rel.RelNode).
The algorithm executes repeatedly in a series of phases. In each phase
the exact rules that may be fired varies. The mapping of phases to rule
sets is maintained in the ruleQueue.
In each phase, the planner sets the initial importance of the existing
RelSubSets (setInitialImportance()). The planner then iterates
over the rule matches presented by the rule queue until:
Furthermore, after every 10 iterations without an implementable plan,
RelSubSets that contain only logical RelNodes are given an importance
boost via injectImportanceBoost(). Once an implementable plan is
found, the artificially raised importance values are cleared (see
clearImportanceBoost()).
public RelSubset register(RelNode rel, RelNode equivRel)
RelOptPlannerAfter it has been registered, you may not modify it.
The expression must not already have been registered. If you are not
sure whether it has been registered, call
RelOptPlanner.ensureRegistered(RelNode, RelNode).
rel - Relational expression to register (must not already be
registered)equivRel - Relational expression it is equivalent to (may be null)public RelSubset ensureRegistered(RelNode rel, RelNode equivRel)
RelOptPlannerIf equivRel is specified, rel is placed in the same
equivalence set. It is OK if equivRel has different traits;
rel will end up in a different subset of the same set.
It is OK if rel is a subset.
rel - Relational expression to registerequivRel - Relational expression it is equivalent to (may be null)protected void validate()
public void registerAbstractRelationalRules()
public void registerSchema(RelOptSchema schema)
RelOptPlannerregisterSchema in interface RelOptPlannerregisterSchema in class AbstractRelOptPlannerpublic RelOptCost getCost(RelNode rel)
RelOptPlannerRelMetadataQuery.getCumulativeCost(org.apache.calcite.rel.RelNode).getCost in interface RelOptPlannergetCost in class AbstractRelOptPlannerrel - expression of interestpublic RelSubset getSubset(RelNode rel)
rel - Relational expressionpublic RelSubset getSubset(RelNode rel, RelTraitSet traits)
public RelSubset getSubset(RelNode rel, RelTraitSet traits, boolean createIfMissing)
public void setImportance(RelNode rel, double importance)
RelOptPlannerAn important use of this method is when a RelOptRule has
created a relational expression which is indisputably better than the
original relational expression. The rule set the original relational
expression's importance to zero, to reduce the search space. Pending rule
calls are cancelled, and future rules will not fire.
setImportance in interface RelOptPlannersetImportance in class AbstractRelOptPlannerrel - Relational expressionimportance - Importancepublic void dump(PrintWriter pw)
pw - Print writernormalizePlan(String)public void addListener(RelOptListener newListener)
RelOptPlanneraddListener in interface RelOptPlanneraddListener in class AbstractRelOptPlannernewListener - new listener to be notified of eventspublic void registerMetadataProviders(List<RelMetadataProvider> list)
RelOptPlannerRelMetadataProviders in the chain which will be used to answer
metadata queries.
Planners which use their own relational expressions internally to represent concepts such as equivalence classes will generally need to supply corresponding metadata providers.
registerMetadataProviders in interface RelOptPlannerregisterMetadataProviders in class AbstractRelOptPlannerlist - receives planner's custom providers, if anypublic long getRelMetadataTimestamp(RelNode rel)
RelOptPlannerCachingRelMetadataProvider to decide whether cached metadata has
gone stale.getRelMetadataTimestamp in interface RelOptPlannergetRelMetadataTimestamp in class AbstractRelOptPlannerrel - rel of interestpublic static String normalizePlan(String plan)
This is useful when writing tests: it helps to ensure that tests don't break when an extra rule is introduced that generates a new subset and causes subsequent subset numbers to be off by one.
For example,
FennelAggRel.FENNEL_EXEC(child=Subset#17.FENNEL_EXEC,groupCount=1, EXPR$1=COUNT())becomes
FennelSortRel.FENNEL_EXEC(child=Subset#2.FENNEL_EXEC, key=[0], discardDuplicates=false)
FennelCalcRel.FENNEL_EXEC( child=Subset#4.FENNEL_EXEC, expr#0..8={inputs}, expr#9=3456, DEPTNO=$t7, $f0=$t9)
MockTableImplRel.FENNEL_EXEC( table=[CATALOG, SALES, EMP])
FennelAggRel.FENNEL_EXEC(child=Subset#{0}.FENNEL_EXEC, groupCount=1, EXPR$1=COUNT())
FennelSortRel.FENNEL_EXEC(child=Subset#{1}.FENNEL_EXEC, key=[0], discardDuplicates=false)
FennelCalcRel.FENNEL_EXEC( child=Subset#{2}.FENNEL_EXEC,expr#0..8={inputs},expr#9=3456,DEPTNO=$t7, $f0=$t9)
MockTableImplRel.FENNEL_EXEC( table=[CATALOG, SALES, EMP])
plan - Planpublic void setLocked(boolean locked)
addRule(org.apache.calcite.plan.RelOptRule) will do
nothing and return false.locked - Whether planner is lockedpublic void ensureRegistered(RelNode rel, RelNode equivRel, VolcanoRuleCall ruleCall)
Copyright © 2012–2015 The Apache Software Foundation. All rights reserved.