Merge pull request #942 from wenshao/master

improved odps sql formatter
This commit is contained in:
高铁
2015-08-31 20:56:29 +08:00
11 changed files with 45 additions and 18 deletions
@@ -30,6 +30,7 @@ import com.alibaba.druid.sql.ast.statement.SQLSelectOrderByItem;
import com.alibaba.druid.sql.ast.statement.SQLSelectQuery;
import com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock;
import com.alibaba.druid.sql.ast.statement.SQLSelectStatement;
import com.alibaba.druid.sql.ast.statement.SQLSetStatement;
import com.alibaba.druid.sql.ast.statement.SQLUpdateSetItem;
import com.alibaba.druid.sql.ast.statement.SQLUpdateStatement;
import com.alibaba.druid.sql.dialect.db2.visitor.DB2OutputVisitor;
@@ -267,6 +268,10 @@ public class SQLUtils {
}
}
visitor.println();
if (!(stmt instanceof SQLSetStatement)) {
visitor.println();
}
}
{
List<String> comments = stmt.getBeforeCommentsDirect();
@@ -21,6 +21,7 @@ import java.util.List;
import com.alibaba.druid.sql.ast.SQLExpr;
import com.alibaba.druid.sql.ast.SQLHint;
import com.alibaba.druid.sql.ast.SQLOrderBy;
import com.alibaba.druid.sql.ast.statement.SQLSelectOrderByItem;
import com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock;
import com.alibaba.druid.sql.dialect.odps.visitor.OdpsASTVisitor;
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
@@ -34,7 +35,7 @@ public class OdpsSelectQueryBlock extends SQLSelectQueryBlock {
protected List<SQLHint> hints;
protected SQLExpr distributeBy;
protected SQLExpr sortBy;
protected SQLSelectOrderByItem sortBy;
public OdpsSelectQueryBlock(){
@@ -56,11 +57,11 @@ public class OdpsSelectQueryBlock extends SQLSelectQueryBlock {
this.distributeBy = distributeBy;
}
public SQLExpr getSortBy() {
public SQLSelectOrderByItem getSortBy() {
return sortBy;
}
public void setSortBy(SQLExpr sortBy) {
public void setSortBy(SQLSelectOrderByItem sortBy) {
this.sortBy = sortBy;
}
@@ -16,7 +16,9 @@
package com.alibaba.druid.sql.dialect.odps.parser;
import com.alibaba.druid.sql.ast.SQLExpr;
import com.alibaba.druid.sql.ast.SQLOrderingSpecification;
import com.alibaba.druid.sql.ast.SQLSetQuantifier;
import com.alibaba.druid.sql.ast.statement.SQLSelectOrderByItem;
import com.alibaba.druid.sql.ast.statement.SQLSelectQuery;
import com.alibaba.druid.sql.dialect.odps.ast.OdpsSelectQueryBlock;
import com.alibaba.druid.sql.parser.SQLExprParser;
@@ -88,7 +90,18 @@ public class OdpsSelectParser extends SQLSelectParser {
if (identifierEquals("SORT")) {
lexer.nextToken();
accept(Token.BY);
SQLExpr sortBy = this.expr();
SQLExpr expr = this.expr();
SQLSelectOrderByItem sortBy = new SQLSelectOrderByItem(expr);
if (lexer.token() == Token.ASC) {
sortBy.setType(SQLOrderingSpecification.ASC);
lexer.nextToken();
} else if (lexer.token() == Token.DESC) {
sortBy.setType(SQLOrderingSpecification.DESC);
lexer.nextToken();
}
queryBlock.setSortBy(sortBy);
}
}
@@ -79,6 +79,8 @@ public class Lexer {
protected boolean keepComments = false;
protected int line = 0;
protected int lines = 0;
public Lexer(String input){
this(input, null);
@@ -293,10 +295,15 @@ public class Lexer {
comments = null;
}
this.lines = 0;
int startLine = line;
for (;;) {
if (isWhitespace(ch)) {
if (ch == '\n') {
line++;
lines = line - startLine;
}
scanChar();
@@ -19,6 +19,7 @@ public class OdpsFormatCommentTest2 extends TestCase {
+ "\n--[Created -2015-06-10 13:19:18]"
+ "\n--[Update ]"
+ "\nSET odps.sql.mapper.split.size = 2048;"
+ "\n" //
+ "\nSELECT *"
+ "\nFROM dual;", SQLUtils.formatOdps(sql));
}
@@ -20,8 +20,10 @@ public class OdpsFormatCommentTest3 extends TestCase {
+ "\nSET odps.service.mode = all;"//
+ "\n-- 使用新引擎"//
+ "\nSET odps.nvm.enabled = true;"//
+ "\n" //
+ "\nSELECT f1 -- aa"//
+ "\nFROM t0;"//
+ "\n" //
+ "\nSELECT f2 -- aa"//
+ "\nFROM t1;", SQLUtils.formatOdps(sql));
}
@@ -15,6 +15,7 @@ public class OdpsFormatCommentTest7 extends TestCase {
Assert.assertEquals("--这里是注释"
+ "\nSELECT *"
+ "\nFROM table1;"
+ "\n"
+ "\nSELECT *"
+ "\nFROM table2;", SQLUtils.formatOdps(sql));
}
@@ -15,9 +15,11 @@ public class OdpsFormatCommentTest8 extends TestCase {
;//
Assert.assertEquals("SELECT *"
+ "\nFROM table1;"
+ "\n"
+ "\n--comment1 goes here"
+ "\nSELECT *"
+ "\nFROM table2;"
+ "\n"
+ "\nSELECT *"
+ "\nFROM table3;--comment2 goes here", SQLUtils.formatOdps(sql));
}
@@ -11,6 +11,7 @@ public class OdpsFormatTest extends TestCase {
String sql = "select * from t1; ;select * from t2;";
Assert.assertEquals("SELECT *" //
+ "\nFROM t1;" //
+ "\n"
+ "\nSELECT *" //
+ "\nFROM t2;", SQLUtils.formatOdps(sql));
}
@@ -19,6 +20,7 @@ public class OdpsFormatTest extends TestCase {
String sql = "select * from t1; ;select * from t2";
Assert.assertEquals("SELECT *" //
+ "\nFROM t1;" //
+ "\n"
+ "\nSELECT *" //
+ "\nFROM t2", SQLUtils.formatOdps(sql));
}
@@ -36,5 +36,12 @@ public class OdpsSelectTest extends TestCase {
+ "\nFROM sale_detail" //
+ "\nDISTRIBUTE BY region SORT BY f1;", SQLUtils.formatOdps(sql));
}
public void test_distribute_by_2() throws Exception {
String sql = " select region from sale_detail distribute by region sort by f1 asc;";//
Assert.assertEquals("SELECT region" //
+ "\nFROM sale_detail" //
+ "\nDISTRIBUTE BY region SORT BY f1 ASC;", SQLUtils.formatOdps(sql));
}
}
@@ -1,14 +0,0 @@
package com.alibaba.druid.bvt.sql.odps.udf;
import com.alibaba.druid.support.opds.udf.ExportColumns;
import junit.framework.TestCase;
public class ExportColumnsTest extends TestCase {
private ExportColumns udf = new ExportColumns();
public ExportColumnsTest() {
}
}