Search in sources :

Example 11 with SortCondition

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

the class SDB_QC method queryOutVars.

/** Find the variables needed out of this query.
     * If we don't do sorting in-DB, then we need the ORDER BY variables as well. 
     * @param query
     */
public static List<Var> queryOutVars(Query query) {
    // If part query, need all variables. 
    // Project variables
    List<Var> vars = toList(map(query.getResultVars().iterator(), Var::alloc));
    // Add the ORDER BY variables
    List<SortCondition> orderConditions = query.getOrderBy();
    if (orderConditions != null) {
        for (SortCondition sc : orderConditions) {
            Set<Var> x = sc.getExpression().getVarsMentioned();
            for (Var v : x) {
                if (!vars.contains(v))
                    vars.add(v);
            }
        }
    }
    return vars;
}
Also used : SortCondition(org.apache.jena.query.SortCondition) Var(org.apache.jena.sparql.core.Var)

Example 12 with SortCondition

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

the class ApplyTransformVisitor method visit.

// Interact with WalkerVisitor.
@Override
public void visit(OpOrder opOrder) {
    List<SortCondition> conditions = opOrder.getConditions();
    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;
    }
    OpOrder x = opOrder;
    if (changed)
        x = new OpOrder(opOrder.getSubOp(), conditions2);
    visit1(x);
}
Also used : SortCondition(org.apache.jena.query.SortCondition)

Example 13 with SortCondition

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

the class QuerySerializer method visitOrderBy.

@Override
public void visitOrderBy(Query query) {
    if (query.hasOrderBy()) {
        out.print("ORDER BY ");
        boolean first = true;
        for (SortCondition sc : query.getOrderBy()) {
            if (!first)
                out.print(" ");
            sc.format(fmtExpr, out);
            first = false;
        }
        out.println();
    }
}
Also used : SortCondition(org.apache.jena.query.SortCondition)

Example 14 with SortCondition

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

the class BuilderOp method scBuilder.

SortCondition scBuilder(Item item) {
    int direction = Query.ORDER_DEFAULT;
    if (item.isTagged("asc") || item.isTagged("desc")) {
        BuilderLib.checkList(item);
        BuilderLib.checkLength(2, item.getList(), "Direction corrupt");
        if (item.isTagged("asc"))
            direction = Query.ORDER_ASCENDING;
        else
            direction = Query.ORDER_DESCENDING;
        item = item.getList().get(1);
    }
    Expr expr = BuilderExpr.buildExpr(item);
    if (expr.isVariable())
        return new SortCondition(expr.getExprVar().asVar(), direction);
    else
        return new SortCondition(expr, direction);
}
Also used : SortCondition(org.apache.jena.query.SortCondition) Expr(org.apache.jena.sparql.expr.Expr)

Example 15 with SortCondition

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

the class TransformEliminateAssignments method transform.

@Override
public Op transform(OpOrder opOrder, Op subOp) {
    if (!this.isApplicable())
        return super.transform(opOrder, subOp);
    // See what vars are used in the sort conditions
    Collection<Var> vars = new ArrayList<>();
    for (SortCondition cond : opOrder.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 order expression
        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(opOrder.getConditions(), conditions, var);
            this.tracker.getAssignments().remove(var);
        }
    }
    // Create a new order if we've substituted any expressions
    if (conditions != null) {
        return new OpOrder(subOp, conditions);
    }
    return super.transform(opOrder, 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