private IQueryPlanNode PlanGroup(IQueryPlanNode node, GroupInfo groupInfo)
{
// If there is more than 1 aggregate function or there is a group by
// clause, then we must add a grouping plan.
if (groupInfo.Columns.AggregateCount > 0 ||
groupInfo.GroupByCount > 0) {
// If there is no GROUP BY clause then assume the entire result is the
// group.
if (groupInfo.GroupByCount == 0) {
node = new GroupNode(node, groupInfo.GroupMax, groupInfo.FunctionExpressions, groupInfo.FunctionNames);
} else {
// Do we have any group by functions that need to be planned first?
int gfsz = groupInfo.GroupByExpressions.Length;
if (gfsz > 0) {
var groupFunList = new SqlExpression[gfsz];
var groupFunName = new string[gfsz];
for (int i = 0; i < gfsz; ++i) {
groupFunList[i] = groupInfo.GroupByExpressions[i];
groupFunName[i] = "#GROUPBY-" + i;
}
node = new CreateFunctionsNode(node, groupFunList, groupFunName);
}
// Otherwise we provide the 'group_by_list' argument
node = new GroupNode(node, groupInfo.GroupByNames, groupInfo.GroupMax, groupInfo.FunctionExpressions, groupInfo.FunctionNames);
}
} else {
// Otherwise no grouping is occurring. We simply need create a function
// node with any functions defined in the SELECT.
// Plan a FunctionsNode with the functions defined in the SELECT.
if (groupInfo.FunctionCount > 0)
node = new CreateFunctionsNode(node, groupInfo.FunctionExpressions, groupInfo.FunctionNames);
}
return node;
}