Search in sources :

Example 1 with SourceImpl

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

the class QueryImpl method prepare.

@Override
public void prepare() {
    if (prepared) {
        return;
    }
    prepared = true;
    List<SourceImpl> sources = source.getInnerJoinSelectors();
    List<JoinConditionImpl> conditions = source.getInnerJoinConditions();
    if (sources.size() <= 1) {
        // simple case (no join)
        estimatedCost = source.prepare().getEstimatedCost();
        isSortedByIndex = canSortByIndex();
        return;
    }
    // use a greedy algorithm
    SourceImpl result = null;
    Set<SourceImpl> available = new HashSet<SourceImpl>();
    // the query is only slow if all possible join orders are slow
    // (in theory, due to using the greedy algorithm, a query might be considered
    // slow even thought there is a plan that doesn't need to use traversal, but
    // only for 3-way and higher joins, and only if traversal is considered very fast)
    boolean isPotentiallySlowJoin = true;
    while (sources.size() > 0) {
        int bestIndex = 0;
        double bestCost = Double.POSITIVE_INFINITY;
        ExecutionPlan bestPlan = null;
        SourceImpl best = null;
        for (int i = 0; i < sources.size(); i++) {
            SourceImpl test = buildJoin(result, sources.get(i), conditions);
            if (test == null) {
                // no join condition
                continue;
            }
            ExecutionPlan testPlan = test.prepare();
            double cost = testPlan.getEstimatedCost();
            if (best == null || cost < bestCost) {
                bestPlan = testPlan;
                bestCost = cost;
                bestIndex = i;
                best = test;
            }
            if (!potentiallySlowTraversalQuery) {
                isPotentiallySlowJoin = false;
            }
            test.unprepare();
        }
        available.add(sources.remove(bestIndex));
        result = best;
        best.prepare(bestPlan);
    }
    potentiallySlowTraversalQuery = isPotentiallySlowJoin;
    estimatedCost = result.prepare().getEstimatedCost();
    source = result;
    isSortedByIndex = canSortByIndex();
}
Also used : ExecutionPlan(org.apache.jackrabbit.oak.query.plan.ExecutionPlan) SelectorExecutionPlan(org.apache.jackrabbit.oak.query.plan.SelectorExecutionPlan) SourceImpl(org.apache.jackrabbit.oak.query.ast.SourceImpl) ChildNodeJoinConditionImpl(org.apache.jackrabbit.oak.query.ast.ChildNodeJoinConditionImpl) JoinConditionImpl(org.apache.jackrabbit.oak.query.ast.JoinConditionImpl) EquiJoinConditionImpl(org.apache.jackrabbit.oak.query.ast.EquiJoinConditionImpl) DescendantNodeJoinConditionImpl(org.apache.jackrabbit.oak.query.ast.DescendantNodeJoinConditionImpl) SameNodeJoinConditionImpl(org.apache.jackrabbit.oak.query.ast.SameNodeJoinConditionImpl) HashSet(java.util.HashSet)

Example 2 with SourceImpl

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

the class SQL2Parser method parseSelect.

private QueryImpl parseSelect() throws ParseException {
    read("SELECT");
    boolean distinct = readIf("DISTINCT");
    ArrayList<ColumnOrWildcard> list = parseColumns();
    if (supportSQL1) {
        addColumnIfNecessary(list, QueryImpl.JCR_PATH, QueryImpl.JCR_PATH);
        addColumnIfNecessary(list, QueryImpl.JCR_SCORE, QueryImpl.JCR_SCORE);
    }
    read("FROM");
    SourceImpl source = parseSource();
    ColumnImpl[] columnArray = resolveColumns(list);
    ConstraintImpl constraint = null;
    if (readIf("WHERE")) {
        constraint = parseConstraint();
    }
    QueryImpl q = new QueryImpl(statement, source, constraint, columnArray, namePathMapper, settings);
    q.setDistinct(distinct);
    return q;
}
Also used : ConstraintImpl(org.apache.jackrabbit.oak.query.ast.ConstraintImpl) SourceImpl(org.apache.jackrabbit.oak.query.ast.SourceImpl) ColumnImpl(org.apache.jackrabbit.oak.query.ast.ColumnImpl)

Example 3 with SourceImpl

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

the class QueryImpl method buildJoin.

private static SourceImpl buildJoin(SourceImpl result, SourceImpl last, List<JoinConditionImpl> conditions) {
    if (result == null) {
        return last;
    }
    List<SourceImpl> selectors = result.getInnerJoinSelectors();
    Set<SourceImpl> oldSelectors = new HashSet<SourceImpl>();
    oldSelectors.addAll(selectors);
    Set<SourceImpl> newSelectors = new HashSet<SourceImpl>();
    newSelectors.addAll(selectors);
    newSelectors.add(last);
    for (JoinConditionImpl j : conditions) {
        // but couldn't be evaluated before
        if (!j.canEvaluate(oldSelectors) && j.canEvaluate(newSelectors)) {
            JoinImpl join = new JoinImpl(result, last, JoinType.INNER, j);
            return join;
        }
    }
    // no join condition was found
    return null;
}
Also used : JoinImpl(org.apache.jackrabbit.oak.query.ast.JoinImpl) SourceImpl(org.apache.jackrabbit.oak.query.ast.SourceImpl) HashSet(java.util.HashSet) ChildNodeJoinConditionImpl(org.apache.jackrabbit.oak.query.ast.ChildNodeJoinConditionImpl) JoinConditionImpl(org.apache.jackrabbit.oak.query.ast.JoinConditionImpl) EquiJoinConditionImpl(org.apache.jackrabbit.oak.query.ast.EquiJoinConditionImpl) DescendantNodeJoinConditionImpl(org.apache.jackrabbit.oak.query.ast.DescendantNodeJoinConditionImpl) SameNodeJoinConditionImpl(org.apache.jackrabbit.oak.query.ast.SameNodeJoinConditionImpl)

Example 4 with SourceImpl

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

the class SQL2Parser method parseSource.

private SourceImpl parseSource() throws ParseException {
    SelectorImpl selector = parseSelector();
    selectors.put(selector.getSelectorName(), selector);
    SourceImpl source = selector;
    while (true) {
        JoinType joinType;
        if (readIf("RIGHT")) {
            read("OUTER");
            joinType = JoinType.RIGHT_OUTER;
        } else if (readIf("LEFT")) {
            read("OUTER");
            joinType = JoinType.LEFT_OUTER;
        } else if (readIf("INNER")) {
            joinType = JoinType.INNER;
        } else {
            break;
        }
        read("JOIN");
        selector = parseSelector();
        selectors.put(selector.getSelectorName(), selector);
        read("ON");
        JoinConditionImpl on = parseJoinCondition();
        source = factory.join(source, selector, joinType, on);
    }
    return source;
}
Also used : SelectorImpl(org.apache.jackrabbit.oak.query.ast.SelectorImpl) SourceImpl(org.apache.jackrabbit.oak.query.ast.SourceImpl) JoinType(org.apache.jackrabbit.oak.query.ast.JoinType) JoinConditionImpl(org.apache.jackrabbit.oak.query.ast.JoinConditionImpl)

Aggregations

SourceImpl (org.apache.jackrabbit.oak.query.ast.SourceImpl)4 JoinConditionImpl (org.apache.jackrabbit.oak.query.ast.JoinConditionImpl)3 HashSet (java.util.HashSet)2 ChildNodeJoinConditionImpl (org.apache.jackrabbit.oak.query.ast.ChildNodeJoinConditionImpl)2 DescendantNodeJoinConditionImpl (org.apache.jackrabbit.oak.query.ast.DescendantNodeJoinConditionImpl)2 EquiJoinConditionImpl (org.apache.jackrabbit.oak.query.ast.EquiJoinConditionImpl)2 SameNodeJoinConditionImpl (org.apache.jackrabbit.oak.query.ast.SameNodeJoinConditionImpl)2 ColumnImpl (org.apache.jackrabbit.oak.query.ast.ColumnImpl)1 ConstraintImpl (org.apache.jackrabbit.oak.query.ast.ConstraintImpl)1 JoinImpl (org.apache.jackrabbit.oak.query.ast.JoinImpl)1 JoinType (org.apache.jackrabbit.oak.query.ast.JoinType)1 SelectorImpl (org.apache.jackrabbit.oak.query.ast.SelectorImpl)1 ExecutionPlan (org.apache.jackrabbit.oak.query.plan.ExecutionPlan)1 SelectorExecutionPlan (org.apache.jackrabbit.oak.query.plan.SelectorExecutionPlan)1