improved odps sql formmatter

This commit is contained in:
shaojin.wensj
2015-07-28 23:19:17 +08:00
parent 87f3f8144b
commit 3de275fa8c
8 changed files with 47 additions and 21 deletions
@@ -17,6 +17,7 @@ package com.alibaba.druid.sql.dialect.odps.visitor;
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;
@@ -62,8 +63,6 @@ public class OdpsOutputVisitor extends SQLASTOutputVisitor implements OdpsASTVis
x.getName().accept(this);
x.getName().accept(this);
if (x.getLike() != null) {
print(" LIKE ");
x.getLike().accept(this);
@@ -702,4 +701,15 @@ public class OdpsOutputVisitor extends SQLASTOutputVisitor implements OdpsASTVis
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;
}
}
@@ -25,7 +25,7 @@ public class OdpsCreateTableTest extends TestCase {
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"//
@@ -17,7 +17,7 @@ public class OdpsCreateTableTest2 extends TestCase {
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);
}
@@ -17,6 +17,6 @@ public class OdpsCreateTableTest_1 extends TestCase {
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);
}
}
@@ -9,12 +9,12 @@ import junit.framework.TestCase;
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));
}
@@ -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;
@@ -18,14 +16,14 @@ public class OdpsFormatCommentTest16 extends TestCase {
+ "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));
}
@@ -9,16 +9,16 @@ import junit.framework.TestCase;
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));
}
}
@@ -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));
}
}