Search in sources :

Example 1 with OrderingImpl

use of org.apache.jackrabbit.oak.query.ast.OrderingImpl in project jackrabbit-oak by apache.

the class QueryImpl method toString.

@Override
public String toString() {
    StringBuilder buff = new StringBuilder();
    buff.append("select ");
    int i = 0;
    for (ColumnImpl c : columns) {
        if (i++ > 0) {
            buff.append(", ");
        }
        buff.append(c);
    }
    buff.append(" from ").append(source);
    if (constraint != null) {
        buff.append(" where ").append(constraint);
    }
    if (orderings != null) {
        buff.append(" order by ");
        i = 0;
        for (OrderingImpl o : orderings) {
            if (i++ > 0) {
                buff.append(", ");
            }
            buff.append(o);
        }
    }
    return buff.toString();
}
Also used : OrderingImpl(org.apache.jackrabbit.oak.query.ast.OrderingImpl) ColumnImpl(org.apache.jackrabbit.oak.query.ast.ColumnImpl)

Example 2 with OrderingImpl

use of org.apache.jackrabbit.oak.query.ast.OrderingImpl in project jackrabbit-oak by apache.

the class QueryImpl method canSortByIndex.

private boolean canSortByIndex() {
    boolean canSortByIndex = false;
    // TODO add issue about order by optimization for multiple selectors
    if (orderings != null && selectors.size() == 1) {
        IndexPlan plan = selectors.get(0).getExecutionPlan().getIndexPlan();
        if (plan != null) {
            List<OrderEntry> list = plan.getSortOrder();
            if (list != null && list.size() == orderings.length) {
                canSortByIndex = true;
                for (int i = 0; i < list.size(); i++) {
                    OrderEntry e = list.get(i);
                    OrderingImpl o = orderings[i];
                    DynamicOperandImpl op = o.getOperand();
                    if (!(op instanceof PropertyValueImpl)) {
                        // ordered by a function: currently not supported
                        canSortByIndex = false;
                        break;
                    }
                    // we only have one selector, so no need to check that
                    // TODO support joins
                    String pn = ((PropertyValueImpl) op).getPropertyName();
                    if (!pn.equals(e.getPropertyName())) {
                        // ordered by another property
                        canSortByIndex = false;
                        break;
                    }
                    if (o.isDescending() != (e.getOrder() == Order.DESCENDING)) {
                        // ordered ascending versus descending
                        canSortByIndex = false;
                        break;
                    }
                }
            }
        }
    }
    return canSortByIndex;
}
Also used : DynamicOperandImpl(org.apache.jackrabbit.oak.query.ast.DynamicOperandImpl) OrderEntry(org.apache.jackrabbit.oak.spi.query.QueryIndex.OrderEntry) IndexPlan(org.apache.jackrabbit.oak.spi.query.QueryIndex.IndexPlan) OrderingImpl(org.apache.jackrabbit.oak.query.ast.OrderingImpl) PropertyValueImpl(org.apache.jackrabbit.oak.query.ast.PropertyValueImpl)

Example 3 with OrderingImpl

use of org.apache.jackrabbit.oak.query.ast.OrderingImpl in project jackrabbit-oak by apache.

the class SQL2Parser method parseOrder.

private OrderingImpl[] parseOrder() throws ParseException {
    ArrayList<OrderingImpl> orderList = new ArrayList<OrderingImpl>();
    do {
        OrderingImpl ordering;
        DynamicOperandImpl op = parseDynamicOperand();
        if (readIf("DESC")) {
            ordering = factory.descending(op);
        } else {
            readIf("ASC");
            ordering = factory.ascending(op);
        }
        orderList.add(ordering);
    } while (readIf(","));
    OrderingImpl[] orderings = new OrderingImpl[orderList.size()];
    orderList.toArray(orderings);
    return orderings;
}
Also used : DynamicOperandImpl(org.apache.jackrabbit.oak.query.ast.DynamicOperandImpl) OrderingImpl(org.apache.jackrabbit.oak.query.ast.OrderingImpl) ArrayList(java.util.ArrayList)

Example 4 with OrderingImpl

use of org.apache.jackrabbit.oak.query.ast.OrderingImpl in project jackrabbit-oak by apache.

the class UnionQueryImpl method setOrderings.

@Override
public void setOrderings(OrderingImpl[] orderings) {
    if (orderings == null) {
        left.setOrderings(null);
        right.setOrderings(null);
        return;
    }
    OrderingImpl[] l = new OrderingImpl[orderings.length];
    OrderingImpl[] r = new OrderingImpl[orderings.length];
    for (int i = 0; i < orderings.length; i++) {
        OrderingImpl o = orderings[i];
        l[i] = o.createCopy();
        r[i] = o.createCopy();
    }
    left.setOrderings(l);
    right.setOrderings(r);
    this.orderings = orderings;
}
Also used : OrderingImpl(org.apache.jackrabbit.oak.query.ast.OrderingImpl)

Example 5 with OrderingImpl

use of org.apache.jackrabbit.oak.query.ast.OrderingImpl in project jackrabbit-oak by apache.

the class SQL2Parser method parse.

/**
 * Parse the statement and return the query.
 *
 * @param query the query string
 * @param initialise if performing the query init ({@code true}) or not ({@code false})
 * @return the query
 * @throws ParseException if parsing fails
 */
public Query parse(final String query, final boolean initialise) throws ParseException {
    // TODO possibly support union,... as available at
    // http://docs.jboss.org/modeshape/latest/manuals/reference/html/jcr-query-and-search.html
    initialize(query);
    selectors.clear();
    expected = new ArrayList<String>();
    bindVariables = new HashMap<String, BindVariableValueImpl>();
    read();
    boolean explain = false, measure = false;
    if (readIf("EXPLAIN")) {
        explain = true;
    }
    if (readIf("MEASURE")) {
        measure = true;
    }
    Query q = parseSelect();
    while (true) {
        if (!readIf("UNION")) {
            break;
        }
        boolean unionAll = readIf("ALL");
        QueryImpl q2 = parseSelect();
        q = new UnionQueryImpl(unionAll, q, q2, settings);
    }
    OrderingImpl[] orderings = null;
    if (readIf("ORDER")) {
        read("BY");
        orderings = parseOrder();
    }
    QueryOptions options = new QueryOptions();
    if (readIf("OPTION")) {
        read("(");
        while (true) {
            if (readIf("TRAVERSAL")) {
                String n = readName().toUpperCase(Locale.ENGLISH);
                options.traversal = Traversal.valueOf(n);
            } else if (readIf("INDEX")) {
                if (readIf("NAME")) {
                    options.indexName = readName();
                } else if (readIf("TAG")) {
                    options.indexTag = readLabel();
                }
            } else {
                break;
            }
            readIf(",");
        }
        read(")");
    }
    if (!currentToken.isEmpty()) {
        throw getSyntaxError("<end>");
    }
    q.setOrderings(orderings);
    q.setExplain(explain);
    q.setMeasure(measure);
    q.setInternal(isInternal(query));
    q.setQueryOptions(options);
    if (initialise) {
        try {
            q.init();
        } catch (Exception e) {
            ParseException e2 = new ParseException(statement + ": " + e.getMessage(), 0);
            e2.initCause(e);
            throw e2;
        }
    }
    return q;
}
Also used : OrderingImpl(org.apache.jackrabbit.oak.query.ast.OrderingImpl) RepositoryException(javax.jcr.RepositoryException) ParseException(java.text.ParseException) BindVariableValueImpl(org.apache.jackrabbit.oak.query.ast.BindVariableValueImpl) ParseException(java.text.ParseException)

Aggregations

OrderingImpl (org.apache.jackrabbit.oak.query.ast.OrderingImpl)7 DynamicOperandImpl (org.apache.jackrabbit.oak.query.ast.DynamicOperandImpl)3 ArrayList (java.util.ArrayList)2 OrderEntry (org.apache.jackrabbit.oak.spi.query.QueryIndex.OrderEntry)2 Lists.newArrayList (com.google.common.collect.Lists.newArrayList)1 ParseException (java.text.ParseException)1 RepositoryException (javax.jcr.RepositoryException)1 BindVariableValueImpl (org.apache.jackrabbit.oak.query.ast.BindVariableValueImpl)1 ColumnImpl (org.apache.jackrabbit.oak.query.ast.ColumnImpl)1 PropertyValueImpl (org.apache.jackrabbit.oak.query.ast.PropertyValueImpl)1 IndexPlan (org.apache.jackrabbit.oak.spi.query.QueryIndex.IndexPlan)1