|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.eigenbase.oj.rel.JavaRelImplementor
public class JavaRelImplementor
JavaRelImplementor
deals with the nastiness of converting a tree
of relational expressions into an implementation, generally an openjava parse tree
.
The bind(org.eigenbase.rel.RelNode, 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 14-June-2004: some of JavaRelImplementor is specific to the JAVA calling convention; those portions should probably be factored out into a subclass.
Nested Class Summary | |
---|---|
private static interface |
JavaRelImplementor.Bind
|
private static class |
JavaRelImplementor.EagerBind
|
private static class |
JavaRelImplementor.Frame
|
private static class |
JavaRelImplementor.LazyBind
|
private class |
JavaRelImplementor.RelBind
Binds a relational expression to whatever another relational expression is currently bound to. |
static class |
JavaRelImplementor.TranslationTester
Similar to RexToOJTranslator , but instead of translating, merely
tests whether an expression can be translated. |
static interface |
JavaRelImplementor.VariableInitializerThunk
A VariableInitializerThunk yields a VariableInitializer . |
Field Summary | |
---|---|
(package private) Statement |
exitStatement
|
protected OJRexImplementorTable |
implementorTable
|
(package private) Map<String,JavaRelImplementor.Frame> |
mapCorrel2Frame
Maps a String to the JavaRelImplementor.Frame whose JavaRelImplementor.Frame.rel .correlVariable == correlName. |
(package private) HashMap<String,Variable> |
mapCorrelNameToVariable
|
(package private) Map<RelNode,JavaRelImplementor.Frame> |
mapRel2Frame
Maps a RelNode to the unique frame whose JavaRelImplementor.Frame.rel is
that relational expression. |
private int |
nextVariableId
|
private RexBuilder |
rexBuilder
|
(package private) Stack<StatementList> |
stmtListStack
Stack of StatementList objects. |
private static Logger |
tracer
|
Constructor Summary | |
---|---|
JavaRelImplementor(RexBuilder rexBuilder,
OJRexImplementorTable implementorTable)
Creates a JavaRelImplementor |
Method Summary | |
---|---|
private void |
bind(RelNode rel,
JavaRelImplementor.Bind bind)
Records the fact that instances of rel are available via
bind (which may be eager or lazy). |
void |
bind(RelNode rel,
RelNode previous)
Shares a variable between relations. |
Variable |
bind(RelNode rel,
StatementList statementList,
VariableInitializer initializer)
Declares a variable, and binds it lazily, so it only gets initialized if it is actually used. |
void |
bind(RelNode rel,
Variable variable)
Records the fact that instances of rel are available in
variable . |
void |
bindCorrel(String correlName,
Variable variable)
Binds a correlating variable. |
private void |
bindDeferred(JavaRelImplementor.Frame frame,
RelNode rel)
|
boolean |
canTranslate(RelNode rel,
RexNode expression,
boolean deep)
Determines whether it is possible to implement an expression in Java. |
boolean |
canTranslate(RelNode rel,
RexProgram program)
Determines whether it is possible to implement a set of expressions in Java. |
private int |
computeFieldOffset(RelNode rel,
int ordinal)
Returns the index of the first field in rel which comes from
its ordinal th input. |
protected void |
createFrame(RelNode parent,
int ordinal,
RelNode child)
|
private RelNode |
findInputRel(RelNode rel,
int offset)
|
private RelNode |
findInputRel(RelNode rel,
int offset,
int[] offsets)
|
Variable |
findInputVariable(RelNode rel)
Returns the variable which, in the generated program, will hold the current row of a given relational expression. |
JavaRel |
findRel(JavaRel rel,
RexNode expression)
|
void |
generateParentBody(RelNode rel,
StatementList stmtList)
Implements the body of the current expression's parent. |
int |
generateVariableId()
|
List<RelNode> |
getAncestorRels(RelNode rel)
Returns a list of the relational expressions which are ancestors of the current one. |
Variable |
getConnectionVariable()
|
Statement |
getExitStatement()
|
RexBuilder |
getRexBuilder()
|
StatementList |
getStatementList()
|
RelDataTypeFactory |
getTypeFactory()
|
JavaRel |
implementFieldAccess(JavaRel rel,
String fieldName)
Burrows into a synthetic record and returns the underlying relation which provides the field called fieldName . |
void |
implementNext(AggregateCall call,
JavaRel rel,
Expression accumulator)
|
Expression |
implementResult(AggregateCall call,
Expression accumulator)
Generates the expression to retrieve the result of this aggregation. |
Expression |
implementRoot(JavaRel rel)
Starts an iteration, by calling JavaRel.implement(org.eigenbase.oj.rel.JavaRelImplementor) on the root element. |
Expression |
implementStart(AggregateCall call,
JavaRel rel)
|
Expression |
implementStartAndNext(AggregateCall call,
JavaRel rel)
|
Expression |
makeReference(String correlName,
RelNode rel)
Creates an expression which references correlating variable
correlName from the context of rel . |
RexToOJTranslator |
newStmtTranslator(JavaRel rel,
StatementList stmtList,
MemberDeclarationList memberList)
Creates a translator which can translate a succession of expressions, possibly using multiple statements, scratch variables, and helper functions. |
protected RexToOJTranslator |
newTranslator(RelNode rel)
Creates a RexToOJTranslator with which to translate the row-expressions within a relational expression into OpenJava
expressions. |
Variable |
newVariable()
Generates a variable with a unique name. |
Variable |
newVariable(String base)
Generates a variable with a unique name and a stem which indicates its purpose. |
void |
popStatementList(StatementList stmtList)
|
void |
pushStatementList(StatementList stmtList)
|
void |
setExitStatement(Statement stmt)
|
Expression |
translate(JavaRel 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
JavaRelImplementor ). |
Expression |
translateInput(JavaRel rel,
int ordinal)
Creates an expression which references the ordinalth input. |
Expression |
translateInputField(JavaRel rel,
int ordinal,
int fieldOrdinal)
Creates an expression which references the fieldOrdinalth field of the ordinalth input. |
ExpressionList |
translateList(JavaRel rel,
RexNode[] exps)
Converts an array of expressions in internal into a list of expressions in generated form. |
Expression |
translateViaStatements(JavaRel rel,
RexNode exp,
StatementList stmtList,
MemberDeclarationList memberList)
Generates code for an expression, possibly using multiple statements, scratch variables, and helper functions. |
Object |
visitChild(RelNode parent,
int ordinal,
RelNode child)
Implements a relational expression according to a calling convention. |
Object |
visitChildInternal(RelNode child)
Called from RelImplementor.visitChild(org.eigenbase.rel.RelNode, int, org.eigenbase.rel.RelNode) after the frame has been set up. |
Object |
visitChildInternal(RelNode child,
int ordinal)
Called from RelImplementor.visitChild(org.eigenbase.rel.RelNode, int, org.eigenbase.rel.RelNode) after the frame has been set up. |
Expression |
visitJavaChild(RelNode parent,
int ordinal,
JavaRel child)
Convenience wrapper around RelImplementor.visitChild(org.eigenbase.rel.RelNode, int, org.eigenbase.rel.RelNode) for the
common case where JavaRel has a child which is a JavaRel . |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
private static final Logger tracer
final Map<String,JavaRelImplementor.Frame> mapCorrel2Frame
String
to the JavaRelImplementor.Frame
whose JavaRelImplementor.Frame.rel
.correlVariable == correlName.
final HashMap<String,Variable> mapCorrelNameToVariable
final Map<RelNode,JavaRelImplementor.Frame> mapRel2Frame
RelNode
to the unique frame whose JavaRelImplementor.Frame.rel
is
that relational expression.
final Stack<StatementList> stmtListStack
StatementList
objects.
Statement exitStatement
private final RexBuilder rexBuilder
private int nextVariableId
protected final OJRexImplementorTable implementorTable
Constructor Detail |
---|
public JavaRelImplementor(RexBuilder rexBuilder, OJRexImplementorTable implementorTable)
rexBuilder
- Builder for RexNode
simplementorTable
- Table of implementations of operators. Must not
be nullMethod Detail |
---|
public void setExitStatement(Statement stmt)
public Statement getExitStatement()
public StatementList getStatementList()
public RexBuilder getRexBuilder()
public RelDataTypeFactory getTypeFactory()
public void bind(RelNode rel, Variable variable)
rel
are available in
variable
.
public Variable bind(RelNode rel, StatementList statementList, VariableInitializer initializer)
public void bind(RelNode rel, RelNode previous)
previous
already has a
variable, and calling this method indicates that rel
's
output will appear in this variable too.
public void bindCorrel(String correlName, Variable variable)
$cor2
will be replaced with java variables such as
$Oj14
.
public JavaRel findRel(JavaRel rel, RexNode expression)
public JavaRel implementFieldAccess(JavaRel rel, String fieldName)
fieldName
.
public void generateParentBody(RelNode rel, StatementList stmtList)
variable
is not null, bind the current expression to
variable
. For example, a nested loops join would generate
which corresponds tofor (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) { <> } } }
[emp:iter [dept:iter [join:body(emp,dept) [parent:body] ] ] ]
rel
- child relationstmtList
- block that child was generating its code intoprivate void bindDeferred(JavaRelImplementor.Frame frame, RelNode rel)
public final Expression visitJavaChild(RelNode parent, int ordinal, JavaRel child)
RelImplementor.visitChild(org.eigenbase.rel.RelNode, int, org.eigenbase.rel.RelNode)
for the
common case where JavaRel
has a child which is a JavaRel
.
public final Object visitChild(RelNode parent, int ordinal, RelNode child)
RelImplementor
visitChild
in interface RelImplementor
parent
- Parent relational expressionordinal
- Ordinal of child within its parentchild
- Child relational expression
protected void createFrame(RelNode parent, int ordinal, RelNode child)
public Object visitChildInternal(RelNode child)
RelImplementor
RelImplementor.visitChild(org.eigenbase.rel.RelNode, int, org.eigenbase.rel.RelNode)
after the frame has been set up. Specific
implementors should override this method.
visitChildInternal
in interface RelImplementor
child
- Child relational expression
public Object visitChildInternal(RelNode child, int ordinal)
RelImplementor
RelImplementor.visitChild(org.eigenbase.rel.RelNode, int, org.eigenbase.rel.RelNode)
after the frame has been set up. Specific
implementors should override this method.
visitChildInternal
in interface RelImplementor
child
- Child relational expressionordinal
- Ordinal of child within its parent
public Expression implementRoot(JavaRel rel)
JavaRel.implement(org.eigenbase.oj.rel.JavaRelImplementor)
on the root element.
public Expression makeReference(String correlName, RelNode rel)
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
.
public Variable newVariable()
public Variable newVariable(String base)
newVariable("binding")
might generate
a variable called "binding_12"
.
public int generateVariableId()
public Variable getConnectionVariable()
public void popStatementList(StatementList stmtList)
public void pushStatementList(StatementList stmtList)
public Expression translate(JavaRel rel, RexNode exp)
$input0
) to generated form (the input
relation is referenced using the bindings in this
JavaRelImplementor
). Compare this method with
net.sf.saffron.oj.xlat.QueryInfo.convertExpToInternal(), which converts
from source form to internal form.
exp
- the expression to translate (it is cloned, not modified)rel
- the relational expression which is the context for
exp
public boolean canTranslate(RelNode rel, RexProgram program)
program
- Program to translate
public boolean canTranslate(RelNode rel, RexNode expression, boolean deep)
rel
- Relational expressionexpression
- Expressiondeep
- if true, operands of the given expression are tested for
translatability as well; if false only the top level expression is tested
public Expression translateViaStatements(JavaRel rel, RexNode exp, StatementList stmtList, MemberDeclarationList memberList)
If you want to avoid generating common expressions, it is better to
create a translator using newStmtTranslator(JavaRel,
StatementList, MemberDeclarationList)
and use it to translate multiple
expressions.
rel
- the relational expression which is the context for expexp
- the row expression to be translatedstmtList
- optional code can be appended herememberList
- optional member declarations can be appended here (if
needed for reusable scratch space or helper functions; local variables
can also be allocated in stmtList)public ExpressionList translateList(JavaRel rel, RexNode[] exps)
translate(JavaRel,RexNode)
protected RexToOJTranslator newTranslator(RelNode rel)
RexToOJTranslator
with which to translate the row-expressions
within a relational expression into OpenJava
expressions.
public RexToOJTranslator newStmtTranslator(JavaRel rel, StatementList stmtList, MemberDeclarationList memberList)
Typical usage:
Translator translator = newStmtTranslator(rel, stmtList, memberList); translator.translateRexNode(exp1); translator.translateRexNode(exp2);
rel
- the relational expression which is the context for expstmtList
- optional code can be appended herememberList
- optional member declarations can be appended here (if
needed for reusable scratch space or helper functions; local variablespublic Expression translateInput(JavaRel rel, int ordinal)
private int computeFieldOffset(RelNode rel, int ordinal)
rel
which comes from
its ordinal
th 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.
public Expression translateInputField(JavaRel rel, int ordinal, int fieldOrdinal)
(We can potentially optimize the generation process, so we can access field values without actually instantiating the row.)
private void bind(RelNode rel, JavaRelImplementor.Bind bind)
rel
are available via
bind
(which may be eager or lazy).
private RelNode findInputRel(RelNode rel, int offset)
private RelNode findInputRel(RelNode rel, int offset, int[] offsets)
public Variable findInputVariable(RelNode rel)
public Expression implementStart(AggregateCall call, JavaRel rel)
public Expression implementStartAndNext(AggregateCall call, JavaRel rel)
public void implementNext(AggregateCall call, JavaRel rel, Expression accumulator)
public Expression implementResult(AggregateCall call, Expression accumulator)
public List<RelNode> getAncestorRels(RelNode rel)
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |