org.eigenbase.oj.rex
Class RexToOJTranslator

java.lang.Object
  extended by org.eigenbase.oj.rex.RexToOJTranslator
All Implemented Interfaces:
RexVisitor<Expression>
Direct Known Subclasses:
FarragoRexToOJTranslator

public class RexToOJTranslator
extends Object
implements RexVisitor<Expression>

Converts expressions in logical format (RexNode) into OpenJava code.

Version:
$Id: //open/dev/farrago/src/org/eigenbase/oj/rex/RexToOJTranslator.java#27 $
Author:
John V. Sichi

Nested Class Summary
private static class RexToOJTranslator.WhichInputResult
          Result of call to whichInput(int, org.eigenbase.rel.RelNode), contains the input relational expression, its index, and the index of the field within that relational expression.
 
Field Summary
private  RelNode contextRel
           
private  JavaRelImplementor implementor
           
private  OJRexImplementorTable implementorTable
           
private  RexProgram program
          Program which the expression is part of.
private  Stack<RexProgram> programStack
           
private  StatementList[] stmtLists
          Statement lists being built up for ROW or CASE expression.
private  Expression translatedExpr
           
 
Constructor Summary
RexToOJTranslator(JavaRelImplementor implementor, RelNode contextRel, OJRexImplementorTable implementorTable)
          Creates a translator.
 
Method Summary
 boolean canConvertCall(RexCall call)
           
protected  Expression convertByteArrayLiteral(byte[] bytes)
           
protected  ArrayInitializer convertByteArrayLiteralToInitializer(byte[] bytes)
           
protected  Expression convertCall(RexCall call, Expression[] operandExprs)
          Converts a call after its operands have already been translated.
 RelNode getContextRel()
           
protected  OJRexImplementorTable getImplementorTable()
           
 RexProgram getProgram()
          Returns the current program.
 JavaRelImplementor getRelImplementor()
           
 StatementList getSubStmtList(int i)
          Returns the StatementList corresponding to a subexpression of a CASE or ROW expression.
protected  Expression getTranslation()
           
 RelDataTypeFactory getTypeFactory()
           
protected  boolean isInputRef(RexLocalRef localRef)
          Tests whether a RexLocalRef refers to an input.
 void popProgram(RexProgram program)
          Restores the current program to the one before pushProgram(RexProgram) was called.
 RexToOJTranslator push(StatementList stmtList)
          Returns a sub-translator to deal with a sub-block.
 void pushProgram(RexProgram program)
          Sets the current program.
protected  Expression setTranslation(Expression expr)
           
 void translateAssignment(RelDataTypeField lhsField, Expression lhs, RexNode rhs)
          Generates code for an assignment.
private  Expression translateInput(int index)
           
 Expression translateRexNode(RexNode node)
          Translates an expression into a Java expression.
 Expression translateSubExpression(RexLocalRef localRef)
          Translates a common subexpression.
 Expression visitCall(RexCall call)
           
 Expression visitCorrelVariable(RexCorrelVariable correlVariable)
           
 Expression visitDynamicParam(RexDynamicParam dynamicParam)
           
 Expression visitFieldAccess(RexFieldAccess fieldAccess)
           
 Expression visitInputRef(RexInputRef inputRef)
           
 Expression visitLiteral(RexLiteral literal)
           
 Expression visitLocalRef(RexLocalRef localRef)
           
 Expression visitOver(RexOver over)
           
 Expression visitRangeRef(RexRangeRef rangeRef)
           
private static RexToOJTranslator.WhichInputResult whichInput(int fieldIndex, RelNode rel)
          Returns the ordinal of the input relational expression which a given column ordinal comes from.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

implementor

private final JavaRelImplementor implementor

contextRel

private final RelNode contextRel

implementorTable

private final OJRexImplementorTable implementorTable

translatedExpr

private Expression translatedExpr

program

private RexProgram program
Program which the expression is part of.


stmtLists

private StatementList[] stmtLists
Statement lists being built up for ROW or CASE expression. For CASE expressions, each WHEN, THEN or ELSE has one statementList. For ROW expressions, each value in a row has one statementList.


programStack

private final Stack<RexProgram> programStack
Constructor Detail

RexToOJTranslator

public RexToOJTranslator(JavaRelImplementor implementor,
                         RelNode contextRel,
                         OJRexImplementorTable implementorTable)
Creates a translator.

Parameters:
implementor - implementation context
contextRel - relational expression which is the context for the row-expressions which are to be translated
implementorTable - table of implementation functors for Rex operators; if null, OJRexImplementorTableImpl.instance is used
Method Detail

getSubStmtList

public StatementList getSubStmtList(int i)
Returns the StatementList corresponding to a subexpression of a CASE or ROW expression.


setTranslation

protected Expression setTranslation(Expression expr)

getTranslation

protected Expression getTranslation()

getProgram

public RexProgram getProgram()
Returns the current program.

See Also:
pushProgram(RexProgram), popProgram(RexProgram)

getImplementorTable

protected OJRexImplementorTable getImplementorTable()

getRelImplementor

public JavaRelImplementor getRelImplementor()

getContextRel

public RelNode getContextRel()

getTypeFactory

public RelDataTypeFactory getTypeFactory()

visitLocalRef

public Expression visitLocalRef(RexLocalRef localRef)
Specified by:
visitLocalRef in interface RexVisitor<Expression>

translateSubExpression

public Expression translateSubExpression(RexLocalRef localRef)
Translates a common subexpression.

Parameters:
localRef - common subexpression to be translated
Returns:
translation

isInputRef

protected boolean isInputRef(RexLocalRef localRef)
Tests whether a RexLocalRef refers to an input.

Parameters:
localRef - reference to test
Returns:
true if an input reference; false if a reference to a common subexpression

visitInputRef

public Expression visitInputRef(RexInputRef inputRef)
Specified by:
visitInputRef in interface RexVisitor<Expression>

translateInput

private Expression translateInput(int index)

visitLiteral

public Expression visitLiteral(RexLiteral literal)
Specified by:
visitLiteral in interface RexVisitor<Expression>

visitCall

public Expression visitCall(RexCall call)
Specified by:
visitCall in interface RexVisitor<Expression>

convertCall

protected Expression convertCall(RexCall call,
                                 Expression[] operandExprs)
Converts a call after its operands have already been translated.

Parameters:
call - call to be translated
operandExprs - translated operands
Returns:
converted call

visitOver

public Expression visitOver(RexOver over)
Specified by:
visitOver in interface RexVisitor<Expression>

visitCorrelVariable

public Expression visitCorrelVariable(RexCorrelVariable correlVariable)
Specified by:
visitCorrelVariable in interface RexVisitor<Expression>

visitDynamicParam

public Expression visitDynamicParam(RexDynamicParam dynamicParam)
Specified by:
visitDynamicParam in interface RexVisitor<Expression>

visitRangeRef

public Expression visitRangeRef(RexRangeRef rangeRef)
Specified by:
visitRangeRef in interface RexVisitor<Expression>

visitFieldAccess

public Expression visitFieldAccess(RexFieldAccess fieldAccess)
Specified by:
visitFieldAccess in interface RexVisitor<Expression>

translateRexNode

public Expression translateRexNode(RexNode node)
Translates an expression into a Java expression. If the program has previously been set via pushProgram(RexProgram), the expression is interpreted in terms of the output fields of the program. Suppose that the program is
   exprs: {$0, $1, $0 + $1}
   projectRefs: {$0, $2}
   conditionRef: null
and the expression is $1 + 5. This would be expanded to (a + b) + 5, because output field $1 of the program is defined to be the expression $0 + $1 in terms of the input fields.

Sometimes a calculator expression is defined in terms of simpler calculator expressions. If this is the case, those expressions will be successively evaluated and assigned to variables. If a variable with the appropriate value is already in scope, it will be used.

If the program is not present, no mapping occurs.

Parameters:
node - Expression to be translated.
Returns:
Java translation of expression

convertByteArrayLiteralToInitializer

protected ArrayInitializer convertByteArrayLiteralToInitializer(byte[] bytes)

convertByteArrayLiteral

protected Expression convertByteArrayLiteral(byte[] bytes)

canConvertCall

public boolean canConvertCall(RexCall call)

whichInput

private static RexToOJTranslator.WhichInputResult whichInput(int fieldIndex,
                                                             RelNode rel)
Returns the ordinal of the input relational expression which a given column ordinal comes from.

For example, if rel has inputs I(a, b, c) and J(d, e), then whichInput(0, rel) returns 0 (column a), whichInput(2, rel) returns 0 (column c), whichInput(3, rel) returns 1 (column d).

Parameters:
fieldIndex - Index of field
rel - Relational expression
Returns:
a RexToOJTranslator.WhichInputResult if found, otherwise null.

translateAssignment

public void translateAssignment(RelDataTypeField lhsField,
                                Expression lhs,
                                RexNode rhs)
Generates code for an assignment.

NOTE: This method is only implemented in translators which can generate sequences of statements. The default implementation of this method throws UnsupportedOperationException.

Parameters:
lhsField - target field
lhs - target field as OpenJava
rhs - the source expression (as RexNode)

push

public RexToOJTranslator push(StatementList stmtList)
Returns a sub-translator to deal with a sub-block.

The default implementation simply returns this translator. Other implementations may create a new translator which contains the expression-to-variable mappings of the sub-block.

Parameters:
stmtList - Sub-block to generate code into
Returns:
A translator

pushProgram

public void pushProgram(RexProgram program)
Sets the current program. The previous program will be restored when popProgram(org.eigenbase.rex.RexProgram) is called. The program may be null.

Parameters:
program - New current program

popProgram

public void popProgram(RexProgram program)
Restores the current program to the one before pushProgram(RexProgram) was called.

Parameters:
program - The program most recently pushed