Search in sources :

Example 1 with SortCondition

use of org.apache.jena.query.SortCondition in project jena by apache.

the class ApplyTransformVisitor method transform.

protected List<SortCondition> transform(List<SortCondition> conditions) {
    List<SortCondition> conditions2 = new ArrayList<>();
    boolean changed = false;
    for (SortCondition sc : conditions) {
        Expr e = sc.getExpression();
        Expr e2 = transform(e);
        conditions2.add(new SortCondition(e2, sc.getDirection()));
        if (e != e2)
            changed = true;
    }
    if (changed)
        return conditions2;
    else
        return conditions;
}
Also used : SortCondition(org.apache.jena.query.SortCondition)

Example 2 with SortCondition

use of org.apache.jena.query.SortCondition 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 3 with SortCondition

use of org.apache.jena.query.SortCondition in project jena by apache.

the class VariableUsageVisitor method visit.

@Override
public void visit(OpTopN opTop) {
    Collection<Var> vars = new ArrayList<>();
    for (SortCondition condition : opTop.getConditions()) {
        ExprVars.varsMentioned(vars, condition);
    }
    action(vars);
}
Also used : SortCondition(org.apache.jena.query.SortCondition) Var(org.apache.jena.sparql.core.Var) ArrayList(java.util.ArrayList)

Example 4 with SortCondition

use of org.apache.jena.query.SortCondition in project jena by apache.

the class VariableUsageVisitor method visit.

@Override
public void visit(OpOrder opOrder) {
    Collection<Var> vars = new ArrayList<>();
    for (SortCondition condition : opOrder.getConditions()) {
        ExprVars.varsMentioned(vars, condition);
    }
    action(vars);
}
Also used : SortCondition(org.apache.jena.query.SortCondition) Var(org.apache.jena.sparql.core.Var) ArrayList(java.util.ArrayList)

Example 5 with SortCondition

use of org.apache.jena.query.SortCondition in project jena by apache.

the class TransformEliminateAssignments method transform.

@Override
public Op transform(OpTopN opTop, Op subOp) {
    if (!this.isApplicable())
        return super.transform(opTop, subOp);
    // See what vars are used in the sort conditions
    Collection<Var> vars = new ArrayList<>();
    for (SortCondition cond : opTop.getConditions()) {
        ExprVars.varsMentioned(vars, cond.getExpression());
    }
    // Are any of these vars single usage?
    List<SortCondition> conditions = null;
    for (Var var : vars) {
        // Usage count will be 2 if we can eliminate the assignment
        // First usage is when it is introduced by the assignment and the
        // second is when it is used now in this filter
        Expr e = getAssignExpr(var);
        if (this.tracker.getUsageCount(var) == 2 && hasAssignment(var) && canInline(e) && shouldInline(e)) {
            // Can go back and eliminate that assignment
            subOp = eliminateAssignment(subOp, var);
            // Replace the variable usage with the expression within the
            // sort conditions
            conditions = processConditions(opTop.getConditions(), conditions, var);
            this.tracker.getAssignments().remove(var);
        }
    }
    // Create a new order if we've substituted any expressions
    if (conditions != null) {
        return new OpTopN(subOp, opTop.getLimit(), conditions);
    }
    return super.transform(opTop, subOp);
}
Also used : SortCondition(org.apache.jena.query.SortCondition) Var(org.apache.jena.sparql.core.Var)

Aggregations

SortCondition (org.apache.jena.query.SortCondition)23 Var (org.apache.jena.sparql.core.Var)10 ArrayList (java.util.ArrayList)5 Binding (org.apache.jena.sparql.engine.binding.Binding)4 BindingComparator (org.apache.jena.sparql.engine.binding.BindingComparator)4 ExprVar (org.apache.jena.sparql.expr.ExprVar)3 SortedDataBag (org.apache.jena.atlas.data.SortedDataBag)2 Op (org.apache.jena.sparql.algebra.Op)2 OpOrder (org.apache.jena.sparql.algebra.op.OpOrder)2 OpProject (org.apache.jena.sparql.algebra.op.OpProject)2 Expr (org.apache.jena.sparql.expr.Expr)2 File (java.io.File)1 HashSet (java.util.HashSet)1 Random (java.util.Random)1 ExprRewriter (org.apache.jena.arq.querybuilder.rewriters.ExprRewriter)1 Node (org.apache.jena.graph.Node)1 QueryCancelledException (org.apache.jena.query.QueryCancelledException)1 QueryExecException (org.apache.jena.query.QueryExecException)1 OpDistinct (org.apache.jena.sparql.algebra.op.OpDistinct)1 VarExprList (org.apache.jena.sparql.core.VarExprList)1