Top Description Methods
org.apache.derby.iapi.sql.compile

public Interface Optimizable

extends Visitable
Known Direct Implementers
org.apache.derby.impl.sql.compile.FromTable
Imports
org.apache.derby.iapi.sql.dictionary.ConglomerateDescriptor, .TableDescriptor, .DataDictionary, org.apache.derby.shared.common.error.StandardException, org.apache.derby.iapi.util.JBitSet, java.util.Properties

Optimizable provides services for optimizing a table in a query.

Method Summary

Modifier and TypeMethod and Description
public boolean
considerSortAvoidancePath()

Check whether this optimizable's sort avoidance path should be considered.

public int

Returns:

The relative 0-based column position for the column.
convertAbsoluteToRelativeColumnPosition
(int
The absolute 0-based column position for the column.
absolutePosition
)

Convert an absolute to a relative 0-based column position.

public CostEstimate

Returns:

The estimated cost of doing the scan
estimateCost
(OptimizablePredicateList
The predicate list to optimize against
predList
,
ConglomerateDescriptor
The conglomerate descriptor to get the cost of
cd
,
CostEstimate
The estimated cost of the part of the plan outer to this optimizable.
outerCost
,
Optimizer
The optimizer to use to help estimate the cost
optimizer
,
RowOrdering
The row ordering for all the tables in the join order, including this one.
rowOrdering
)

Estimate the cost of scanning this Optimizable using the given predicate list with the given conglomerate.

public boolean

Returns:

true means feasible
feasibleJoinStrategy
(OptimizablePredicateList
The predicate list that has been pushed down to this optimizable
predList
,
Optimizer
The optimizer to use.
optimizer
)

Is the current proposed join strategy for this optimizable feasible given the predicate list?

public boolean
forUpdate()

Return true if this is the target table of an update

public String
getBaseTableName()

Get the table name of this Optimizable.

public AccessPath
getBestAccessPath()

Get the best access path for this Optimizable.

public AccessPath
getBestSortAvoidancePath()

Get the best sort-avoidance path for this Optimizable.

public AccessPath
getCurrentAccessPath()

Get the current access path under consideration for this Optimizable

public DataDictionary

Returns:

The DataDictionary to use.
getDataDictionary
()

Get the DataDictionary from this Optimizable.

public String

Returns:

The name of this Optimizable.
getName
()

Get the (exposed) name of this Optimizable

public int

Returns:

The number of the number of columns returned by this Optimizable.
getNumColumnsReturned
()

Get the number of the number of columns returned by this Optimizable.

public OptTrace
getOptimizerTracer()

Get the optimizer tracer, if any

public Properties

Returns:

The Properties list, if any, associated with this optimizable.
getProperties
()

Get the Properties list, if any, associated with this optimizable.

public JBitSet

Returns:

JBitSet Referenced table map.
getReferencedTableMap
()

Get the map of referenced tables for this Optimizable.

public int
getResultSetNumber()

Get this Optimizable's result set number

public TableDescriptor
getTableDescriptor()

Get the table descriptor for this table (if any).

public int
getTableNumber()

Get this Optimizable's table number

public AccessPath
getTrulyTheBestAccessPath()

Get the best access path overall for this Optimizable.

public int[]
hashKeyColumns()

Return the hash key column numbers, for hash join strategy

public boolean
hasLargeObjectColumns()

Tell whether this Optimizable has any large object (LOB) columns.

public boolean
hasTableNumber()

Return true if this Optimizable has a table number

public void
initAccessPaths(Optimizer
The optimizer being used.
optimizer
)

Init the access paths for this optimizable.

public int
initialCapacity()

Return the initial capacity of the hash table, for hash join strategy

public boolean
isBaseTable()

Tell whether this Optimizable represents a base table

public boolean

Returns:

boolean Whether or not this is a covering index.
isCoveringIndex
(ConglomerateDescriptor
ConglomerateDesriptor for index to consider
cd
)

Return whether or not this is a covering index.

public boolean
isMaterializable()

Tell whether this Optimizable is materializable

public boolean

Returns:

Whether or not the optimizable will return at most 1 row per scan?
isOneRowScan
()

Will the optimizable return at most 1 row per scan?

public boolean

Returns:

Whether or not the optimizable the target table of an update or delete.
isTargetTable
()

Is the optimizable the target table of an update or delete?

public boolean

Returns:

Where or not this Optimizable can appear at the current location in the join order.
legalJoinOrder
(JBitSet
The tables that have been placed so far in the join order.
assignedTableMap
)

Can this Optimizable appear at the current location in the join order.

public float
loadFactor()

Return the load factor of the hash table, for hash join strategy

public int
maxCapacity(JoinStrategy joinStrategy, int
The maximum number of bytes to be used. Ignored if the user has set a maximum number of rows for the Optimizable.
maxMemoryPerTable
)

Return the maximum capacity of the hash table, for hash join strategy

public boolean

Returns:

true if the memory usage of the proposed access path is OK, false if not.
memoryUsageOK
(double rowCount, int maxMemoryPerTable)

public Optimizable

Returns:

The (potentially new) Optimizable at the top of the tree.
modifyAccessPath
(JBitSet
Bit map of the tables that are outer to this one in the join order.
outerTables
)

Modify the access path for this Optimizable, as necessary.

public boolean

Returns:

true means another access path was chosen, false means no more access paths to evaluate.
nextAccessPath
(Optimizer
Optimizer to use.
optimizer
,
OptimizablePredicateList
The predicate list for this optimizable. The optimizer always passes null, and it is up to the optimizable object to pass along its own predicate list, if appropriate, when delegating this method.
predList
,
RowOrdering
The row ordering for all the outer tables in the join order. This method will add the ordering of the next access path to the given RowOrdering.
rowOrdering
)

Choose the next access path to evaluate for this Optimizable.

public CostEstimate

Returns:

The optimizer's estimated cost of the best access path.
optimizeIt
(Optimizer
Optimizer to use.
optimizer
,
OptimizablePredicateList
The predicate list to optimize against
predList
,
CostEstimate
The CostEstimate for the outer tables in the join order, telling how many times this Optimizable will be scanned.
outerCost
,
RowOrdering
The row ordering for all the tables in the join order, including this one.
rowOrdering
)

Choose the best access path for this Optimizable.

public boolean
optimizerTracingIsOn()

Report whether optimizer tracing is on

public void
pullOptPredicates(OptimizablePredicateList
The list to put the pulled predicates in.
optimizablePredicates
)

Pull all the OptimizablePredicates from this Optimizable and put them in the given OptimizablePredicateList.

public boolean

Returns:

Whether or not the predicate was pushed down.
pushOptPredicate
(OptimizablePredicate
OptimizablePredicate to push down.
optimizablePredicate
)

Push an OptimizablePredicate down, if this node accepts it.

public void
rememberAsBest(int
The type of plan (one of Optimizer.NORMAL_PLAN or Optimizer.SORT_AVOIDANCE_PLAN)
planType
,
Optimizer
The OptimizerImpl that is telling this Optimizable to remember its current path as "truly the best".
optimizer
)

Remember the current access path as the best one (so far).

public void
rememberJoinStrategyAsBest(AccessPath ap)

Remember the current join strategy as the best one so far in this join order.

public void
rememberSortAvoidancePath()

Mark this optimizable so that its sort avoidance path will be considered.

public void
setHashKeyColumns(int[] columnNumbers)

Set the hash key column numbers, for hash join strategy

public void
setProperties(Properties
The Properties list for this optimizable.
tableProperties
)

Set the Properties list for this optimizalbe.

public void
startOptimizing(Optimizer optimizer, RowOrdering rowOrdering)

Begin the optimization process for this Optimizable.

public boolean
supportsMultipleInstantiations()

Tell whether this Optimizable can be instantiated multiple times

public double

Returns:

<= 0 means there is no uniqueness condition > 0 means there is a uniqueness condition, and the return value is the number of rows per scan.
uniqueJoin
(OptimizablePredicateList
The predicate list to check
predList
)

Does this optimizable have a uniqueness condition on the given predicate list, and if so, how many unique keys will be returned per scan.

public void
updateBestPlanMap(short
Indicates whether we're adding, loading, or removing a best plan for the OptimizerImpl/Optimizable.
action
,
Object
Object to use as the map key when adding/looking up a plan. If it is an instance of OptimizerImpl then it corresponds to an outer query; otherwise it's some Optimizable above this Optimizable that could potentially reject plans chosen by the OptimizerImpl to which this Optimizable belongs.
planKey
)

When remembering "truly the best" access path for an Optimizable, we have to keep track of which OptimizerImpl the "truly the best" access is for.

public void
verifyProperties(DataDictionary
The DataDictionary to use.
dDictionary
)

Verify that the Properties list with optimizer overrides, if specified, is valid

Inherited from org.apache.derby.iapi.sql.compile.Visitable:
acceptaddTagtaggedWith

Method Detail

considerSortAvoidancePathback to summary
public boolean considerSortAvoidancePath()

Check whether this optimizable's sort avoidance path should be considered.

convertAbsoluteToRelativeColumnPositionback to summary
public int convertAbsoluteToRelativeColumnPosition(int absolutePosition)

Convert an absolute to a relative 0-based column position. This is useful when generating qualifiers for partial rows from the store.

Parameters
absolutePosition:int

The absolute 0-based column position for the column.

Returns:int

The relative 0-based column position for the column.

estimateCostback to summary
public CostEstimate estimateCost(OptimizablePredicateList predList, ConglomerateDescriptor cd, CostEstimate outerCost, Optimizer optimizer, RowOrdering rowOrdering) throws StandardException

Estimate the cost of scanning this Optimizable using the given predicate list with the given conglomerate. It is assumed that the predicate list has already been classified. This cost estimate is just for one scan, not for the life of the query.

Parameters
predList:OptimizablePredicateList

The predicate list to optimize against

cd:ConglomerateDescriptor

The conglomerate descriptor to get the cost of

outerCost:CostEstimate

The estimated cost of the part of the plan outer to this optimizable.

optimizer:Optimizer

The optimizer to use to help estimate the cost

rowOrdering:RowOrdering

The row ordering for all the tables in the join order, including this one.

Returns:CostEstimate

The estimated cost of doing the scan

Exceptions
StandardException:
Thrown on error
See Also
OptimizablePredicateList#classify
feasibleJoinStrategyback to summary
public boolean feasibleJoinStrategy(OptimizablePredicateList predList, Optimizer optimizer) throws StandardException

Is the current proposed join strategy for this optimizable feasible given the predicate list?

Parameters
predList:OptimizablePredicateList

The predicate list that has been pushed down to this optimizable

optimizer:Optimizer

The optimizer to use.

Returns:boolean

true means feasible

Exceptions
StandardException:
Thrown on error
forUpdateback to summary
public boolean forUpdate()

Return true if this is the target table of an update

getBaseTableNameback to summary
public String getBaseTableName()

Get the table name of this Optimizable. Only base tables have table names (by the time we use this method, all views will have been resolved).

getBestAccessPathback to summary
public AccessPath getBestAccessPath()

Get the best access path for this Optimizable.

getBestSortAvoidancePathback to summary
public AccessPath getBestSortAvoidancePath()

Get the best sort-avoidance path for this Optimizable.

getCurrentAccessPathback to summary
public AccessPath getCurrentAccessPath()

Get the current access path under consideration for this Optimizable

getDataDictionaryback to summary
public DataDictionary getDataDictionary() throws StandardException

Get the DataDictionary from this Optimizable. This is useful for code generation because we need to get the constraint name if scanning a back index so that RunTimeStatistics can display the correct info.

Returns:DataDictionary

The DataDictionary to use.

Exceptions
StandardException:
Thrown on error
getNameback to summary
public String getName() throws StandardException

Get the (exposed) name of this Optimizable

Returns:String

The name of this Optimizable.

Exceptions
StandardException:
Thrown on error
getNumColumnsReturnedback to summary
public int getNumColumnsReturned()

Get the number of the number of columns returned by this Optimizable.

Returns:int

The number of the number of columns returned by this Optimizable.

getOptimizerTracerback to summary
public OptTrace getOptimizerTracer()

Get the optimizer tracer, if any

getPropertiesback to summary
public Properties getProperties()

Get the Properties list, if any, associated with this optimizable.

Returns:Properties

The Properties list, if any, associated with this optimizable.

getReferencedTableMapback to summary
public JBitSet getReferencedTableMap()

Get the map of referenced tables for this Optimizable.

Returns:JBitSet

JBitSet Referenced table map.

getResultSetNumberback to summary
public int getResultSetNumber()

Get this Optimizable's result set number

getTableDescriptorback to summary
public TableDescriptor getTableDescriptor()

Get the table descriptor for this table (if any). Only base tables have table descriptors - for the rest of the optimizables, this method returns null.

getTableNumberback to summary
public int getTableNumber()

Get this Optimizable's table number

getTrulyTheBestAccessPathback to summary
public AccessPath getTrulyTheBestAccessPath()

Get the best access path overall for this Optimizable.

hashKeyColumnsback to summary
public int[] hashKeyColumns()

Return the hash key column numbers, for hash join strategy

hasLargeObjectColumnsback to summary
public boolean hasLargeObjectColumns()

Tell whether this Optimizable has any large object (LOB) columns.

hasTableNumberback to summary
public boolean hasTableNumber()

Return true if this Optimizable has a table number

initAccessPathsback to summary
public void initAccessPaths(Optimizer optimizer)

Init the access paths for this optimizable.

Parameters
optimizer:Optimizer

The optimizer being used.

initialCapacityback to summary
public int initialCapacity()

Return the initial capacity of the hash table, for hash join strategy

isBaseTableback to summary
public boolean isBaseTable()

Tell whether this Optimizable represents a base table

isCoveringIndexback to summary
public boolean isCoveringIndex(ConglomerateDescriptor cd) throws StandardException

Return whether or not this is a covering index. We expect to call this during generation, after access path selection is complete.

Parameters
cd:ConglomerateDescriptor

ConglomerateDesriptor for index to consider

Returns:boolean

boolean Whether or not this is a covering index.

Exceptions
StandardException:
Thrown on error
isMaterializableback to summary
public boolean isMaterializable() throws StandardException

Tell whether this Optimizable is materializable

Exceptions
StandardException:
Thrown on error
isOneRowScanback to summary
public boolean isOneRowScan() throws StandardException

Will the optimizable return at most 1 row per scan?

Returns:boolean

Whether or not the optimizable will return at most 1 row per scan?

Exceptions
StandardException:
Thrown on error
isTargetTableback to summary
public boolean isTargetTable()

Is the optimizable the target table of an update or delete?

Returns:boolean

Whether or not the optimizable the target table of an update or delete.

legalJoinOrderback to summary
public boolean legalJoinOrder(JBitSet assignedTableMap)

Can this Optimizable appear at the current location in the join order. In other words, have the Optimizable's dependencies been satisfied?

Parameters
assignedTableMap:JBitSet

The tables that have been placed so far in the join order.

Returns:boolean

Where or not this Optimizable can appear at the current location in the join order.

loadFactorback to summary
public float loadFactor()

Return the load factor of the hash table, for hash join strategy

maxCapacityback to summary
public int maxCapacity(JoinStrategy joinStrategy, int maxMemoryPerTable) throws StandardException

Return the maximum capacity of the hash table, for hash join strategy

Parameters
maxMemoryPerTable:int

The maximum number of bytes to be used. Ignored if the user has set a maximum number of rows for the Optimizable.

Exceptions
StandardException:
Standard error policy
memoryUsageOKback to summary
public boolean memoryUsageOK(double rowCount, int maxMemoryPerTable) throws StandardException
Returns:boolean

true if the memory usage of the proposed access path is OK, false if not.

Exceptions
StandardException:
standard error policy
modifyAccessPathback to summary
public Optimizable modifyAccessPath(JBitSet outerTables) throws StandardException

Modify the access path for this Optimizable, as necessary. This includes things like adding a result set to translate from index rows to base rows

Parameters
outerTables:JBitSet

Bit map of the tables that are outer to this one in the join order.

Returns:Optimizable

The (potentially new) Optimizable at the top of the tree.

Exceptions
StandardException:
Thrown on error
nextAccessPathback to summary
public boolean nextAccessPath(Optimizer optimizer, OptimizablePredicateList predList, RowOrdering rowOrdering) throws StandardException

Choose the next access path to evaluate for this Optimizable.

Parameters
optimizer:Optimizer

Optimizer to use.

predList:OptimizablePredicateList

The predicate list for this optimizable. The optimizer always passes null, and it is up to the optimizable object to pass along its own predicate list, if appropriate, when delegating this method.

rowOrdering:RowOrdering

The row ordering for all the outer tables in the join order. This method will add the ordering of the next access path to the given RowOrdering.

Returns:boolean

true means another access path was chosen, false means no more access paths to evaluate.

Exceptions
StandardException:
Thrown on error
optimizeItback to summary
public CostEstimate optimizeIt(Optimizer optimizer, OptimizablePredicateList predList, CostEstimate outerCost, RowOrdering rowOrdering) throws StandardException

Choose the best access path for this Optimizable.

Parameters
optimizer:Optimizer

Optimizer to use.

predList:OptimizablePredicateList

The predicate list to optimize against

outerCost:CostEstimate

The CostEstimate for the outer tables in the join order, telling how many times this Optimizable will be scanned.

rowOrdering:RowOrdering

The row ordering for all the tables in the join order, including this one.

Returns:CostEstimate

The optimizer's estimated cost of the best access path.

Exceptions
StandardException:
Thrown on error
optimizerTracingIsOnback to summary
public boolean optimizerTracingIsOn()

Report whether optimizer tracing is on

pullOptPredicatesback to summary
public void pullOptPredicates(OptimizablePredicateList optimizablePredicates) throws StandardException

Pull all the OptimizablePredicates from this Optimizable and put them in the given OptimizablePredicateList.

Parameters
optimizablePredicates:OptimizablePredicateList

The list to put the pulled predicates in.

Exceptions
StandardException:
Thrown on error
pushOptPredicateback to summary
public boolean pushOptPredicate(OptimizablePredicate optimizablePredicate) throws StandardException

Push an OptimizablePredicate down, if this node accepts it.

Parameters
optimizablePredicate:OptimizablePredicate

OptimizablePredicate to push down.

Returns:boolean

Whether or not the predicate was pushed down.

Exceptions
StandardException:
Thrown on error
rememberAsBestback to summary
public void rememberAsBest(int planType, Optimizer optimizer) throws StandardException

Remember the current access path as the best one (so far).

Parameters
planType:int

The type of plan (one of Optimizer.NORMAL_PLAN or Optimizer.SORT_AVOIDANCE_PLAN)

optimizer:Optimizer

The OptimizerImpl that is telling this Optimizable to remember its current path as "truly the best".

Exceptions
StandardException:
thrown on error.
rememberJoinStrategyAsBestback to summary
public void rememberJoinStrategyAsBest(AccessPath ap)

Remember the current join strategy as the best one so far in this join order.

rememberSortAvoidancePathback to summary
public void rememberSortAvoidancePath()

Mark this optimizable so that its sort avoidance path will be considered.

setHashKeyColumnsback to summary
public void setHashKeyColumns(int[] columnNumbers)

Set the hash key column numbers, for hash join strategy

setPropertiesback to summary
public void setProperties(Properties tableProperties)

Set the Properties list for this optimizalbe.

Parameters
tableProperties:Properties

The Properties list for this optimizable.

startOptimizingback to summary
public void startOptimizing(Optimizer optimizer, RowOrdering rowOrdering)

Begin the optimization process for this Optimizable. This can be called many times for an Optimizable while optimizing a query - it will typically be called every time the Optimizable is placed in a potential join order.

supportsMultipleInstantiationsback to summary
public boolean supportsMultipleInstantiations()

Tell whether this Optimizable can be instantiated multiple times

uniqueJoinback to summary
public double uniqueJoin(OptimizablePredicateList predList) throws StandardException

Does this optimizable have a uniqueness condition on the given predicate list, and if so, how many unique keys will be returned per scan.

Parameters
predList:OptimizablePredicateList

The predicate list to check

Returns:double

<= 0 means there is no uniqueness condition > 0 means there is a uniqueness condition, and the return value is the number of rows per scan.

Exceptions
StandardException:
Thrown on error
updateBestPlanMapback to summary
public void updateBestPlanMap(short action, Object planKey) throws StandardException

When remembering "truly the best" access path for an Optimizable, we have to keep track of which OptimizerImpl the "truly the best" access is for. In most queries there will only be one OptimizerImpl in question, but in cases where there are nested subqueries, there will be one OptimizerImpl for every level of nesting, and each OptimizerImpl might have its own idea of what this Optimizable's "truly the best path" access path really is. In addition, there could be Optimizables above this Optimizable that might need to override the best path chosen during optimization. So whenever we save a "truly the best" path, we take note of which Optimizer/Optimizable told us to do so. Then as each level of subquery finishes optimization, the corresponding OptimizerImpl/Optimizable can load its preferred access path into this Optimizable's trulyTheBestAccessPath field and pass it up the tree, until eventually the outer-most OptimizerImpl can choose to either use the best path that it received from below (by calling "rememberAsBest()") or else use the path that it found to be "best" for itself. This method is what allows us to keep track of which OptimizerImpl or Optimizable saved which "best plan", and allows us to load the appropriate plans after each round of optimization.

Parameters
action:short

Indicates whether we're adding, loading, or removing a best plan for the OptimizerImpl/Optimizable.

planKey:Object

Object to use as the map key when adding/looking up a plan. If it is an instance of OptimizerImpl then it corresponds to an outer query; otherwise it's some Optimizable above this Optimizable that could potentially reject plans chosen by the OptimizerImpl to which this Optimizable belongs.

verifyPropertiesback to summary
public void verifyProperties(DataDictionary dDictionary) throws StandardException

Verify that the Properties list with optimizer overrides, if specified, is valid

Parameters
dDictionary:DataDictionary

The DataDictionary to use.

Exceptions
StandardException:
Thrown on error