// make private? public Clause maxClause() { Clause maxClause = null; for (Clause clause : clauses) { if (maxClause == null || clause.ordinal() > maxClause.ordinal()) { maxClause = clause; } } assert maxClause != null; return maxClause; }
/** * Once you have a Result of implementing a child relational expression, call this method to * create a Builder to implement the current relational expression by adding additional clauses * to the SQL query. * * <p>You need to declare which clauses you intend to add. If the clauses are "later", you can * add to the same query. For example, "GROUP BY" comes after "WHERE". But if they are the same * or earlier, this method will start a new SELECT that wraps the previous result. * * <p>When you have called {@link Builder#setSelect(org.eigenbase.sql.SqlNodeList)}, {@link * Builder#setWhere(org.eigenbase.sql.SqlNode)} etc. call {@link * Builder#result(org.eigenbase.sql.SqlNode, java.util.Collection, org.eigenbase.rel.RelNode)} * to fix the new query. * * @param rel Relational expression being implemented * @param clauses Clauses that will be generated to implement current relational expression * @return A builder */ public Builder builder(JdbcRel rel, Clause... clauses) { final Clause maxClause = maxClause(); boolean needNew = false; for (Clause clause : clauses) { if (maxClause.ordinal() >= clause.ordinal()) { needNew = true; } } SqlSelect select; Expressions.FluentList<Clause> clauseList = Expressions.list(); if (needNew) { select = subSelect(); } else { select = asSelect(); clauseList.addAll(this.clauses); } clauseList.addAll(Arrays.asList(clauses)); Context newContext; final SqlNodeList selectList = select.getSelectList(); if (selectList != null) { newContext = new Context(selectList.size()) { @Override public SqlNode field(int ordinal) { final SqlNode selectItem = selectList.get(ordinal); switch (selectItem.getKind()) { case AS: return ((SqlCall) selectItem).operand(0); } return selectItem; } }; } else { newContext = new AliasContext(aliases, aliases.size() > 1); } return new Builder(rel, clauseList, select, newContext); }