Class SubstitutionVisitor
- Direct Known Subclasses:
MaterializedViewSubstitutionVisitor
The call new SubstitutionVisitor(target, query).go(replacement))
will return query with every occurrence of target replaced
by replacement.
The following example shows how SubstitutionVisitor can be used
for materialized view recognition.
- query = SELECT a, c FROM t WHERE x = 5 AND b = 4
- target = SELECT a, b, c FROM t WHERE x = 5
- replacement = SELECT * FROM mv
- result = SELECT a, c FROM mv WHERE b = 4
Note that result uses the materialized view table mv and a
simplified condition b = 4.
Uses a bottom-up matching algorithm. Nodes do not need to be identical. At each level, returns the residue.
The inputs must only include the core relational operators:
TableScan,
Filter,
Project,
Calc,
Join,
Union,
Intersect,
Aggregate.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classAbstract base class for implementingSubstitutionVisitor.UnifyRule.protected static classException thrown to exit a matcher.static classOperand to aSubstitutionVisitor.UnifyRule.static classResult of an application of aSubstitutionVisitor.UnifyRuleindicating that the rule successfully matchedqueryagainsttargetand generated aresultthat is equivalent toqueryand containstarget.static classRule that attempts to match a query relational expression against a target relational expression.classArguments to an application of aSubstitutionVisitor.UnifyRule. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final com.google.common.collect.ImmutableList<SubstitutionVisitor.UnifyRule>protected final RelBuilderFactory for a builder for relational expressions.protected final MutableRel[]Workspace while rule is being matched. -
Constructor Summary
ConstructorsConstructorDescriptionSubstitutionVisitor(RelNode target_, RelNode query_) Creates a SubstitutionVisitor with the default rule set.SubstitutionVisitor(RelNode target_, RelNode query_, com.google.common.collect.ImmutableList<SubstitutionVisitor.UnifyRule> rules) Creates a SubstitutionVisitor with the default logical builder.SubstitutionVisitor(RelNode target_, RelNode query_, com.google.common.collect.ImmutableList<SubstitutionVisitor.UnifyRule> rules, RelBuilderFactory relBuilderFactory) -
Method Summary
Modifier and TypeMethodDescriptionstatic booleanequalType(String desc0, MutableRel rel0, String desc1, MutableRel rel1, Litmus litmus) Returns whether two relational expressions have the same row-type.explainCalc(MutableCalc calc) Explain filtering condition and projections from MutableCalc.static @Nullable SqlAggFunctiongetRollup(SqlAggFunction aggregation) Deprecated.Returns a list of all possible rels that result from substituting the matched RelNode with the replacement RelNode within the query.@Nullable RelNodeprotected booleanisWeaker(MutableRel rel0, MutableRel rel) Returns if one rel is weaker than another.static booleanReturns whether a boolean expression ever returns true.static MutableAggregatepermute(MutableAggregate aggregate, MutableRel input, Mapping mapping) static @Nullable org.apache.calcite.plan.SubstitutionVisitor.Replacementreplace(MutableRel query, MutableRel find, MutableRel replace) Within a relational expressionquery, replaces occurrences offindwithreplace.static @Nullable RexNodesplitFilter(RexSimplify simplify, RexNode condition, RexNode target) Maps a condition onto a target.static @Nullable MutableRelunifyAggregates(MutableAggregate query, @Nullable RexNode targetCond, MutableAggregate target)
-
Field Details
-
DEFAULT_RULES
public static final com.google.common.collect.ImmutableList<SubstitutionVisitor.UnifyRule> DEFAULT_RULES -
relBuilder
Factory for a builder for relational expressions. -
slots
Workspace while rule is being matched. Careful, re-entrant! Assumes no rule needs more than 2 slots.
-
-
Constructor Details
-
SubstitutionVisitor
Creates a SubstitutionVisitor with the default rule set. -
SubstitutionVisitor
public SubstitutionVisitor(RelNode target_, RelNode query_, com.google.common.collect.ImmutableList<SubstitutionVisitor.UnifyRule> rules) Creates a SubstitutionVisitor with the default logical builder. -
SubstitutionVisitor
public SubstitutionVisitor(RelNode target_, RelNode query_, com.google.common.collect.ImmutableList<SubstitutionVisitor.UnifyRule> rules, RelBuilderFactory relBuilderFactory)
-
-
Method Details
-
splitFilter
public static @Nullable RexNode splitFilter(RexSimplify simplify, RexNode condition, RexNode target) Maps a condition onto a target.If condition is stronger than target, returns the residue. If it is equal to target, returns the expression that evaluates to the constant
true. If it is weaker than target, returnsnull.The terms satisfy the relation
condition = target AND residueand
residuemust be as weak as possible.Example #1: condition stronger than target
- condition: x = 1 AND y = 2
- target: x = 1
- residue: y = 2
Note that residue
x > 0 AND y = 2would also satisfy the relationcondition = target AND residuebut is stronger than necessary, so we prefery = 2.Example #2: target weaker than condition (valid, but not currently implemented)
- condition: x = 1
- target: x = 1 OR z = 3
- residue: x = 1
Example #3: condition and target are equivalent
- condition: x = 1 AND y = 2
- target: y = 2 AND x = 1
- residue: TRUE
Example #4: condition weaker than target
- condition: x = 1
- target: x = 1 AND y = 2
- residue: null (i.e. no match)
There are many other possible examples. It amounts to solving whether
condition AND NOT targetcan ever evaluate to true, and therefore is a form of the NP-complete Satisfiability problem. -
mayBeSatisfiable
Returns whether a boolean expression ever returns true.This method may give false positives. For instance, it will say that
x = 5 AND x > 10is satisfiable, because at present it cannot prove that it is not. -
go0
-
go
Returns a list of all possible rels that result from substituting the matched RelNode with the replacement RelNode within the query.For example, the substitution result of A join B, while A and B are both a qualified match for replacement R, is R join B, R join R, A join R.
-
replace
public static @Nullable org.apache.calcite.plan.SubstitutionVisitor.Replacement replace(MutableRel query, MutableRel find, MutableRel replace) Within a relational expressionquery, replaces occurrences offindwithreplace.Assumes relational expressions (and their descendants) are not null. Does not handle cycles.
-
explainCalc
Explain filtering condition and projections from MutableCalc. -
permute
public static MutableAggregate permute(MutableAggregate aggregate, MutableRel input, Mapping mapping) -
unifyAggregates
public static @Nullable MutableRel unifyAggregates(MutableAggregate query, @Nullable RexNode targetCond, MutableAggregate target) -
getRollup
Deprecated. -
isWeaker
Returns if one rel is weaker than another. -
equalType
public static boolean equalType(String desc0, MutableRel rel0, String desc1, MutableRel rel1, Litmus litmus) Returns whether two relational expressions have the same row-type.
-