Search in sources :

Example 1 with LeftJoin

use of org.eclipse.rdf4j.query.algebra.LeftJoin in project rdf4j by eclipse.

the class GraphPattern method buildTupleExpr.

/**
 * Builds a combined tuple expression from the tuple expressions and constraints in this graph pattern.
 *
 * @return A tuple expression for this graph pattern.
 */
public TupleExpr buildTupleExpr() {
    TupleExpr result;
    if (requiredTEs.isEmpty()) {
        result = new SingletonSet();
    } else {
        result = requiredTEs.get(0);
        for (int i = 1; i < requiredTEs.size(); i++) {
            TupleExpr te = requiredTEs.get(i);
            // if (containsProjection(te) || containsProjection(result))
            // {
            // result = new BottomUpJoin(result, te);
            // }
            // else {
            result = new Join(result, te);
        // }
        }
    }
    for (Map.Entry<TupleExpr, List<ValueExpr>> entry : optionalTEs) {
        List<ValueExpr> constraints = entry.getValue();
        if (constraints != null && !constraints.isEmpty()) {
            ValueExpr condition = constraints.get(0);
            for (int i = 1; i < constraints.size(); i++) {
                condition = new And(condition, constraints.get(i));
            }
            result = new LeftJoin(result, entry.getKey(), condition);
        } else {
            result = new LeftJoin(result, entry.getKey());
        }
    }
    for (ValueExpr constraint : constraints) {
        result = new Filter(result, constraint);
    }
    return result;
}
Also used : ValueExpr(org.eclipse.rdf4j.query.algebra.ValueExpr) LeftJoin(org.eclipse.rdf4j.query.algebra.LeftJoin) SingletonSet(org.eclipse.rdf4j.query.algebra.SingletonSet) Filter(org.eclipse.rdf4j.query.algebra.Filter) And(org.eclipse.rdf4j.query.algebra.And) LeftJoin(org.eclipse.rdf4j.query.algebra.LeftJoin) Join(org.eclipse.rdf4j.query.algebra.Join) ArrayList(java.util.ArrayList) List(java.util.List) AbstractMap(java.util.AbstractMap) Map(java.util.Map) TupleExpr(org.eclipse.rdf4j.query.algebra.TupleExpr)

Example 2 with LeftJoin

use of org.eclipse.rdf4j.query.algebra.LeftJoin in project rdf4j by eclipse.

the class BasicGroup method expr.

private TupleExpr expr(boolean filterExpr) {
    TupleExpr aExpr = null;
    if (mExpressions.isEmpty() && mFilters.isEmpty()) {
        if (mChildren.isEmpty()) {
            return null;
        }
    } else if (mExpressions.isEmpty() && !mFilters.isEmpty()) {
        if (mChildren.isEmpty()) {
            aExpr = new Filter(new EmptySet(), filtersAsAnd());
        }
    } else {
        aExpr = asJoin(mExpressions);
        if (filterExpr) {
            aExpr = filteredTuple(aExpr);
        }
    }
    if (!mChildren.isEmpty()) {
        for (Group aGroup : mChildren) {
            if (aExpr == null) {
                if (mExpressions.isEmpty() && !mFilters.isEmpty()) {
                    aExpr = new Filter(aGroup.expr(), filtersAsAnd());
                } else {
                    aExpr = aGroup.expr();
                }
            } else {
                BinaryTupleOperator aJoin = aGroup.isOptional() ? new LeftJoin() : new Join();
                aJoin.setLeftArg(aExpr);
                if (aGroup.isOptional() && aJoin instanceof LeftJoin && aGroup instanceof BasicGroup && !((BasicGroup) aGroup).mFilters.isEmpty()) {
                    BasicGroup aBasicGroup = (BasicGroup) aGroup;
                    aJoin.setRightArg(aBasicGroup.expr(false));
                    ((LeftJoin) aJoin).setCondition(aBasicGroup.filtersAsAnd());
                } else {
                    aJoin.setRightArg(aGroup.expr());
                }
                aExpr = aJoin;
            }
        }
    }
    return aExpr;
}
Also used : LeftJoin(org.eclipse.rdf4j.query.algebra.LeftJoin) Filter(org.eclipse.rdf4j.query.algebra.Filter) EmptySet(org.eclipse.rdf4j.query.algebra.EmptySet) BinaryTupleOperator(org.eclipse.rdf4j.query.algebra.BinaryTupleOperator) LeftJoin(org.eclipse.rdf4j.query.algebra.LeftJoin) Join(org.eclipse.rdf4j.query.algebra.Join) TupleExpr(org.eclipse.rdf4j.query.algebra.TupleExpr)

Example 3 with LeftJoin

use of org.eclipse.rdf4j.query.algebra.LeftJoin in project rdf4j by eclipse.

the class AbstractQueryBuilder method groupAsJoin.

private TupleExpr groupAsJoin(List<Group> theList) {
    BinaryTupleOperator aJoin = new Join();
    Filter aFilter = null;
    for (Group aGroup : theList) {
        TupleExpr aExpr = aGroup.expr();
        if (aExpr == null) {
            continue;
        }
        if (aExpr instanceof Filter && (((Filter) aExpr).getArg() == null || ((Filter) aExpr).getArg() instanceof EmptySet)) {
            if (aFilter == null) {
                aFilter = (Filter) aExpr;
            } else {
                // if we already have a filter w/ an empty arg, let's And the
                // conditions together.
                aFilter.setCondition(new And(aFilter.getCondition(), ((Filter) aExpr).getCondition()));
            }
            continue;
        }
        if (aFilter != null) {
            aFilter.setArg(aExpr);
            aExpr = aFilter;
            aFilter = null;
        }
        if (aGroup.isOptional()) {
            LeftJoin lj = new LeftJoin();
            TupleExpr aLeft = joinOrExpr(aJoin);
            if (aLeft != null) {
                lj.setLeftArg(aLeft);
                lj.setRightArg(aExpr);
                aJoin = lj;
                continue;
            }
        }
        if (aJoin.getLeftArg() == null) {
            aJoin.setLeftArg(aExpr);
        } else if (aJoin.getRightArg() == null) {
            aJoin.setRightArg(aExpr);
        } else {
            Join aNewJoin = new Join();
            aNewJoin.setLeftArg(aJoin);
            aNewJoin.setRightArg(aExpr);
            aJoin = aNewJoin;
        }
    }
    TupleExpr aExpr = joinOrExpr(aJoin);
    if (aFilter != null) {
        aFilter.setArg(aExpr);
        aExpr = aFilter;
    }
    return aExpr;
}
Also used : LeftJoin(org.eclipse.rdf4j.query.algebra.LeftJoin) Filter(org.eclipse.rdf4j.query.algebra.Filter) And(org.eclipse.rdf4j.query.algebra.And) BinaryTupleOperator(org.eclipse.rdf4j.query.algebra.BinaryTupleOperator) EmptySet(org.eclipse.rdf4j.query.algebra.EmptySet) LeftJoin(org.eclipse.rdf4j.query.algebra.LeftJoin) Join(org.eclipse.rdf4j.query.algebra.Join) TupleExpr(org.eclipse.rdf4j.query.algebra.TupleExpr)

Aggregations

Filter (org.eclipse.rdf4j.query.algebra.Filter)3 Join (org.eclipse.rdf4j.query.algebra.Join)3 LeftJoin (org.eclipse.rdf4j.query.algebra.LeftJoin)3 TupleExpr (org.eclipse.rdf4j.query.algebra.TupleExpr)3 And (org.eclipse.rdf4j.query.algebra.And)2 BinaryTupleOperator (org.eclipse.rdf4j.query.algebra.BinaryTupleOperator)2 EmptySet (org.eclipse.rdf4j.query.algebra.EmptySet)2 AbstractMap (java.util.AbstractMap)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Map (java.util.Map)1 SingletonSet (org.eclipse.rdf4j.query.algebra.SingletonSet)1 ValueExpr (org.eclipse.rdf4j.query.algebra.ValueExpr)1