use of io.crate.sql.tree.SortItem in project crate by crate.
the class OrderByAnalyzerTest method analyzeSortItems.
@Test
public void analyzeSortItems() {
List<SortItem> sortItems = new ArrayList<>(2);
QualifiedName tx = QualifiedName.of("t", "x");
SortItem firstSort = new SortItem(new QualifiedNameReference(tx), SortItem.Ordering.ASCENDING, SortItem.NullOrdering.FIRST);
sortItems.add(firstSort);
QualifiedName ty = QualifiedName.of("t", "y");
SortItem second = new SortItem(new QualifiedNameReference(ty), SortItem.Ordering.DESCENDING, SortItem.NullOrdering.LAST);
sortItems.add(second);
OrderBy orderBy = OrderyByAnalyzer.analyzeSortItems(sortItems, e -> Literal.of(((QualifiedNameReference) e).getName().toString()));
assertThat(orderBy, is(notNullValue()));
List<Symbol> orderBySymbols = orderBy.orderBySymbols();
assertThat(orderBySymbols.size(), is(2));
assertThat(orderBySymbols.get(0), SymbolMatchers.isLiteral("t.x"));
assertThat(orderBySymbols.get(1), SymbolMatchers.isLiteral("t.y"));
boolean[] reverseFlags = orderBy.reverseFlags();
assertThat(reverseFlags.length, is(2));
assertThat(reverseFlags[0], is(false));
assertThat(reverseFlags[1], is(true));
boolean[] nullsFirst = orderBy.nullsFirst();
assertThat(nullsFirst.length, is(2));
assertThat(nullsFirst[0], is(true));
assertThat(nullsFirst[1], is(false));
}
use of io.crate.sql.tree.SortItem in project crate by crate.
the class OrderyByAnalyzer method analyzeSortItems.
@Nullable
public static OrderBy analyzeSortItems(List<SortItem> sortItems, Function<Expression, Symbol> expressionToSymbolFunction) {
int size = sortItems.size();
if (size == 0) {
return null;
}
List<Symbol> symbols = new ArrayList<>(size);
boolean[] reverseFlags = new boolean[size];
boolean[] nullsFirst = new boolean[size];
for (int i = 0; i < size; i++) {
SortItem sortItem = sortItems.get(i);
Expression sortKey = sortItem.getSortKey();
Symbol symbol = expressionToSymbolFunction.apply(sortKey);
symbols.add(symbol);
switch(sortItem.getNullOrdering()) {
case FIRST:
nullsFirst[i] = true;
break;
case LAST:
nullsFirst[i] = false;
break;
case UNDEFINED:
nullsFirst[i] = sortItem.getOrdering() == SortItem.Ordering.DESCENDING;
break;
default:
}
reverseFlags[i] = sortItem.getOrdering() == SortItem.Ordering.DESCENDING;
}
return new OrderBy(symbols, reverseFlags, nullsFirst);
}
Aggregations