Search in sources :

Example 6 with SortCondition

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

the class AlgebraGenerator method compileModifiers.

/** Compile query modifiers */
protected Op compileModifiers(Query query, Op pattern) {
    /* The modifier order in algebra is:
          * 
          * Limit/Offset
          *   Distinct/reduce
          *     project
          *       OrderBy
          *         Bindings
          *           having
          *             select expressions
          *               group
          */
    // Preparation: sort SELECT clause into assignments and projects.
    VarExprList projectVars = query.getProject();
    // Assignments to be done.
    VarExprList exprs = new VarExprList();
    // projection variables
    List<Var> vars = new ArrayList<>();
    Op op = pattern;
    if (query.hasGroupBy()) {
        // When there is no GroupBy but there are some aggregates, it's a group of no variables.
        op = OpGroup.create(op, query.getGroupBy(), query.getAggregators());
    }
    // Look for assignments in SELECT expressions.
    if (!projectVars.isEmpty() && !query.isQueryResultStar()) {
        // through in SELECT *
        if (projectVars.size() == 0 && query.isSelectType())
            Log.warn(this, "No project variables");
        // Separate assignments and variable projection.
        for (Var v : query.getProject().getVars()) {
            Expr e = query.getProject().getExpr(v);
            if (e != null) {
                Expr e2 = ExprLib.replaceAggregateByVariable(e);
                exprs.add(v, e2);
            }
            // Include in project
            vars.add(v);
        }
    }
    // ---- Assignments from SELECT and other places (so available to ORDER and HAVING)
    for (Var v : exprs.getVars()) {
        Expr e = exprs.getExpr(v);
        op = OpExtend.create(op, v, e);
    }
    // ---- HAVING
    if (query.hasHaving()) {
        for (Expr expr : query.getHavingExprs()) {
            // HAVING expression to refer to the aggregate via the variable.
            Expr expr2 = ExprLib.replaceAggregateByVariable(expr);
            op = OpFilter.filter(expr2, op);
        }
    }
    // ---- VALUES
    if (query.hasValues()) {
        Table table = TableFactory.create(query.getValuesVariables());
        for (Binding binding : query.getValuesData()) table.addBinding(binding);
        OpTable opTable = OpTable.create(table);
        op = OpJoin.create(op, opTable);
    }
    // ---- ToList
    if (context.isTrue(ARQ.generateToList))
        // Listify it.
        op = new OpList(op);
    // ---- ORDER BY
    if (query.getOrderBy() != null) {
        List<SortCondition> scList = new ArrayList<>();
        // Aggregates in ORDER BY
        for (SortCondition sc : query.getOrderBy()) {
            Expr e = sc.getExpression();
            e = ExprLib.replaceAggregateByVariable(e);
            scList.add(new SortCondition(e, sc.getDirection()));
        }
        op = new OpOrder(op, scList);
    }
    if (vars.size() > 0)
        op = new OpProject(op, vars);
    // ---- DISTINCT
    if (query.isDistinct())
        op = OpDistinct.create(op);
    // ---- REDUCED
    if (query.isReduced())
        op = OpReduced.create(op);
    // ---- LIMIT/OFFSET
    if (query.hasLimit() || query.hasOffset())
        op = new OpSlice(op, query.getOffset(), /*start*/
        query.getLimit());
    return op;
}
Also used : Binding(org.apache.jena.sparql.engine.binding.Binding) SortCondition(org.apache.jena.query.SortCondition)

Example 7 with SortCondition

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

the class BindingComparator method compare.

// Compare bindings by iterating.
// Node comparsion is:
//  Compare by 
@Override
public int compare(Binding bind1, Binding bind2) {
    for (SortCondition sc : conditions) {
        if (sc.expression == null) {
            throw new QueryExecException("Broken sort condition");
        }
        NodeValue nv1 = null;
        NodeValue nv2 = null;
        try {
            nv1 = sc.expression.eval(bind1, env);
        } catch (VariableNotBoundException ex) {
        } catch (ExprEvalException ex) {
            Log.warn(this, ex.getMessage());
        }
        try {
            nv2 = sc.expression.eval(bind2, env);
        } catch (VariableNotBoundException ex) {
        } catch (ExprEvalException ex) {
            Log.warn(this, ex.getMessage());
        }
        Node n1 = NodeValue.toNode(nv1);
        Node n2 = NodeValue.toNode(nv2);
        int x = compareNodes(nv1, nv2, sc.direction);
        if (x != Expr.CMP_EQUAL) {
            return x;
        }
    }
    // Same by the SortConditions - now do any extra tests to make sure they are unique.
    return compareBindingsSyntactic(bind1, bind2);
//return 0 ;
}
Also used : SortCondition(org.apache.jena.query.SortCondition) NodeValue(org.apache.jena.sparql.expr.NodeValue) VariableNotBoundException(org.apache.jena.sparql.expr.VariableNotBoundException) Node(org.apache.jena.graph.Node) QueryExecException(org.apache.jena.query.QueryExecException) ExprEvalException(org.apache.jena.sparql.expr.ExprEvalException)

Example 8 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 9 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 10 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)

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