com.akiban.sql.compiler
Class BooleanNormalizer

java.lang.Object
  extended by com.akiban.sql.compiler.BooleanNormalizer
All Implemented Interfaces:
Visitor

public class BooleanNormalizer
extends Object
implements Visitor

Perform normalization such as CNF on boolean expressions.


Field Summary
static int NOT_IN_AND_LIMIT
           
 
Constructor Summary
BooleanNormalizer(SQLParserContext parserContext)
           
 
Method Summary
protected  ValueNode castToBoolean(ValueNode node)
           
protected  ValueNode changeToCNF(ValueNode node, boolean underTopAndNode)
          Finish putting an expression into conjunctive normal form.
 void conditionalNode(ConditionalNode node)
          Normalize WHEN clause in this CASE node.
protected  ValueNode eliminateNots(ValueNode node, boolean underNotNode)
          Eliminate NotNodes in the current query block.
protected  ValueNode equalsBooleanConstant(ValueNode node, Boolean constant)
           
protected  ValueNode getNotEqual(ValueNode left, ValueNode right)
           
protected  ValueNode inWithNestedTuples(InListOperatorNode node)
           
 void joinNode(JoinNode node)
          Normalize ON clause in this JOIN node.
 StatementNode normalize(StatementNode stmt)
          Normalize conditions anywhere in this statement.
 ValueNode normalizeExpression(ValueNode boolClause)
          Normalize a top-level boolean expression.
protected  AndNode putAndsOnTop(ValueNode node)
          Do the 1st step in putting an expression into conjunctive normal form.
 void selectNode(SelectNode node)
          Normalize WHERE clause in this SELECT node.
 boolean skipChildren(Visitable node)
          Method that is called to indicate whether we should skip all nodes below this node for traversal.
 boolean stopTraversal()
          Method that is called to see if query tree traversal should be stopped before visiting all nodes.
protected  boolean verifyChangeToCNF(ValueNode node, boolean top)
          Verify that changeToCNF() did its job correctly.
protected  boolean verifyEliminateNots(ValueNode node)
          Verify that eliminateNots() did its job correctly.
protected  boolean verifyPutAndsOnTop(ValueNode node)
          Verify that putAndsOnTop() did its job correctly.
 Visitable visit(Visitable node)
          This is the default visit operation on a QueryTreeNode.
 boolean visitChildrenFirst(Visitable node)
          Method that is called to see if visit() should be called on the children of node before it is called on node itself.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

NOT_IN_AND_LIMIT

public static final int NOT_IN_AND_LIMIT
See Also:
Constant Field Values
Constructor Detail

BooleanNormalizer

public BooleanNormalizer(SQLParserContext parserContext)
Method Detail

normalize

public StatementNode normalize(StatementNode stmt)
                        throws StandardException
Normalize conditions anywhere in this statement.

Throws:
StandardException

selectNode

public void selectNode(SelectNode node)
                throws StandardException
Normalize WHERE clause in this SELECT node.

Throws:
StandardException

joinNode

public void joinNode(JoinNode node)
              throws StandardException
Normalize ON clause in this JOIN node.

Throws:
StandardException

conditionalNode

public void conditionalNode(ConditionalNode node)
                     throws StandardException
Normalize WHEN clause in this CASE node.

Throws:
StandardException

normalizeExpression

public ValueNode normalizeExpression(ValueNode boolClause)
                              throws StandardException
Normalize a top-level boolean expression.

Throws:
StandardException

eliminateNots

protected ValueNode eliminateNots(ValueNode node,
                                  boolean underNotNode)
                           throws StandardException
Eliminate NotNodes in the current query block. We traverse the tree, inverting ANDs and ORs and eliminating NOTs as we go. We stop at ComparisonOperators and boolean expressions. We invert ComparisonOperators and replace boolean expressions with boolean expression = false. NOTE: Since we do not recurse under ComparisonOperators, there still could be NotNodes left in the tree.

Parameters:
node - An expression node.
underNotNode - Whether or not we are under a NotNode.
Returns:
The modified expression
Throws:
StandardException - Thrown on error

getNotEqual

protected ValueNode getNotEqual(ValueNode left,
                                ValueNode right)
                         throws StandardException
Throws:
StandardException

inWithNestedTuples

protected ValueNode inWithNestedTuples(InListOperatorNode node)
                                throws StandardException
Throws:
StandardException

castToBoolean

protected ValueNode castToBoolean(ValueNode node)
                           throws StandardException
Throws:
StandardException

equalsBooleanConstant

protected ValueNode equalsBooleanConstant(ValueNode node,
                                          Boolean constant)
                                   throws StandardException
Throws:
StandardException

verifyEliminateNots

protected boolean verifyEliminateNots(ValueNode node)
Verify that eliminateNots() did its job correctly. Verify that there are no NotNodes above the top level comparison operators and boolean expressions.

Returns:
Boolean which reflects validity of the tree.

putAndsOnTop

protected AndNode putAndsOnTop(ValueNode node)
                        throws StandardException
Do the 1st step in putting an expression into conjunctive normal form. This step ensures that the top level of the expression is a chain of AndNodes terminated by a true BooleanConstantNode.

Parameters:
node - An expression node.
Returns:
The modified expression
Throws:
StandardException - Thrown on error

verifyPutAndsOnTop

protected boolean verifyPutAndsOnTop(ValueNode node)
Verify that putAndsOnTop() did its job correctly. Verify that the top level of the expression is a chain of AndNodes terminated by a true BooleanConstantNode.

Parameters:
node - An expression node.
Returns:
Boolean which reflects validity of the tree.

changeToCNF

protected ValueNode changeToCNF(ValueNode node,
                                boolean underTopAndNode)
                         throws StandardException
Finish putting an expression into conjunctive normal form. An expression tree in conjunctive normal form meets the following criteria: o If the expression tree is not null, the top level will be a chain of AndNodes terminating in a true BooleanConstantNode. o The left child of an AndNode will never be an AndNode. o Any right-linked chain that includes an AndNode will be entirely composed of AndNodes terminated by a true BooleanConstantNode. o The left child of an OrNode will never be an OrNode. o Any right-linked chain that includes an OrNode will be entirely composed of OrNodes terminated by a false BooleanConstantNode. o ValueNodes other than AndNodes and OrNodes are considered leaf nodes for purposes of expression normalization. In other words, we won't do any normalization under those nodes. In addition, we track whether or not we are under a top level AndNode. SubqueryNodes need to know this for subquery flattening.

Parameters:
node - An expression node.
underTopAndNode - Whether or not we are under a top level AndNode.
Returns:
The modified expression
Throws:
StandardException - Thrown on error

verifyChangeToCNF

protected boolean verifyChangeToCNF(ValueNode node,
                                    boolean top)
Verify that changeToCNF() did its job correctly. Verify that: o AndNode - rightOperand is not instanceof OrNode leftOperand is not instanceof AndNode o OrNode - rightOperand is not instanceof AndNode leftOperand is not instanceof OrNode

Parameters:
node - An expression node.
Returns:
Boolean which reflects validity of the tree.

visit

public Visitable visit(Visitable node)
                throws StandardException
Description copied from interface: Visitor
This is the default visit operation on a QueryTreeNode. It just returns the node. This will typically suffice as the default visit operation for most visitors unless the visitor needs to count the number of nodes visited or something like that.

Visitors will overload this method by implementing a version with a signature that matches a specific type of node. For example, if I want to do something special with aggregate nodes, then that Visitor will implement a visit(AggregateNode node) method which does the aggregate specific processing.

Specified by:
visit in interface Visitor
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).

visitChildrenFirst

public boolean visitChildrenFirst(Visitable node)
Description copied from interface: Visitor
Method that is called to see if visit() should be called on the children of node before it is called on node itself. If this method always returns true, the visitor will walk the tree bottom-up. If it always returns false, the tree is visited top-down.

Specified by:
visitChildrenFirst in interface Visitor
Parameters:
node - the top node of a sub-tree about to be visited
Returns:
true if node's children should be visited before node, false otherwise

stopTraversal

public boolean stopTraversal()
Description copied from interface: Visitor
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:
stopTraversal in interface Visitor
Returns:
true/false

skipChildren

public boolean skipChildren(Visitable node)
                     throws StandardException
Description copied from interface: Visitor
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:
skipChildren in interface Visitor
Parameters:
node - the node to process
Returns:
true/false
Throws:
StandardException


Copyright © 2013 Akiban Technologies, Inc. All rights reserved.