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

java.lang.Object
  extended byorg.apache.derby.impl.sql.compile.QueryTreeNode
      extended byorg.apache.derby.impl.sql.compile.QueryTreeNodeVector
          extended byorg.apache.derby.impl.sql.compile.PredicateList
All Implemented Interfaces:
OptimizablePredicateList, Visitable

public class PredicateList
extends QueryTreeNodeVector
implements OptimizablePredicateList

A PredicateList represents the list of top level predicates. Each top level predicate consists of an AndNode whose leftOperand is the top level predicate and whose rightOperand is true. It extends QueryTreeNodeVector.

Author:
Jerry Brenner

Nested Class Summary
private  class PredicateList.PredicateWrapper
          Inner class which helps statistics routines do their work.
private  class PredicateList.PredicateWrapperList
          Another inner class which is basically a List of Predicate Wrappers.
 
Field Summary
private  int numberOfQualifiers
           
private  int numberOfStartPredicates
           
private  int numberOfStopPredicates
           
private static int QUALIFIER_NUM_CATEGORIES
           
private static int QUALIFIER_ORDER_EQUALS
           
private static int QUALIFIER_ORDER_NON_QUAL
           
private static int QUALIFIER_ORDER_NOT_EQUALS
           
private static int QUALIFIER_ORDER_OR_CLAUSE
           
private static int QUALIFIER_ORDER_OTHER_RELOP
           
 
Fields inherited from class org.apache.derby.impl.sql.compile.QueryTreeNode
AUTOINCREMENT_INC_INDEX, AUTOINCREMENT_IS_AUTOINCREMENT_INDEX, AUTOINCREMENT_START_INDEX, beginOffset, endOffset
 
Constructor Summary
PredicateList()
           
 
Method Summary
 Visitable accept(Visitor v)
          Accept a visitor, and call v.visit() on child nodes as necessary.
 void addElement(QueryTreeNode qt)
           
 void addOptPredicate(OptimizablePredicate optPredicate)
          Add the given OptimizablePredicate to the end of this list.
 void addOptPredicate(OptimizablePredicate optPredicate, int position)
          Another flavor of addOptPredicate that inserts the given predicate at a given position.
 void addPredicate(Predicate predicate)
          Add a Predicate to the list.
(package private)  boolean allPushable()
          Check if all of the predicates in the list are pushable.
private  void calculateWeight(PredicateList.PredicateWrapperList[] pwList, int numUsefulPredicates)
           
 void categorize()
          Categorize the predicates in the list.
(package private)  void checkTopPredicatesForEqualsConditions(int tableNumber, boolean[] eqOuterCols, int[] tableNumbers, JBitSet[] tableColMap, boolean resultColTable)
          Update the array of columns in = conditions with constants or correlation or join columns.
private  int chooseLongestMatch(PredicateList.PredicateWrapperList[] predArray, java.util.Vector ret, int numWorkingPredicates)
          choose the statistic which has the maximum match with the predicates.
 void classify(Optimizable optTable, ConglomerateDescriptor cd)
          Classify the predicates in this list according to the given table and conglomerate.
(package private)  boolean constantColumn(ColumnReference colRef)
           
 void copyPredicatesToOtherList(OptimizablePredicateList otherList)
          Non-destructive copy of all of the predicates from this list to the other list.
private  void countScanFlags()
           
(package private)  void decrementLevel(FromList fromList, int decrement)
          Decrement the level of any CRs from the subquery's FROM list that are interesting to transitive closure.
 void destructiveAppend(QueryTreeNodeVector qtnv)
           
 QueryTreeNode elementAt(int index)
           
 void eliminateBooleanTrueAndBooleanTrue()
          Eliminate predicates of the form: AndNode / \ true BooleanConstantNode true BooleanConstantNode This is useful when checking for a NOP PRN as the Like transformation on c1 like 'ASDF%' can leave one of these predicates in the list.
private  void finishKey(ExpressionClassBuilder acb, MethodBuilder mb, MethodBuilder exprFun, LocalField rowField)
          Finish generating a start or stop key
private  LocalField generateIndexableRow(ExpressionClassBuilder acb, int numberOfColumns)
          Generate the indexable row for a start key or stop key.
 void generateQualifiers(ExpressionClassBuilderInterface acbi, MethodBuilder mb, Optimizable optTable, boolean absolute)
          Generate the qualifiers for a scan.
private  void generateSetColumn(ExpressionClassBuilder acb, MethodBuilder exprFun, int columnNumber, Predicate pred, Optimizable optTable, LocalField rowField, boolean isStartKey)
          Generate the code to set the value from a predicate in an index column.
private  void generateSingleQualifierCode(MethodBuilder consMB, Optimizable optTable, boolean absolute, ExpressionClassBuilder acb, RelationalOperator or_node, LocalField qualField, int array_idx_1, int array_idx_2)
           
 void generateStartKey(ExpressionClassBuilderInterface acbi, MethodBuilder mb, Optimizable optTable)
          Generate the start key for a heap or index scan.
 void generateStopKey(ExpressionClassBuilderInterface acbi, MethodBuilder mb, Optimizable optTable)
          Generate the stop key for a heap or index scan.
 OptimizablePredicate getOptPredicate(int index)
          Return the nth OptimizablePredicate in the list.
(package private)  PredicateList getPushablePredicates(JBitSet referencedTableMap)
          Build a list of pushable predicates, if any, that satisfy the referencedTableMap.
 boolean hasOptimizableEqualityPredicate(Optimizable optTable, int columnNumber, boolean isNullOkay)
          Is there an optimizable equality predicate on the specified column?
 boolean hasOptimizableEquijoin(Optimizable optTable, int columnNumber)
          Is there an optimizable equijoin on the specified column?
(package private)  int indexOf(QueryTreeNode qt)
           
(package private)  void insertElementAt(QueryTreeNode qt, int index)
           
 boolean isRedundantPredicate(int predNum)
          Return whether or not the specified entry in the list is a redundant predicate.
(package private)  void joinClauseTransitiveClosure(int numTables, FromList fromList, CompilerContext cc)
          Perform transitive closure on join clauses.
 void markAllPredicatesQualifiers()
          Mark all of the predicates as Qualifiers and set the numberOfQualifiers to reflect this.
(package private)  void markReferencedColumns()
          Mark all of the RCs and the RCs in their RC/VCN chain referenced in the predicate list as referenced.
 void nondestructiveAppend(QueryTreeNodeVector qtnv)
           
private  void orderQualifiers()
           
private  void orderUsefulPredicates(Optimizable optTable, ConglomerateDescriptor cd, boolean pushPreds, boolean nonMatchingIndexScan, boolean coveringIndexScan)
           
 void printSubNodes(int depth)
          Prints the sub-nodes of this object.
(package private)  void pullExpressions(int numTables, ValueNode searchClause)
          Break apart the search clause into matching a PredicateList where each top level predicate is a separate element in the list.
(package private)  void pushExpressionsIntoSelect(SelectNode select)
          Push all predicates, which can be pushed, into the underlying select.
 void pushUsefulPredicates(Optimizable optTable)
          Determine which predicates in this list are useful for limiting the scan on the given table using its best conglomerate.
 void putOptimizableEqualityPredicateFirst(Optimizable optTable, int columnNumber)
          Find the optimizable equality predicate on the specified column and make it the first predicate in this list.
 void remapColumnReferencesToExpressions()
          Remap all ColumnReferences in this tree to be clones of the underlying expression.
(package private)  java.lang.Object remove(int index)
           
(package private)  void removeAllElements()
           
(package private)  void removeElement(QueryTreeNode qt)
           
(package private)  void removeElementAt(int index)
           
 void removeOptPredicate(int predCtr)
          Remove the OptimizablePredicate at the specified index (0-based) from the list.
 void removeOptPredicate(OptimizablePredicate pred)
          Another version of removeOptPredicate that takes the Predicate to be removed, rather than the position of the Predicate.
(package private)  void removeRedundantPredicates()
          Remove redundant predicates.
 ValueNode restoreConstantPredicates()
          Rebuild a constant expression tree from the remaining constant predicates and delete those entries from the PredicateList.
 ValueNode restorePredicates()
          Rebuild an expression tree from the remaining predicates and delete those entries from the PredicateList.
 boolean sameStartStopPosition()
          Can we use the same key for both the start and stop key.
(package private)  void searchClauseTransitiveClosure(int numTables, boolean hashJoinSpecified)
          Perform transitive closure on search clauses.
 double selectivity(Optimizable optTable)
          calculate the selectivity for a set of predicates.
private  double selectivityNoStatistics(Optimizable optTable)
          Compute selectivity the old fashioned way.
(package private)  void setElementAt(QueryTreeNode qt, int index)
           
 void setPredicatesAndProperties(OptimizablePredicateList otherList)
          Sets the given list to have the same elements as this one, and the same properties as this one (number of qualifiers and start and stop predicates.
 int size()
          Return the number of OptimizablePredicates in the list.
 int startOperator(Optimizable optTable)
          Get the start operator for the given Optimizable for a heap or index scan.
 int stopOperator(Optimizable optTable)
          Get the stop operator for the given Optimizable for a heap or index scan.
 java.lang.String toString()
          Format this list as a string We can simply iterate through the list.
 void transferAllPredicates(OptimizablePredicateList otherList)
          Transfer all the predicates from this list to the given list.
protected  void transferNonQualifiers(Optimizable optTable, PredicateList otherPL)
          Transfer the non-qualifiers from this predicate list to the specified predicate list.
 void transferPredicates(OptimizablePredicateList otherList, JBitSet referencedTableMap, Optimizable table)
          Transfer the predicates whose referenced set is contained by the specified referencedTableMap from this list to the other list.
 boolean useful(Optimizable optTable, ConglomerateDescriptor cd)
          Return true if this predicate list is useful for limiting the scan on the given table using the given conglomerate.
 void xorReferencedSet(JBitSet fromMap)
          XOR fromMap with the referenced table map in every remaining Predicate in the list.
 
Methods inherited from class org.apache.derby.impl.sql.compile.QueryTreeNode
bind, convertDefaultNode, debugFlush, debugPrint, executeSchemaName, executeStatementName, formatNodeString, foundString, generate, generate, generateAuthorizeCheck, getBeginOffset, getClassFactory, getCompilerContext, getContextManager, getCursorInfo, getDataDictionary, getDependencyManager, getEndOffset, getExecutionFactory, getGenericConstantActionFactory, getIntProperty, getLanguageConnectionContext, getNodeFactory, getNodeType, getNullNode, getParameterTypes, getRowEstimate, getSchemaDescriptor, getSchemaDescriptor, getSPSName, getStatementType, getTableDescriptor, getTypeCompiler, init, init, init, init, init, init, init, init, init, init, init, init, init, init, isAtomic, isInstanceOf, isSessionSchema, isSessionSchema, makeConstantAction, makeResultDescription, makeTableName, needsSavepoint, nodeHeader, optimize, parseQueryText, printLabel, referencesSessionSchema, setBeginOffset, setContextManager, setEndOffset, setNodeType, setRefActionInfo, treePrint, treePrint, verifyClassExist
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface org.apache.derby.iapi.sql.compile.OptimizablePredicateList
size
 

Field Detail

numberOfStartPredicates

private int numberOfStartPredicates

numberOfStopPredicates

private int numberOfStopPredicates

numberOfQualifiers

private int numberOfQualifiers

QUALIFIER_ORDER_EQUALS

private static final int QUALIFIER_ORDER_EQUALS
See Also:
Constant Field Values

QUALIFIER_ORDER_OTHER_RELOP

private static final int QUALIFIER_ORDER_OTHER_RELOP
See Also:
Constant Field Values

QUALIFIER_ORDER_NOT_EQUALS

private static final int QUALIFIER_ORDER_NOT_EQUALS
See Also:
Constant Field Values

QUALIFIER_ORDER_NON_QUAL

private static final int QUALIFIER_ORDER_NON_QUAL
See Also:
Constant Field Values

QUALIFIER_ORDER_OR_CLAUSE

private static final int QUALIFIER_ORDER_OR_CLAUSE
See Also:
Constant Field Values

QUALIFIER_NUM_CATEGORIES

private static final int QUALIFIER_NUM_CATEGORIES
See Also:
Constant Field Values
Constructor Detail

PredicateList

public PredicateList()
Method Detail

getOptPredicate

public OptimizablePredicate getOptPredicate(int index)
Description copied from interface: OptimizablePredicateList
Return the nth OptimizablePredicate in the list.

Specified by:
getOptPredicate in interface OptimizablePredicateList
Parameters:
index - "index" (0 based) into the list.
Returns:
OptimizablePredicate The nth OptimizablePredicate in the list.
See Also:
OptimizablePredicateList.getOptPredicate(int)

removeOptPredicate

public final void removeOptPredicate(int predCtr)
                              throws StandardException
Description copied from interface: OptimizablePredicateList
Remove the OptimizablePredicate at the specified index (0-based) from the list.

Specified by:
removeOptPredicate in interface OptimizablePredicateList
Parameters:
predCtr - The index.
Returns:
Nothing.
Throws:
StandardException - Thrown on error
See Also:
OptimizablePredicateList.removeOptPredicate(int)

removeOptPredicate

public final void removeOptPredicate(OptimizablePredicate pred)
Another version of removeOptPredicate that takes the Predicate to be removed, rather than the position of the Predicate. This is not part any interface (yet).


addOptPredicate

public void addOptPredicate(OptimizablePredicate optPredicate)
Description copied from interface: OptimizablePredicateList
Add the given OptimizablePredicate to the end of this list.

Specified by:
addOptPredicate in interface OptimizablePredicateList
Parameters:
optPredicate - The predicate to add
See Also:
OptimizablePredicateList.addOptPredicate(org.apache.derby.iapi.sql.compile.OptimizablePredicate)

addOptPredicate

public void addOptPredicate(OptimizablePredicate optPredicate,
                            int position)
Another flavor of addOptPredicate that inserts the given predicate at a given position. This is not yet part of any interface.


useful

public boolean useful(Optimizable optTable,
                      ConglomerateDescriptor cd)
               throws StandardException
Description copied from interface: OptimizablePredicateList
Return true if this predicate list is useful for limiting the scan on the given table using the given conglomerate.

Specified by:
useful in interface OptimizablePredicateList
Parameters:
optTable - An Optimizable for the table in question
cd - A ConglomerateDescriptor for the conglomerate in question
Returns:
true if this predicate list can limit the scan
Throws:
StandardException - Thrown on error
See Also:
OptimizablePredicateList.useful(org.apache.derby.iapi.sql.compile.Optimizable, org.apache.derby.iapi.sql.dictionary.ConglomerateDescriptor)

pushUsefulPredicates

public void pushUsefulPredicates(Optimizable optTable)
                          throws StandardException
Description copied from interface: OptimizablePredicateList
Determine which predicates in this list are useful for limiting the scan on the given table using its best conglomerate. Remove those predicates from this list and push them down to the given Optimizable table. The predicates are pushed down in the order of the index columns that they qualify. Also, the predicates are "marked" as start predicates, stop predicates, or qualifier predicates. Finally, the start and stop operators are set in the given Optimizable.

Specified by:
pushUsefulPredicates in interface OptimizablePredicateList
Parameters:
optTable - An Optimizable for the table in question
Returns:
Nothing
Throws:
StandardException - Thrown on error
See Also:
OptimizablePredicateList.pushUsefulPredicates(org.apache.derby.iapi.sql.compile.Optimizable)

classify

public void classify(Optimizable optTable,
                     ConglomerateDescriptor cd)
              throws StandardException
Description copied from interface: OptimizablePredicateList
Classify the predicates in this list according to the given table and conglomerate. Each predicate can be a start key, stop key, and/or qualifier, or it can be none of the above. This method also orders the predicates to match the order of the columns in a keyed conglomerate. No ordering is done for heaps.

Specified by:
classify in interface OptimizablePredicateList
Parameters:
optTable - The Optimizable table for which to classify the predicates in this list.
cd - The ConglomerateDescriptor for which to classify the predicates in this list.
Throws:
StandardException - Thrown on error
See Also:
OptimizablePredicateList.classify(org.apache.derby.iapi.sql.compile.Optimizable, org.apache.derby.iapi.sql.dictionary.ConglomerateDescriptor)

markAllPredicatesQualifiers

public void markAllPredicatesQualifiers()
Description copied from interface: OptimizablePredicateList
Mark all of the predicates as Qualifiers and set the numberOfQualifiers to reflect this. This is useful for hash joins where all of the predicates in the list to be evaluated during the probe into the hash table on a next are qualifiers.

Specified by:
markAllPredicatesQualifiers in interface OptimizablePredicateList
See Also:
OptimizablePredicateList.markAllPredicatesQualifiers()

hasOptimizableEqualityPredicate

public boolean hasOptimizableEqualityPredicate(Optimizable optTable,
                                               int columnNumber,
                                               boolean isNullOkay)
                                        throws StandardException
Description copied from interface: OptimizablePredicateList
Is there an optimizable equality predicate on the specified column?

Specified by:
hasOptimizableEqualityPredicate in interface OptimizablePredicateList
Parameters:
optTable - The optimizable the column comes from.
columnNumber - The column number within the base table.
isNullOkay - boolean, whether or not the IS NULL operator satisfies the search
Returns:
Whether or not there is an optimizable equality predicate on the specified column.
Throws:
StandardException - Thrown on error
See Also:
OptimizablePredicateList.hasOptimizableEqualityPredicate(org.apache.derby.iapi.sql.compile.Optimizable, int, boolean)

hasOptimizableEquijoin

public boolean hasOptimizableEquijoin(Optimizable optTable,
                                      int columnNumber)
                               throws StandardException
Description copied from interface: OptimizablePredicateList
Is there an optimizable equijoin on the specified column?

Specified by:
hasOptimizableEquijoin in interface OptimizablePredicateList
Parameters:
optTable - The optimizable the column comes from.
columnNumber - The column number within the base table.
Returns:
Whether or not there is an optimizable equijoin on the specified column.
Throws:
StandardException - Thrown on error
See Also:
OptimizablePredicateList.hasOptimizableEquijoin(org.apache.derby.iapi.sql.compile.Optimizable, int)

putOptimizableEqualityPredicateFirst

public void putOptimizableEqualityPredicateFirst(Optimizable optTable,
                                                 int columnNumber)
                                          throws StandardException
Description copied from interface: OptimizablePredicateList
Find the optimizable equality predicate on the specified column and make it the first predicate in this list. This is useful for hash joins where Qualifier[0] is assumed to be on the hash key.

Specified by:
putOptimizableEqualityPredicateFirst in interface OptimizablePredicateList
Parameters:
optTable - The optimizable the column comes from.
columnNumber - The column number within the base table.
Returns:
Nothing
Throws:
StandardException - Thrown on error
See Also:
OptimizablePredicateList.putOptimizableEqualityPredicateFirst(org.apache.derby.iapi.sql.compile.Optimizable, int)

orderUsefulPredicates

private void orderUsefulPredicates(Optimizable optTable,
                                   ConglomerateDescriptor cd,
                                   boolean pushPreds,
                                   boolean nonMatchingIndexScan,
                                   boolean coveringIndexScan)
                            throws StandardException
Throws:
StandardException

addPredicate

public void addPredicate(Predicate predicate)
                  throws StandardException
Add a Predicate to the list.

Parameters:
predicate - A Predicate to add to the list
Returns:
Nothing
Throws:
StandardException - Thrown on error

transferNonQualifiers

protected void transferNonQualifiers(Optimizable optTable,
                                     PredicateList otherPL)
                              throws StandardException
Transfer the non-qualifiers from this predicate list to the specified predicate list. This is useful for arbitrary hash join, where we need to separate the 2 as the qualifiers get applied when probing the hash table and the non-qualifiers get * applied afterwards.

Parameters:
optTable - The optimizable that we want qualifiers for
otherPL - ParameterList for non-qualifiers
Returns:
Nothing
Throws:
StandardException - Thrown on error

categorize

public void categorize()
                throws StandardException
Categorize the predicates in the list. Initially, this means building a bit map of the referenced tables for each predicate.

Returns:
None.
Throws:
StandardException - Thrown on error

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
Returns:
Nothing

eliminateBooleanTrueAndBooleanTrue

public void eliminateBooleanTrueAndBooleanTrue()
Eliminate predicates of the form: AndNode / \ true BooleanConstantNode true BooleanConstantNode This is useful when checking for a NOP PRN as the Like transformation on c1 like 'ASDF%' can leave one of these predicates in the list.

Returns:
Nothing.

restoreConstantPredicates

public ValueNode restoreConstantPredicates()
Rebuild a constant expression tree from the remaining constant predicates and delete those entries from the PredicateList. The rightOperand of every top level AndNode is always a true BooleanConstantNode, so we can blindly overwrite that pointer. Optimizations: We take this opportunity to eliminate: AndNode / \ true BooleanConstantNode true BooleanConstantNode We remove the AndNode if the predicate list is a single AndNode: AndNode / \ LeftOperand RightOperand becomes: LeftOperand If the leftOperand of any AndNode is False, then the entire expression will be False. The expression simple becomes: false BooleanConstantNode

Returns:
ValueNode The rebuilt expression tree.

restorePredicates

public ValueNode restorePredicates()
Rebuild an expression tree from the remaining predicates and delete those entries from the PredicateList. The rightOperand of every top level AndNode is always a true BooleanConstantNode, so we can blindly overwrite that pointer. Optimizations: We take this opportunity to eliminate: AndNode / \ true BooleanConstantNode true BooleanConstantNode We remove the AndNode if the predicate list is a single AndNode: AndNode / \ LeftOperand RightOperand becomes: LeftOperand If the leftOperand of any AndNode is False, then the entire expression will be False. The expression simple becomes: false BooleanConstantNode

Returns:
ValueNode The rebuilt expression tree.

remapColumnReferencesToExpressions

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

Returns:
Nothing.
Throws:
StandardException - Thrown on error

pullExpressions

void pullExpressions(int numTables,
                     ValueNode searchClause)
               throws StandardException
Break apart the search clause into matching a PredicateList where each top level predicate is a separate element in the list. Build a bit map to represent the FromTables referenced within each top level predicate. NOTE: We want the rightOperand of every AndNode to be true, in order to simplify the algorithm for putting the predicates back into the tree. (As we put an AndNode back into the tree, we can ignore it's rightOperand.)

Parameters:
numTables - Number of tables in the DML Statement
searchClause - The search clause to operate on.
Returns:
None.
Throws:
StandardException - Thrown on error

xorReferencedSet

public void xorReferencedSet(JBitSet fromMap)
XOR fromMap with the referenced table map in every remaining Predicate in the list. This is useful when pushing down multi-table predicates.

Parameters:
fromMap - The JBitSet to XOR with.
Returns:
Nothing.

countScanFlags

private void countScanFlags()

pushExpressionsIntoSelect

void pushExpressionsIntoSelect(SelectNode select)
                         throws StandardException
Push all predicates, which can be pushed, into the underlying select. A predicate can be pushed into an underlying select if the source of every ColumnReference in the predicate is itself a ColumnReference. This is useful when attempting to push predicates into non-flattenable views or derived tables.

Parameters:
select - The underlying SelectNode.
Returns:
Nothing.
Throws:
StandardException - Thrown on error

markReferencedColumns

void markReferencedColumns()
                     throws StandardException
Mark all of the RCs and the RCs in their RC/VCN chain referenced in the predicate list as referenced.

Returns:
Nothing.
Throws:
StandardException - Thrown on error

checkTopPredicatesForEqualsConditions

void checkTopPredicatesForEqualsConditions(int tableNumber,
                                           boolean[] eqOuterCols,
                                           int[] tableNumbers,
                                           JBitSet[] tableColMap,
                                           boolean resultColTable)
                                     throws StandardException
Update the array of columns in = conditions with constants or correlation or join columns. This is useful when doing subquery flattening on the basis of an equality condition.

Parameters:
tableNumber - The tableNumber of the table from which the columns of interest come from.
eqOuterCols - Array of booleans for noting which columns are in = predicates with constants or correlation columns.
tableNumbers - Array of table numbers in this query block.
resultColTable - tableNumber is the table the result columns are coming from
Returns:
Nothing.
Throws:
StandardException - Thrown on error

allPushable

boolean allPushable()
Check if all of the predicates in the list are pushable.

Returns:
Whether or not all of the predicates in the list are pushable.

getPushablePredicates

PredicateList getPushablePredicates(JBitSet referencedTableMap)
                              throws StandardException
Build a list of pushable predicates, if any, that satisfy the referencedTableMap.

Parameters:
referencedTableMap - The referenced table map
Returns:
A list of pushable predicates, if any, that satisfy the referencedTableMap.
Throws:
StandardException - Thrown on error

decrementLevel

void decrementLevel(FromList fromList,
                    int decrement)
Decrement the level of any CRs from the subquery's FROM list that are interesting to transitive closure.

Parameters:
fromList - The subquery's FROM list.
decrement - Decrement size.
Returns:
Nothing.

joinClauseTransitiveClosure

void joinClauseTransitiveClosure(int numTables,
                                 FromList fromList,
                                 CompilerContext cc)
                           throws StandardException
Perform transitive closure on join clauses. For each table in the query, we build a list of equijoin clauses of the form: <=> Each join clause is put on 2 lists since it joins 2 tables. We then walk the array of lists. We first walk it as the outer list. For each equijoin predicate, we assign an equivalence class if it does not yet have one. We then walk the predicate list (as middle) for the other table, searching for other equijoins with the middle table number and column number. All such predicates are assigned the same equivalence class. We then walk the predicate list (as inner) for the other side of the middle predicate to see if we can find an equijoin between outer and inner. If so, then we simply assign it to the same equivalence class. If not, then we add the new equijoin clause.

Parameters:
numTables - The number of tables in the query
fromList - The FromList in question.
cc - The CompilerContext to use
Returns:
Nothing.
Throws:
StandardException - Thrown on error

searchClauseTransitiveClosure

void searchClauseTransitiveClosure(int numTables,
                                   boolean hashJoinSpecified)
                             throws StandardException
Perform transitive closure on search clauses. We build a list of search clauses of the form: [] We also build a list of equijoin conditions of form: = where both columns are from different tables in the same query block. For each search clause in the list, we search the equijoin list to see if there is an equijoin clause on the same column. If so, then we search the search clause list for a search condition on the column being joined against with the same relation operator and constant. If a match is found, then there is no need to add a new predicate. Otherwise, we add a new search condition on the column being joined with. In either case, if the relational operator in the search clause is an "=" then we mark the equijoin clause as being redundant. Redundant equijoin clauses will be removed at the end of the search as they are * unnecessary.

Parameters:
numTables - The number of tables in the query
hashJoinSpecified - Whether or not user specified a hash join
Returns:
Nothing.
Throws:
StandardException - Thrown on error

removeRedundantPredicates

void removeRedundantPredicates()
Remove redundant predicates. A redundant predicate has an equivalence class (!= -1) and there are other predicates in the same equivalence class after it in the list. (Actually, we remove all of the predicates in the same equivalence class that appear after this one.)

Returns:
Nothing.

transferPredicates

public void transferPredicates(OptimizablePredicateList otherList,
                               JBitSet referencedTableMap,
                               Optimizable table)
                        throws StandardException
Description copied from interface: OptimizablePredicateList
Transfer the predicates whose referenced set is contained by the specified referencedTableMap from this list to the other list. This is useful when splitting out a set of predicates from a larger set, like when generating a HashScanResultSet.

Specified by:
transferPredicates in interface OptimizablePredicateList
Parameters:
otherList - The predicateList to xfer to
referencedTableMap - The table map to check against
table - The table to order the new predicates against
Returns:
Nothing.
Throws:
StandardException - Thrown on error
See Also:
OptimizablePredicateList.transferPredicates(org.apache.derby.iapi.sql.compile.OptimizablePredicateList, org.apache.derby.iapi.util.JBitSet, org.apache.derby.iapi.sql.compile.Optimizable)

transferAllPredicates

public void transferAllPredicates(OptimizablePredicateList otherList)
                           throws StandardException
Description copied from interface: OptimizablePredicateList
Transfer all the predicates from this list to the given list.

Specified by:
transferAllPredicates in interface OptimizablePredicateList
Throws:
StandardException - Thrown on error
See Also:
OptimizablePredicateList.transferAllPredicates(org.apache.derby.iapi.sql.compile.OptimizablePredicateList)

copyPredicatesToOtherList

public void copyPredicatesToOtherList(OptimizablePredicateList otherList)
                               throws StandardException
Description copied from interface: OptimizablePredicateList
Non-destructive copy of all of the predicates from this list to the other list. This is useful when splitting out a set of predicates from a larger set, like when generating a HashScanResultSet.

Specified by:
copyPredicatesToOtherList in interface OptimizablePredicateList
Parameters:
otherList - The predicateList to xfer to
Returns:
Nothing.
Throws:
StandardException - Thrown on error
See Also:
OptimizablePredicateList.copyPredicatesToOtherList(org.apache.derby.iapi.sql.compile.OptimizablePredicateList)

isRedundantPredicate

public boolean isRedundantPredicate(int predNum)
Description copied from interface: OptimizablePredicateList
Return whether or not the specified entry in the list is a redundant predicate. This is useful for selectivity calculations because we do not want redundant predicates included in the selectivity calculation.

Specified by:
isRedundantPredicate in interface OptimizablePredicateList
Parameters:
predNum - The entry in the list
Returns:
Whether or not the specified entry in the list is a redundant predicate.
See Also:
OptimizablePredicateList.isRedundantPredicate(int)

setPredicatesAndProperties

public void setPredicatesAndProperties(OptimizablePredicateList otherList)
                                throws StandardException
Description copied from interface: OptimizablePredicateList
Sets the given list to have the same elements as this one, and the same properties as this one (number of qualifiers and start and stop predicates.

Specified by:
setPredicatesAndProperties in interface OptimizablePredicateList
Parameters:
otherList - The list to set the same as this one.
Throws:
StandardException - Thrown on error
See Also:
OptimizablePredicateList.setPredicatesAndProperties(org.apache.derby.iapi.sql.compile.OptimizablePredicateList)

startOperator

public int startOperator(Optimizable optTable)
Description copied from interface: OptimizablePredicateList
Get the start operator for the given Optimizable for a heap or index scan.

Specified by:
startOperator in interface OptimizablePredicateList
See Also:
OptimizablePredicateList.startOperator(org.apache.derby.iapi.sql.compile.Optimizable)

generateStopKey

public void generateStopKey(ExpressionClassBuilderInterface acbi,
                            MethodBuilder mb,
                            Optimizable optTable)
                     throws StandardException
Description copied from interface: OptimizablePredicateList
Generate the stop key for a heap or index scan.

Specified by:
generateStopKey in interface OptimizablePredicateList
Parameters:
acbi - The ExpressionClassBuilderInterface for the class we're building
mb - the method the generated code is to go into
optTable - The Optimizable table the stop key is for
Returns:
An expression which, when evaluated, returns the method to get the stop key.
Throws:
StandardException - Thrown on error
See Also:
OptimizablePredicateList.generateStopKey(org.apache.derby.iapi.sql.compile.ExpressionClassBuilderInterface, org.apache.derby.iapi.services.compiler.MethodBuilder, org.apache.derby.iapi.sql.compile.Optimizable)

stopOperator

public int stopOperator(Optimizable optTable)
Description copied from interface: OptimizablePredicateList
Get the stop operator for the given Optimizable for a heap or index scan.

Specified by:
stopOperator in interface OptimizablePredicateList
See Also:
OptimizablePredicateList.stopOperator(org.apache.derby.iapi.sql.compile.Optimizable)

generateSingleQualifierCode

private void generateSingleQualifierCode(MethodBuilder consMB,
                                         Optimizable optTable,
                                         boolean absolute,
                                         ExpressionClassBuilder acb,
                                         RelationalOperator or_node,
                                         LocalField qualField,
                                         int array_idx_1,
                                         int array_idx_2)
                                  throws StandardException
Throws:
StandardException

generateQualifiers

public void generateQualifiers(ExpressionClassBuilderInterface acbi,
                               MethodBuilder mb,
                               Optimizable optTable,
                               boolean absolute)
                        throws StandardException
Description copied from interface: OptimizablePredicateList
Generate the qualifiers for a scan. This method generates an array of Qualifiers, and fills them in with calls to the factory method for generating Qualifiers in the constructor for the activation. It stores the array of Qualifiers in a field in the activation, and returns a reference to that field. If there are no qualifiers, it initializes the array of Qualifiers to null.

Specified by:
generateQualifiers in interface OptimizablePredicateList
Parameters:
acbi - The ExpressionClassBuilderInterface for the class we are building
mb - The method the generated code is going into
optTable - The Optimizable table the Qualifiers are on
absolute - Generate absolute column positions if true, else relative column positions (within the underlying row)
Throws:
StandardException - Thrown on error
See Also:
OptimizablePredicateList.generateQualifiers(org.apache.derby.iapi.sql.compile.ExpressionClassBuilderInterface, org.apache.derby.iapi.services.compiler.MethodBuilder, org.apache.derby.iapi.sql.compile.Optimizable, boolean)

orderQualifiers

private void orderQualifiers()

generateStartKey

public void generateStartKey(ExpressionClassBuilderInterface acbi,
                             MethodBuilder mb,
                             Optimizable optTable)
                      throws StandardException
Description copied from interface: OptimizablePredicateList
Generate the start key for a heap or index scan.

Specified by:
generateStartKey in interface OptimizablePredicateList
Parameters:
acbi - The ExpressionClassBuilderInterface for the class we're building
mb - The method the generated code is to go into
optTable - The Optimizable table the start key is for
Returns:
An expression which, when evaluated, returns the method to get the start key.
Throws:
StandardException - Thrown on error
See Also:
OptimizablePredicateList.generateStartKey(org.apache.derby.iapi.sql.compile.ExpressionClassBuilderInterface, org.apache.derby.iapi.services.compiler.MethodBuilder, org.apache.derby.iapi.sql.compile.Optimizable)

sameStartStopPosition

public boolean sameStartStopPosition()
                              throws StandardException
Description copied from interface: OptimizablePredicateList
Can we use the same key for both the start and stop key. This is possible when doing an exact match on an index where there are no other sargable predicates.

Specified by:
sameStartStopPosition in interface OptimizablePredicateList
Returns:
Whether or not we can use the same key for both the start and stop key.
Throws:
StandardException - Thrown on error
See Also:
OptimizablePredicateList.sameStartStopPosition()

generateIndexableRow

private LocalField generateIndexableRow(ExpressionClassBuilder acb,
                                        int numberOfColumns)
Generate the indexable row for a start key or stop key.

Parameters:
acb - The ActivationClassBuilder for the class we're building
numberOfColumns - The number of columns in the key
Returns:
The field that holds the indexable row

generateSetColumn

private void generateSetColumn(ExpressionClassBuilder acb,
                               MethodBuilder exprFun,
                               int columnNumber,
                               Predicate pred,
                               Optimizable optTable,
                               LocalField rowField,
                               boolean isStartKey)
                        throws StandardException
Generate the code to set the value from a predicate in an index column.

Parameters:
acb - The ActivationClassBuilder for the class we're building
exprFun - The MethodBuilder for the method we're building
columnNumber - The position number of the column we're setting the value in (zero-based)
pred - The Predicate with the value to put in the index column
optTable - The Optimizable table the column is in
rowField - The field that holds the indexable row
isStartKey - Are we generating start or stop key? This information is useful for "in"'s dynamic start/stop key, bug 3858
Throws:
StandardException - Thrown on error

finishKey

private void finishKey(ExpressionClassBuilder acb,
                       MethodBuilder mb,
                       MethodBuilder exprFun,
                       LocalField rowField)
Finish generating a start or stop key

Parameters:
acb - The ActivationClassBuilder for the class we're building
exprFun - The MethodBuilder for the method we're building
rowField - The name of the field that holds the indexable row
Returns:
An Expression that evaluates to the start or stop key getter

constantColumn

boolean constantColumn(ColumnReference colRef)

selectivity

public double selectivity(Optimizable optTable)
                   throws StandardException
Description copied from interface: OptimizablePredicateList
calculate the selectivity for a set of predicates. If statistics exist for the predicates this method uses the statistics. If statistics do not exist, then simply call selectivity for each of the predicates and return the result.

Specified by:
selectivity in interface OptimizablePredicateList
Parameters:
optTable - the Optimizable that the predicate list restricts.
Throws:
StandardException
See Also:
OptimizablePredicateList.selectivity(org.apache.derby.iapi.sql.compile.Optimizable)

calculateWeight

private void calculateWeight(PredicateList.PredicateWrapperList[] pwList,
                             int numUsefulPredicates)

chooseLongestMatch

private int chooseLongestMatch(PredicateList.PredicateWrapperList[] predArray,
                               java.util.Vector ret,
                               int numWorkingPredicates)
choose the statistic which has the maximum match with the predicates. value is returned in ret.


selectivityNoStatistics

private double selectivityNoStatistics(Optimizable optTable)
Compute selectivity the old fashioned way.


size

public final int size()

elementAt

public QueryTreeNode elementAt(int index)

addElement

public final void addElement(QueryTreeNode qt)

removeElementAt

final void removeElementAt(int index)

removeElement

final void removeElement(QueryTreeNode qt)

remove

final java.lang.Object remove(int index)

indexOf

final int indexOf(QueryTreeNode qt)

setElementAt

final void setElementAt(QueryTreeNode qt,
                        int index)

destructiveAppend

public void destructiveAppend(QueryTreeNodeVector qtnv)

nondestructiveAppend

public void nondestructiveAppend(QueryTreeNodeVector qtnv)

removeAllElements

final void removeAllElements()

insertElementAt

final void insertElementAt(QueryTreeNode qt,
                           int index)

toString

public java.lang.String toString()
Format this list as a string We can simply iterate through the list. Note each list member is a QueryTreeNode, and so should have its specialization of toString defined.

Overrides:
toString in class QueryTreeNode
Returns:
This list formatted as a String

accept

public Visitable accept(Visitor v)
                 throws StandardException
Accept a visitor, and call v.visit() on child nodes as necessary.

Specified by:
accept in interface Visitable
Overrides:
accept in class QueryTreeNode
Parameters:
v - the visitor
Throws:
StandardException - on error


Apache Derby V10.0 Engine Documentation - Copyright © 1997,2004 The Apache Software Foundation or its licensors, as applicable.