improved sql parser.

This commit is contained in:
wenshao
2017-07-23 16:44:43 +08:00
parent 82fe08109f
commit 6829a23bce
41 changed files with 1183 additions and 514 deletions
@@ -10,7 +10,7 @@ public class OdpsFormatCommentTest12 extends TestCase {
public void test_column_comment() throws Exception {
String sql = "select * from t --abc";
Assert.assertEquals("SELECT *" //
+ "\nFROM t --abc", SQLUtils.formatOdps(sql));
+ "\nFROM t -- abc", SQLUtils.formatOdps(sql));
}
@@ -22,13 +22,13 @@ public class OdpsFormatCommentTest14 extends TestCase {
Assert.assertEquals("SELECT *"
+ "\nFROM ("
+ "\n\tSELECT 1"
+ "\n\tFROM t1 --c_0"
+ "\n\tFROM t1 -- c_0"
+ "\n\tUNION ALL"
+ "\n\t--c_1"
+ "\n\t-- c_1"
+ "\n\tSELECT 2"
+ "\n\tFROM t2 --c_2"
+ "\n\tFROM t2 -- c_2"
+ "\n\tUNION ALL"
+ "\n\t--c_3"
+ "\n\t-- c_3"
+ "\n\tSELECT 3"
+ "\n\tFROM t3"
+ "\n) xx", SQLUtils.formatOdps(sql));
@@ -9,7 +9,7 @@ import junit.framework.TestCase;
public class OdpsFormatCommentTest17 extends TestCase {
public void test_column_comment() throws Exception {
String sql = "set xxx=aaa;--ssss";
Assert.assertEquals("SET xxx = aaa;--ssss", SQLUtils.formatOdps(sql));
Assert.assertEquals("SET xxx = aaa;-- ssss", SQLUtils.formatOdps(sql));
}
@@ -10,7 +10,7 @@ public class OdpsFormatCommentTest19 extends TestCase {
public void test_column_comment() throws Exception {
String sql = "set xxx=aaa;--ssss"
+ "\nset yyy=123;";
Assert.assertEquals("SET xxx = aaa;--ssss"
Assert.assertEquals("SET xxx = aaa;-- ssss"
+ "\nSET yyy = 123;", SQLUtils.formatOdps(sql));
}
@@ -14,10 +14,10 @@ public class OdpsFormatCommentTest2 extends TestCase {
+ "\n--[Update ]"//
+ "\nset odps.sql.mapper.split.size=2048;"//
+ "\nselect * from dual;";
Assert.assertEquals("--[Subject -]"
+ "\n--[Author -高铁/035139]"
+ "\n--[Created -2015-06-10 13:19:18]"
+ "\n--[Update ]"
Assert.assertEquals("-- [Subject -]"
+ "\n-- [Author -高铁/035139]"
+ "\n-- [Created -2015-06-10 13:19:18]"
+ "\n-- [Update ]"
+ "\nSET odps.sql.mapper.split.size = 2048;"
+ "\n" //
+ "\nSELECT *"
@@ -12,9 +12,9 @@ public class OdpsFormatCommentTest29 extends TestCase {
+ "\ncol string, --测试2"
+ "\ncol2 string --测试3"
+ "\n)";
Assert.assertEquals("CREATE TABLE xxxx001 ( --测试"
+ "\n\tcol STRING, --测试2"
+ "\n\tcol2 STRING --测试3"
Assert.assertEquals("CREATE TABLE xxxx001 ( -- 测试"
+ "\n\tcol STRING, -- 测试2"
+ "\n\tcol2 STRING -- 测试3"
+ "\n)", SQLUtils.formatOdps(sql));
}
}
@@ -14,7 +14,7 @@ public class OdpsFormatCommentTest30 extends TestCase {
+ "\n asdasd STRING,"
+ "\n asasd STRING"
+ "\n);";
Assert.assertEquals("--啊实打实大啊实打实大"
Assert.assertEquals("-- 啊实打实大啊实打实大"
+ "\nCREATE TABLE xxx ("
+ "\n\taa STRING,"
+ "\n\tasdasd STRING,"
@@ -12,7 +12,7 @@ public class OdpsFormatCommentTest7 extends TestCase {
+ "\nselect * from table1;" //
+ "\nselect * from table2;;"
;//
Assert.assertEquals("--这里是注释"
Assert.assertEquals("-- 这里是注释"
+ "\nSELECT *"
+ "\nFROM table1;"
+ "\n"
@@ -16,22 +16,22 @@ public class OdpsFormatCommentTest8 extends TestCase {
Assert.assertEquals("SELECT *"
+ "\nFROM table1;"
+ "\n"
+ "\n--comment1 goes here"
+ "\n-- comment1 goes here"
+ "\nSELECT *"
+ "\nFROM table2;"
+ "\n"
+ "\nSELECT *"
+ "\nFROM table3;--comment2 goes here", SQLUtils.formatOdps(sql));
+ "\nFROM table3;-- comment2 goes here", SQLUtils.formatOdps(sql));
Assert.assertEquals("select *"
+ "\nfrom table1;"
+ "\n"
+ "\n--comment1 goes here"
+ "\n-- comment1 goes here"
+ "\nselect *"
+ "\nfrom table2;"
+ "\n"
+ "\nselect *"
+ "\nfrom table3;--comment2 goes here", SQLUtils.formatOdps(sql, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION));
+ "\nfrom table3;-- comment2 goes here", SQLUtils.formatOdps(sql, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION));
}
@@ -32,13 +32,13 @@ public class OdpsSelectTest29 extends TestCase {
"--hhhhh\n" +
"FROM emp e\n" +
"LEFT OUTER JOIN emp_datahub";//
assertEquals("--aaaaa\n" +
"SELECT * --hhhhh\n" +
assertEquals("-- aaaaa\n" +
"SELECT * -- hhhhh\n" +
"FROM emp e\n" +
"LEFT OUTER JOIN emp_datahub", SQLUtils.formatOdps(sql));
assertEquals("--aaaaa\n" +
"select * --hhhhh\n" +
assertEquals("-- aaaaa\n" +
"select * -- hhhhh\n" +
"from emp e\n" +
"left outer join emp_datahub", SQLUtils.formatOdps(sql, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION));