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.
Comparator class for GROUP BY expression substitution. |
| Modifier and Type | Field and Description |
|---|---|
| private boolean | |
| private int | |
| private AggregatorInfoList | aggInfo
Information that is used at execution time to process aggregates. |
| private List | aggregates
The list of all aggregates in the query block that contains this group by. |
| pack-priv GroupByList | groupingList
The GROUP BY list |
| private ValueNode | |
| private SubqueryList | |
| private final boolean | |
| pack-priv FromTable | parent
The parent to the GroupByNode. |
| private boolean |
| Access | Constructor and Description |
|---|---|
| pack-priv | GroupByNode(ResultSetNode
The child FromTable bottomPR, GroupByList The groupingList groupingList, List<AggregateNode> The list of aggregates from
the query block. Since aggregation is done
at the same time as grouping, we need them
here. aggregates, ValueNode The having clause. havingClause, SubqueryList subqueries in the having clause. havingSubquerys, int NestingLevel of this group by node. This is used for
error checking of group by queries with having clause. nestingLevel, ContextManager The context manager cm)Constructor for a GroupByNode. |
| Modifier and Type | Method and Description |
|---|---|
| private void | |
| private void | |
| private void | |
| private void | |
| private void | |
| private ArrayList | Returns: havingRefsToSubstitute visitors array. Return any havingRefsToSubstitute visitors since it is too early to apply them yet; we need the AggregateNodes unmodified until after we add the new columns for aggregation (DERBY-4071).In the query rewrite for group by, add the columns on which we are doing the group by. |
| pack-priv void | considerPostOptimizeOptimizations(boolean
true if SELECT containing this
vector/scalar aggregate has a restriction selectHasPredicates)Consider any optimizations after the optimizer has chosen a plan. |
| public CostEstimate | 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)Overrides org. Implements org. Estimate the cost of scanning this Optimizable using the given predicate list with the given conglomerate. |
| pack-priv boolean | Returns: boolean Whether or not the FromSubquery is flattenable.The outer from list fromList)Overrides org. Evaluate whether or not the subquery in a FromSubquery is flattenable. |
| pack-priv void | generate(ActivationClassBuilder
The ActivationClassBuilder for the class being built acb, MethodBuilder The method for the generated code to go into mb)Overrides org. generate the sort result set operating over the source result set. |
| 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 | Returns: the new result columnthe source targetRC, DataDictionary dd)Method for creating a new result column referencing the one passed in. |
| pack-priv boolean | Returns: Whether or not the source is in sorted order.Get whether or not the source is in sorted order. |
| pack-priv final FromTable | |
| pack-priv boolean | Returns: Whether or not the underlying ResultSet tree will return a single row.Overrides org. Return whether or not the underlying ResultSet tree will return a single row, at most. |
| pack-priv ResultColumnDescriptor[] | |
| pack-priv ResultSetNode | Returns: ResultSetNode The top of the optimized subtreeThe DataDictionary to use for optimization dataDictionary, PredicateList The PredicateList to optimize. This should
be a join predicate. predicates, double The number of outer joining rows outerRows)Overrides org. Optimize this GroupByNode. |
| public CostEstimate | 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)Overrides org. Implements org. Choose the best access path for this Optimizable. |
| pack-priv void | printSubNodes(int
The depth of this node in the tree depth)Overrides org. Prints the sub-nodes of this object. |
| public boolean | pushOptPredicate(OptimizablePredicate
OptimizablePredicate to push down. optimizablePredicate)Overrides org. Implements org. Push an OptimizablePredicate down, if this node accepts it. |
| public String | Returns: This object as a StringOverrides org. Convert this object to a String. |
| addDistinctAggregate | back to summary |
|---|---|
| private boolean addDistinctAggregate | |
| addDistinctAggregateColumnNum | back to summary |
|---|---|
| private int addDistinctAggregateColumnNum | |
| aggInfo | back to summary |
|---|---|
| private AggregatorInfoList aggInfo Information that is used at execution time to process aggregates. | |
| aggregates | back to summary |
|---|---|
| private List<AggregateNode> aggregates The list of all aggregates in the query block that contains this group by. | |
| groupingList | back to summary |
|---|---|
| pack-priv GroupByList groupingList The GROUP BY list | |
| havingClause | back to summary |
|---|---|
| private ValueNode havingClause | |
| havingSubquerys | back to summary |
|---|---|
| private SubqueryList havingSubquerys | |
| isInSortedOrder | back to summary |
|---|---|
| private final boolean isInSortedOrder | |
| parent | back to summary |
|---|---|
| pack-priv FromTable parent The parent to the GroupByNode. If we need to generate a ProjectRestrict over the group by then this is set to that node. Otherwise it is null. | |
| singleInputRowOptimization | back to summary |
|---|---|
| private boolean singleInputRowOptimization | |
| GroupByNode | back to summary |
|---|---|
| pack-priv GroupByNode(ResultSetNode bottomPR, GroupByList groupingList, List<AggregateNode> aggregates, ValueNode havingClause, SubqueryList havingSubquerys, int nestingLevel, ContextManager cm) throws StandardException Constructor for a GroupByNode.
| |
| addAggregateColumns | back to summary |
|---|---|
| private void addAggregateColumns() throws StandardException In the query rewrite involving aggregates, add the columns for aggregation.
| |
| addAggregates | back to summary |
|---|---|
| private void addAggregates() throws StandardException Add the extra result columns required by the aggregates to the result list.
| |
| addDistinctAggregatesToOrderBy | back to summary |
|---|---|
| private void addDistinctAggregatesToOrderBy() Add any distinct aggregates to the order by list. Asserts that there are 0 or more distincts. | |
| addNewColumnsForAggregation | back to summary |
|---|---|
| private void addNewColumnsForAggregation() throws StandardException Add a whole slew of columns needed for aggregation. Basically, for each aggregate we add 3 columns: the aggregate input expression and the aggregator column and a column where the aggregate result is stored. The input expression is taken directly from the aggregator node. The aggregator is the run time aggregator. We add it to the RC list as a new object coming into the sort node. At this point this is invoked, we have the following tree:
For each ColumnReference in PR RCL
aggregates
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), <agg-input>,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.
| |
| addNewPRNode | back to summary |
|---|---|
| private void addNewPRNode() throws StandardException Add a new PR node for aggregation. Put the new PR under the sort.
| |
| addUnAggColumns | back to summary |
|---|---|
| private ArrayList In the query rewrite for group by, add the columns on which we are doing the group by.
| |
| considerPostOptimizeOptimizations | back to summary |
|---|---|
| pack-priv void considerPostOptimizeOptimizations(boolean selectHasPredicates) throws StandardException Consider any optimizations after the optimizer has chosen a plan. Optimizations include: o min optimization for scalar aggregates o max optimization for scalar aggregates
| |
| estimateCost | back to summary |
|---|---|
| public CostEstimate estimateCost(OptimizablePredicateList predList, ConglomerateDescriptor cd, CostEstimate outerCost, Optimizer optimizer, RowOrdering rowOrdering) throws StandardException Overrides org. Implements org. Doc from org. 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.
| |
| flattenableInFromSubquery | back to summary |
|---|---|
| pack-priv boolean flattenableInFromSubquery(FromList fromList) Overrides org. Evaluate whether or not the subquery in a FromSubquery is flattenable. Currently, a FSqry is flattenable if all of the following are true: o Subquery is a SelectNode. o It contains no top level subqueries. (RESOLVE - we can relax this) o It does not contain a group by or having clause o It does not contain aggregates. | |
| generate | back to summary |
|---|---|
| pack-priv void generate(ActivationClassBuilder acb, MethodBuilder mb) throws StandardException Overrides org. generate the sort result set operating over the source result set. Adds distinct aggregates to the sort if necessary.
| |
| genGroupedAggregateResultSet | back to summary |
|---|---|
| private void genGroupedAggregateResultSet(ActivationClassBuilder acb, MethodBuilder mb) throws StandardException Generate the code to evaluate grouped aggregates. | |
| genScalarAggregateResultSet | back to summary |
|---|---|
| private void genScalarAggregateResultSet(ActivationClassBuilder acb, MethodBuilder mb) Generate the code to evaluate scalar aggregates. | |
| getColumnReference | back to summary |
|---|---|
| private ResultColumn getColumnReference(ResultColumn targetRC, DataDictionary dd) throws StandardException Method for creating a new result column referencing the one passed in.
| |
| getIsInSortedOrder | back to summary |
|---|---|
| pack-priv boolean getIsInSortedOrder() Get whether or not the source is in sorted order.
| |
| getParent | back to summary |
|---|---|
| pack-priv final FromTable getParent() Return the parent node to this one, if there is one. It will return 'this' if there is no generated node above this one.
| |
| isOneRowResultSet | back to summary |
|---|---|
| pack-priv boolean isOneRowResultSet() throws StandardException Overrides org. Return whether or not the underlying ResultSet tree will return a single row, at most. This is important for join nodes where we can save the extra next on the right side if we know that it will return at most 1 row.
| |
| makeResultDescriptors | back to summary |
|---|---|
| pack-priv ResultColumnDescriptor[] makeResultDescriptors() Overrides org.
| |
| optimize | back to summary |
|---|---|
| pack-priv ResultSetNode optimize(DataDictionary dataDictionary, PredicateList predicates, double outerRows) throws StandardException Overrides org. Optimize this GroupByNode.
| |
| optimizeIt | back to summary |
|---|---|
| public CostEstimate optimizeIt(Optimizer optimizer, OptimizablePredicateList predList, CostEstimate outerCost, RowOrdering rowOrdering) throws StandardException Overrides org. Implements org. Doc from org. Choose the best access path for this Optimizable.
| |
| printSubNodes | back to summary |
|---|---|
| pack-priv void printSubNodes(int depth) Overrides org. Prints the sub-nodes of this object. See QueryTreeNode.java for how tree printing is supposed to work.
| |
| pushOptPredicate | back to summary |
|---|---|
| public boolean pushOptPredicate(OptimizablePredicate optimizablePredicate) throws StandardException Overrides org. Implements org. Doc from org. Push an OptimizablePredicate down, if this node accepts it.
| |
| toString | back to summary |
|---|---|
| public String toString() Overrides org. Convert this object to a String. See comments in QueryTreeNode.java for how this should be done for tree printing. | |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| public int | compare(SubstituteExpressionVisitor
the first object to be compared. o1, SubstituteExpressionVisitor the second object to be compared. o2)Implements java. Compares its two arguments for order. |
| ExpressionSorter | back to summary |
|---|---|
| private ExpressionSorter() | |
| compare | back to summary |
|---|---|
| public int compare(SubstituteExpressionVisitor o1, SubstituteExpressionVisitor o2) Implements java. Doc from java. Compares its two arguments for order. Returns a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.
The implementor must ensure that
The implementor must also ensure that the relation is transitive:
Finally, the implementor must ensure that
| |