net.sf.saffron.opt
Class RelImplementor

java.lang.Object
  |
  +--net.sf.saffron.opt.RelImplementor

public class RelImplementor
extends Object

RelImplementor deals with the nastiness of converting a tree of relational expressions into an implementation, generally an openjava parse tree.

The bind(net.sf.saffron.rel.SaffronRel, openjava.ptree.Variable) method allows relational expressions to register which Java variable holds their row. They can bind 'lazily', so that the variable is only declared and initialized if it is actually used in another expression.

TODO jvs 29-Sept-2003: This class is way too Java-dependent to remain in opt. Most of it needs to be factored out to oj, leaving an interface or abstract base class behind. I intentionally left one macker failure unsuppressed so that this will get some attention.


Nested Class Summary
private static interface RelImplementor.Bind
           
private static class RelImplementor.EagerBind
           
private static class RelImplementor.Frame
           
private static class RelImplementor.LazyBind
           
private  class RelImplementor.RelBind
          Binds a relational expression to whatever another relational expression is currently bound to.
private static class RelImplementor.Translator
          Translator is a shuttle used to implement translate(net.sf.saffron.rel.SaffronRel, net.sf.saffron.rex.RexNode).
static interface RelImplementor.VariableInitializerThunk
          A VariableInitializerThunk yields a VariableInitializer.
 
Field Summary
(package private)  Statement exitStatement
           
(package private)  HashMap mapCorrel2Frame
          Map String --> Frame such that frame.rel.correlVariable == correlName
(package private)  HashMap mapCorrelNameToVariable
           
(package private)  HashMap mapRel2Frame
           
(package private)  RexBuilder rexBuilder
           
(package private)  Stack stmtListStack
           
 
Constructor Summary
RelImplementor(RexBuilder rexBuilder)
           
 
Method Summary
private  void bind(SaffronRel rel, RelImplementor.Bind bind)
          Record the fact that instances of rel are available via bind (which may be eager or lazy).
 void bind(SaffronRel rel, SaffronRel previous)
          Shares a variable between relations.
 Variable bind(SaffronRel rel, StatementList statementList, VariableInitializer initializer)
          Declare a variable, and bind it lazily, so it only gets initialized if it is actually used.
 void bind(SaffronRel rel, Variable variable)
          Record the fact that instances of rel are available in variable.
 void bindCorrel(String correlName, Variable variable)
          Binds a correlating variable.
private  void bindDeferred(RelImplementor.Frame frame, SaffronRel rel)
           
private  int computeFieldOffset(SaffronRel rel, int ordinal)
          Returns the index of the first field in rel which comes from its ordinalth input.
private  SaffronRel findInputRel(SaffronRel rel, int offset)
           
private  SaffronRel findInputRel(SaffronRel rel, int offset, int[] offsets)
           
(package private)  Variable findInputVariable(SaffronRel rel)
          Returns the variable which, in the generated program, will hold the current row of a given relational expression.
 SaffronRel findRel(SaffronRel rel, RexNode expression)
           
 void generateParentBody(SaffronRel rel, StatementList stmtList)
          Implements the body of the current expression's parent.
 Statement getExitStatement()
           
 StatementList getStatementList()
           
 Object implementChild(SaffronRel parent, int ordinal, SaffronRel child)
          Implements a relational expression according to a calling convention.
 Object implementRoot(SaffronRel rel)
          Starts an iteration, by calling SaffronRel.implement(net.sf.saffron.opt.RelImplementor, int) on the root element.
 Expression makeReference(String correlName, SaffronRel rel)
          Creates an expression which references correlating variable correlName from the context of rel.
 Variable newVariable()
           
 void popStatementList(StatementList stmtList)
           
 void pushStatementList(StatementList stmtList)
           
 void setExitStatement(Statement stmt)
           
 Expression translate(SaffronRel rel, RexNode exp)
          Converts an expression in internal form (the input relation is referenced using the variable $input0) to generated form (the input relation is referenced using the bindings in this RelImplementor).
 Expression translateInput(SaffronRel rel, int ordinal)
          Creates an expression which references the ordinalth input.
 Expression translateInputField(SaffronRel rel, int ordinal, int fieldOrdinal)
          Creates an expression which references the fieldOrdinalth field of the ordinalth input.
 ExpressionList translateList(SaffronRel rel, RexNode[] exps)
          Converts an array of expressions in internal into a list of expressions in generated form.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

mapCorrel2Frame

final HashMap mapCorrel2Frame
Map String --> Frame such that frame.rel.correlVariable == correlName


mapCorrelNameToVariable

final HashMap mapCorrelNameToVariable

mapRel2Frame

final HashMap mapRel2Frame

stmtListStack

final Stack stmtListStack

exitStatement

Statement exitStatement

rexBuilder

RexBuilder rexBuilder
Constructor Detail

RelImplementor

public RelImplementor(RexBuilder rexBuilder)
Method Detail

setExitStatement

public void setExitStatement(Statement stmt)

getExitStatement

public Statement getExitStatement()

getStatementList

public StatementList getStatementList()

bind

public void bind(SaffronRel rel,
                 Variable variable)
Record the fact that instances of rel are available in variable.


bind

public Variable bind(SaffronRel rel,
                     StatementList statementList,
                     VariableInitializer initializer)
Declare a variable, and bind it lazily, so it only gets initialized if it is actually used.

Returns:
the Variable so declared

bind

public void bind(SaffronRel rel,
                 SaffronRel previous)
Shares a variable between relations. previous already has a variable, and calling this method indicates that rel's output will appear in this variable too.


bindCorrel

public void bindCorrel(String correlName,
                       Variable variable)
Binds a correlating variable. References to correlating variables such as $cor2 will be replaced with java variables such as $Oj14.


findRel

public SaffronRel findRel(SaffronRel rel,
                          RexNode expression)

generateParentBody

public void generateParentBody(SaffronRel rel,
                               StatementList stmtList)
Implements the body of the current expression's parent. If variable is not null, bind the current expression to variable. For example, a nested loops join would generate
 for (int i = 0; i < emps.length; i++) {
   Emp emp = emps[i];
   for (int j = 0; j < depts.length; j++) {
     Dept dept = depts[j];
     if (emp.deptno == dept.deptno) {
       <>
     }
   }
 }
 
which corresponds to
 [emp:iter
   [dept:iter
     [join:body(emp,dept)
       [parent:body]
     ]
   ]
 ]
 

Parameters:
rel - child relation
stmtList - block that child was generating its code into

bindDeferred

private void bindDeferred(RelImplementor.Frame frame,
                          SaffronRel rel)

implementChild

public Object implementChild(SaffronRel parent,
                             int ordinal,
                             SaffronRel child)
Implements a relational expression according to a calling convention.


implementRoot

public Object implementRoot(SaffronRel rel)
Starts an iteration, by calling SaffronRel.implement(net.sf.saffron.opt.RelImplementor, int) on the root element.


makeReference

public Expression makeReference(String correlName,
                                SaffronRel rel)
Creates an expression which references correlating variable correlName from the context of rel. For example, if correlName is set by the 1st child of rel's 2nd child, then this method returns $input2.$input1.


newVariable

public Variable newVariable()

popStatementList

public void popStatementList(StatementList stmtList)

pushStatementList

public void pushStatementList(StatementList stmtList)

translate

public Expression translate(SaffronRel rel,
                            RexNode exp)
Converts an expression in internal form (the input relation is referenced using the variable $input0) to generated form (the input relation is referenced using the bindings in this RelImplementor). Compare this method with QueryInfo.convertExpToInternal(openjava.ptree.Expression), which converts from source form to internal form.

Parameters:
exp - the expression to translate (it is cloned, not modified)
rel - the relational expression which is the context for exp

translateList

public ExpressionList translateList(SaffronRel rel,
                                    RexNode[] exps)
Converts an array of expressions in internal into a list of expressions in generated form.

See Also:
translate(SaffronRel,RexNode)

translateInput

public Expression translateInput(SaffronRel rel,
                                 int ordinal)
Creates an expression which references the ordinalth input.


computeFieldOffset

private int computeFieldOffset(SaffronRel rel,
                               int ordinal)
Returns the index of the first field in rel which comes from its ordinalth input.

For example, if rel joins T0(A,B,C) to T1(D,E), then countFields(0,rel) yields 0, and countFields(1,rel) yields 3.


translateInputField

public Expression translateInputField(SaffronRel rel,
                                      int ordinal,
                                      int fieldOrdinal)
Creates an expression which references the fieldOrdinalth field of the ordinalth input.

(We can potentially optimize the generation process, so we can access field values without actually instantiating the row.)


bind

private void bind(SaffronRel rel,
                  RelImplementor.Bind bind)
Record the fact that instances of rel are available via bind (which may be eager or lazy).


findInputRel

private SaffronRel findInputRel(SaffronRel rel,
                                int offset)

findInputRel

private SaffronRel findInputRel(SaffronRel rel,
                                int offset,
                                int[] offsets)

findInputVariable

Variable findInputVariable(SaffronRel rel)
Returns the variable which, in the generated program, will hold the current row of a given relational expression. This method is only applicable if the relational expression is the current one or an input; if it is an ancestor, there is no current value, and this method returns null.


SourceForge.net_Logo