Search in sources :

Example 1 with OpProject

use of org.apache.jena.sparql.algebra.op.OpProject in project jena by apache.

the class TransformOrderByDistinctApplication method transform.

@Override
public Op transform(OpReduced opReduced, Op subOp) {
    if (subOp instanceof OpProject) {
        OpProject project = (OpProject) subOp;
        // Inner operation must be an ORDER BY
        if (project.getSubOp() instanceof OpOrder) {
            List<Var> projectVars = project.getVars();
            OpOrder order = (OpOrder) project.getSubOp();
            // Everything we wish to order by must only use variables that
            // appear in the project list
            boolean ok = true;
            for (SortCondition condition : order.getConditions()) {
                if (!isValidSortCondition(condition, projectVars)) {
                    ok = false;
                    break;
                }
            }
            // Everything checks out so we can make the change
            if (ok) {
                OpProject newProject = new OpProject(order.getSubOp(), project.getVars());
                Op newReduced = OpReduced.create(newProject);
                return new OpOrder(newReduced, order.getConditions());
            }
        }
    }
    // If we reach here then this transform is not applicable
    return super.transform(opReduced, subOp);
}
Also used : SortCondition(org.apache.jena.query.SortCondition) Op(org.apache.jena.sparql.algebra.Op) OpProject(org.apache.jena.sparql.algebra.op.OpProject) OpOrder(org.apache.jena.sparql.algebra.op.OpOrder) Var(org.apache.jena.sparql.core.Var)

Example 2 with OpProject

use of org.apache.jena.sparql.algebra.op.OpProject in project jena by apache.

the class TransformSliceProject method transform.

@Override
public Op transform(OpSlice opSlice, Op subOp) {
    // (slice (project...)) ==> (project (slice ...))
    if (subOp instanceof OpProject) {
        OpProject p = (OpProject) subOp;
        Op sub = p.getSubOp();
        Op x = new OpSlice(sub, opSlice.getStart(), opSlice.getLength());
        x = new OpProject(x, p.getVars());
        return x;
    }
    return super.transform(opSlice, subOp);
}
Also used : Op(org.apache.jena.sparql.algebra.Op) OpProject(org.apache.jena.sparql.algebra.op.OpProject) OpSlice(org.apache.jena.sparql.algebra.op.OpSlice)

Example 3 with OpProject

use of org.apache.jena.sparql.algebra.op.OpProject in project jena by apache.

the class TransformDistinctToReduced method transform1.

// Best is this is after TransformTopN but they are order independent
// TopN of "reduced or distinct of order" is handled.
//@Override
public Op transform1(OpDistinct opDistinct, Op subOp) {
    if (subOp instanceof OpProject) {
        OpProject opProject = (OpProject) subOp;
        if (opProject.getSubOp() instanceof OpOrder) {
            OpOrder opOrder = (OpOrder) opProject.getSubOp();
            Set<Var> projectVars = new HashSet<>(opProject.getVars());
            if (isSafe(projectVars, opOrder)) {
                return OpReduced.create(subOp);
            }
        }
    }
    return super.transform(opDistinct, subOp);
}
Also used : OpProject(org.apache.jena.sparql.algebra.op.OpProject) OpOrder(org.apache.jena.sparql.algebra.op.OpOrder) Var(org.apache.jena.sparql.core.Var) HashSet(java.util.HashSet)

Example 4 with OpProject

use of org.apache.jena.sparql.algebra.op.OpProject in project jena by apache.

the class QueryExecUtils method execute.

public static void execute(Op op, DatasetGraph dsg, ResultsFormat outputFormat) {
    QueryIterator qIter = Algebra.exec(op, dsg);
    List<String> vars = null;
    if (op instanceof OpProject)
        vars = Var.varNames(((OpProject) op).getVars());
    else
        // The variables defined in patterns (not Filters, nor NOT EXISTS,
        // nor ORDER BY)
        vars = Var.varNames(OpVars.visibleVars(op));
    ResultSet results = ResultSetFactory.create(qIter, vars);
    outputResultSet(results, null, outputFormat);
}
Also used : OpProject(org.apache.jena.sparql.algebra.op.OpProject) QueryIterator(org.apache.jena.sparql.engine.QueryIterator)

Example 5 with OpProject

use of org.apache.jena.sparql.algebra.op.OpProject in project jena by apache.

the class TransformDistinctToReduced method transform.

@Override
public Op transform(OpDistinct opDistinct, Op subOp) {
    OpOrder opOrder = null;
    Set<Var> projectVars = null;
    /*   SELECT DISTINCT * {} ORDER BY
         * giving an alegbra expression of the form:  
         *   (distinct
         *     (order 
         */
    if (subOp instanceof OpOrder) {
        opOrder = (OpOrder) subOp;
        projectVars = OpVars.visibleVars(subOp);
    } else if (subOp instanceof OpProject) {
        OpProject opProject = (OpProject) subOp;
        if (opProject.getSubOp() instanceof OpOrder) {
            projectVars = new HashSet<>(opProject.getVars());
            opOrder = (OpOrder) opProject.getSubOp();
        }
    }
    if (projectVars == null)
        return super.transform(opDistinct, subOp);
    if (isSafe(projectVars, opOrder))
        return OpReduced.create(subOp);
    return super.transform(opDistinct, subOp);
}
Also used : OpProject(org.apache.jena.sparql.algebra.op.OpProject) OpOrder(org.apache.jena.sparql.algebra.op.OpOrder) Var(org.apache.jena.sparql.core.Var) HashSet(java.util.HashSet)

Aggregations

OpProject (org.apache.jena.sparql.algebra.op.OpProject)6 OpOrder (org.apache.jena.sparql.algebra.op.OpOrder)4 Var (org.apache.jena.sparql.core.Var)4 HashSet (java.util.HashSet)2 SortCondition (org.apache.jena.query.SortCondition)2 Op (org.apache.jena.sparql.algebra.Op)2 OpDistinct (org.apache.jena.sparql.algebra.op.OpDistinct)1 OpSlice (org.apache.jena.sparql.algebra.op.OpSlice)1 QueryIterator (org.apache.jena.sparql.engine.QueryIterator)1