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;
}
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);
}
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();
}
}
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);
}
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);
}
Aggregations