for(int i=0; i<getElements().size(); i++) {
SingleElementSymbol symbol = (SingleElementSymbol)getElements().get(i);
Class<?> outputType = symbol.getType();
Class<?> inputType = symbol.getType();
if(symbol instanceof AggregateSymbol) {
AggregateSymbol aggSymbol = (AggregateSymbol) symbol;
if(aggSymbol.getExpression() == null) {
functions[i] = new Count();
} else {
Expression ex = aggSymbol.getExpression();
inputType = ex.getType();
int index = collectExpression(ex);
Type function = aggSymbol.getAggregateFunction();
switch (function) {
case COUNT:
functions[i] = new Count();
break;
case SUM:
functions[i] = new Sum();
break;
case AVG:
functions[i] = new Avg();
break;
case MIN:
functions[i] = new Min();
break;
case MAX:
functions[i] = new Max();
break;
case XMLAGG:
functions[i] = new XMLAgg(context);
break;
case ARRAY_AGG:
functions[i] = new ArrayAgg(context);
break;
case TEXTAGG:
functions[i] = new TextAgg(context, (TextLine)ex);
break;
default:
functions[i] = new StatsFunction(function);
}
if(aggSymbol.isDistinct() && !function.equals(NonReserved.MIN) && !function.equals(NonReserved.MAX)) {
SortingFilter filter = new SortingFilter(functions[i], getBufferManager(), getConnectionID(), true);
ElementSymbol element = new ElementSymbol("val"); //$NON-NLS-1$
element.setType(inputType);
filter.setElements(Arrays.asList(element));
functions[i] = filter;
} else if (aggSymbol.getOrderBy() != null) { //handle the xmlagg case
int[] orderIndecies = new int[aggSymbol.getOrderBy().getOrderByItems().size()];
List<OrderByItem> orderByItems = new ArrayList<OrderByItem>(orderIndecies.length);
List<ElementSymbol> schema = new ArrayList<ElementSymbol>(orderIndecies.length + 1);
ElementSymbol element = new ElementSymbol("val"); //$NON-NLS-1$
element.setType(inputType);
schema.add(element);
for (ListIterator<OrderByItem> iterator = aggSymbol.getOrderBy().getOrderByItems().listIterator(); iterator.hasNext();) {
OrderByItem item = iterator.next();
orderIndecies[iterator.previousIndex()] = collectExpression(item.getSymbol());
element = new ElementSymbol(String.valueOf(iterator.previousIndex()));
element.setType(inputType);
schema.add(element);