improved odps sql formatter

This commit is contained in:
shaojin.wensj
2015-07-20 06:02:49 +08:00
parent a5811a4f3e
commit eb24ee61fc
16 changed files with 181 additions and 44 deletions
@@ -356,13 +356,30 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Printab
for (int i = groupList.size() - 1; i >= 0; --i) {
SQLExpr item = groupList.get(i);
if (relational) {
if (isPrettyFormat() && item.hasBeforeComment()) {
printlnComments(item.getBeforeCommentsDirect());
}
}
if (isPrettyFormat() && item.hasBeforeComment()) {
printlnComments(item.getBeforeCommentsDirect());
}
visitBinaryLeft(item, x.getOperator());
if (isPrettyFormat() && item.hasAfterComment()) {
print(' ');
printComment(item.getAfterCommentsDirect(), "\n");
}
if (i != groupList.size() - 1 && isPrettyFormat() && item.getParent().hasAfterComment()) {
print(' ');
printComment(item.getParent().getAfterCommentsDirect(), "\n");
}
if (relational) {
if (isPrettyFormat() && item.hasAfterComment()) {
print(' ');
printComment(item.getAfterCommentsDirect(), "\n");
}
println();
} else {
print(" ");
@@ -381,6 +398,10 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Printab
}
private void visitorBinaryRight(SQLBinaryOpExpr x) {
if (isPrettyFormat() && x.getRight().hasBeforeComment()) {
printlnComments(x.getRight().getBeforeCommentsDirect());
}
if (x.getRight() instanceof SQLBinaryOpExpr) {
SQLBinaryOpExpr right = (SQLBinaryOpExpr) x.getRight();
boolean rightRational = right.getOperator() == SQLBinaryOperator.BooleanAnd
@@ -404,6 +425,11 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Printab
} else {
x.getRight().accept(this);
}
if (x.getRight().hasAfterComment() && isPrettyFormat()) {
print(' ');
printlnComments(x.getRight().getAfterCommentsDirect());
}
}
private void visitBinaryLeft(SQLExpr left, SQLBinaryOperator op) {
@@ -2140,9 +2166,10 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Printab
public boolean visit(SQLUnionQueryTableSource x) {
print("(");
incrementIndent();
x.getUnion().accept(this);
println();
x.getUnion().accept(this);
decrementIndent();
println();
print(")");
if (x.getAlias() != null) {
@@ -2217,6 +2244,15 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Printab
}
}
protected void printlnComments(List<String> comments) {
if (comments != null) {
for (int i = 0; i < comments.size(); ++i) {
String comment = comments.get(i);
println(comment);
}
}
}
@Override
public boolean visit(SQLAlterViewRenameStatement x) {
print("ALTER VIEW ");