org.eigenbase.sql2rel
Class RelDecorrelator

java.lang.Object
  extended by org.eigenbase.sql2rel.RelDecorrelator
All Implemented Interfaces:
ReflectiveVisitor

public class RelDecorrelator
extends Object
implements ReflectiveVisitor

RelDecorrelator replaces all correlated expressions(corExp) in a relational expression (RelNode) tree with non-correlated expressions that are produced from joining the RelNode that produces the corExp with the RelNode that references it.

Version:
$Id: //open/dev/farrago/src/org/eigenbase/sql2rel/RelDecorrelator.java#22 $
Author:
Rushan Chen

Nested Class Summary
private  class RelDecorrelator.AdjustProjectForCountAggregateRule
           
private  class RelDecorrelator.DecorrelateRelVisitor
           
private  class RelDecorrelator.DecorrelateRexShuttle
           
private  class RelDecorrelator.RemoveCorrelationForScalarAggregateRule
           
private  class RelDecorrelator.RemoveCorrelationForScalarProjectRule
           
private  class RelDecorrelator.RemoveCorrelationRexShuttle
           
private  class RelDecorrelator.RemoveSingleAggregateRule
          Rule to remove single_value rel.
 
Field Summary
private  RelNode currentRel
           
private  RelDecorrelator.DecorrelateRelVisitor decorrelateVisitor
           
private  HashSet<CorrelatorRel> generatedCorRels
           
private  SortedMap<CorrelatorRel.Correlation,CorrelatorRel> mapCorVarToCorRel
           
private  Map<RexFieldAccess,CorrelatorRel.Correlation> mapFieldAccessToCorVar
           
private  Map<RelNode,SortedMap<CorrelatorRel.Correlation,Integer>> mapNewRelToMapCorVarToOutputPos
           
private  Map<RelNode,Map<Integer,Integer>> mapNewRelToMapOldToNewOutputPos
           
private  Map<RelNode,RelNode> mapOldToNewRel
           
private  Map<RelNode,SortedSet<CorrelatorRel.Correlation>> mapRefRelToCorVar
           
private  RexBuilder rexBuilder
           
private static Logger sqlToRelTracer
           
 
Constructor Summary
RelDecorrelator(RexBuilder rexBuilder, Map<RelNode,SortedSet<CorrelatorRel.Correlation>> mapRefRelToCorVar, SortedMap<CorrelatorRel.Correlation,CorrelatorRel> mapCorVarToCorRel, Map<RexFieldAccess,CorrelatorRel.Correlation> mapFieldAccessToCorVar)
           
 
Method Summary
private  RelNode aggregateCorrelatorOutput(CorrelatorRel corRel, ProjectRel projRel, Set<Integer> isCount)
          Pulls projRel above the joinRel from its RHS input.
private  boolean checkCorVars(CorrelatorRel corRel, ProjectRel projRel, FilterRel filterRel, List<RexFieldAccess> correlatedJoinKeys)
          Checks whether the correlations in projRel and filterRel are related to the correlated variables provided by corRel.
private  RelNode createProjectWithAdditionalExprs(RelNode childRel, RexNode[] additionalExprs, String[] additionalExprNames)
          Project all childRel output fields plus the additional expressions.
private  RelNode createValueGenerator(SortedSet<CorrelatorRel.Correlation> correlations, int valueGenFieldOffset, SortedMap<CorrelatorRel.Correlation,Integer> mapCorVarToOutputPos)
          Create RelNode tree that produces a list of correlated variables.
 RelNode decorrelate(RelNode root)
           
protected  RexNode decorrelateExpr(RexNode exp)
           
private  void decorrelateInputWithValueGenerator(RelNode rel)
           
 void decorrelateRel(AggregateRel rel)
          Rewrite AggregateRel.
 void decorrelateRel(CorrelatorRel rel)
          Rewrite CorrelatorRel into a left outer join.
 void decorrelateRel(FilterRel rel)
          Rewrite FilterRel.
 void decorrelateRel(JoinRel rel)
          Rewrite JoinRel.
 void decorrelateRel(ProjectRel rel)
          Rewrite ProjectRel.
 void decorrelateRel(SortRel rel)
          Rewrite SortRel.
 void decorrelateRelGeneric(RelNode rel)
           
private  RexInputRef getNewForOldInputRef(RexInputRef oldInputRef)
           
private  RelNode projectJoinOutputWithNullability(JoinRel joinRel, ProjectRel projRel, int nullIndicatorPos)
          Pull projRel above the joinRel from its RHS input.
protected  RexNode removeCorrelationExpr(RexNode exp, boolean projectPulledAboveLeftCorrelator)
           
protected  RexNode removeCorrelationExpr(RexNode exp, boolean projectPulledAboveLeftCorrelator, RexInputRef nullIndicator)
           
protected  RexNode removeCorrelationExpr(RexNode exp, boolean projectPulledAboveLeftCorrelator, Set<Integer> isCount)
           
 RelNode removeCorrelationViaRule(RelNode root)
           
private  void removeCorVarFromTree(CorrelatorRel corRel)
          Remove correlated variables from the tree at root corRel
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

sqlToRelTracer

private static final Logger sqlToRelTracer

mapRefRelToCorVar

private final Map<RelNode,SortedSet<CorrelatorRel.Correlation>> mapRefRelToCorVar

mapCorVarToCorRel

private final SortedMap<CorrelatorRel.Correlation,CorrelatorRel> mapCorVarToCorRel

mapFieldAccessToCorVar

private final Map<RexFieldAccess,CorrelatorRel.Correlation> mapFieldAccessToCorVar

decorrelateVisitor

private final RelDecorrelator.DecorrelateRelVisitor decorrelateVisitor

rexBuilder

private final RexBuilder rexBuilder

currentRel

private RelNode currentRel

mapOldToNewRel

private final Map<RelNode,RelNode> mapOldToNewRel

mapNewRelToMapCorVarToOutputPos

private final Map<RelNode,SortedMap<CorrelatorRel.Correlation,Integer>> mapNewRelToMapCorVarToOutputPos

mapNewRelToMapOldToNewOutputPos

private final Map<RelNode,Map<Integer,Integer>> mapNewRelToMapOldToNewOutputPos

generatedCorRels

private final HashSet<CorrelatorRel> generatedCorRels
Constructor Detail

RelDecorrelator

public RelDecorrelator(RexBuilder rexBuilder,
                       Map<RelNode,SortedSet<CorrelatorRel.Correlation>> mapRefRelToCorVar,
                       SortedMap<CorrelatorRel.Correlation,CorrelatorRel> mapCorVarToCorRel,
                       Map<RexFieldAccess,CorrelatorRel.Correlation> mapFieldAccessToCorVar)
Method Detail

decorrelate

public RelNode decorrelate(RelNode root)

removeCorrelationViaRule

public RelNode removeCorrelationViaRule(RelNode root)

decorrelateExpr

protected RexNode decorrelateExpr(RexNode exp)

removeCorrelationExpr

protected RexNode removeCorrelationExpr(RexNode exp,
                                        boolean projectPulledAboveLeftCorrelator)

removeCorrelationExpr

protected RexNode removeCorrelationExpr(RexNode exp,
                                        boolean projectPulledAboveLeftCorrelator,
                                        RexInputRef nullIndicator)

removeCorrelationExpr

protected RexNode removeCorrelationExpr(RexNode exp,
                                        boolean projectPulledAboveLeftCorrelator,
                                        Set<Integer> isCount)

decorrelateRelGeneric

public void decorrelateRelGeneric(RelNode rel)

decorrelateRel

public void decorrelateRel(SortRel rel)
Rewrite SortRel.

Parameters:
rel - SortRel to be rewritten

decorrelateRel

public void decorrelateRel(AggregateRel rel)
Rewrite AggregateRel.

Parameters:
rel - the project rel to rewrite

decorrelateRel

public void decorrelateRel(ProjectRel rel)
Rewrite ProjectRel.

Parameters:
rel - the project rel to rewrite

createValueGenerator

private RelNode createValueGenerator(SortedSet<CorrelatorRel.Correlation> correlations,
                                     int valueGenFieldOffset,
                                     SortedMap<CorrelatorRel.Correlation,Integer> mapCorVarToOutputPos)
Create RelNode tree that produces a list of correlated variables.

Parameters:
correlations - correlated variables to generate
valueGenFieldOffset - offset in the output that generated columns will start
mapCorVarToOutputPos - output positions for the correlated variables generated
Returns:
RelNode the root of the resultant RelNode tree

decorrelateInputWithValueGenerator

private void decorrelateInputWithValueGenerator(RelNode rel)

decorrelateRel

public void decorrelateRel(FilterRel rel)
Rewrite FilterRel.

Parameters:
rel - the filter rel to rewrite

decorrelateRel

public void decorrelateRel(CorrelatorRel rel)
Rewrite CorrelatorRel into a left outer join.

Parameters:
rel - CorrelatorRel

decorrelateRel

public void decorrelateRel(JoinRel rel)
Rewrite JoinRel.

Parameters:
rel - JoinRel

getNewForOldInputRef

private RexInputRef getNewForOldInputRef(RexInputRef oldInputRef)

projectJoinOutputWithNullability

private RelNode projectJoinOutputWithNullability(JoinRel joinRel,
                                                 ProjectRel projRel,
                                                 int nullIndicatorPos)
Pull projRel above the joinRel from its RHS input. Enforce nullability for join output.

Parameters:
joinRel -
projRel - the orginal projRel as the RHS input of the join.
nullIndicatorPos -
Returns:
the subtree with the new ProjectRel at the root

aggregateCorrelatorOutput

private RelNode aggregateCorrelatorOutput(CorrelatorRel corRel,
                                          ProjectRel projRel,
                                          Set<Integer> isCount)
Pulls projRel above the joinRel from its RHS input. Enforces nullability for join output.

Parameters:
corRel - Correlator
projRel - the orginal ProjectRel as the RHS input of the join
isCount - Positions which are calls to the COUNT aggregation function
Returns:
the subtree with the new ProjectRel at the root

checkCorVars

private boolean checkCorVars(CorrelatorRel corRel,
                             ProjectRel projRel,
                             FilterRel filterRel,
                             List<RexFieldAccess> correlatedJoinKeys)
Checks whether the correlations in projRel and filterRel are related to the correlated variables provided by corRel.

Parameters:
corRel - Correlator
projRel - the orginal ProjectRel as the RHS input of the join
filterRel - Filter
correlatedJoinKeys - Correlated join keys
Returns:
true if filter and proj only references corVar provided by corRel

removeCorVarFromTree

private void removeCorVarFromTree(CorrelatorRel corRel)
Remove correlated variables from the tree at root corRel

Parameters:
corRel -

createProjectWithAdditionalExprs

private RelNode createProjectWithAdditionalExprs(RelNode childRel,
                                                 RexNode[] additionalExprs,
                                                 String[] additionalExprNames)
Project all childRel output fields plus the additional expressions.

Parameters:
childRel -
additionalExprs -
additionalExprNames -
Returns:
the new ProjectRel