Search in sources :

Example 6 with Distinct

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

the class QueryModelBuilder method visit.

@Override
public TupleExpr visit(ASTSelect node, Object data) throws VisitorException {
    TupleExpr result = (TupleExpr) data;
    Extension extension = new Extension();
    ProjectionElemList projElemList = new ProjectionElemList();
    for (ASTProjectionElem projElemNode : node.getProjectionElemList()) {
        ValueExpr valueExpr = (ValueExpr) projElemNode.getValueExpr().jjtAccept(this, null);
        String alias = projElemNode.getAlias();
        if (alias != null) {
            // aliased projection element
            extension.addElement(new ExtensionElem(valueExpr, alias));
            projElemList.addElement(new ProjectionElem(alias));
        } else if (valueExpr instanceof Var) {
            // unaliased variable
            Var projVar = (Var) valueExpr;
            projElemList.addElement(new ProjectionElem(projVar.getName()));
        } else {
            throw new IllegalStateException("required alias for non-Var projection elements not found");
        }
    }
    if (!extension.getElements().isEmpty()) {
        extension.setArg(result);
        result = extension;
    }
    result = new Projection(result, projElemList);
    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) ValueExpr(org.eclipse.rdf4j.query.algebra.ValueExpr) ASTValueExpr(org.eclipse.rdf4j.query.parser.serql.ast.ASTValueExpr) ASTVar(org.eclipse.rdf4j.query.parser.serql.ast.ASTVar) Var(org.eclipse.rdf4j.query.algebra.Var) ExtensionElem(org.eclipse.rdf4j.query.algebra.ExtensionElem) Projection(org.eclipse.rdf4j.query.algebra.Projection) ASTString(org.eclipse.rdf4j.query.parser.serql.ast.ASTString) ASTProjectionElem(org.eclipse.rdf4j.query.parser.serql.ast.ASTProjectionElem) TupleExpr(org.eclipse.rdf4j.query.algebra.TupleExpr) Reduced(org.eclipse.rdf4j.query.algebra.Reduced) Extension(org.eclipse.rdf4j.query.algebra.Extension) Distinct(org.eclipse.rdf4j.query.algebra.Distinct) ProjectionElem(org.eclipse.rdf4j.query.algebra.ProjectionElem) ASTProjectionElem(org.eclipse.rdf4j.query.parser.serql.ast.ASTProjectionElem)

Example 7 with Distinct

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

the class ConstructorBuilder method buildConstructor.

private TupleExpr buildConstructor(TupleExpr bodyExpr, TupleExpr constructExpr, boolean explicitConstructor, boolean distinct, boolean reduced) {
    TupleExpr result = bodyExpr;
    // Retrieve all StatementPattern's from the construct expression
    List<StatementPattern> statementPatterns = StatementPatternCollector.process(constructExpr);
    Set<Var> constructVars = getConstructVars(statementPatterns);
    // Finally, the spo-bindings are again filtered for duplicates.
    if (distinct || reduced) {
        // Create projection that removes all bindings that are not used in the
        // constructor
        ProjectionElemList projElemList = new ProjectionElemList();
        for (Var var : constructVars) {
            // the distinct
            if (!var.isAnonymous() && !var.hasValue()) {
                projElemList.addElement(new ProjectionElem(var.getName()));
            }
        }
        result = new Projection(result, projElemList);
        // Filter the duplicates from these projected bindings
        if (distinct) {
            result = new Distinct(result);
        } else {
            result = new Reduced(result);
        }
    }
    // Create BNodeGenerator's for all anonymous variables
    Map<Var, ExtensionElem> extElemMap = new HashMap<Var, ExtensionElem>();
    for (Var var : constructVars) {
        if (var.isAnonymous() && !extElemMap.containsKey(var)) {
            ValueExpr valueExpr = null;
            if (var.hasValue()) {
                valueExpr = new ValueConstant(var.getValue());
            } else if (explicitConstructor) {
                // only generate bnodes in case of an explicit constructor
                valueExpr = new BNodeGenerator();
            }
            if (valueExpr != null) {
                extElemMap.put(var, new ExtensionElem(valueExpr, var.getName()));
            }
        }
    }
    if (!extElemMap.isEmpty()) {
        result = new Extension(result, extElemMap.values());
    }
    // Create a Projection for each StatementPattern in the constructor
    List<ProjectionElemList> projections = 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"));
        projections.add(projElemList);
    }
    if (projections.size() == 1) {
        result = new Projection(result, projections.get(0));
    // Note: no need to apply the second duplicate elimination step if
    // there's just one projection
    } else if (projections.size() > 1) {
        result = new MultiProjection(result, projections);
        if (distinct) {
            // Add another distinct to filter duplicate statements
            result = new Distinct(result);
        } else if (reduced) {
            result = new Reduced(result);
        }
    } else {
        // Empty constructor
        result = new EmptySet();
    }
    return result;
}
Also used : ProjectionElemList(org.eclipse.rdf4j.query.algebra.ProjectionElemList) ValueExpr(org.eclipse.rdf4j.query.algebra.ValueExpr) HashMap(java.util.HashMap) Var(org.eclipse.rdf4j.query.algebra.Var) EmptySet(org.eclipse.rdf4j.query.algebra.EmptySet) ArrayList(java.util.ArrayList) MultiProjection(org.eclipse.rdf4j.query.algebra.MultiProjection) Projection(org.eclipse.rdf4j.query.algebra.Projection) ExtensionElem(org.eclipse.rdf4j.query.algebra.ExtensionElem) TupleExpr(org.eclipse.rdf4j.query.algebra.TupleExpr) Reduced(org.eclipse.rdf4j.query.algebra.Reduced) Extension(org.eclipse.rdf4j.query.algebra.Extension) StatementPattern(org.eclipse.rdf4j.query.algebra.StatementPattern) Distinct(org.eclipse.rdf4j.query.algebra.Distinct) BNodeGenerator(org.eclipse.rdf4j.query.algebra.BNodeGenerator) ValueConstant(org.eclipse.rdf4j.query.algebra.ValueConstant) MultiProjection(org.eclipse.rdf4j.query.algebra.MultiProjection) ProjectionElem(org.eclipse.rdf4j.query.algebra.ProjectionElem)

Example 8 with Distinct

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

the class AbstractQueryBuilder method query.

/**
 * @inheritDoc
 */
public T query() {
    UnaryTupleOperator aRoot = null;
    UnaryTupleOperator aCurr = null;
    if (mLimit != -1 || mOffset != -1) {
        Slice aSlice = new Slice();
        if (mLimit != -1) {
            aSlice.setLimit(mLimit);
        }
        if (mOffset != -1) {
            aSlice.setOffset(mOffset);
        }
        aRoot = aCurr = aSlice;
    }
    if (mOrderByElems != null && !mOrderByElems.isEmpty()) {
        Order aOrder = new Order();
        aOrder.addElements(mOrderByElems);
        if (aRoot == null) {
            aRoot = aCurr = aOrder;
        } else {
            aCurr.setArg(aOrder);
            aCurr = aOrder;
        }
    }
    if (mDistinct) {
        Distinct aDistinct = new Distinct();
        if (aRoot == null) {
            aRoot = aCurr = aDistinct;
        } else {
            aCurr.setArg(aDistinct);
            aCurr = aDistinct;
        }
    }
    if (mReduced) {
        Reduced aReduced = new Reduced();
        if (aRoot == null) {
            aRoot = aCurr = aReduced;
        } else {
            aCurr.setArg(aReduced);
            aCurr = aReduced;
        }
    }
    TupleExpr aJoin = join();
    if (mQuery instanceof ParsedTupleQuery && mProjectionVars.isEmpty()) {
        VarNameCollector aCollector = new VarNameCollector();
        aJoin.visit(aCollector);
        mProjectionVars.addAll(aCollector.getVarNames());
    } else if (mQuery instanceof ParsedGraphQuery && mProjectionPatterns.isEmpty()) {
        StatementPatternCollector aCollector = new StatementPatternCollector();
        aJoin.visit(aCollector);
        mProjectionPatterns.addAll(aCollector.getStatementPatterns());
    }
    UnaryTupleOperator aProjection = projection();
    if (aRoot == null) {
        aRoot = aCurr = aProjection;
    } else {
        aCurr.setArg(aProjection);
    }
    if (aProjection.getArg() == null) {
        aCurr = aProjection;
    } else {
        // I think this is always a safe cast
        aCurr = (UnaryTupleOperator) aProjection.getArg();
    }
    if (aJoin != null) {
        aCurr.setArg(aJoin);
    }
    mQuery.setTupleExpr(aRoot);
    if (!mFrom.isEmpty() || !mFromNamed.isEmpty()) {
        SimpleDataset aDataset = new SimpleDataset();
        for (IRI aFrom : mFrom) {
            aDataset.addDefaultGraph(aFrom);
        }
        for (IRI aFrom : mFromNamed) {
            aDataset.addNamedGraph(aFrom);
        }
        mQuery.setDataset(aDataset);
    }
    return mQuery;
}
Also used : Order(org.eclipse.rdf4j.query.algebra.Order) StatementPatternCollector(org.eclipse.rdf4j.query.algebra.helpers.StatementPatternCollector) IRI(org.eclipse.rdf4j.model.IRI) Distinct(org.eclipse.rdf4j.query.algebra.Distinct) UnaryTupleOperator(org.eclipse.rdf4j.query.algebra.UnaryTupleOperator) Slice(org.eclipse.rdf4j.query.algebra.Slice) ParsedGraphQuery(org.eclipse.rdf4j.query.parser.ParsedGraphQuery) VarNameCollector(org.eclipse.rdf4j.query.algebra.helpers.VarNameCollector) SimpleDataset(org.eclipse.rdf4j.query.impl.SimpleDataset) Reduced(org.eclipse.rdf4j.query.algebra.Reduced) TupleExpr(org.eclipse.rdf4j.query.algebra.TupleExpr) ParsedTupleQuery(org.eclipse.rdf4j.query.parser.ParsedTupleQuery)

Aggregations

Distinct (org.eclipse.rdf4j.query.algebra.Distinct)8 TupleExpr (org.eclipse.rdf4j.query.algebra.TupleExpr)8 Projection (org.eclipse.rdf4j.query.algebra.Projection)5 ProjectionElem (org.eclipse.rdf4j.query.algebra.ProjectionElem)5 ProjectionElemList (org.eclipse.rdf4j.query.algebra.ProjectionElemList)5 Reduced (org.eclipse.rdf4j.query.algebra.Reduced)5 Extension (org.eclipse.rdf4j.query.algebra.Extension)4 ExtensionElem (org.eclipse.rdf4j.query.algebra.ExtensionElem)4 MultiProjection (org.eclipse.rdf4j.query.algebra.MultiProjection)4 ValueExpr (org.eclipse.rdf4j.query.algebra.ValueExpr)4 Var (org.eclipse.rdf4j.query.algebra.Var)4 ArrayList (java.util.ArrayList)3 Union (org.eclipse.rdf4j.query.algebra.Union)3 HashMap (java.util.HashMap)2 BNodeGenerator (org.eclipse.rdf4j.query.algebra.BNodeGenerator)2 EmptySet (org.eclipse.rdf4j.query.algebra.EmptySet)2 Order (org.eclipse.rdf4j.query.algebra.Order)2 StatementPattern (org.eclipse.rdf4j.query.algebra.StatementPattern)2 ValueConstant (org.eclipse.rdf4j.query.algebra.ValueConstant)2 ASTGraphUnion (org.eclipse.rdf4j.query.parser.serql.ast.ASTGraphUnion)2