Class ConstantExpressionVisitor
- java.lang.Object
-
- org.apache.derby.impl.sql.compile.ConstantExpressionVisitor
-
- All Implemented Interfaces:
Visitor
class ConstantExpressionVisitor extends java.lang.Object implements Visitor
This visitor replaces a
ValueNodewith a node representing a constant value, if theValueNodeis known to always evaluate to the same value. It may for instance replace a sub-tree representing1=1with a constantTRUE.The actual evaluation of the
ValueNodes is performed by invokingValueNode.evaluateConstantExpressions()on everyValueNodein the query tree.In contrast to most other visitors, this visitor walks the tree bottom-up. Top-down processing of the tree would only evaluate constant expressions at the leaf level, so for instance
(1=1)=(1=2)would only be simplified toTRUE=FALSE. With bottom-up processing, the top-level = node will be processed after the leaves, and it sees the intermediate treeTRUE=FALSEwhich it is able to transform into the even simpler treeFALSE.
-
-
Constructor Summary
Constructors Constructor Description ConstantExpressionVisitor()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanskipChildren(Visitable node)Method that is called to indicate whether we should skip all nodes below this node for traversal.booleanstopTraversal()Method that is called to see if query tree traversal should be stopped before visiting all nodes.Visitablevisit(Visitable node)Visit the node and callevaluateConstantExpressions()if it is aValueNode.booleanvisitChildrenFirst(Visitable node)Method that is called to see ifvisit()should be called on the children ofnodebefore it is called onnodeitself.
-
-
-
Method Detail
-
visit
public Visitable visit(Visitable node) throws StandardException
Visit the node and callevaluateConstantExpressions()if it is aValueNode.- Specified by:
visitin interfaceVisitor- Parameters:
node- the node to process- Returns:
- a query tree node. Often times this is the same node that was passed in, but Visitors that replace nodes with other nodes will use this to return the new replacement node.
- Throws:
StandardException- may be throw an error as needed by the visitor (i.e. may be a normal error if a particular node is found, e.g. if checking a group by, we don't expect to find any ColumnReferences that aren't under an AggregateNode -- the easiest thing to do is just throw an error when we find the questionable node).- See Also:
ValueNode.evaluateConstantExpressions()
-
stopTraversal
public boolean stopTraversal()
Method that is called to see if query tree traversal should be stopped before visiting all nodes. Useful for short circuiting traversal if we already know we are done.- Specified by:
stopTraversalin interfaceVisitor- Returns:
false, since the entire tree should be visited
-
skipChildren
public boolean skipChildren(Visitable node)
Method that is called to indicate whether we should skip all nodes below this node for traversal. Useful if we want to effectively ignore/prune all branches under a particular node.Differs from stopTraversal() in that it only affects subtrees, rather than the entire traversal.
- Specified by:
skipChildrenin interfaceVisitor- Parameters:
node- the node to process- Returns:
false, since the entire tree should be visited
-
visitChildrenFirst
public boolean visitChildrenFirst(Visitable node)
Method that is called to see ifvisit()should be called on the children ofnodebefore it is called onnodeitself. If this method always returnstrue, the visitor will walk the tree bottom-up. If it always returnsfalse, the tree is visited top-down.- Specified by:
visitChildrenFirstin interfaceVisitor- Parameters:
node- the top node of a sub-tree about to be visited- Returns:
true, since the tree should be walked bottom-up
-
-