public SqlNode visit(SqlNodeList list) { SqlNodeList copy = new SqlNodeList(list.getParserPosition()); for (SqlNode node : list) { copy.add(node.accept(this)); } return copy; }
void commaList(SqlWriter writer) { // The precedence of the comma operator if low but not zero. For // instance, this ensures parentheses in // select x, (select * from foo order by z), y from t for (SqlNode node : list) { writer.sep(","); node.unparse(writer, 2, 3); } }
public void validateExpr(SqlValidator validator, SqlValidatorScope scope) { // While a SqlNodeList is not always a valid expression, this // implementation makes that assumption. It just validates the members // of the list. // // One example where this is valid is the IN operator. The expression // // empno IN (10, 20) // // results in a call with operands // // { SqlIdentifier({"empno"}), // SqlNodeList(SqlLiteral(10), SqlLiteral(20)) } for (SqlNode node : list) { node.validateExpr(validator, scope); } }
void andOrList(SqlWriter writer, SqlKind sepKind) { SqlBinaryOperator sepOp = sepKind == SqlKind.AND ? SqlStdOperatorTable.AND : SqlStdOperatorTable.OR; for (int i = 0; i < list.size(); i++) { SqlNode node = list.get(i); writer.sep(sepKind.name(), false); // The precedence pulling on the LHS of a node is the // right-precedence of the separator operator, except at the start // of the list; similarly for the RHS of a node. If the operator // has left precedence 4 and right precedence 5, the precedences // in a 3-node list will look as follows: // 0 <- node1 -> 4 5 <- node2 -> 4 5 <- node3 -> 0 int lprec = (i == 0) ? 0 : sepOp.getRightPrec(); int rprec = (i == (list.size() - 1)) ? 0 : sepOp.getLeftPrec(); node.unparse(writer, lprec, rprec); } }
public boolean equalsDeep(SqlNode node, boolean fail) { if (!(node instanceof SqlNodeList)) { assert !fail : this + "!=" + node; return false; } SqlNodeList that = (SqlNodeList) node; if (this.size() != that.size()) { assert !fail : this + "!=" + node; return false; } for (int i = 0; i < list.size(); i++) { SqlNode thisChild = list.get(i); final SqlNode thatChild = that.list.get(i); if (!thisChild.equalsDeep(thatChild, fail)) { return false; } } return true; }
/** * Derives an alias for a node, and invents a mangled identifier if it cannot. * * <p>Examples: * * <ul> * <li>Alias: "1 + 2 as foo" yields "foo" * <li>Identifier: "foo.bar.baz" yields "baz" * <li>Anything else yields "expr$<i>ordinal</i>" * </ul> * * @return An alias, if one can be derived; or a synthetic alias "expr$<i>ordinal</i>" if ordinal * >= 0; otherwise null */ public static String getAlias(SqlNode node, int ordinal) { switch (node.getKind()) { case AS: // E.g. "1 + 2 as foo" --> "foo" return ((SqlCall) node).getOperands()[1].toString(); case OVER: // E.g. "bids over w" --> "bids" return getAlias(((SqlCall) node).getOperands()[0], ordinal); case IDENTIFIER: // E.g. "foo.bar" --> "bar" return Util.last(((SqlIdentifier) node).names); default: if (ordinal < 0) { return null; } else { return SqlUtil.deriveAliasFromOrdinal(ordinal); } } }
/** Converts an expression "expr" into "expr AS alias". */ public static SqlNode addAlias(SqlNode expr, String alias) { final SqlParserPos pos = expr.getParserPosition(); final SqlIdentifier id = new SqlIdentifier(alias, pos); return SqlStdOperatorTable.AS.createCall(pos, expr, id); }
public void validate(SqlValidator validator, SqlValidatorScope scope) { for (SqlNode child : list) { child.validate(validator, scope); } }