net.sf.farrago.ojrex
Class FarragoRexToOJTranslator

java.lang.Object
  extended by org.eigenbase.oj.rex.RexToOJTranslator
      extended by net.sf.farrago.ojrex.FarragoRexToOJTranslator
All Implemented Interfaces:
RexVisitor<Expression>

public class FarragoRexToOJTranslator
extends RexToOJTranslator

FarragoRexToOJTranslator refines RexToOJTranslator with Farrago-specifics.

NOTE jvs 22-June-2004: If you're scratching your head trying to understand the code generation methods in this package, it might help to look at examples of the generated code. One way to do this is by turning on FarragoTrace.getDynamicTracer() and then examining the code for generated classes after running queries. See also the .ref files under farrago/testlog/FarragoRexToOJTranslatorTest; these correspond to the test cases in FarragoRexToOJTranslatorTest. You can also add new test cases to that class, run the test, and examine the output.

Version:
$Id: //open/dev/farrago/src/net/sf/farrago/ojrex/FarragoRexToOJTranslator.java#32 $
Author:
John V. Sichi

Field Summary
private  FarragoOJRexCastImplementor castImplementor
           
private  Map<Integer,String> localRefMap
           
private  MemberDeclarationList memberList
           
private  OJClass ojNullablePrimitive
           
private  FarragoRepos repos
           
private  StatementList stmtList
           
private static String TO_STRING_METHOD_NAME
           
 
Constructor Summary
FarragoRexToOJTranslator(FarragoRepos repos, JavaRelImplementor relImplementor, RelNode contextRel, OJRexImplementorTable implementorTable, StatementList stmtList, MemberDeclarationList memberList, RexProgram program)
           
FarragoRexToOJTranslator(FarragoRepos repos, JavaRelImplementor relImplementor, RelNode contextRel, OJRexImplementorTable implementorTable, StatementList stmtList, MemberDeclarationList memberList, RexProgram program, Map<Integer,String> localRefMap)
          Creates a translator based on a OJRexImplementorTable.
 
Method Summary
 void addAssignmentStatement(StatementList stmtList, Expression funcResult, RelDataType retType, Variable varResult, boolean needCast)
           
 void addMember(MemberDeclaration member)
           
 void addStatement(Statement stmt)
           
 void addStatementsFromList(StatementList newStmtList)
           
 Statement assign(Expression lhs, Expression rhs)
          Generates a simple assignment statement
 Expression convertCastOrAssignment(String targetName, RelDataType lhsType, RelDataType rhsType, Expression lhsExp, Expression rhsExp)
           
 Expression convertCastOrAssignmentWithStmtList(StatementList stmtList, String targetName, RelDataType lhsType, RelDataType rhsType, Expression lhsExp, Expression rhsExp)
           
 FieldAccess convertFieldAccess(Variable variable, RelDataTypeField field)
           
 Expression convertPrimitiveAccess(Expression expr, RexNode op)
           
 Expression convertVariable(RelDataType type, String accessorName, ExpressionList accessorArgList)
           
 Expression convertVariableWithCast(RelDataType type, Class accessorClassCast, String accessorName, ExpressionList accessorArgList)
           
 Expression createNullTest(RexNode node, Expression originalOperand, Expression nullTest)
           
 Variable createPrimitiveScratchVariable(OJClass ojClass)
          Creates a primitive scratch variable without initialization.
 Variable createScratchVariable(OJClass ojClass, ExpressionList exprs, MemberDeclarationList mdlst)
           
 Variable createScratchVariable(RelDataType type)
           
 Variable createScratchVariableWithExpression(OJClass ojClass, Expression exp)
          Creates a member of this class with a given set of modifiers, and initializes it with an expression.
 Statement createSetNullStatement(Expression varResult, boolean isNull)
           
 Statement declareLocalVariable(OJClass ojClass, Variable var, Expression init)
          Generates a local variable declaration
 FarragoTypeFactory getFarragoTypeFactory()
           
 FarragoRepos getRepos()
           
 Expression isNull(Expression exp)
          Generates a boolean expression describing whether an input expression is null
 boolean isNullablePrimitive(RelDataType type)
           
 FieldDeclaration newMember(int modifiers, OJClass ojClass, Variable var, VariableInitializer init)
          Generates a declaration for a field in a class.
 Variable newVariable()
          Generates a variable with a unique name
 FarragoRexToOJTranslator push(StatementList stmtList)
          Returns a sub-translator to deal with a sub-block.
 Statement setIfNull(Variable var, Expression init)
          Generates a statement to initializes a variable if the variable is null
 Expression toString(Expression exp)
          Generates the string expression exp.toString()
 void translateAssignment(RelDataTypeField lhsField, Expression lhsExp, RexNode rhs)
          Generates code for an assignment.
 OJClass typeToOJClass(RelDataType type)
          Retrieves the OpenJava type corresponding to a Sql type
 Expression visitDynamicParam(RexDynamicParam dynamicParam)
           
 Expression visitLiteral(RexLiteral literal)
           
 Expression visitLocalRef(RexLocalRef localRef)
           
 
Methods inherited from class org.eigenbase.oj.rex.RexToOJTranslator
canConvertCall, convertByteArrayLiteral, convertByteArrayLiteralToInitializer, convertCall, getContextRel, getImplementorTable, getProgram, getRelImplementor, getSubStmtList, getTranslation, getTypeFactory, isInputRef, popProgram, pushProgram, setTranslation, translateRexNode, translateSubExpression, visitCall, visitCorrelVariable, visitFieldAccess, visitInputRef, visitOver, visitRangeRef
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

TO_STRING_METHOD_NAME

private static String TO_STRING_METHOD_NAME

repos

private final FarragoRepos repos

stmtList

private StatementList stmtList

memberList

private final MemberDeclarationList memberList

castImplementor

private final FarragoOJRexCastImplementor castImplementor

ojNullablePrimitive

private final OJClass ojNullablePrimitive

localRefMap

private final Map<Integer,String> localRefMap
Constructor Detail

FarragoRexToOJTranslator

public FarragoRexToOJTranslator(FarragoRepos repos,
                                JavaRelImplementor relImplementor,
                                RelNode contextRel,
                                OJRexImplementorTable implementorTable,
                                StatementList stmtList,
                                MemberDeclarationList memberList,
                                RexProgram program,
                                Map<Integer,String> localRefMap)
Creates a translator based on a OJRexImplementorTable.

Parameters:
repos - repository
relImplementor - implementation context
contextRel - relational expression which is the context for the row-expressions which are to be translated
implementorTable - table of implementations for SQL operators
stmtList - statement list for side-effects of translation
memberList - member list for class-level state required by
program - Program, may be null
localRefMap - map from RexLocalRef index to name of method which

FarragoRexToOJTranslator

public FarragoRexToOJTranslator(FarragoRepos repos,
                                JavaRelImplementor relImplementor,
                                RelNode contextRel,
                                OJRexImplementorTable implementorTable,
                                StatementList stmtList,
                                MemberDeclarationList memberList,
                                RexProgram program)
Method Detail

push

public FarragoRexToOJTranslator push(StatementList stmtList)
Description copied from class: RexToOJTranslator
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.

Overrides:
push in class RexToOJTranslator
Parameters:
stmtList - Sub-block to generate code into
Returns:
A translator

addMember

public void addMember(MemberDeclaration member)

addStatement

public void addStatement(Statement stmt)

addStatementsFromList

public void addStatementsFromList(StatementList newStmtList)

visitLocalRef

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

visitDynamicParam

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

convertVariable

public Expression convertVariable(RelDataType type,
                                  String accessorName,
                                  ExpressionList accessorArgList)

convertVariableWithCast

public Expression convertVariableWithCast(RelDataType type,
                                          Class accessorClassCast,
                                          String accessorName,
                                          ExpressionList accessorArgList)

visitLiteral

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

createScratchVariable

public Variable createScratchVariable(OJClass ojClass,
                                      ExpressionList exprs,
                                      MemberDeclarationList mdlst)

createScratchVariableWithExpression

public Variable createScratchVariableWithExpression(OJClass ojClass,
                                                    Expression exp)
Creates a member of this class with a given set of modifiers, and initializes it with an expression.

Parameters:
ojClass - Type of the variable
exp - Expression to initialize it
Returns:
Variable

createScratchVariable

public Variable createScratchVariable(RelDataType type)

createPrimitiveScratchVariable

public Variable createPrimitiveScratchVariable(OJClass ojClass)
Creates a primitive scratch variable without initialization.

Parameters:
ojClass - Variable's type
Returns:
scratch variable

newVariable

public Variable newVariable()
Generates a variable with a unique name


newMember

public FieldDeclaration newMember(int modifiers,
                                  OJClass ojClass,
                                  Variable var,
                                  VariableInitializer init)
Generates a declaration for a field in a class. This should be followed by addMember(openjava.ptree.MemberDeclaration).

Parameters:
modifiers - bitmap of modifiers such as private, or static
ojClass - type of the member
var - uniquely named variable
init - the initial value of the member, may be null

typeToOJClass

public OJClass typeToOJClass(RelDataType type)
Retrieves the OpenJava type corresponding to a Sql type

Parameters:
type - the Sql type

declareLocalVariable

public Statement declareLocalVariable(OJClass ojClass,
                                      Variable var,
                                      Expression init)
Generates a local variable declaration

Parameters:
ojClass - type of the variable
var - the uniquely named variable
init - the initial value of the variable, may be null

isNull

public Expression isNull(Expression exp)
Generates a boolean expression describing whether an input expression is null

Parameters:
exp - the input expression

toString

public Expression toString(Expression exp)
Generates the string expression exp.toString()

Parameters:
exp - expression to be converted into a string

assign

public Statement assign(Expression lhs,
                        Expression rhs)
Generates a simple assignment statement

Parameters:
lhs - the expression on left side of the assignment
rhs - the expression on the right side of the assignment

setIfNull

public Statement setIfNull(Variable var,
                           Expression init)
Generates a statement to initializes a variable if the variable is null

Parameters:
var - the variable to be initialed
init - the initial value for the variable

createSetNullStatement

public Statement createSetNullStatement(Expression varResult,
                                        boolean isNull)

createNullTest

public Expression createNullTest(RexNode node,
                                 Expression originalOperand,
                                 Expression nullTest)

isNullablePrimitive

public boolean isNullablePrimitive(RelDataType type)

convertFieldAccess

public FieldAccess convertFieldAccess(Variable variable,
                                      RelDataTypeField field)

convertPrimitiveAccess

public Expression convertPrimitiveAccess(Expression expr,
                                         RexNode op)

convertCastOrAssignmentWithStmtList

public Expression convertCastOrAssignmentWithStmtList(StatementList stmtList,
                                                      String targetName,
                                                      RelDataType lhsType,
                                                      RelDataType rhsType,
                                                      Expression lhsExp,
                                                      Expression rhsExp)

convertCastOrAssignment

public Expression convertCastOrAssignment(String targetName,
                                          RelDataType lhsType,
                                          RelDataType rhsType,
                                          Expression lhsExp,
                                          Expression rhsExp)

getFarragoTypeFactory

public FarragoTypeFactory getFarragoTypeFactory()

getRepos

public FarragoRepos getRepos()

translateAssignment

public void translateAssignment(RelDataTypeField lhsField,
                                Expression lhsExp,
                                RexNode rhs)
Description copied from class: RexToOJTranslator
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.

Overrides:
translateAssignment in class RexToOJTranslator
Parameters:
lhsField - target field
lhsExp - target field as OpenJava
rhs - the source expression (as RexNode)

addAssignmentStatement

public void addAssignmentStatement(StatementList stmtList,
                                   Expression funcResult,
                                   RelDataType retType,
                                   Variable varResult,
                                   boolean needCast)