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
@@ -108,7 +108,6 @@ public class MySqlLexer extends Lexer {
stringVal = subString(mark - 1, bufPos + 1);
token = Token.LINE_COMMENT;
hasComment = true;
if (keepComments) {
addComment(stringVal);
}
@@ -450,7 +449,6 @@ public class MySqlLexer extends Lexer {
} else {
stringVal = subString(mark, bufPos);
token = Token.MULTI_LINE_COMMENT;
hasComment = true;
if (keepComments) {
addComment(stringVal);
}
@@ -497,7 +495,6 @@ public class MySqlLexer extends Lexer {
stringVal = subString(mark, bufPos + 1);
token = Token.LINE_COMMENT;
hasComment = true;
if (keepComments) {
addComment(stringVal);
}
@@ -112,7 +112,6 @@ public class OdpsLexer extends Lexer {
} else {
stringVal = subString(mark, bufPos + 1);
token = Token.MULTI_LINE_COMMENT;
hasComment = true;
if (keepComments) {
addComment(stringVal);
}
@@ -160,9 +159,8 @@ public class OdpsLexer extends Lexer {
bufPos++;
}
stringVal = subString(mark, bufPos);
stringVal = subString(mark, ch != EOI ? bufPos : bufPos + 1);
token = Token.LINE_COMMENT;
hasComment = true;
if (keepComments) {
addComment(stringVal);
}
@@ -450,6 +450,10 @@ public class OdpsOutputVisitor extends SQLASTOutputVisitor implements OdpsASTVis
if (x.getWhere() != null) {
println();
print("WHERE ");
if (x.getWhere().hasBeforeComment() && isPrettyFormat()) {
printlnComments(x.getWhere().getBeforeCommentsDirect());
}
x.getWhere().setParent(x);
x.getWhere().accept(this);
if (x.getWhere().hasAfterComment() && isPrettyFormat()) {
@@ -230,7 +230,6 @@ public class OracleLexer extends Lexer {
} else {
stringVal = subString(mark, bufPos);
token = Token.MULTI_LINE_COMMENT;
hasComment = true;
if (keepComments) {
addComment(stringVal);
}
@@ -276,7 +275,6 @@ public class OracleLexer extends Lexer {
stringVal = subString(mark + 1, bufPos);
token = Token.LINE_COMMENT;
hasComment = true;
if (keepComments) {
addComment(stringVal);
}
@@ -105,7 +105,6 @@ public class SQLServerLexer extends Lexer {
} else {
stringVal = subString(mark, bufPos);
token = Token.MULTI_LINE_COMMENT;
hasComment = true;
if (keepComments) {
addComment(stringVal);
}
@@ -147,7 +146,6 @@ public class SQLServerLexer extends Lexer {
stringVal = subString(mark + 1, bufPos);
token = Token.LINE_COMMENT;
hasComment = true;
if (keepComments) {
addComment(stringVal);
}
@@ -74,8 +74,6 @@ public class Lexer {
protected CommentHandler commentHandler;
protected boolean hasComment = false;
protected boolean endOfComment = false;
protected boolean keepComments = false;
@@ -289,8 +287,9 @@ public class Lexer {
public final void nextToken() {
bufPos = 0;
hasComment = false;
comments = null;
if (comments != null) {
comments = null;
}
for (;;) {
if (isWhitespace(ch)) {
@@ -804,7 +803,6 @@ public class Lexer {
stringVal = subString(mark, bufPos);
token = Token.MULTI_LINE_COMMENT;
hasComment = true;
if (keepComments) {
addComment(stringVal);
}
@@ -852,7 +850,6 @@ public class Lexer {
stringVal = subString(mark, bufPos);
token = Token.LINE_COMMENT;
hasComment = true;
if (keepComments) {
addComment(stringVal);
}
@@ -1020,23 +1017,9 @@ public class Lexer {
return stringVal;
}
public final String readAndResetCommentValue() {
if (comments.size() == 0) {
return null;
}
String val = comments.get(0);
this.hasComment = false;
this.comments.clear();
return val;
}
public final List<String> readAndResetComments() {
List<String> comments = this.comments;
this.hasComment = false;
this.comments = null;
return comments;
@@ -1153,7 +1136,7 @@ public class Lexer {
}
public boolean hasComment() {
return hasComment;
return comments != null;
}
public void skipToEOF() {
@@ -185,6 +185,11 @@ public class SQLExprParser extends SQLParser {
}
public SQLExpr primary() {
List<String> beforeComments = null;
if (lexer.isKeepComments() && lexer.hasComment()) {
beforeComments = lexer.readAndResetComments();
}
SQLExpr sqlExpr = null;
final Token tok = lexer.token();
@@ -555,7 +560,13 @@ public class SQLExprParser extends SQLParser {
throw new ParserException("ERROR. token : " + tok + ", pos : " + lexer.pos());
}
return primaryRest(sqlExpr);
SQLExpr expr = primaryRest(sqlExpr);
if (beforeComments != null) {
expr.addBeforeComment(beforeComments);
}
return expr;
}
protected SQLExpr parseAll() {
@@ -1184,6 +1195,7 @@ public class SQLExprParser extends SQLParser {
}
lexer.nextToken();
SQLExpr rightExp = relational();
expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.BooleanAnd, rightExp, getDbType());
@@ -162,7 +162,7 @@ public class SQLSelectParser extends SQLParser {
SQLSelectQueryBlock queryBlock = new SQLSelectQueryBlock();
if (lexer.hasComment() && lexer.isKeepComments()) {
queryBlock.addBeforeComment(lexer.readAndResetCommentValue());
queryBlock.addBeforeComment(lexer.readAndResetComments());
}
accept(Token.SELECT);
@@ -238,12 +238,20 @@ public class SQLSelectParser extends SQLParser {
if (lexer.token() == Token.WHERE) {
lexer.nextToken();
List<String> beforeComments = null;
if (lexer.hasComment() && lexer.isKeepComments()) {
beforeComments = lexer.readAndResetComments();
}
SQLExpr where = expr();
if (where != null && beforeComments != null) {
where.addBeforeComment(beforeComments);
}
if (lexer.hasComment() && lexer.isKeepComments() //
&& lexer.token() != Token.INSERT // odps multi-insert
) {
where.addAfterComment(lexer.readAndResetCommentValue());
where.addAfterComment(lexer.readAndResetComments());
}
queryBlock.setWhere(where);
@@ -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 ");