use of org.teiid.language.SortSpecification.NullOrdering in project teiid by teiid.
the class ListNestedSortComparator method compare.
/**
* 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. <p>
*
* The <code>compare</code> method returns <p>
* <ul>
* <li>-1 if object1 less than object 2 </li>
* <li> 0 if object1 equal to object 2 </li>
* <li>+1 if object1 greater than object 2 </li>
* </ul>
*
* @param o1 The first object being compared
* @param o2 The second object being compared
*/
public int compare(java.util.List<T> list1, java.util.List<T> list2) {
if (!init) {
if (nullOrdering == null) {
nullOrdering = Collections.nCopies(sortParameters.length, null);
}
for (int i = 0; i < sortParameters.length; i++) {
if (nullOrdering.get(i) == null) {
if (defaultNullOrder == NullOrder.FIRST) {
nullOrdering.set(i, NullOrdering.FIRST);
} else if (defaultNullOrder == NullOrder.LAST) {
nullOrdering.set(i, NullOrdering.LAST);
}
}
}
if (defaultNullOrder == NullOrder.HIGH) {
nullValue = 1;
}
init = true;
}
int compare = 0;
for (int k = 0; k < sortParameters.length; k++) {
if (list1.size() <= sortParameters[k]) {
return 1;
}
T param1 = list1.get(sortParameters[k]);
if (list2.size() <= sortParameters[k]) {
return -1;
}
T param2 = list2.get(sortParameters[k]);
if (param1 == null) {
if (param2 == null) {
// Both are null
compare = 0;
} else {
// param1 = null, so is less than a non-null
compare = nullValue;
NullOrdering no = getNullOrdering(k);
if (no == NullOrdering.FIRST) {
return -1;
}
if (no == NullOrdering.LAST) {
return 1;
}
}
} else if (param2 == null) {
// param1 != null, param2 == null
compare = -nullValue;
NullOrdering no = getNullOrdering(k);
if (no == NullOrdering.FIRST) {
return 1;
}
if (no == NullOrdering.LAST) {
return -1;
}
} else {
compare = Constant.COMPARATOR.compare(param1, param2);
}
if (compare != 0) {
boolean asc = orderTypes != null ? orderTypes.get(k) : this.ascendingOrder;
return asc ? compare : -compare;
} else if (k == distinctIndex) {
isDistinct = false;
}
}
return 0;
}
use of org.teiid.language.SortSpecification.NullOrdering in project teiid by teiid.
the class GroupingNode method collectionPhase.
private void collectionPhase() {
if (this.orderBy == null) {
// No need to sort
this.groupTupleSource = getGroupSortTupleSource();
this.phase = GROUP;
} else {
List<NullOrdering> nullOrdering = new ArrayList<NullOrdering>(orderBy.size());
List<Boolean> sortTypes = new ArrayList<Boolean>(orderBy.size());
int size = orderBy.size();
if (this.removeDuplicates) {
// sort on all inputs
size = distinctCols;
}
int[] sortIndexes = new int[size];
for (int i = 0; i < size; i++) {
int index = i;
if (i < this.orderBy.size()) {
OrderByItem item = this.orderBy.get(i);
nullOrdering.add(item.getNullOrdering());
sortTypes.add(item.isAscending());
index = collectedExpressions.get(SymbolMap.getExpression(item.getSymbol()));
} else {
nullOrdering.add(null);
sortTypes.add(OrderBy.ASC);
}
sortIndexes[i] = index;
}
this.indexes = Arrays.copyOf(sortIndexes, orderBy.size());
if (rollup) {
this.indexMap = new HashMap<Integer, Integer>();
for (int i = 0; i < indexes.length; i++) {
this.indexMap.put(indexes[i], orderBy.size() - i);
}
} else if (!removeDuplicates) {
boolean groupSort = true;
List<AggregateFunction> aggs = new ArrayList<AggregateFunction>();
List<Class<?>> allTypes = new ArrayList<Class<?>>();
accumulatorStateCount = new int[this.functions.length];
for (AggregateFunction[] afs : this.functions) {
if (afs[0] instanceof ConstantFunction) {
continue;
}
aggs.add(afs[0]);
List<? extends Class<?>> types = afs[0].getStateTypes();
if (types == null) {
groupSort = false;
break;
}
accumulatorStateCount[aggs.size() - 1] = types.size();
allTypes.addAll(types);
}
if (groupSort) {
this.groupSortfunctions = aggs.toArray(new AggregateFunction[aggs.size()]);
List<Expression> schema = new ArrayList<Expression>();
for (OrderByItem item : this.orderBy) {
schema.add(SymbolMap.getExpression(item.getSymbol()));
}
List<? extends Expression> elements = getElements();
this.projection = new int[elements.size()];
int index = 0;
for (int i = 0; i < elements.size(); i++) {
Expression symbol = elements.get(i);
if (this.outputMapping != null) {
symbol = outputMapping.getMappedExpression((ElementSymbol) symbol);
}
if (symbol instanceof AggregateSymbol) {
projection[i] = schema.size() + index++;
} else {
projection[i] = schema.indexOf(symbol);
}
}
// add in accumulator value types
for (Class<?> type : allTypes) {
ElementSymbol es = new ElementSymbol("x");
es.setType(type);
schema.add(es);
}
tree = this.getBufferManager().createSTree(schema, this.getConnectionID(), orderBy.size());
// non-default order needs to update the comparator
tree.getComparator().setNullOrdering(nullOrdering);
tree.getComparator().setOrderTypes(sortTypes);
this.groupSortTupleSource = this.getGroupSortTupleSource();
this.phase = GROUP_SORT;
return;
}
}
this.sortUtility = new SortUtility(getGroupSortTupleSource(), removeDuplicates ? Mode.DUP_REMOVE_SORT : Mode.SORT, getBufferManager(), getConnectionID(), new ArrayList<Expression>(collectedExpressions.keySet()), sortTypes, nullOrdering, sortIndexes);
this.phase = SORT;
}
}
Aggregations