org.apache.derby.impl.sql.compile
Class ConditionalNode

java.lang.Object
  extended by org.apache.derby.impl.sql.compile.QueryTreeNode
      extended by org.apache.derby.impl.sql.compile.ValueNode
          extended by org.apache.derby.impl.sql.compile.ConditionalNode
All Implemented Interfaces:
Visitable

public class ConditionalNode
extends ValueNode

A ConditionalNode represents an if/then/else operator with a single boolean expression on the "left" of the operator and a list of expressions on the "right". This is used to represent the java conditional (aka immediate if).


Field Summary
(package private)  ValueNode testCondition
           
(package private)  ValueNodeList thenElseList
           
(package private)  boolean thisIsNullIfNode
           
 
Fields inherited from class org.apache.derby.impl.sql.compile.ValueNode
transformed
 
Fields inherited from class org.apache.derby.impl.sql.compile.QueryTreeNode
AUTOINCREMENT_CREATE_MODIFY, AUTOINCREMENT_INC_INDEX, AUTOINCREMENT_IS_AUTOINCREMENT_INDEX, AUTOINCREMENT_START_INDEX, isPrivilegeCollectionRequired
 
Constructor Summary
ConditionalNode()
           
 
Method Summary
(package private)  void acceptChildren(Visitor v)
          Accept the visitor for all visitable children of this node.
 ValueNode bindExpression(FromList fromList, SubqueryList subqueryList, java.util.Vector aggregateVector)
          Bind this expression.
 boolean categorize(JBitSet referencedTabs, boolean simplePredsOnly)
          Categorize this predicate.
 boolean constantExpression(PredicateList whereClause)
          Return whether or not this expression tree represents a constant value.
(package private)  ValueNode eliminateNots(boolean underNotNode)
          Eliminate NotNodes in the current query block.
private  DataTypeDescriptor findType(ValueNodeList thenElseList, FromList fromList, SubqueryList subqueryList, java.util.Vector aggregateVector)
          This method is a 'prebind.'
 void generateExpression(ExpressionClassBuilder acb, MethodBuilder mb)
          Do code generation for this conditional expression.
 void init(java.lang.Object testCondition, java.lang.Object thenElseList, java.lang.Object thisIsNullIfNode)
          Initializer for a ConditionalNode
private  boolean isCastNode(ValueNode node)
          Checks if the provided node is a CastNode.
private  boolean isCastToChar(ValueNode node)
          Checks if the provided CastNode is cast to a SQL CHAR type.
private  boolean isConditionalNode(ValueNode node)
          Checks to see if the provided node represents a ConditionalNode.
 boolean isConstantExpression()
          Return whether or not this expression tree represents a constant expression.
protected  boolean isEquivalent(ValueNode o)
          Tests if this node is equivalent to the specified ValueNode.
private  boolean isNullNode(ValueNode node)
          Checks to see if the provided node represents a parsing of an SQL NULL.
 ValueNode preprocess(int numTables, FromList outerFromList, SubqueryList outerSubqueryList, PredicateList outerPredicateList)
          Preprocess an expression tree.
 void printSubNodes(int depth)
          Prints the sub-nodes of this object.
private  QueryTreeNode recastNullNode(ValueNode nodeToCast, DataTypeDescriptor typeToUse)
          recastNullNode casts the nodeToCast node to the typeToUse.
private  void recastNullNodes(ValueNodeList thenElseList, DataTypeDescriptor castType, FromList fromList, SubqueryList subqueryList, java.util.Vector aggregateVector)
          This recursive method will hunt through the ValueNodeList thenElseList looking for SQL NULL's.
 ValueNode remapColumnReferencesToExpressions()
          Remap all ColumnReferences in this tree to be clones of the underlying expression.
private  boolean shouldCast(DataTypeDescriptor newType, DataTypeDescriptor oldType)
          Checks to see if oldType should be casted to the newType.
 
Methods inherited from class org.apache.derby.impl.sql.compile.ValueNode
bindExpression, changeToCNF, checkIsBoolean, checkTopPredicatesForEqualsConditions, copyFields, evaluateConstantExpressions, genEqualsFalseTree, generate, generateFilter, genIsNullTree, genSQLJavaSQLTree, getClone, getColumnName, getConstantValueAsObject, getDataValueFactory, getOrderableVariantType, getSchemaName, getSourceResultColumn, getTableName, getTablesReferenced, getTransformed, getTypeCompiler, getTypeId, getTypeServices, init, isBinaryEqualsOperatorNode, isBooleanFalse, isBooleanTrue, isCloneable, isInListProbeNode, isParameterNode, isRelationalOperator, isSameNodeType, optimizableEqualityNode, putAndsOnTop, requiresTypeFromContext, selectivity, setCollationInfo, setCollationInfo, setCollationUsingCompilationSchema, setCollationUsingCompilationSchema, setNullability, setTransformed, setType, setType, setType, toString, updatableByCursor, verifyChangeToCNF, verifyEliminateNots, verifyPutAndsOnTop
 
Methods inherited from class org.apache.derby.impl.sql.compile.QueryTreeNode
accept, bindOffsetFetch, bindRowMultiSet, bindUserType, checkReliability, checkReliability, convertDefaultNode, createTypeDependency, debugFlush, debugPrint, disablePrivilegeCollection, formatNodeString, foundString, generateAuthorizeCheck, getBeginOffset, getClassFactory, getCompilerContext, getContextManager, getCursorInfo, getDataDictionary, getDependencyManager, getEndOffset, getExecutionFactory, getGenericConstantActionFactory, getIntProperty, getLanguageConnectionContext, getNodeFactory, getNodeType, getNullNode, getParameterTypes, getRowEstimate, getSchemaDescriptor, getSchemaDescriptor, getStatementType, getTableDescriptor, getTypeCompiler, init, init, init, init, init, init, init, init, init, init, init, init, isAtomic, isInstanceOf, isPrivilegeCollectionRequired, isSessionSchema, isSessionSchema, makeConstantAction, makeTableName, makeTableName, nodeHeader, orReliability, parseStatement, printLabel, referencesSessionSchema, resolveTableToSynonym, setBeginOffset, setContextManager, setEndOffset, setNodeType, setRefActionInfo, stackPrint, treePrint, treePrint, verifyClassExist
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

testCondition

ValueNode testCondition

thenElseList

ValueNodeList thenElseList

thisIsNullIfNode

boolean thisIsNullIfNode
Constructor Detail

ConditionalNode

public ConditionalNode()
Method Detail

init

public void init(java.lang.Object testCondition,
                 java.lang.Object thenElseList,
                 java.lang.Object thisIsNullIfNode)
Initializer for a ConditionalNode

Overrides:
init in class QueryTreeNode
Parameters:
testCondition - The boolean test condition
thenElseList - ValueNodeList with then and else expressions

printSubNodes

public void printSubNodes(int depth)
Prints the sub-nodes of this object. See QueryTreeNode.java for how tree printing is supposed to work.

Overrides:
printSubNodes in class QueryTreeNode
Parameters:
depth - The depth of this node in the tree

isCastNode

private boolean isCastNode(ValueNode node)
Checks if the provided node is a CastNode.

Parameters:
node - The node to check.
Returns:
True if this node is a CastNode, false otherwise.

isCastToChar

private boolean isCastToChar(ValueNode node)
                      throws StandardException
Checks if the provided CastNode is cast to a SQL CHAR type.

Parameters:
node - The CastNode to check.
Returns:
True if this CastNode's target type is CHAR, false otherwise.
Throws:
StandardException

isNullNode

private boolean isNullNode(ValueNode node)
Checks to see if the provided node represents a parsing of an SQL NULL.

Parameters:
node - The node to check.
Returns:
True if this node represents a SQL NULL, false otherwise.

isConditionalNode

private boolean isConditionalNode(ValueNode node)
Checks to see if the provided node represents a ConditionalNode.

Parameters:
node - The node to check.
Returns:
True if this node is a CondtionalNode, false otherwise.

shouldCast

private boolean shouldCast(DataTypeDescriptor newType,
                           DataTypeDescriptor oldType)
                    throws StandardException
Checks to see if oldType should be casted to the newType. Returns TRUE if the two DataTypeDescriptors have different TypeID's or if the oldType is NULL. Returns FALSE if the newType is NULL or if the two Types are identical.

Parameters:
newType - The type to cast oldType to if they're different.
oldType - The type that should be casted to the newType if they're different.
Returns:
False if the newType is null or they have the same TypeId, true otherwise.
Throws:
StandardException

findType

private DataTypeDescriptor findType(ValueNodeList thenElseList,
                                    FromList fromList,
                                    SubqueryList subqueryList,
                                    java.util.Vector aggregateVector)
                             throws StandardException
This method is a 'prebind.' We need to determine what the types of the nodes are going to be before we can set all the SQLParsed NULL's to the appropriate type. After we bind, however, we want to ignore the SQLParsed NULL's which will be bound to CHAR. Also, we might have to delve into the CASE Expression tree.

Parameters:
thenElseList - The thenElseList (recursive method)
fromList - The fromList (required for Column References).
Throws:
StandardException - Thrown on error.

recastNullNodes

private void recastNullNodes(ValueNodeList thenElseList,
                             DataTypeDescriptor castType,
                             FromList fromList,
                             SubqueryList subqueryList,
                             java.util.Vector aggregateVector)
                      throws StandardException
This recursive method will hunt through the ValueNodeList thenElseList looking for SQL NULL's. If it finds any, it casts them to the provided castType.

Parameters:
thenElseList - The thenElseList to update.
castType - The type to cast SQL parsed NULL's too.
fromList - FromList to pass on to bindExpression if recast is performed
subqueryList - SubqueryList to pass on to bindExpression if recast is performed
aggregateVector - AggregateVector to pass on to bindExpression if recast is performed
Throws:
StandardException - Thrown on error.

recastNullNode

private QueryTreeNode recastNullNode(ValueNode nodeToCast,
                                     DataTypeDescriptor typeToUse)
                              throws StandardException
recastNullNode casts the nodeToCast node to the typeToUse. recastNullNode is called by recastNullNodes. It is called when the nodeToCast is an UntypedNullConstantNode that's been cast by the SQLParser to a CHAR. The node needs to be recasted to the same type of the other nodes in order to prevent the type compatibility error 42X89 from occuring. SQL Standard requires that: VALUES CASE WHEN 1=2 THEN 3 ELSE NULL END returns NULL and not an error message.

Parameters:
nodeToCast - The node that represents a SQL NULL value.
typeToUse - The type which the nodeToCast should be recasted too.
Throws:
StandardException - Thrown on error.

bindExpression

public ValueNode bindExpression(FromList fromList,
                                SubqueryList subqueryList,
                                java.util.Vector aggregateVector)
                         throws StandardException
Bind this expression. This means binding the sub-expressions, as well as figuring out what the return type is for this expression.

Overrides:
bindExpression in class ValueNode
Parameters:
fromList - The FROM list for the query this expression is in, for binding columns.
subqueryList - The subquery list being built as we find SubqueryNodes
aggregateVector - The aggregate vector being built as we find AggregateNodes
Returns:
The new top of the expression tree.
Throws:
StandardException - Thrown on error

preprocess

public ValueNode preprocess(int numTables,
                            FromList outerFromList,
                            SubqueryList outerSubqueryList,
                            PredicateList outerPredicateList)
                     throws StandardException
Preprocess an expression tree. We do a number of transformations here (including subqueries, IN lists, LIKE and BETWEEN) plus subquery flattening. NOTE: This is done before the outer ResultSetNode is preprocessed.

Overrides:
preprocess in class ValueNode
Parameters:
numTables - Number of tables in the DML Statement
outerFromList - FromList from outer query block
outerSubqueryList - SubqueryList from outer query block
outerPredicateList - PredicateList from outer query block
Returns:
The modified expression
Throws:
StandardException - Thrown on error

categorize

public boolean categorize(JBitSet referencedTabs,
                          boolean simplePredsOnly)
                   throws StandardException
Categorize this predicate. Initially, this means building a bit map of the referenced tables for each predicate. If the source of this ColumnReference (at the next underlying level) is not a ColumnReference or a VirtualColumnNode then this predicate will not be pushed down. For example, in: select * from (select 1 from s) a (x) where x = 1 we will not push down x = 1. NOTE: It would be easy to handle the case of a constant, but if the inner SELECT returns an arbitrary expression, then we would have to copy that tree into the pushed predicate, and that tree could contain subqueries and method calls. RESOLVE - revisit this issue once we have views.

Overrides:
categorize in class ValueNode
Parameters:
referencedTabs - JBitSet with bit map of referenced FromTables
simplePredsOnly - Whether or not to consider method calls, field references and conditional nodes when building bit map
Returns:
boolean Whether or not source.expression is a ColumnReference or a VirtualColumnNode.
Throws:
StandardException - Thrown on error

remapColumnReferencesToExpressions

public ValueNode remapColumnReferencesToExpressions()
                                             throws StandardException
Remap all ColumnReferences in this tree to be clones of the underlying expression.

Overrides:
remapColumnReferencesToExpressions in class ValueNode
Returns:
ValueNode The remapped expression tree.
Throws:
StandardException - Thrown on error

isConstantExpression

public boolean isConstantExpression()
Return whether or not this expression tree represents a constant expression.

Overrides:
isConstantExpression in class ValueNode
Returns:
Whether or not this expression tree represents a constant expression.

constantExpression

public boolean constantExpression(PredicateList whereClause)
Description copied from class: ValueNode
Return whether or not this expression tree represents a constant value. In this case, "constant" means that it will always evaluate to the same thing, even if it includes columns. A column is constant if it is compared to a constant expression.

Overrides:
constantExpression in class ValueNode
Returns:
True means this expression tree represents a constant value.
See Also:
ValueNode.constantExpression(org.apache.derby.impl.sql.compile.PredicateList)

eliminateNots

ValueNode eliminateNots(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.

Overrides:
eliminateNots in class ValueNode
Parameters:
underNotNode - Whether or not we are under a NotNode.
Returns:
The modified expression
Throws:
StandardException - Thrown on error

generateExpression

public void generateExpression(ExpressionClassBuilder acb,
                               MethodBuilder mb)
                        throws StandardException
Do code generation for this conditional expression.

Overrides:
generateExpression in class ValueNode
Parameters:
acb - The ExpressionClassBuilder for the class we're generating
mb - The method the expression will go into
Throws:
StandardException - Thrown on error

acceptChildren

void acceptChildren(Visitor v)
              throws StandardException
Accept the visitor for all visitable children of this node.

Overrides:
acceptChildren in class QueryTreeNode
Parameters:
v - the visitor
Throws:
StandardException - on error

isEquivalent

protected boolean isEquivalent(ValueNode o)
                        throws StandardException
Tests if this node is equivalent to the specified ValueNode. Two ValueNodes are considered equivalent if they will evaluate to the same value during query execution.

This method provides basic expression matching facility for the derived class of ValueNode and it is used by the language layer to compare the node structural form of the two expressions for equivalence at bind phase.

Note that it is not comparing the actual row values at runtime to produce a result; hence, when comparing SQL NULLs, they are considered to be equivalent and not unknown.

One usage case of this method in this context is to compare the select column expression against the group by expression to check if they are equivalent. e.g.:

SELECT c1+c2 FROM t1 GROUP BY c1+c2

In general, node equivalence is determined by the derived class of ValueNode. But they generally abide to the rules below:

Specified by:
isEquivalent in class ValueNode
Parameters:
o - the node to compare this ValueNode against.
Returns:
true if the two nodes are equivalent, false otherwise.
Throws:
StandardException

Built on Thu 2012-03-29 21:53:33+0000, from revision ???

Apache Derby V10.6 Internals - Copyright © 2004,2007 The Apache Software Foundation. All Rights Reserved.