Skip to content

Commit

Permalink
Merge pull request alibaba#942 from wenshao/master
Browse files Browse the repository at this point in the history
improved odps sql formatter
  • Loading branch information
wenshao committed Aug 31, 2015
2 parents ac2e74d + 549c66a commit 2aff049
Showing 11 changed files with 45 additions and 18 deletions.
5 changes: 5 additions & 0 deletions src/main/java/com/alibaba/druid/sql/SQLUtils.java
Original file line number Diff line number Diff line change
@@ -30,6 +30,7 @@
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 static String toSQLString(List<SQLStatement> statementList, String dbType
}
}
visitor.println();

if (!(stmt instanceof SQLSetStatement)) {
visitor.println();
}
}
{
List<String> comments = stmt.getBeforeCommentsDirect();
Original file line number Diff line number Diff line change
@@ -21,6 +21,7 @@
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 void setDistributeBy(SQLExpr distributeBy) {
this.distributeBy = distributeBy;
}

public SQLExpr getSortBy() {
public SQLSelectOrderByItem getSortBy() {
return sortBy;
}

public void setSortBy(SQLExpr sortBy) {
public void setSortBy(SQLSelectOrderByItem sortBy) {
this.sortBy = sortBy;
}

Original file line number Diff line number Diff line change
@@ -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 SQLSelectQuery query() {
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);
}
}
7 changes: 7 additions & 0 deletions src/main/java/com/alibaba/druid/sql/parser/Lexer.java
Original file line number Diff line number Diff line change
@@ -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 final void nextToken() {
comments = null;
}

this.lines = 0;
int startLine = line;

for (;;) {
if (isWhitespace(ch)) {
if (ch == '\n') {
line++;

lines = line - startLine;
}

scanChar();
Original file line number Diff line number Diff line change
@@ -19,6 +19,7 @@ public void test_column_comment() throws Exception {
+ "\n--[Created -2015-06-10 13:19:18]"
+ "\n--[Update ]"
+ "\nSET odps.sql.mapper.split.size = 2048;"
+ "\n" //
+ "\nSELECT *"
+ "\nFROM dual;", SQLUtils.formatOdps(sql));
}
Original file line number Diff line number Diff line change
@@ -20,8 +20,10 @@ public void test_column_comment() throws Exception {
+ "\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));
}
Original file line number Diff line number Diff line change
@@ -15,6 +15,7 @@ public void test_column_comment() throws Exception {
Assert.assertEquals("--这里是注释"
+ "\nSELECT *"
+ "\nFROM table1;"
+ "\n"
+ "\nSELECT *"
+ "\nFROM table2;", SQLUtils.formatOdps(sql));
}
Original file line number Diff line number Diff line change
@@ -15,9 +15,11 @@ public void test_column_comment() throws Exception {
;//
Assert.assertEquals("SELECT *"
+ "\nFROM table1;"
+ "\n"
+ "\n--comment1 goes here"
+ "\nSELECT *"
+ "\nFROM table2;"
+ "\n"
+ "\nSELECT *"
+ "\nFROM table3;--comment2 goes here", SQLUtils.formatOdps(sql));
}
Original file line number Diff line number Diff line change
@@ -11,6 +11,7 @@ public void test_format() throws Exception {
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 void test_no_semi() throws Exception {
String sql = "select * from t1; ;select * from t2";
Assert.assertEquals("SELECT *" //
+ "\nFROM t1;" //
+ "\n"
+ "\nSELECT *" //
+ "\nFROM t2", SQLUtils.formatOdps(sql));
}
Original file line number Diff line number Diff line change
@@ -36,5 +36,12 @@ public void test_distribute_by_1() throws Exception {
+ "\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));
}

}

This file was deleted.

0 comments on commit 2aff049

Please sign in to comment.