Search in sources :

Example 1 with OpOrder

use of org.apache.jena.sparql.algebra.op.OpOrder 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 OpOrder

use of org.apache.jena.sparql.algebra.op.OpOrder 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 3 with OpOrder

use of org.apache.jena.sparql.algebra.op.OpOrder 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)

Example 4 with OpOrder

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

the class TransformOrderByDistinctApplication method transform.

@Override
public Op transform(OpDistinct opDistinct, 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());
                OpDistinct newDistinct = new OpDistinct(newProject);
                return new OpOrder(newDistinct, order.getConditions());
            }
        }
    }
    // If we reach here then this transform is not applicable
    return super.transform(opDistinct, subOp);
}
Also used : SortCondition(org.apache.jena.query.SortCondition) OpProject(org.apache.jena.sparql.algebra.op.OpProject) OpOrder(org.apache.jena.sparql.algebra.op.OpOrder) Var(org.apache.jena.sparql.core.Var) OpDistinct(org.apache.jena.sparql.algebra.op.OpDistinct)

Aggregations

OpOrder (org.apache.jena.sparql.algebra.op.OpOrder)4 OpProject (org.apache.jena.sparql.algebra.op.OpProject)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)1 OpDistinct (org.apache.jena.sparql.algebra.op.OpDistinct)1