net.sf.farrago.fennel.rel
Class CalcRelSplitter

java.lang.Object
  extended by net.sf.farrago.fennel.rel.CalcRelSplitter
Direct Known Subclasses:
FarragoAutoCalcRule.AutoCalcRelSplitter, FarragoMultisetSplitterRule.MultisetRelSplitter, FarragoMultisetSplitterRule.NestedRelSplitter, WindowedAggSplitterRule.WindowedAggRelSplitter

public abstract class CalcRelSplitter
extends Object

CalcRelSplitter operates on a CalcRel with multiple RexCall sub-expressions that cannot all be implemented by a single concrete RelNode.

For example, the Java and Fennel calculator do not implement an identical set of operators. The CalcRel can be used to split a single CalcRel with mixed Java- and Fennel-only operators into a tree of CalcRel object that can each be individually implemented by either Java or Fennel.and splits it into several CalcRel instances.

Currently the splitter is only capable of handling two "rel types". That is, it can deal with Java vs. Fennel CalcRels, but not Java vs. Fennel vs. some other type of CalcRel.

See FarragoAutoCalcRule for an example of how this class is used.


Nested Class Summary
private static class CalcRelSplitter.CannotImplement
          Control exception for CalcRelSplitter.ImplementTester.
private static class CalcRelSplitter.HighestUsageFinder
          Builds an array of the highest level which contains an expression which uses each expression as an input.
private static class CalcRelSplitter.ImplementTester
          Visitor which returns whether an expression can be implemented in a given type of relational expression.
private static class CalcRelSplitter.InputToCommonExprConverter
          Shuttle which converts every reference to an input field in an expression to a reference to a common sub-expression.
private static class CalcRelSplitter.MaxInputFinder
          Finds the highest level used by any of the inputs of a given expression.
static class CalcRelSplitter.RelType
           
 
Field Summary
private  RelNode child
           
private  RelOptCluster cluster
           
private  RexProgram program
           
private  CalcRelSplitter.RelType[] relTypes
           
private static Logger ruleTracer
           
private  RelTraitSet traits
           
private  RelDataTypeFactory typeFactory
           
 
Constructor Summary
CalcRelSplitter(CalcRel calc, CalcRelSplitter.RelType[] relTypes)
          Constructs a CalcRelSplitter.
 
Method Summary
protected  boolean canImplement(CalcRel rel, String relTypeName)
          Returns whether a relational expression can be implemented solely in a given CalcRelSplitter.RelType.
private  int chooseLevels(RexNode[] exprs, int conditionOrdinal, int[] exprLevels, int[] levelTypeOrdinals)
          Figures out which expressions to calculate at which level.
private static int count(boolean[] booleans)
          Returns the number of bits set in an array.
private  RexProgram createProgramForLevel(int level, RelDataType inputRowType, RexNode[] allExprs, int[] exprLevels, int[] inputExprOrdinals, int[] projectExprOrdinals, int conditionExprOrdinal, RelDataType outputRowType)
          Creates a program containing the expressions for a given level.
(package private)  RelNode execute()
           
private static int firstSet(boolean[] booleans)
          Returns the index of the first set bit in an array.
private  int[] identityArray(int length)
           
private static int indexOf(int value, int[] map)
          Searches for a value in a map, and returns the position where it was found, or -1.
private  void traceLevelExpressions(RexNode[] exprs, int[] exprLevels, int[] levelTypeOrdinals, int levelCount)
          Traces the given array of level expression lists at the finer level.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ruleTracer

private static final Logger ruleTracer

program

private final RexProgram program

typeFactory

private final RelDataTypeFactory typeFactory

relTypes

private final CalcRelSplitter.RelType[] relTypes

cluster

private final RelOptCluster cluster

traits

private final RelTraitSet traits

child

private final RelNode child
Constructor Detail

CalcRelSplitter

CalcRelSplitter(CalcRel calc,
                CalcRelSplitter.RelType[] relTypes)
Constructs a CalcRelSplitter.

Parameters:
calc - CalcRel to split
relTypes - Array of rel types, e.g. {Java, Fennel}. Must be distinct.
Method Detail

execute

RelNode execute()

chooseLevels

private int chooseLevels(RexNode[] exprs,
                         int conditionOrdinal,
                         int[] exprLevels,
                         int[] levelTypeOrdinals)
Figures out which expressions to calculate at which level.

Parameters:
exprs - Array of expressions
conditionOrdinal - Ordinal of the condition expression, or -1 if no condition
exprLevels - Level ordinal for each expression (output)
levelTypeOrdinals - The type of each level (output)

identityArray

private int[] identityArray(int length)

createProgramForLevel

private RexProgram createProgramForLevel(int level,
                                         RelDataType inputRowType,
                                         RexNode[] allExprs,
                                         int[] exprLevels,
                                         int[] inputExprOrdinals,
                                         int[] projectExprOrdinals,
                                         int conditionExprOrdinal,
                                         RelDataType outputRowType)
Creates a program containing the expressions for a given level.

The expression list of the program will consist of all entries in the expression list allExprs[i] for which the corresponding level ordinal exprLevels[i] is equal to level. Expressions are mapped according to inputExprOrdinals.

Parameters:
level - Level ordinal
inputRowType - Input row type
allExprs - Array of all expressions
exprLevels - Array of the level ordinal of each expression
inputExprOrdinals - Ordinals in the expression list of input expressions. Input expression i will be found at position inputExprOrdinals[i].
projectExprOrdinals - Ordinals of the expressions to be output this level.
conditionExprOrdinal - Ordinal of the expression to form the condition for this level, or -1 if there is no condition.
outputRowType - Output row type
Returns:
Relational expression

traceLevelExpressions

private void traceLevelExpressions(RexNode[] exprs,
                                   int[] exprLevels,
                                   int[] levelTypeOrdinals,
                                   int levelCount)
Traces the given array of level expression lists at the finer level.

Parameters:
exprs - Array expressions
exprLevels - For each expression, the ordinal of its level
levelTypeOrdinals - For each level, the ordinal of its reltype in the relTypes array
levelCount - The number of levels

count

private static int count(boolean[] booleans)
Returns the number of bits set in an array.


firstSet

private static int firstSet(boolean[] booleans)
Returns the index of the first set bit in an array.


indexOf

private static int indexOf(int value,
                           int[] map)
Searches for a value in a map, and returns the position where it was found, or -1.

Parameters:
value - Value to search for
map - Map to search in
Returns:
Ordinal of value in map, or -1 if not found

canImplement

protected boolean canImplement(CalcRel rel,
                               String relTypeName)
Returns whether a relational expression can be implemented solely in a given CalcRelSplitter.RelType.

Parameters:
rel - Calculation relational expression
relTypeName - Name of a CalcRelSplitter.RelType
Returns:
Whether relational expression can be implemented