public class GroupByNode extends SingleChildResultSetNode
For the most part, it simply delegates operations to its bottomPRSet, which is currently expected to be a ProjectRestrictResultSet generated for a SelectNode.
NOTE: A GroupByNode extends FromTable since it can exist in a FromList.
There is a lot of room for optimizations here:
| Modifier and Type | Class and Description |
|---|---|
private static class |
GroupByNode.ExpressionSorter
Comparator class for GROUP BY expression substitution.
|
| Modifier and Type | Field and Description |
|---|---|
private boolean |
addDistinctAggregate |
private int |
addDistinctAggregateColumnNum |
private AggregatorInfoList |
aggInfo
Information that is used at execution time to
process aggregates.
|
private java.util.List |
aggregateVector
The list of all aggregates in the query block
that contains this group by.
|
(package private) GroupByList |
groupingList
The GROUP BY list
|
private ValueNode |
havingClause |
private SubqueryList |
havingSubquerys |
private boolean |
isInSortedOrder |
(package private) FromTable |
parent
The parent to the GroupByNode.
|
private boolean |
singleInputRowOptimization |
childResult, hasTrulyTheBestAccessPathADD_PLAN, bestAccessPath, bestCostEstimate, bestSortAvoidancePath, correlationName, corrTableName, currentAccessPath, hashKeyColumns, initialCapacity, level, LOAD_PLAN, loadFactor, maxCapacity, origTableName, REMOVE_PLAN, tableNumber, tableProperties, trulyTheBestAccessPath, userSpecifiedJoinStrategycostEstimate, cursorTargetTable, finalCostEstimate, insertSource, optimizer, referencedTableMap, resultColumns, resultSetNumber, scratchCostEstimate, statementResultSetAUTOINCREMENT_CREATE_MODIFY, AUTOINCREMENT_INC_INDEX, AUTOINCREMENT_IS_AUTOINCREMENT_INDEX, AUTOINCREMENT_START_INDEX| Constructor and Description |
|---|
GroupByNode() |
| Modifier and Type | Method and Description |
|---|---|
private void |
addAggregateColumns()
In the query rewrite involving aggregates, add the columns for
aggregation.
|
private void |
addAggregates()
Add the extra result columns required by the aggregates
to the result list.
|
private void |
addDistinctAggregatesToOrderBy()
Add any distinct aggregates to the order by list.
|
private void |
addNewColumnsForAggregation()
Add a whole slew of columns needed for
aggregation.
|
private void |
addNewPRNode()
Add a new PR node for aggregation.
|
private java.util.ArrayList |
addUnAggColumns()
In the query rewrite for group by, add the columns on which we are doing
the group by.
|
(package private) void |
considerPostOptimizeOptimizations(boolean selectHasPredicates)
Consider any optimizations after the optimizer has chosen a plan.
|
CostEstimate |
estimateCost(OptimizablePredicateList predList,
ConglomerateDescriptor cd,
CostEstimate outerCost,
Optimizer optimizer,
RowOrdering rowOrdering)
Estimate the cost of scanning this Optimizable using the given
predicate list with the given conglomerate.
|
boolean |
flattenableInFromSubquery(FromList fromList)
Evaluate whether or not the subquery in a FromSubquery is flattenable.
|
void |
generate(ActivationClassBuilder acb,
MethodBuilder mb)
generate the sort result set operating over the source
resultset.
|
private void |
genGroupedAggregateResultSet(ActivationClassBuilder acb,
MethodBuilder mb)
Generate the code to evaluate grouped aggregates.
|
private void |
genScalarAggregateResultSet(ActivationClassBuilder acb,
MethodBuilder mb)
Generate the code to evaluate scalar aggregates.
|
private ResultColumn |
getColumnReference(ResultColumn targetRC,
DataDictionary dd)
Method for creating a new result column referencing
the one passed in.
|
(package private) boolean |
getIsInSortedOrder()
Get whether or not the source is in sorted order.
|
FromTable |
getParent()
Return the parent node to this one, if there is
one.
|
void |
init(java.lang.Object bottomPR,
java.lang.Object groupingList,
java.lang.Object aggregateVector,
java.lang.Object havingClause,
java.lang.Object havingSubquerys,
java.lang.Object tableProperties,
java.lang.Object nestingLevel)
Intializer for a GroupByNode.
|
boolean |
isOneRowResultSet()
Return whether or not the underlying ResultSet tree will return
a single row, at most.
|
(package private) ResultColumnDescriptor[] |
makeResultDescriptors() |
ResultSetNode |
optimize(DataDictionary dataDictionary,
PredicateList predicates,
double outerRows)
Optimize this GroupByNode.
|
CostEstimate |
optimizeIt(Optimizer optimizer,
OptimizablePredicateList predList,
CostEstimate outerCost,
RowOrdering rowOrdering)
Choose the best access path for this Optimizable.
|
void |
printSubNodes(int depth)
Prints the sub-nodes of this object.
|
boolean |
pushOptPredicate(OptimizablePredicate optimizablePredicate)
Push an OptimizablePredicate down, if this node accepts it.
|
java.lang.String |
toString()
Convert this object to a String.
|
acceptChildren, addNewPredicate, adjustForSortElimination, adjustForSortElimination, changeAccessPath, decrementLevel, ensurePredicateList, forUpdate, getChildResult, getFinalCostEstimate, getFromTableByName, getTrulyTheBestAccessPath, init, initAccessPaths, isNotExists, isOrderedOn, modifyAccessPaths, preprocess, pullOptPredicates, pushExpressions, referencesSessionSchema, referencesTarget, reflectionNeededForProjection, setChildResult, setLevel, subqueryReferencesTarget, updateBestPlanMap, updateTargetLockModeassignCostEstimate, canBeOrdered, considerSortAvoidancePath, convertAbsoluteToRelativeColumnPosition, cursorTargetTable, feasibleJoinStrategy, fillInReferencedTableMap, flatten, getBaseTableName, getBestAccessPath, getBestSortAvoidancePath, getCorrelationName, getCostEstimate, getCurrentAccessPath, getExposedName, getLevel, getName, getNumColumnsReturned, getOrigTableName, getProperties, getResultColumnsForList, getSchemaDescriptor, getSchemaDescriptor, getScratchCostEstimate, getTableDescriptor, getTableName, getTableNumber, getUserSpecifiedJoinStrategy, hashKeyColumns, hasLargeObjectColumns, hasTableNumber, initialCapacity, isBaseTable, isCoveringIndex, isFlattenableJoinNode, isJoinColumnForRightOuterJoin, isMaterializable, isOneRowScan, isTargetTable, legalJoinOrder, loadFactor, LOJ_reorderable, markUpdatableByCursor, maxCapacity, memoryUsageOK, modifyAccessPath, needsSpecialRCLBinding, nextAccessPath, optimizeSubqueries, rememberAsBest, rememberJoinStrategyAsBest, rememberSortAvoidancePath, resetJoinStrategies, setCostEstimate, setHashKeyColumns, setOrigTableName, setProperties, setTableNumber, startOptimizing, supportsMultipleInstantiations, tellRowOrderingAboutConstantColumns, transformOuterJoins, uniqueJoin, verifyPropertiesassignResultSetNumber, bindExpressions, bindExpressionsWithTables, bindNonVTITables, bindResultColumns, bindResultColumns, bindTargetExpressions, bindUntypedNullsToResultColumns, bindVTITables, columnTypesAndLengthsMatch, considerMaterialization, enhanceRCLForInsert, generateNormalizationResultSet, generateResultSet, genProjectRestrict, genProjectRestrict, genProjectRestrictForReordering, getAllResultColumns, getCostEstimate, getCursorTargetTable, getFromList, getMatchingColumn, getNewCostEstimate, getOptimizer, getOptimizerImpl, getRCLForInsert, getReferencedTableMap, getResultColumns, getResultSetNumber, isPossibleDistinctScan, isUpdatableCursor, LOJgetReferencedTables, makeResultDescription, markAsCursorTargetTable, markForDistinctScan, markStatementResultSet, modifyAccessPaths, notCursorTargetTable, notFlattenableJoin, numDistinctAggregates, parseDefault, performMaterialization, projectResultColumns, pushOffsetFetchFirst, pushOrderByList, rejectParameters, rejectXMLValues, renameGeneratedResultNames, replaceOrForbidDefaults, returnsAtMostOneRow, setInsertSource, setReferencedTableMap, setResultColumns, setResultToBooleanTrueNode, setTableConstructorTypes, verifySelectStarSubqueryaccept, bindOffsetFetch, bindRowMultiSet, bindUserCatalogType, 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, 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, resolveTableToSynonym, setBeginOffset, setContextManager, setEndOffset, setNodeType, setRefActionInfo, stackPrint, treePrint, treePrint, verifyClassExistclone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitgetDataDictionary, getReferencedTableMap, getResultSetNumberGroupByList groupingList
private java.util.List aggregateVector
private AggregatorInfoList aggInfo
FromTable parent
private boolean addDistinctAggregate
private boolean singleInputRowOptimization
private int addDistinctAggregateColumnNum
private boolean isInSortedOrder
private ValueNode havingClause
private SubqueryList havingSubquerys
public void init(java.lang.Object bottomPR,
java.lang.Object groupingList,
java.lang.Object aggregateVector,
java.lang.Object havingClause,
java.lang.Object havingSubquerys,
java.lang.Object tableProperties,
java.lang.Object nestingLevel)
throws StandardException
init in interface Nodeinit in class QueryTreeNodebottomPR - The child FromTablegroupingList - The groupingListaggregateVector - The vector of aggregates from
the query block. Since aggregation is done
at the same time as grouping, we need them
here.havingClause - The having clause.havingSubquerys - subqueries in the having clause.tableProperties - Properties list associated with the tablenestingLevel - nestingLevel of this group by node. This is used for
error checking of group by queries with having clause.StandardException - Thrown on errorboolean getIsInSortedOrder()
private void addAggregates()
throws StandardException
standard - exceptionStandardExceptionprivate void addDistinctAggregatesToOrderBy()
private void addNewPRNode()
throws StandardException
standard - exceptionStandardExceptionprivate java.util.ArrayList addUnAggColumns()
throws StandardException
StandardExceptionaddNewColumnsForAggregation()private void addNewColumnsForAggregation()
throws StandardException
At this point this is invoked, we have the following tree:
For each ColumnReference in PR RCL
For a query like,
select c1, sum(c2), max(c3)
from t1
group by c1;
the query tree ends up looking like this:
ProjectRestrictNode RCL -> (ptr to GBN(column[0]), ptr to GBN(column[1]), ptr to GBN(column[4]))
|
GroupByNode RCL->(C1, SUM(C2), , , MAX(C3), , )
|
ProjectRestrict RCL->(C1, C2, C3)
|
FromBaseTable
The RCL of the GroupByNode contains all the unagg (or grouping columns)
followed by 3 RC's for each aggregate in this order: the final computed
aggregate value, the aggregate input and the aggregator function.
The Aggregator function puts the results in the first of the 3 RC's and the PR resultset in turn picks up the value from there.
The notation (ptr to GBN(column[0])) basically means that it is a pointer to the 0th RC in the RCL of the GroupByNode.
The addition of these unagg and agg columns to the GroupByNode and to the PRN is performed in addUnAggColumns and addAggregateColumns.
Note that that addition of the GroupByNode is done after the query is optimized (in SelectNode#modifyAccessPaths) which means a fair amount of patching up is needed to account for generated group by columns.
standard - exceptionStandardExceptionprivate void addAggregateColumns()
throws StandardException
StandardExceptionaddNewColumnsForAggregation()public FromTable getParent()
public CostEstimate optimizeIt(Optimizer optimizer, OptimizablePredicateList predList, CostEstimate outerCost, RowOrdering rowOrdering) throws StandardException
OptimizableoptimizeIt in interface OptimizableoptimizeIt in class FromTableoptimizer - Optimizer to use.predList - The predicate list to optimize againstouterCost - The CostEstimate for the outer tables in the join order,
telling how many times this Optimizable will be scanned.rowOrdering - The row ordering for all the tables in the
join order, including this one.StandardException - Thrown on errorOptimizable.optimizeIt(org.apache.derby.iapi.sql.compile.Optimizer, org.apache.derby.iapi.sql.compile.OptimizablePredicateList, org.apache.derby.iapi.sql.compile.CostEstimate, org.apache.derby.iapi.sql.compile.RowOrdering)public CostEstimate estimateCost(OptimizablePredicateList predList, ConglomerateDescriptor cd, CostEstimate outerCost, Optimizer optimizer, RowOrdering rowOrdering) throws StandardException
OptimizableestimateCost in interface OptimizableestimateCost in class FromTablepredList - The predicate list to optimize againstcd - The conglomerate descriptor to get the cost ofouterCost - The estimated cost of the part of the plan outer
to this optimizable.optimizer - The optimizer to use to help estimate the costrowOrdering - The row ordering for all the tables in the
join order, including this one.StandardException - Thrown on errorOptimizable.estimateCost(org.apache.derby.iapi.sql.compile.OptimizablePredicateList, org.apache.derby.iapi.sql.dictionary.ConglomerateDescriptor, org.apache.derby.iapi.sql.compile.CostEstimate, org.apache.derby.iapi.sql.compile.Optimizer, org.apache.derby.iapi.sql.compile.RowOrdering)public boolean pushOptPredicate(OptimizablePredicate optimizablePredicate) throws StandardException
OptimizablepushOptPredicate in interface OptimizablepushOptPredicate in class FromTableoptimizablePredicate - OptimizablePredicate to push down.StandardException - Thrown on errorOptimizable.pushOptPredicate(org.apache.derby.iapi.sql.compile.OptimizablePredicate)public java.lang.String toString()
public void printSubNodes(int depth)
printSubNodes in class SingleChildResultSetNodedepth - The depth of this node in the treepublic boolean flattenableInFromSubquery(FromList fromList)
flattenableInFromSubquery in class SingleChildResultSetNodefromList - The outer from listpublic ResultSetNode optimize(DataDictionary dataDictionary, PredicateList predicates, double outerRows) throws StandardException
optimize in class SingleChildResultSetNodedataDictionary - The DataDictionary to use for optimizationpredicates - The PredicateList to optimize. This should
be a join predicate.outerRows - The number of outer joining rowsStandardException - Thrown on errorResultColumnDescriptor[] makeResultDescriptors()
makeResultDescriptors in class ResultSetNodepublic boolean isOneRowResultSet()
throws StandardException
isOneRowResultSet in class SingleChildResultSetNodeStandardException - Thrown on errorpublic void generate(ActivationClassBuilder acb, MethodBuilder mb) throws StandardException
generate in class QueryTreeNodeacb - The ActivationClassBuilder for the class being builtmb - The method for the generated code to go intoStandardException - Thrown on errorprivate void genScalarAggregateResultSet(ActivationClassBuilder acb, MethodBuilder mb)
private void genGroupedAggregateResultSet(ActivationClassBuilder acb, MethodBuilder mb) throws StandardException
StandardExceptionprivate ResultColumn getColumnReference(ResultColumn targetRC, DataDictionary dd) throws StandardException
targetRC - the sourcedd - StandardException - on errorvoid considerPostOptimizeOptimizations(boolean selectHasPredicates)
throws StandardException
selectHasPredicates - true if SELECT containing this
vector/scalar aggregate has a restrictionStandardException - on errorApache Derby V10.10 Internals - Copyright © 2004,2014 The Apache Software Foundation. All Rights Reserved.