public class SubstitutionVisitor extends Object
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.
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:
LogicalTableScan
,
LogicalFilter
,
LogicalProject
,
LogicalJoin
,
LogicalUnion
,
LogicalAggregate
.
Modifier and Type | Class and Description |
---|---|
protected static class |
SubstitutionVisitor.AbstractUnifyRule
Abstract base class for implementing
SubstitutionVisitor.UnifyRule . |
static class |
SubstitutionVisitor.FilterOnProjectRule
Rule that converts a
LogicalFilter
on top of a LogicalProject into a
trivial filter (on a boolean column). |
protected static class |
SubstitutionVisitor.MatchFailed
Exception thrown to exit a matcher.
|
protected static class |
SubstitutionVisitor.MutableAggregate
Mutable equivalent of
LogicalAggregate . |
protected static class |
SubstitutionVisitor.MutableFilter
Mutable equivalent of
LogicalFilter . |
protected static class |
SubstitutionVisitor.MutableLeafRel
Abstract base class for implementations of
SubstitutionVisitor.MutableRel that have
no inputs. |
protected static class |
SubstitutionVisitor.MutableProject
Mutable equivalent of
LogicalProject . |
protected static class |
SubstitutionVisitor.MutableRel
Mutable equivalent of
RelNode . |
protected static class |
SubstitutionVisitor.MutableRelDumper
Visitor that prints an indented tree of
SubstitutionVisitor.MutableRel s. |
protected static class |
SubstitutionVisitor.MutableRels
Utilities for dealing with
SubstitutionVisitor.MutableRel s. |
protected static class |
SubstitutionVisitor.MutableScan
Mutable equivalent of
LogicalTableScan . |
protected static class |
SubstitutionVisitor.MutableSetOp
Base class for set-operations.
|
protected static class |
SubstitutionVisitor.MutableSingleRel
Mutable equivalent of
SingleRel . |
protected static class |
SubstitutionVisitor.MutableSort
Mutable equivalent of
Sort . |
protected static class |
SubstitutionVisitor.MutableUnion
Mutable equivalent of
LogicalUnion . |
protected static class |
SubstitutionVisitor.MutableValues
Mutable equivalent of
Values . |
protected static class |
SubstitutionVisitor.Operand
Operand to a
SubstitutionVisitor.UnifyRule . |
protected static class |
SubstitutionVisitor.UnifyResult
Result of an application of a
SubstitutionVisitor.UnifyRule indicating that the
rule successfully matched query against target and
generated a result that is equivalent to query and
contains target . |
protected static class |
SubstitutionVisitor.UnifyRule
Rule that attempts to match a query relational expression
against a target relational expression.
|
protected class |
SubstitutionVisitor.UnifyRuleCall
Arguments to an application of a
SubstitutionVisitor.UnifyRule . |
Modifier and Type | Field and Description |
---|---|
protected static com.google.common.collect.ImmutableList<SubstitutionVisitor.UnifyRule> |
DEFAULT_RULES |
protected org.apache.calcite.tools.RelBuilder |
relBuilder
Factory for a builder for relational expressions.
|
protected SubstitutionVisitor.MutableRel[] |
slots
Workspace while rule is being matched.
|
Constructor and Description |
---|
SubstitutionVisitor(org.apache.calcite.rel.RelNode target_,
org.apache.calcite.rel.RelNode query_)
Creates a SubstitutionVisitor with the default rule set.
|
SubstitutionVisitor(org.apache.calcite.rel.RelNode target_,
org.apache.calcite.rel.RelNode query_,
com.google.common.collect.ImmutableList<SubstitutionVisitor.UnifyRule> rules) |
SubstitutionVisitor(org.apache.calcite.rel.RelNode target_,
org.apache.calcite.rel.RelNode query_,
com.google.common.collect.ImmutableList<SubstitutionVisitor.UnifyRule> rules,
org.apache.calcite.tools.RelBuilderFactory relBuilderFactory)
Creates a SubstitutionVisitor.
|
Modifier and Type | Method and Description |
---|---|
protected static org.apache.calcite.rex.RexShuttle |
getRexShuttle(SubstitutionVisitor.MutableProject target)
Builds a shuttle that stores a list of expressions, and can map incoming
expressions to references to them.
|
static org.apache.calcite.sql.SqlAggFunction |
getRollup(org.apache.calcite.sql.SqlAggFunction aggregation) |
List<org.apache.calcite.rel.RelNode> |
go(org.apache.calcite.rel.RelNode replacement_)
Returns a list of all possible rels that result from substituting the
matched RelNode with the replacement RelNode within the query.
|
org.apache.calcite.rel.RelNode |
go0(org.apache.calcite.rel.RelNode replacement_) |
protected boolean |
isWeaker(SubstitutionVisitor.MutableRel rel0,
SubstitutionVisitor.MutableRel rel)
Returns if one rel is weaker than another.
|
static boolean |
mayBeSatisfiable(org.apache.calcite.rex.RexNode e)
Returns whether a boolean expression ever returns true.
|
static SubstitutionVisitor.MutableAggregate |
permute(SubstitutionVisitor.MutableAggregate aggregate,
SubstitutionVisitor.MutableRel input,
org.apache.calcite.util.mapping.Mapping mapping) |
static org.apache.calcite.rex.RexNode |
splitFilter(org.apache.calcite.rex.RexBuilder rexBuilder,
org.apache.calcite.rex.RexNode condition,
org.apache.calcite.rex.RexNode target)
Maps a condition onto a target.
|
static SubstitutionVisitor.MutableRel |
unifyAggregates(SubstitutionVisitor.MutableAggregate query,
SubstitutionVisitor.MutableAggregate target) |
protected static final com.google.common.collect.ImmutableList<SubstitutionVisitor.UnifyRule> DEFAULT_RULES
protected final org.apache.calcite.tools.RelBuilder relBuilder
The actual builder is available via RelOptRuleCall.builder()
.
protected final SubstitutionVisitor.MutableRel[] slots
public SubstitutionVisitor(org.apache.calcite.rel.RelNode target_, org.apache.calcite.rel.RelNode query_)
public SubstitutionVisitor(org.apache.calcite.rel.RelNode target_, org.apache.calcite.rel.RelNode query_, com.google.common.collect.ImmutableList<SubstitutionVisitor.UnifyRule> rules)
public SubstitutionVisitor(org.apache.calcite.rel.RelNode target_, org.apache.calcite.rel.RelNode query_, com.google.common.collect.ImmutableList<SubstitutionVisitor.UnifyRule> rules, org.apache.calcite.tools.RelBuilderFactory relBuilderFactory)
public static org.apache.calcite.rex.RexNode splitFilter(org.apache.calcite.rex.RexBuilder rexBuilder, org.apache.calcite.rex.RexNode condition, org.apache.calcite.rex.RexNode 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, returns
null
.
The terms satisfy the relation
condition = target AND residue
and residue
must be as weak as possible.
Example #1: condition stronger than target
Note that residue x > 0 AND y = 2
would also satisfy the
relation condition = target AND residue
but is stronger than
necessary, so we prefer y = 2
.
Example #2: target weaker than condition (valid, but not currently implemented)
Example #3: condition and target are equivalent
Example #4: condition weaker than target
There are many other possible examples. It amounts to solving
whether condition AND NOT target
can ever evaluate to
true, and therefore is a form of the NP-complete
Satisfiability
problem.
public static boolean mayBeSatisfiable(org.apache.calcite.rex.RexNode e)
This method may give false positives. For instance, it will say
that x = 5 AND x > 10
is satisfiable, because at present it
cannot prove that it is not.
public org.apache.calcite.rel.RelNode go0(org.apache.calcite.rel.RelNode replacement_)
public List<org.apache.calcite.rel.RelNode> go(org.apache.calcite.rel.RelNode replacement_)
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.
public static SubstitutionVisitor.MutableAggregate permute(SubstitutionVisitor.MutableAggregate aggregate, SubstitutionVisitor.MutableRel input, org.apache.calcite.util.mapping.Mapping mapping)
public static SubstitutionVisitor.MutableRel unifyAggregates(SubstitutionVisitor.MutableAggregate query, SubstitutionVisitor.MutableAggregate target)
public static org.apache.calcite.sql.SqlAggFunction getRollup(org.apache.calcite.sql.SqlAggFunction aggregation)
protected static org.apache.calcite.rex.RexShuttle getRexShuttle(SubstitutionVisitor.MutableProject target)
protected boolean isWeaker(SubstitutionVisitor.MutableRel rel0, SubstitutionVisitor.MutableRel rel)
Copyright © 2021 The Apache Software Foundation. All rights reserved.