Search in sources :

Example 1 with SortItem

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));
}
Also used : OrderBy(io.crate.analyze.OrderBy) SortItem(io.crate.sql.tree.SortItem) Symbol(io.crate.expression.symbol.Symbol) QualifiedName(io.crate.sql.tree.QualifiedName) ArrayList(java.util.ArrayList) QualifiedNameReference(io.crate.sql.tree.QualifiedNameReference) Test(org.junit.Test)

Example 2 with SortItem

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);
}
Also used : OrderBy(io.crate.analyze.OrderBy) SortItem(io.crate.sql.tree.SortItem) Expression(io.crate.sql.tree.Expression) Symbol(io.crate.expression.symbol.Symbol) ArrayList(java.util.ArrayList) Nullable(javax.annotation.Nullable)

Aggregations

OrderBy (io.crate.analyze.OrderBy)2 Symbol (io.crate.expression.symbol.Symbol)2 SortItem (io.crate.sql.tree.SortItem)2 ArrayList (java.util.ArrayList)2 Expression (io.crate.sql.tree.Expression)1 QualifiedName (io.crate.sql.tree.QualifiedName)1 QualifiedNameReference (io.crate.sql.tree.QualifiedNameReference)1 Nullable (javax.annotation.Nullable)1 Test (org.junit.Test)1