use of org.apache.derby.iapi.sql.compile.Optimizable in project derby by apache.
the class IntersectOrExceptNode method modifyAccessPath.
// End of estimateCost
/**
* @see Optimizable#modifyAccessPath
*
* @exception StandardException Thrown on error
*/
@Override
public Optimizable modifyAccessPath(JBitSet outerTables) throws StandardException {
Optimizable retOptimizable;
retOptimizable = super.modifyAccessPath(outerTables);
/* We only want call addNewNodes() once */
if (addNewNodesCalled) {
return retOptimizable;
}
return (Optimizable) addNewNodes();
}
use of org.apache.derby.iapi.sql.compile.Optimizable in project derby by apache.
the class OrderByList method sortRequired.
/**
* @see RequiredRowOrdering#sortRequired(RowOrdering, JBitSet, OptimizableList, int[])
*
* @exception StandardException Thrown on error
*/
public int sortRequired(RowOrdering rowOrdering, JBitSet tableMap, OptimizableList optimizableList, int[] proposedJoinOrder) throws StandardException {
/*
** Currently, all indexes are ordered ascending, so a descending
** ORDER BY always requires a sort.
*/
if (alwaysSort) {
return RequiredRowOrdering.SORT_REQUIRED;
}
/*
** Step through the columns in this list, and ask the
** row ordering whether it is ordered on each column.
*/
int position = 0;
int size = size();
for (int loc = 0; loc < size; loc++) {
OrderByColumn obc = getOrderByColumn(loc);
//
if (obc.isNullsOrderedLow())
return RequiredRowOrdering.SORT_REQUIRED;
// ResultColumn rc = obc.getResultColumn();
/*
** This presumes that the OrderByColumn refers directly to
** the base column, i.e. there is no intervening VirtualColumnNode.
*/
// ValueNode expr = obc.getNonRedundantExpression();
ValueNode expr = obc.getResultColumn().getExpression();
if (!(expr instanceof ColumnReference)) {
return RequiredRowOrdering.SORT_REQUIRED;
}
ColumnReference cr = (ColumnReference) expr;
/*
** Check whether the table referred to is in the table map (if any).
** If it isn't, we may have an ordering that does not require
** sorting for the tables in a partial join order. Look for
** columns beyond this column to see whether a referenced table
** is found - if so, sorting is required (for example, in a
** case like ORDER BY S.A, T.B, S.C, sorting is required).
*/
if (tableMap != null) {
if (!tableMap.get(cr.getTableNumber())) {
/* Table not in partial join order */
for (int remainingPosition = loc + 1; remainingPosition < size(); remainingPosition++) {
OrderByColumn remainingobc = getOrderByColumn(loc);
ResultColumn remainingrc = remainingobc.getResultColumn();
ValueNode remainingexpr = remainingrc.getExpression();
if (remainingexpr instanceof ColumnReference) {
ColumnReference remainingcr = (ColumnReference) remainingexpr;
if (tableMap.get(remainingcr.getTableNumber())) {
return RequiredRowOrdering.SORT_REQUIRED;
}
}
}
return RequiredRowOrdering.NOTHING_REQUIRED;
}
}
/*
* Does this order by column belong to the outermost optimizable in
* the current join order?
*
* If yes, then we do not need worry about the ordering of the rows
* feeding into it. Because the order by column is associated with
* the outermost optimizable, optimizer will not have to deal with
* the order of any rows coming in from the previous optimizables.
*
* But if the current order by column belongs to an inner
* optimizable in the join order, then go through the following
* if condition logic.
*/
/* If the following boolean is true, then it means that the join
* order being considered has more than one table
*/
boolean moreThanOneTableInJoinOrder = tableMap != null ? (!tableMap.hasSingleBitSet()) : false;
if (moreThanOneTableInJoinOrder) {
/*
* First check if the order by column has a constant comparison
* predicate on it or it belongs to an optimizable which is
* always ordered(that means it is a single row table) or the
* column is involved in an equijoin with an optimizable which
* is always ordered on the column on which the equijoin is
* happening. If yes, then we know that the rows will always be
* sorted and hence we do not need to worry if (any) prior
* optimizables in join order are one-row resultsets or not.
*/
if ((!rowOrdering.alwaysOrdered(cr.getTableNumber())) && (!rowOrdering.isColumnAlwaysOrdered(cr.getTableNumber(), cr.getColumnNumber()))) {
for (int i = 0; i < proposedJoinOrder.length && // -1: partial order
proposedJoinOrder[i] != -1; i++) {
// Get one outer optimizable at a time from the join
// order
Optimizable considerOptimizable = optimizableList.getOptimizable(proposedJoinOrder[i]);
// avoidance.
if (considerOptimizable.getTableNumber() == cr.getTableNumber())
break;
/*
* The following if condition is checking if the
* outer optimizable to the order by column's
* optimizable is one row resultset or not.
*
* If the outer optimizable is one row resultset,
* then move on to the next optimizable in the join
* order and do the same check on that optimizable.
* Continue this until we are done checking that all
* the outer optimizables in the join order are single
* row resultsets. If we run into an outer optimizable
* which is not one row resultset, then we can not
* consider sort avoidance for the query.
*/
if (rowOrdering.alwaysOrdered(considerOptimizable.getTableNumber()))
continue;
else
// sorting for this query.
return RequiredRowOrdering.SORT_REQUIRED;
}
}
}
if (!rowOrdering.alwaysOrdered(cr.getTableNumber())) {
/*
** Check whether the ordering is ordered on this column in
** this position.
*/
if (!rowOrdering.orderedOnColumn(obc.isAscending() ? RowOrdering.ASCENDING : RowOrdering.DESCENDING, position, cr.getTableNumber(), cr.getColumnNumber())) {
return RequiredRowOrdering.SORT_REQUIRED;
}
/*
** The position to ask about is for the columns in tables
** that are *not* always ordered. The always-ordered tables
** are not counted as part of the list of ordered columns
*/
position++;
}
}
return RequiredRowOrdering.NOTHING_REQUIRED;
}
use of org.apache.derby.iapi.sql.compile.Optimizable in project derby by apache.
the class GroupByNode method estimateCost.
/**
* @see Optimizable#estimateCost
*
* @exception StandardException Thrown on error
*/
@Override
public CostEstimate estimateCost(OptimizablePredicateList predList, ConglomerateDescriptor cd, CostEstimate outerCost, Optimizer optimizer, RowOrdering rowOrdering) throws StandardException {
// RESOLVE: NEED TO FACTOR IN THE COST OF GROUPING (SORTING) HERE
//
CostEstimate childCost = ((Optimizable) childResult).estimateCost(predList, cd, outerCost, optimizer, rowOrdering);
CostEstimate costEst = getCostEstimate(optimizer);
costEst.setCost(childCost.getEstimatedCost(), childCost.rowCount(), childCost.singleScanRowCount());
return costEst;
}
Aggregations