Search in sources :

Example 1 with Projection

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

the class TupleExprs method containsProjection.

/**
 * Verifies if the supplied {@link TupleExpr} contains a {@link Projection}. If the supplied TupleExpr is
 * a {@link Join} or contains a {@link Join}, projections inside that Join's arguments will not be taken
 * into account.
 *
 * @param t
 *        a tuple expression.
 * @return <code>true</code> if the TupleExpr contains a projection (outside of a Join),
 *         <code>false</code> otherwise.
 * @deprecated Since 2.3. Use {@link TupleExprs#containsSubQuery(TupleExpr)} instead.
 */
@Deprecated
public static boolean containsProjection(TupleExpr t) {
    Deque<TupleExpr> queue = new ArrayDeque<>();
    queue.add(t);
    while (!queue.isEmpty()) {
        TupleExpr n = queue.removeFirst();
        if (n instanceof Projection) {
            return true;
        } else if (n instanceof Join) {
            // taken into account
            return false;
        } else {
            queue.addAll(getChildren(n));
        }
    }
    return false;
}
Also used : Projection(org.eclipse.rdf4j.query.algebra.Projection) Join(org.eclipse.rdf4j.query.algebra.Join) TupleExpr(org.eclipse.rdf4j.query.algebra.TupleExpr) ArrayDeque(java.util.ArrayDeque)

Example 2 with Projection

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

the class TupleExprBuilder method visit.

@Override
public TupleExpr visit(ASTSelect node, Object data) throws VisitorException {
    TupleExpr result = (TupleExpr) data;
    final Order orderClause = result instanceof Order ? (Order) result : null;
    Extension extension = new Extension();
    ProjectionElemList projElemList = new ProjectionElemList();
    GroupFinder groupFinder = new GroupFinder();
    result.visit(groupFinder);
    Group group = groupFinder.getGroup();
    boolean existingGroup = group != null;
    List<String> aliasesInProjection = new ArrayList<String>();
    for (ASTProjectionElem projElemNode : node.getProjectionElemList()) {
        Node child = projElemNode.jjtGetChild(0);
        String alias = projElemNode.getAlias();
        if (alias != null) {
            // aliased projection element
            if (aliasesInProjection.contains(alias)) {
                throw new VisitorException("duplicate use of alias '" + alias + "' in projection.");
            }
            // check if alias is not previously used.
            if (result.getBindingNames().contains(alias)) {
                throw new VisitorException("projection alias '" + alias + "' was previously used");
            }
            aliasesInProjection.add(alias);
            ValueExpr valueExpr = (ValueExpr) child.jjtAccept(this, null);
            String targetName = alias;
            String sourceName = alias;
            if (child instanceof ASTVar) {
                sourceName = ((ASTVar) child).getName();
            }
            ProjectionElem elem = new ProjectionElem(sourceName, targetName);
            projElemList.addElement(elem);
            AggregateCollector collector = new AggregateCollector();
            valueExpr.visit(collector);
            if (collector.getOperators().size() > 0) {
                elem.setAggregateOperatorInExpression(true);
                for (AggregateOperator operator : collector.getOperators()) {
                    // Apply implicit grouping if necessary
                    if (group == null) {
                        group = new Group(result);
                    }
                    if (operator.equals(valueExpr)) {
                        group.addGroupElement(new GroupElem(alias, operator));
                        extension.setArg(group);
                    } else {
                        ValueExpr expr = (ValueExpr) operator.getParentNode();
                        Extension anonymousExtension = new Extension();
                        Var anonVar = createAnonVar();
                        expr.replaceChildNode(operator, anonVar);
                        anonymousExtension.addElement(new ExtensionElem(operator, anonVar.getName()));
                        anonymousExtension.setArg(result);
                        result = anonymousExtension;
                        group.addGroupElement(new GroupElem(anonVar.getName(), operator));
                    }
                    if (!existingGroup) {
                        result = group;
                    }
                }
            }
            // add extension element reference to the projection element and
            // to
            // the extension
            ExtensionElem extElem = new ExtensionElem(valueExpr, alias);
            extension.addElement(extElem);
            elem.setSourceExpression(extElem);
        } else if (child instanceof ASTVar) {
            Var projVar = (Var) child.jjtAccept(this, null);
            ProjectionElem elem = new ProjectionElem(projVar.getName());
            projElemList.addElement(elem);
            VarCollector whereClauseVarCollector = new VarCollector();
            result.visit(whereClauseVarCollector);
            if (!whereClauseVarCollector.collectedVars.contains(projVar)) {
                ExtensionElem extElem = new ExtensionElem(projVar, projVar.getName());
                extension.addElement(extElem);
                elem.setSourceExpression(extElem);
            }
        } else {
            throw new IllegalStateException("required alias for non-Var projection elements not found");
        }
    }
    if (!extension.getElements().isEmpty()) {
        if (orderClause != null) {
            // Extensions produced by SELECT expressions should be nested
            // inside
            // the ORDER BY clause, to make sure
            // sorting can work on the newly introduced variable. See
            // SES-892
            // and SES-1809.
            TupleExpr arg = orderClause.getArg();
            extension.setArg(arg);
            orderClause.setArg(extension);
            result = orderClause;
        } else {
            extension.setArg(result);
            result = extension;
        }
    }
    result = new Projection(result, projElemList);
    if (group != null) {
        for (ProjectionElem elem : projElemList.getElements()) {
            if (!elem.hasAggregateOperatorInExpression()) {
                Set<String> groupNames = group.getBindingNames();
                ExtensionElem extElem = elem.getSourceExpression();
                if (extElem != null) {
                    ValueExpr expr = extElem.getExpr();
                    VarCollector collector = new VarCollector();
                    expr.visit(collector);
                    for (Var var : collector.getCollectedVars()) {
                        if (!groupNames.contains(var.getName())) {
                            throw new VisitorException("variable '" + var.getName() + "' in projection not present in GROUP BY.");
                        }
                    }
                } else {
                    if (!groupNames.contains(elem.getTargetName())) {
                        throw new VisitorException("variable '" + elem.getTargetName() + "' in projection not present in GROUP BY.");
                    } else if (!groupNames.contains(elem.getSourceName())) {
                        throw new VisitorException("variable '" + elem.getSourceName() + "' in projection not present in GROUP BY.");
                    }
                }
            }
        }
    }
    if (node.isSubSelect()) {
        // set context var at the level of the projection. This allows us
        // to distinguish named graphs selected in the
        // outer query from named graphs selected as part of the sub-select.
        ((Projection) result).setProjectionContext(graphPattern.getContextVar());
    }
    if (node.isDistinct()) {
        result = new Distinct(result);
    } else if (node.isReduced()) {
        result = new Reduced(result);
    }
    return result;
}
Also used : ProjectionElemList(org.eclipse.rdf4j.query.algebra.ProjectionElemList) Group(org.eclipse.rdf4j.query.algebra.Group) Var(org.eclipse.rdf4j.query.algebra.Var) QueryModelNode(org.eclipse.rdf4j.query.algebra.QueryModelNode) IsBNode(org.eclipse.rdf4j.query.algebra.IsBNode) ArrayList(java.util.ArrayList) GroupElem(org.eclipse.rdf4j.query.algebra.GroupElem) ExtensionElem(org.eclipse.rdf4j.query.algebra.ExtensionElem) Projection(org.eclipse.rdf4j.query.algebra.Projection) MultiProjection(org.eclipse.rdf4j.query.algebra.MultiProjection) Reduced(org.eclipse.rdf4j.query.algebra.Reduced) Distinct(org.eclipse.rdf4j.query.algebra.Distinct) AggregateOperator(org.eclipse.rdf4j.query.algebra.AggregateOperator) Order(org.eclipse.rdf4j.query.algebra.Order) ValueExpr(org.eclipse.rdf4j.query.algebra.ValueExpr) TupleExpr(org.eclipse.rdf4j.query.algebra.TupleExpr) Extension(org.eclipse.rdf4j.query.algebra.Extension) ProjectionElem(org.eclipse.rdf4j.query.algebra.ProjectionElem)

Example 3 with Projection

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

the class TupleExprBuilder method visit.

@Override
public TupleExpr visit(ASTConstruct node, Object data) throws VisitorException {
    TupleExpr result = (TupleExpr) data;
    // Collect construct triples
    graphPattern = new GraphPattern();
    super.visit(node, null);
    TupleExpr constructExpr = graphPattern.buildTupleExpr();
    // Retrieve all StatementPatterns from the construct expression
    List<StatementPattern> statementPatterns = StatementPatternCollector.process(constructExpr);
    if (constructExpr instanceof Filter) {
        // sameTerm filters in construct (this can happen when there's a
        // cyclic
        // path defined, see SES-1685 and SES-2104)
        // we remove the sameTerm filters by simply replacing all mapped
        // variable occurrences
        Set<SameTerm> sameTermConstraints = getSameTermConstraints((Filter) constructExpr);
        statementPatterns = replaceSameTermVars(statementPatterns, sameTermConstraints);
    }
    Set<Var> constructVars = getConstructVars(statementPatterns);
    VarCollector whereClauseVarCollector = new VarCollector();
    result.visit(whereClauseVarCollector);
    // Create BNodeGenerators for all anonymous variables
    // NB: preserve order for a deterministic output
    Map<Var, ExtensionElem> extElemMap = new LinkedHashMap<Var, ExtensionElem>();
    for (Var var : constructVars) {
        if (var.isAnonymous() && !extElemMap.containsKey(var)) {
            ValueExpr valueExpr;
            if (var.hasValue()) {
                valueExpr = new ValueConstant(var.getValue());
            } else {
                valueExpr = new BNodeGenerator();
            }
            extElemMap.put(var, new ExtensionElem(valueExpr, var.getName()));
        } else if (!whereClauseVarCollector.collectedVars.contains(var)) {
            // non-anon var in construct clause not present in where clause
            if (!extElemMap.containsKey(var)) {
                // assign non-anonymous vars not present in where clause as
                // extension elements. This is necessary to make external
                // binding
                // assingnment possible (see SES-996)
                extElemMap.put(var, new ExtensionElem(var, var.getName()));
            }
        }
    }
    if (!extElemMap.isEmpty()) {
        result = new Extension(result, extElemMap.values());
    }
    // Create a Projection for each StatementPattern in the constructor
    List<ProjectionElemList> projList = new ArrayList<ProjectionElemList>();
    for (StatementPattern sp : statementPatterns) {
        ProjectionElemList projElemList = new ProjectionElemList();
        projElemList.addElement(new ProjectionElem(sp.getSubjectVar().getName(), "subject"));
        projElemList.addElement(new ProjectionElem(sp.getPredicateVar().getName(), "predicate"));
        projElemList.addElement(new ProjectionElem(sp.getObjectVar().getName(), "object"));
        if (sp.getContextVar() != null) {
            projElemList.addElement(new ProjectionElem(sp.getContextVar().getName(), "context"));
        }
        projList.add(projElemList);
    }
    if (projList.size() == 1) {
        result = new Projection(result, projList.get(0));
    } else if (projList.size() > 1) {
        result = new MultiProjection(result, projList);
    } else {
        // Empty constructor
        result = new EmptySet();
    }
    return new Reduced(result);
}
Also used : ProjectionElemList(org.eclipse.rdf4j.query.algebra.ProjectionElemList) Var(org.eclipse.rdf4j.query.algebra.Var) EmptySet(org.eclipse.rdf4j.query.algebra.EmptySet) ArrayList(java.util.ArrayList) ExtensionElem(org.eclipse.rdf4j.query.algebra.ExtensionElem) Projection(org.eclipse.rdf4j.query.algebra.Projection) MultiProjection(org.eclipse.rdf4j.query.algebra.MultiProjection) Reduced(org.eclipse.rdf4j.query.algebra.Reduced) LinkedHashMap(java.util.LinkedHashMap) StatementPattern(org.eclipse.rdf4j.query.algebra.StatementPattern) MultiProjection(org.eclipse.rdf4j.query.algebra.MultiProjection) ValueExpr(org.eclipse.rdf4j.query.algebra.ValueExpr) SameTerm(org.eclipse.rdf4j.query.algebra.SameTerm) TupleExpr(org.eclipse.rdf4j.query.algebra.TupleExpr) Extension(org.eclipse.rdf4j.query.algebra.Extension) BNodeGenerator(org.eclipse.rdf4j.query.algebra.BNodeGenerator) Filter(org.eclipse.rdf4j.query.algebra.Filter) ValueConstant(org.eclipse.rdf4j.query.algebra.ValueConstant) ProjectionElem(org.eclipse.rdf4j.query.algebra.ProjectionElem)

Example 4 with Projection

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

the class SPARQLParserTest method testParseWildcardSubselectInUpdate.

/**
 * Verify that an INSERT with a subselect using a wildcard correctly adds vars to projection
 * @see <a href="https://github.com/eclipse/rdf4j/issues/686">#686</a>
 */
@Test
public void testParseWildcardSubselectInUpdate() throws Exception {
    StringBuilder update = new StringBuilder();
    update.append("INSERT { <urn:a> <urn:b> <urn:c> . } WHERE { SELECT * {?s ?p ?o } }");
    ParsedUpdate parsedUpdate = parser.parseUpdate(update.toString(), null);
    List<UpdateExpr> exprs = parsedUpdate.getUpdateExprs();
    assertEquals(1, exprs.size());
    UpdateExpr expr = exprs.get(0);
    assertTrue(expr instanceof Modify);
    Modify m = (Modify) expr;
    TupleExpr whereClause = m.getWhereExpr();
    assertTrue(whereClause instanceof Projection);
    ProjectionElemList projectionElemList = ((Projection) whereClause).getProjectionElemList();
    assertNotNull(projectionElemList);
    List<ProjectionElem> elements = projectionElemList.getElements();
    assertNotNull(elements);
    assertEquals("projection should contain all three variables", 3, elements.size());
}
Also used : ProjectionElemList(org.eclipse.rdf4j.query.algebra.ProjectionElemList) ParsedUpdate(org.eclipse.rdf4j.query.parser.ParsedUpdate) UpdateExpr(org.eclipse.rdf4j.query.algebra.UpdateExpr) Projection(org.eclipse.rdf4j.query.algebra.Projection) Modify(org.eclipse.rdf4j.query.algebra.Modify) TupleExpr(org.eclipse.rdf4j.query.algebra.TupleExpr) ProjectionElem(org.eclipse.rdf4j.query.algebra.ProjectionElem) Test(org.junit.Test)

Example 5 with Projection

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

the class SPARQLParserTest method testOrderByWithAliases1.

@Test
public void testOrderByWithAliases1() throws Exception {
    String queryString = " SELECT ?x (SUM(?v1)/COUNT(?v1) as ?r) WHERE { ?x <urn:foo> ?v1 } GROUP BY ?x ORDER BY ?r";
    ParsedQuery query = parser.parseQuery(queryString, null);
    assertNotNull(query);
    TupleExpr te = query.getTupleExpr();
    assertTrue(te instanceof Projection);
    te = ((Projection) te).getArg();
    assertTrue(te instanceof Order);
    te = ((Order) te).getArg();
    assertTrue(te instanceof Extension);
}
Also used : Order(org.eclipse.rdf4j.query.algebra.Order) Extension(org.eclipse.rdf4j.query.algebra.Extension) ParsedQuery(org.eclipse.rdf4j.query.parser.ParsedQuery) Projection(org.eclipse.rdf4j.query.algebra.Projection) TupleExpr(org.eclipse.rdf4j.query.algebra.TupleExpr) Test(org.junit.Test)

Aggregations

Projection (org.eclipse.rdf4j.query.algebra.Projection)14 TupleExpr (org.eclipse.rdf4j.query.algebra.TupleExpr)13 ProjectionElem (org.eclipse.rdf4j.query.algebra.ProjectionElem)9 ProjectionElemList (org.eclipse.rdf4j.query.algebra.ProjectionElemList)9 Extension (org.eclipse.rdf4j.query.algebra.Extension)8 MultiProjection (org.eclipse.rdf4j.query.algebra.MultiProjection)7 ExtensionElem (org.eclipse.rdf4j.query.algebra.ExtensionElem)6 ValueExpr (org.eclipse.rdf4j.query.algebra.ValueExpr)6 Var (org.eclipse.rdf4j.query.algebra.Var)6 Distinct (org.eclipse.rdf4j.query.algebra.Distinct)5 Reduced (org.eclipse.rdf4j.query.algebra.Reduced)5 ArrayList (java.util.ArrayList)4 Test (org.junit.Test)4 BNodeGenerator (org.eclipse.rdf4j.query.algebra.BNodeGenerator)3 EmptySet (org.eclipse.rdf4j.query.algebra.EmptySet)3 StatementPattern (org.eclipse.rdf4j.query.algebra.StatementPattern)3 ValueConstant (org.eclipse.rdf4j.query.algebra.ValueConstant)3 ArrayDeque (java.util.ArrayDeque)2 HashMap (java.util.HashMap)2 Join (org.eclipse.rdf4j.query.algebra.Join)2