Search in sources :

Example 1 with Join

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

the class TupleExprBuilder method visit.

@Override
public TupleExpr visit(ASTSelectQuery node, Object data) throws VisitorException {
    GraphPattern parentGP = graphPattern;
    // Start with building the graph pattern
    graphPattern = new GraphPattern(parentGP);
    node.getWhereClause().jjtAccept(this, null);
    TupleExpr tupleExpr = graphPattern.buildTupleExpr();
    // Apply grouping
    Group group = null;
    ASTGroupClause groupNode = node.getGroupClause();
    if (groupNode != null) {
        tupleExpr = (TupleExpr) groupNode.jjtAccept(this, tupleExpr);
        group = (Group) tupleExpr;
    }
    final ASTHavingClause havingClause = node.getHavingClause();
    if (havingClause != null) {
        if (group == null) {
            // create implicit group
            group = new Group(tupleExpr);
        }
        // Apply HAVING group filter condition
        tupleExpr = processHavingClause(havingClause, tupleExpr, group);
    }
    // process bindings clause
    final ASTBindingsClause bindingsClause = node.getBindingsClause();
    if (bindingsClause != null) {
        tupleExpr = new Join((BindingSetAssignment) bindingsClause.jjtAccept(this, null), tupleExpr);
    }
    final ASTOrderClause orderClause = node.getOrderClause();
    if (orderClause != null) {
        if (group == null) {
            // create implicit group
            group = new Group(tupleExpr);
        }
        // Apply result ordering
        tupleExpr = processOrderClause(node.getOrderClause(), tupleExpr, group);
    }
    // Apply projection
    tupleExpr = (TupleExpr) node.getSelect().jjtAccept(this, tupleExpr);
    // Process limit and offset clauses
    ASTLimit limitNode = node.getLimit();
    long limit = -1L;
    if (limitNode != null) {
        limit = (Long) limitNode.jjtAccept(this, null);
    }
    ASTOffset offsetNode = node.getOffset();
    long offset = -1L;
    if (offsetNode != null) {
        offset = (Long) offsetNode.jjtAccept(this, null);
    }
    if (offset >= 1L || limit >= 0L) {
        tupleExpr = new Slice(tupleExpr, offset, limit);
    }
    if (parentGP != null) {
        parentGP.addRequiredTE(tupleExpr);
        graphPattern = parentGP;
    }
    return tupleExpr;
}
Also used : Group(org.eclipse.rdf4j.query.algebra.Group) BindingSetAssignment(org.eclipse.rdf4j.query.algebra.BindingSetAssignment) Join(org.eclipse.rdf4j.query.algebra.Join) TupleExpr(org.eclipse.rdf4j.query.algebra.TupleExpr) Slice(org.eclipse.rdf4j.query.algebra.Slice)

Example 2 with Join

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

the class TupleExprBuilder method visit.

@Override
public TupleExpr visit(ASTConstructQuery node, Object data) throws VisitorException {
    // Start with building the graph pattern
    graphPattern = new GraphPattern();
    node.getWhereClause().jjtAccept(this, null);
    TupleExpr tupleExpr = graphPattern.buildTupleExpr();
    // Apply grouping
    ASTGroupClause groupNode = node.getGroupClause();
    if (groupNode != null) {
        tupleExpr = (TupleExpr) groupNode.jjtAccept(this, tupleExpr);
    }
    Group group = null;
    if (tupleExpr instanceof Group) {
        group = (Group) tupleExpr;
    } else {
        // create a new implicit group. Note that this group will only
        // actually
        // be used in the query model if the query has HAVING or ORDER BY
        // clause
        group = new Group(tupleExpr);
    }
    // Apply HAVING group filter condition
    tupleExpr = processHavingClause(node.getHavingClause(), tupleExpr, group);
    // process bindings clause
    ASTBindingsClause bindingsClause = node.getBindingsClause();
    if (bindingsClause != null) {
        tupleExpr = new Join((BindingSetAssignment) bindingsClause.jjtAccept(this, null), tupleExpr);
    }
    // Apply result ordering
    tupleExpr = processOrderClause(node.getOrderClause(), tupleExpr, null);
    // Process construct clause
    ASTConstruct constructNode = node.getConstruct();
    if (!constructNode.isWildcard()) {
        tupleExpr = (TupleExpr) constructNode.jjtAccept(this, tupleExpr);
    } else {
        // create construct clause from graph pattern.
        ConstructorBuilder cb = new ConstructorBuilder();
        // possible future use.
        try {
            tupleExpr = cb.buildConstructor(tupleExpr, false, false);
        } catch (MalformedQueryException e) {
            throw new VisitorException(e.getMessage());
        }
    }
    // process limit and offset clauses
    ASTLimit limitNode = node.getLimit();
    long limit = -1L;
    if (limitNode != null) {
        limit = (Long) limitNode.jjtAccept(this, null);
    }
    ASTOffset offsetNode = node.getOffset();
    long offset = -1;
    if (offsetNode != null) {
        offset = (Long) offsetNode.jjtAccept(this, null);
    }
    if (offset >= 1 || limit >= 0) {
        tupleExpr = new Slice(tupleExpr, offset, limit);
    }
    return tupleExpr;
}
Also used : Group(org.eclipse.rdf4j.query.algebra.Group) BindingSetAssignment(org.eclipse.rdf4j.query.algebra.BindingSetAssignment) Join(org.eclipse.rdf4j.query.algebra.Join) TupleExpr(org.eclipse.rdf4j.query.algebra.TupleExpr) Slice(org.eclipse.rdf4j.query.algebra.Slice) MalformedQueryException(org.eclipse.rdf4j.query.MalformedQueryException)

Example 3 with Join

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

the class SPARQLParserTest method testSES1927UnequalLiteralValueConstants2.

@Test
public void testSES1927UnequalLiteralValueConstants2() throws Exception {
    StringBuilder qb = new StringBuilder();
    qb.append("ASK {?a <foo:bar> \"test\". ?a <foo:foo> \"test\"^^<foo:bar> .} ");
    ParsedQuery q = parser.parseQuery(qb.toString(), null);
    TupleExpr te = q.getTupleExpr();
    assertNotNull(te);
    assertTrue(te instanceof Slice);
    Slice s = (Slice) te;
    assertTrue(s.getArg() instanceof Join);
    Join j = (Join) s.getArg();
    assertTrue(j.getLeftArg() instanceof StatementPattern);
    assertTrue(j.getRightArg() instanceof StatementPattern);
    StatementPattern leftArg = (StatementPattern) j.getLeftArg();
    StatementPattern rightArg = (StatementPattern) j.getRightArg();
    assertFalse(leftArg.getObjectVar().equals(rightArg.getObjectVar()));
    assertNotEquals(leftArg.getObjectVar().getName(), rightArg.getObjectVar().getName());
}
Also used : StatementPattern(org.eclipse.rdf4j.query.algebra.StatementPattern) ParsedQuery(org.eclipse.rdf4j.query.parser.ParsedQuery) Slice(org.eclipse.rdf4j.query.algebra.Slice) Join(org.eclipse.rdf4j.query.algebra.Join) TupleExpr(org.eclipse.rdf4j.query.algebra.TupleExpr) Test(org.junit.Test)

Example 4 with Join

use of org.eclipse.rdf4j.query.algebra.Join 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 5 with Join

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

the class TupleExprBuilder method createTupleExprForNegatedPropertySet.

private TupleExpr createTupleExprForNegatedPropertySet(NegatedPropertySet nps, int index) {
    Var subjVar = nps.getSubjectVar();
    Var predVar = createAnonVar();
    ValueExpr filterCondition = null;
    ValueExpr filterConditionInverse = null;
    // build (inverted) filter conditions for each negated path element.
    for (PropertySetElem elem : nps.getPropertySetElems()) {
        ValueConstant predicate = elem.getPredicate();
        if (elem.isInverse()) {
            Compare compare = new Compare(predVar, predicate, CompareOp.NE);
            if (filterConditionInverse == null) {
                filterConditionInverse = compare;
            } else {
                filterConditionInverse = new And(compare, filterConditionInverse);
            }
        } else {
            Compare compare = new Compare(predVar, predicate, CompareOp.NE);
            if (filterCondition == null) {
                filterCondition = compare;
            } else {
                filterCondition = new And(compare, filterCondition);
            }
        }
    }
    TupleExpr patternMatch = null;
    // one item)
    if (filterCondition != null) {
        for (ValueExpr objVar : nps.getObjectList()) {
            if (patternMatch == null) {
                patternMatch = new StatementPattern(nps.getScope(), subjVar, predVar, (Var) objVar, nps.getContextVar());
            } else {
                patternMatch = new Join(new StatementPattern(nps.getScope(), subjVar, predVar, (Var) objVar, nps.getContextVar()), patternMatch);
            }
        }
    }
    TupleExpr patternMatchInverse = null;
    // one item):
    if (filterConditionInverse != null) {
        for (ValueExpr objVar : nps.getObjectList()) {
            if (patternMatchInverse == null) {
                patternMatchInverse = new StatementPattern(nps.getScope(), (Var) objVar, predVar, subjVar, nps.getContextVar());
            } else {
                patternMatchInverse = new Join(new StatementPattern(nps.getScope(), (Var) objVar, predVar, subjVar, nps.getContextVar()), patternMatchInverse);
            }
        }
    }
    TupleExpr completeMatch = null;
    if (patternMatch != null) {
        completeMatch = new Filter(patternMatch, filterCondition);
    }
    if (patternMatchInverse != null) {
        if (completeMatch == null) {
            completeMatch = new Filter(patternMatchInverse, filterConditionInverse);
        } else {
            completeMatch = new Union(new Filter(patternMatchInverse, filterConditionInverse), completeMatch);
        }
    }
    return completeMatch;
}
Also used : ValueExpr(org.eclipse.rdf4j.query.algebra.ValueExpr) StatementPattern(org.eclipse.rdf4j.query.algebra.StatementPattern) Filter(org.eclipse.rdf4j.query.algebra.Filter) Var(org.eclipse.rdf4j.query.algebra.Var) And(org.eclipse.rdf4j.query.algebra.And) ValueConstant(org.eclipse.rdf4j.query.algebra.ValueConstant) Join(org.eclipse.rdf4j.query.algebra.Join) Compare(org.eclipse.rdf4j.query.algebra.Compare) TupleExpr(org.eclipse.rdf4j.query.algebra.TupleExpr) Union(org.eclipse.rdf4j.query.algebra.Union)

Aggregations

Join (org.eclipse.rdf4j.query.algebra.Join)12 TupleExpr (org.eclipse.rdf4j.query.algebra.TupleExpr)12 Slice (org.eclipse.rdf4j.query.algebra.Slice)6 Filter (org.eclipse.rdf4j.query.algebra.Filter)4 StatementPattern (org.eclipse.rdf4j.query.algebra.StatementPattern)4 And (org.eclipse.rdf4j.query.algebra.And)3 BindingSetAssignment (org.eclipse.rdf4j.query.algebra.BindingSetAssignment)3 Group (org.eclipse.rdf4j.query.algebra.Group)3 LeftJoin (org.eclipse.rdf4j.query.algebra.LeftJoin)3 ParsedQuery (org.eclipse.rdf4j.query.parser.ParsedQuery)3 Test (org.junit.Test)3 ArrayDeque (java.util.ArrayDeque)2 BinaryTupleOperator (org.eclipse.rdf4j.query.algebra.BinaryTupleOperator)2 EmptySet (org.eclipse.rdf4j.query.algebra.EmptySet)2 Projection (org.eclipse.rdf4j.query.algebra.Projection)2 ValueExpr (org.eclipse.rdf4j.query.algebra.ValueExpr)2 AbstractMap (java.util.AbstractMap)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Map (java.util.Map)1