org.eigenbase.rel
Class AbstractRelNode

java.lang.Object
  extended by org.eigenbase.rel.AbstractRelNode
All Implemented Interfaces:
Cloneable, RelNode
Direct Known Subclasses:
EmptyRel, FennelDoubleRel, FennelMultipleRel, HepRelVertex, JdbcQuery, JoinRelBase, MultiJoinRel, OneRowRelBase, RelSubset, SetOpRel, SingleRel, TableAccessRelBase, TableFunctionRelBase, ValuesRelBase, VolcanoPlannerTest.TestLeafRel, VolcanoPlannerTraitTest.TestLeafRel

public abstract class AbstractRelNode
extends Object
implements RelNode

Base class for every relational expression (RelNode).


Field Summary
private  RelOptCluster cluster
           
private  String correlVariable
          The variable by which to refer to rows from this relational expression, as correlating expressions; null if this expression is not correlated on.
private  String desc
          Description, consists of id plus digest.
protected  String digest
          A short description of this relational expression's type, inputs, and other properties.
protected  int id
          unique id of this object -- for debugging
(package private) static int nextId
          generator for id values
protected  RelDataType rowType
          Cached type of this relational expression.
private static Logger tracer
           
protected  RelTraitSet traits
          The RelTraitSet that describes the traits of this RelNode.
 
Fields inherited from interface org.eigenbase.rel.RelNode
emptyArray
 
Constructor Summary
AbstractRelNode(RelOptCluster cluster, RelTraitSet traits)
          Creates a AbstractRelNode.
 
Method Summary
 void childrenAccept(RelVisitor visitor)
          Interacts with the RelVisitor in a visitor pattern to traverse the tree of relational expressions.
abstract  RelNode clone()
          Clones this RelNode.
 RelTraitSet cloneTraits()
          Returns a clone of this RelNode's traits.
 void collectVariablesSet(Set<String> variableSet)
          Collects variables set by this expression.
 void collectVariablesUsed(Set<String> variableSet)
          Collects variables known to be used by this expression or its descendants.
protected  String computeDigest()
          Computes the digest.
 RelOptCost computeSelfCost(RelOptPlanner planner)
          Returns the cost of this plan (not including children).
protected  RelDataType deriveRowType()
           
 void explain(RelOptPlanWriter pw)
           
 RexNode[] getChildExps()
          Returns an array of this relational expression's child expressions (not including the inputs returned by RelNode.getInputs().
 RelOptCluster getCluster()
          Returns the cluster this relational expression belongs to.
 List<RelCollation> getCollationList()
          Returns a description of the physical ordering (or orderings) of this relational expression.
 CallingConvention getConvention()
          Return the CallingConvention trait from this RelNode's trait set.
 String getCorrelVariable()
          Returns the name of the variable which is to be implicitly set at runtime each time a row is returned from this relational expression; or null if there is no variable.
 String getDescription()
          Returns a string which describes the relational expression and, unlike RelNode.getDigest(), also includes the identity.
 String getDigest()
          Returns a string which concisely describes the definition of this relational expression.
 RelDataType getExpectedInputRowType(int ordinalInParent)
          Returns the type of the rows expected for an input.
 int getId()
          Returns the ID of this relational expression, unique among all relational expressions created since the server was started.
 RelNode getInput(int i)
          Returns the ith input relational expression.
 RelNode[] getInputs()
          Returns an array of this relational expression's inputs.
 String getOrCreateCorrelVariable()
          Returns a variable with which to reference the current row of this relational expression as a correlating variable.
 RelOptQuery getQuery()
          Returns the sub-query this relational expression belongs to.
 String getRelTypeName()
          Returns the name of this relational expression's class, sans package name, for use in RelNode.explain(org.eigenbase.relopt.RelOptPlanWriter).
 double getRows()
          Returns an estimate of the number of rows this relational expression will return.
 RelDataType getRowType()
          Returns the type of the rows returned by this relational expression.
 RelOptTable getTable()
          If this relational expression represents an access to a table, returns that table, otherwise returns null.
 RelTraitSet getTraits()
          Retrieves this RelNode's traits.
 Set<String> getVariablesStopped()
          Returns the names of variables which are set in this relational expression but also used and therefore not available to parents of this relational expression.
<T extends AbstractRelNode>
T
inheritTraitsFrom(T rel)
          Sets this relational expression's traits to the same as another relational expression.
 boolean isAccessTo(RelOptTable table)
          Returns whether this relational expression is an access to table.
 boolean isDistinct()
          Returns whether the same value will not come out twice.
 boolean isValid(boolean fail)
          Returns whether this relational expression is valid.
 void onRegister(RelOptPlanner planner)
          Receives notification that this expression is about to be registered.
 String recomputeDigest()
          Computes the digest, assigns it, and returns it.
static void register(RelOptPlanner planner)
          Registers any special rules specific to this kind of relational expression.
 void registerCorrelVariable(String correlVariable)
          Registers a correlation variable.
 void replaceInput(int ordinalInParent, RelNode p)
          Replaces the ordinalInParentth input.
 void setCorrelVariable(String correlVariable)
          Sets the name of the variable which is to be implicitly set at runtime each time a row is returned from this relational expression
 String toString()
           
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

nextId

static int nextId
generator for id values


tracer

private static final Logger tracer

desc

private String desc
Description, consists of id plus digest.


rowType

protected RelDataType rowType
Cached type of this relational expression.


digest

protected String digest
A short description of this relational expression's type, inputs, and other properties. The string uniquely identifies the node; another node is equivalent if and only if it has the same value. Computed by computeDigest(), assigned by onRegister(org.eigenbase.relopt.RelOptPlanner), returned by getDigest().

See Also:
desc

cluster

private RelOptCluster cluster

id

protected int id
unique id of this object -- for debugging


correlVariable

private String correlVariable
The variable by which to refer to rows from this relational expression, as correlating expressions; null if this expression is not correlated on.


traits

protected RelTraitSet traits
The RelTraitSet that describes the traits of this RelNode.

Constructor Detail

AbstractRelNode

public AbstractRelNode(RelOptCluster cluster,
                       RelTraitSet traits)
Creates a AbstractRelNode.

"Precondition:"
cluster != null
Method Detail

clone

public abstract RelNode clone()
Description copied from interface: RelNode
Clones this RelNode.

Traits of the RelNode must be explicitly cloned, using inheritTraitsFrom(AbstractRelNode), as the RelNode may have traits of which it has no knowledge. Example implementation:

     public MyRelNode clone()
     {
         MyRelNode clone = new MyRelNode(...);
         clone.inheritTraitsFrom(this);
         return clone;
     }
 
N.B.: This method must be overridden whenever an existing, concrete RelNode is extended. Otherwise, calling clone() will produce a differently typed RelNode, resulting in invalid or incorrect query plans.

Specified by:
clone in interface RelNode
Overrides:
clone in class Object
Returns:
a clone of this RelNode

isAccessTo

public boolean isAccessTo(RelOptTable table)
Description copied from interface: RelNode
Returns whether this relational expression is an access to table.

Specified by:
isAccessTo in interface RelNode

getChildExps

public RexNode[] getChildExps()
Description copied from interface: RelNode
Returns an array of this relational expression's child expressions (not including the inputs returned by RelNode.getInputs(). If there are no child expressions, returns an empty array, not null.

Specified by:
getChildExps in interface RelNode

getCluster

public RelOptCluster getCluster()
Description copied from interface: RelNode
Returns the cluster this relational expression belongs to.

Specified by:
getCluster in interface RelNode
Returns:
cluster

getConvention

public final CallingConvention getConvention()
Description copied from interface: RelNode
Return the CallingConvention trait from this RelNode's trait set.

Specified by:
getConvention in interface RelNode
Returns:
this RelNode's CallingConvention

getTraits

public RelTraitSet getTraits()
Description copied from interface: RelNode
Retrieves this RelNode's traits. Note that although the RelTraitSet returned is modifiable, it must not be modified during optimization. It is legal to modify the traits of a RelNode before or after optimization, although doing so could render a tree of RelNodes unimplementable. If a RelNode's traits need to be modified during optimization, clone the RelNode and change the clone's traits.

Specified by:
getTraits in interface RelNode
Returns:
this RelNode's trait set

cloneTraits

public RelTraitSet cloneTraits()
Returns a clone of this RelNode's traits. Useful for implementing clone().

Returns:
a clone of this RelNode's traits.

inheritTraitsFrom

public <T extends AbstractRelNode> T inheritTraitsFrom(T rel)
Sets this relational expression's traits to the same as another relational expression. The other relational expression must be the same type as this.

The typical use of this method is in the implementation of a clone() method:

class MyRel { public MyRel clone() { return new MyRel( getCluster(), getChild().clone(), fieldX.clone(), fieldY.clone()) .inheritTraitsFrom(this); } }

To enable calls to be chained in this way, this method returns this as a convenience.

Parameters:
rel - Relational expression whose traits to copy
Returns:
This relational expression

setCorrelVariable

public void setCorrelVariable(String correlVariable)
Description copied from interface: RelNode
Sets the name of the variable which is to be implicitly set at runtime each time a row is returned from this relational expression

Specified by:
setCorrelVariable in interface RelNode
Parameters:
correlVariable - Name of correlating variable

getCorrelVariable

public String getCorrelVariable()
Description copied from interface: RelNode
Returns the name of the variable which is to be implicitly set at runtime each time a row is returned from this relational expression; or null if there is no variable.

Specified by:
getCorrelVariable in interface RelNode
Returns:
Name of correlating variable, or null

isDistinct

public boolean isDistinct()
Description copied from interface: RelNode
Returns whether the same value will not come out twice. Default value is false, derived classes should override.

Specified by:
isDistinct in interface RelNode

getId

public int getId()
Description copied from interface: RelNode
Returns the ID of this relational expression, unique among all relational expressions created since the server was started.

Specified by:
getId in interface RelNode
Returns:
Unique ID

getInput

public RelNode getInput(int i)
Description copied from interface: RelNode
Returns the ith input relational expression.

Specified by:
getInput in interface RelNode
Parameters:
i - Ordinal of input
Returns:
ith input

getOrCreateCorrelVariable

public String getOrCreateCorrelVariable()
Description copied from interface: RelNode
Returns a variable with which to reference the current row of this relational expression as a correlating variable. Creates a variable if none exists.

Specified by:
getOrCreateCorrelVariable in interface RelNode

getQuery

public RelOptQuery getQuery()
Description copied from interface: RelNode
Returns the sub-query this relational expression belongs to. A sub-query determines the scope for correlating variables (see RelNode.setCorrelVariable(String)).

Specified by:
getQuery in interface RelNode
Returns:
Sub-query

register

public static void register(RelOptPlanner planner)
Registers any special rules specific to this kind of relational expression.

The planner calls this method this first time that it sees a relational expression of this class. The derived class should call RelOptPlanner.addRule(org.eigenbase.relopt.RelOptRule) for each rule, and then call register(org.eigenbase.relopt.RelOptPlanner) on its base class.


getRelTypeName

public final String getRelTypeName()
Description copied from interface: RelNode
Returns the name of this relational expression's class, sans package name, for use in RelNode.explain(org.eigenbase.relopt.RelOptPlanWriter). For example, for a org.eigenbase.rel.ArrayRel.ArrayReader, this method returns "ArrayReader".

Specified by:
getRelTypeName in interface RelNode

isValid

public boolean isValid(boolean fail)
Description copied from interface: RelNode
Returns whether this relational expression is valid.

If assertions are enabled, this method is typically called with fail = true, as follows:

assert rel.isValid(true)
This signals that the method can throw an AssertionError if it is not valid.

Specified by:
isValid in interface RelNode
Parameters:
fail - Whether to fail if invalid
Returns:
Whether relational expression is valid

getCollationList

public List<RelCollation> getCollationList()
Description copied from interface: RelNode
Returns a description of the physical ordering (or orderings) of this relational expression.

Specified by:
getCollationList in interface RelNode

getRowType

public final RelDataType getRowType()
Description copied from interface: RelNode
Returns the type of the rows returned by this relational expression.

Specified by:
getRowType in interface RelNode

deriveRowType

protected RelDataType deriveRowType()

getExpectedInputRowType

public RelDataType getExpectedInputRowType(int ordinalInParent)
Description copied from interface: RelNode
Returns the type of the rows expected for an input. Defaults to RelNode.getRowType().

Specified by:
getExpectedInputRowType in interface RelNode
Parameters:
ordinalInParent - input's 0-based ordinal with respect to this parent rel
Returns:
expected row type

getInputs

public RelNode[] getInputs()
Description copied from interface: RelNode
Returns an array of this relational expression's inputs. If there are no inputs, returns an empty array, not null.

Specified by:
getInputs in interface RelNode

getRows

public double getRows()
Description copied from interface: RelNode
Returns an estimate of the number of rows this relational expression will return.

NOTE jvs 29-Mar-2006: Don't call this method directly. Instead, use RelMetadataQuery.getRowCount(org.eigenbase.rel.RelNode), which gives plugins a chance to override the rel's default ideas about row count.

Specified by:
getRows in interface RelNode

getVariablesStopped

public Set<String> getVariablesStopped()
Description copied from interface: RelNode
Returns the names of variables which are set in this relational expression but also used and therefore not available to parents of this relational expression.

By default, returns the empty set. Derived classes may override this method.

Specified by:
getVariablesStopped in interface RelNode

collectVariablesUsed

public void collectVariablesUsed(Set<String> variableSet)
Description copied from interface: RelNode
Collects variables known to be used by this expression or its descendants. By default, no such information is available and must be derived by analyzing sub-expressions, but some optimizer implementations may insert special expressions which remember such information.

Specified by:
collectVariablesUsed in interface RelNode
Parameters:
variableSet - receives variables used

collectVariablesSet

public void collectVariablesSet(Set<String> variableSet)
Description copied from interface: RelNode
Collects variables set by this expression.

Specified by:
collectVariablesSet in interface RelNode
Parameters:
variableSet - receives variables known to be set by

childrenAccept

public void childrenAccept(RelVisitor visitor)
Description copied from interface: RelNode
Interacts with the RelVisitor in a visitor pattern to traverse the tree of relational expressions.

Specified by:
childrenAccept in interface RelNode

computeSelfCost

public RelOptCost computeSelfCost(RelOptPlanner planner)
Description copied from interface: RelNode
Returns the cost of this plan (not including children). The base implementation throws an error; derived classes should override.

NOTE jvs 29-Mar-2006: Don't call this method directly. Instead, use RelMetadataQuery.getNonCumulativeCost(org.eigenbase.rel.RelNode), which gives plugins a chance to override the rel's default ideas about cost.

Specified by:
computeSelfCost in interface RelNode

explain

public void explain(RelOptPlanWriter pw)
Specified by:
explain in interface RelNode

onRegister

public void onRegister(RelOptPlanner planner)
Description copied from interface: RelNode
Receives notification that this expression is about to be registered. The implementation of this method must at least register all child expressions.

Specified by:
onRegister in interface RelNode

recomputeDigest

public String recomputeDigest()
Description copied from interface: RelNode
Computes the digest, assigns it, and returns it. For planner use only.

Specified by:
recomputeDigest in interface RelNode

registerCorrelVariable

public void registerCorrelVariable(String correlVariable)
Description copied from interface: RelNode
Registers a correlation variable.

Specified by:
registerCorrelVariable in interface RelNode
See Also:
RelNode.getVariablesStopped()

replaceInput

public void replaceInput(int ordinalInParent,
                         RelNode p)
Description copied from interface: RelNode
Replaces the ordinalInParentth input. You must override this method if you override RelNode.getInputs().

Specified by:
replaceInput in interface RelNode

toString

public String toString()
Overrides:
toString in class Object

getDescription

public final String getDescription()
Description copied from interface: RelNode
Returns a string which describes the relational expression and, unlike RelNode.getDigest(), also includes the identity. Typically returns "rel#{id}:{digest}".

Specified by:
getDescription in interface RelNode

getDigest

public final String getDigest()
Description copied from interface: RelNode
Returns a string which concisely describes the definition of this relational expression. Two relational expressions are equivalent if and only if their digests are the same.

The digest does not contain the relational expression's identity -- that would prevent similar relational expressions from ever comparing equal -- but does include the identity of children (on the assumption that children have already been normalized).

If you want a descriptive string which contains the identity, call Object.toString(), which always returns "rel#{id}:{digest}".

Specified by:
getDigest in interface RelNode

getTable

public RelOptTable getTable()
Description copied from interface: RelNode
If this relational expression represents an access to a table, returns that table, otherwise returns null.

Specified by:
getTable in interface RelNode

computeDigest

protected String computeDigest()
Computes the digest. Does not modify this object.

"Postcondition:"
return != null