Skip to content

Commit

Permalink
improved odps sql formmatter
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Jul 28, 2015
1 parent 87f3f81 commit 3de275f
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import java.util.List;

import com.alibaba.druid.sql.ast.SQLDataType;
import com.alibaba.druid.sql.ast.SQLHint;
import com.alibaba.druid.sql.ast.SQLOrderBy;
import com.alibaba.druid.sql.ast.SQLSetQuantifier;
Expand Down Expand Up @@ -62,8 +63,6 @@ public boolean visit(OdpsCreateTableStatement x) {

x.getName().accept(this);

x.getName().accept(this);

if (x.getLike() != null) {
print(" LIKE ");
x.getLike().accept(this);
Expand Down Expand Up @@ -702,4 +701,15 @@ protected void printJoinType(JoinType joinType) {
print(JoinType.toString(joinType));
}
}

public boolean visit(SQLDataType x) {
print(x.getName().toUpperCase());
if (x.getArguments().size() > 0) {
print("(");
printAndAccept(x.getArguments(), ", ");
print(")");
}

return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public void test_0() throws Exception {
SQLStatement stmt = parser.parseStatementList().get(0);
parser.match(Token.EOF);
String output = SQLUtils.toOdpsString(stmt);
Assert.assertEquals("CREATE TABLE IF NOT EXISTS sale_detailsale_detail ("//
Assert.assertEquals("CREATE TABLE IF NOT EXISTS sale_detail ("//
+ "\n\tshop_name STRING,"//
+ "\n\tcustomer_id STRING,"//
+ "\n\ttotal_price DOUBLE"//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public void test_0() throws Exception {
SQLStatement stmt = parser.parseStatementList().get(0);
parser.match(Token.EOF);
String output = SQLUtils.toOdpsString(stmt);
Assert.assertEquals("CREATE TABLE sale_detailsale_detail"
Assert.assertEquals("CREATE TABLE sale_detail"
+ "\nSELECT *" //
+ "\nFROM dual", output);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ public void test_0() throws Exception {
SQLStatement stmt = parser.parseStatementList().get(0);
parser.match(Token.EOF);
String output = SQLUtils.toOdpsString(stmt);
Assert.assertEquals("CREATE TABLE sale_detail_likesale_detail_like LIKE sale_detail", output);
Assert.assertEquals("CREATE TABLE sale_detail_like LIKE sale_detail", output);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
public class OdpsFormatCommentTest15 extends TestCase {
public void test_column_comment() throws Exception {
String sql = "create table t1 (f0 bigint) partitioned by (ds string, hh string);";
Assert.assertEquals("CREATE TABLE t1t1 ("
+ "\n\tf0 bigint"
Assert.assertEquals("CREATE TABLE t1 ("
+ "\n\tf0 BIGINT"
+ "\n)"
+ "\nPARTITIONED BY ("
+ "\n\tds string,"
+ "\n\thh string"
+ "\n\tds STRING,"
+ "\n\thh STRING"
+ "\n);", SQLUtils.formatOdps(sql));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.alibaba.druid.bvt.sql.odps;

import java.net.URLDecoder;

import org.junit.Assert;

import com.alibaba.druid.sql.SQLUtils;
Expand All @@ -18,14 +16,14 @@ public void test_column_comment() throws Exception {
+ "ds string, -- c_1"
+ "\nhh string -- c_2"
+ "\n);";
Assert.assertEquals("CREATE TABLE t1t1 ("
+ "\n\tf0 bigint, -- fc_0"
+ "\n\tf1 string, -- fc_1"
+ "\n\tf2 string -- fc_2"
Assert.assertEquals("CREATE TABLE t1 ("
+ "\n\tf0 BIGINT, -- fc_0"
+ "\n\tf1 STRING, -- fc_1"
+ "\n\tf2 STRING -- fc_2"
+ "\n)"
+ "\nPARTITIONED BY ("
+ "\n\tds string, -- c_1"
+ "\n\thh string -- c_2"
+ "\n\tds STRING, -- c_1"
+ "\n\thh STRING -- c_2"
+ "\n);", SQLUtils.formatOdps(sql));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@
public class OdpsFormatCommentTest18 extends TestCase {
public void test_column_comment() throws Exception {
String sql = "create table t (f1 string comment \"xxx\")";
Assert.assertEquals("CREATE TABLE tt ("
+ "\n\tf1 string COMMENT 'xxx'"
Assert.assertEquals("CREATE TABLE t ("
+ "\n\tf1 STRING COMMENT 'xxx'"
+ "\n)", SQLUtils.formatOdps(sql));
}


public void test_column_comment_2() throws Exception {
String sql = "create table t (f1 string comment \"xxx's\")";
Assert.assertEquals("CREATE TABLE tt ("
+ "\n\tf1 string COMMENT 'xxx''s'"
Assert.assertEquals("CREATE TABLE t ("
+ "\n\tf1 STRING COMMENT 'xxx''s'"
+ "\n)", SQLUtils.formatOdps(sql));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.alibaba.druid.bvt.sql.odps;

import org.junit.Assert;

import com.alibaba.druid.sql.SQLUtils;

import junit.framework.TestCase;

public class OdpsFormatCommentTest21 extends TestCase {
public void test_column_comment() throws Exception {
String sql = "create table sales (f1 bigint)";
Assert.assertEquals("CREATE TABLE sales ("
+ "\n\tf1 BIGINT"
+ "\n)", SQLUtils.formatOdps(sql));
}


}

0 comments on commit 3de275f

Please sign in to comment.