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 ; }
/** * Extracts any {@code SortCondition} instances from the SPARQL query and holds them in an index of their keys * where the value is that keys sorting direction. */ private static Map<String, Order> createOrderIndexFromQuery(final Query query) { final Map<String, Order> orderingIndex = new HashMap<>(); if (query.hasOrderBy()) { final List<SortCondition> sortingConditions = query.getOrderBy(); for (SortCondition sortCondition : sortingConditions) { final Expr expr = sortCondition.getExpression(); // by default, the sort will be ascending. getDirection() returns -2 if the DESC/ASC isn't // supplied - weird orderingIndex.put(expr.getVarName(), sortCondition.getDirection() == -1 ? Order.decr : Order.incr); } } return orderingIndex; }
public void addOrderBy(Expr expr, int direction) { SortCondition sc = new SortCondition(expr, direction) ; addOrderBy(sc) ; }
public static void varsMentioned(Collection<Var> acc, SortCondition sortCondition) { varsMentioned(acc, sortCondition.getExpression()); }
@Override public void output(IndentedWriter out, SerializationContext sCxt) { if ( sCxt == null ) sCxt = new SerializationContext() ; FmtExprSPARQL fmt = new FmtExprSPARQL(out, sCxt) ; format(fmt, out) ; } }
@Override public boolean equals(Object other) { if ( this == other ) return true ; if ( ! ( other instanceof SortCondition ) ) return false ; SortCondition sc = (SortCondition)other ; if ( sc.getDirection() != this.getDirection() ) return false ; if ( ! Objects.equals(this.getExpression(), sc.getExpression()) ) return false ; // if ( ! Utils.eq(this.getVariable(), sc.getVariable()) ) // return false ; return true ; }
public SparqlSelectFunction<T> addOrderBy(Expr expr, int direction) { SortCondition sc = new SortCondition(expr, direction) ; addOrderBy(sc) ; return this; }
/** * Determines whether a sort condition is valid in terms of this optimizer * * @param cond * Sort Condition * @param projectVars * Project Variables * @return True if valid, false otherwise */ private boolean isValidSortCondition(SortCondition cond, List<Var> projectVars) { if (cond.getExpression().isVariable()) { return projectVars.contains(cond.getExpression().asVar()); } else { for (Var v : cond.getExpression().getVarsMentioned()) { if (!projectVars.contains(v)) return false; } return true; } } }
@Override public void output(IndentedWriter out) { out.print(Plan.startMarker) ; out.print("SortCondition ") ; FmtExprSPARQL fmt = new FmtExprSPARQL(out, null) ; format(fmt, out) ; out.print(Plan.finishMarker) ; }
public final SortCondition rewrite(SortCondition sortCondition) { sortCondition.getExpression().visit(this); return new SortCondition(pop(), sortCondition.getDirection()); }
@Override public int hashCode() { int x = this.getDirection() ; if ( getExpression() != null ) x ^= getExpression().hashCode() ; return x ; }
private static void transformSortConditions(List<SortCondition> conditions, ExprTransform exprTransform) { for (int i = 0; i < conditions.size(); i++) { SortCondition s1 = conditions.get(i); Expr e = ExprTransformer.transform(exprTransform, s1.expression); if (e == null || s1.expression.equals(e)) continue; conditions.set(i, new SortCondition(e, s1.direction)); } }
/** * Determines whether a sort condition is valid in terms of this optimizer * * @param cond * Sort Condition * @param projectVars * Project Variables * @return True if valid, false otherwise */ private boolean isValidSortCondition(SortCondition cond, Collection<Var> projectVars, Set<Var> seenVars) { if (cond.getExpression().isVariable()) { if (projectVars.contains(cond.getExpression().asVar())) { seenVars.add(cond.getExpression().asVar()); return true; } return false; } else { for (Var v : cond.getExpression().getVarsMentioned()) { if (!projectVars.contains(v)) return false; seenVars.add(v); } return true; } } }
@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() ; } }
public final SortCondition rewrite(SortCondition sortCondition) { sortCondition.getExpression().visit(this); return new SortCondition(pop(), sortCondition.getDirection()); }
private void formatSortCondition(SortCondition sc) { boolean close = true ; String tag = null ; if ( sc.getDirection() != Query.ORDER_DEFAULT ) { if ( sc.getDirection() == Query.ORDER_ASCENDING ) { tag = Tags.tagAsc ; WriterLib.start(out, tag, NoNL) ; } if ( sc.getDirection() == Query.ORDER_DESCENDING ) { tag = Tags.tagDesc ; WriterLib.start(out, tag, NoNL) ; } } WriterExpr.output(out, sc.getExpression(), sContext) ; if ( tag != null ) WriterLib.finish(out, tag) ; }
public void addOrderBy(Node var, int direction) { if ( ! var.isVariable() ) throw new QueryException("Not a variable: "+var) ; SortCondition sc = new SortCondition(var, direction) ; addOrderBy(sc) ; }
@Override public void visit(OpOrder op) { for(SortCondition sc :op.getConditions()) { processExpr(sc.getExpression()); } }
public static List<SortCondition> transform(NodeTransform nodeTransform, List<SortCondition> conditions) { List<SortCondition> conditions2 = new ArrayList<>() ; boolean same = true ; for ( SortCondition sc : conditions ) { Expr expr = sc.getExpression() ; Expr expr2 = NodeTransformLib.transform(nodeTransform, expr) ; if ( expr != expr2 ) same = false ; SortCondition sc2 = new SortCondition(expr2, sc.getDirection()) ; conditions2.add(sc2) ; } if ( same ) return conditions ; return conditions2 ; }
Expr expr = sc.getExpression(); SqlSortCondition sqlCond = new SqlSortCondition(sqlExpr, sc.getDirection());