mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
improved odps sql formatter
This commit is contained in:
@@ -278,11 +278,14 @@ public class OdpsOutputVisitor extends SQLASTOutputVisitor implements OdpsASTVis
|
||||
SQLSelectItem selectItem = selectList.get(i);
|
||||
|
||||
if (i != 0) {
|
||||
print(", ");
|
||||
|
||||
printComment(selectList.get(i - 1).getAfterCommentsDirect(), "\n");
|
||||
|
||||
SQLSelectItem preSelectItem = selectList.get(i - 1);
|
||||
if (preSelectItem.hasAfterComment()) {
|
||||
print(' ');
|
||||
printComment(preSelectItem.getAfterCommentsDirect(), "\n");
|
||||
}
|
||||
|
||||
println();
|
||||
print(", ");
|
||||
}
|
||||
|
||||
selectItem.accept(this);
|
||||
@@ -321,14 +324,15 @@ public class OdpsOutputVisitor extends SQLASTOutputVisitor implements OdpsASTVis
|
||||
print(",");
|
||||
} else {
|
||||
println();
|
||||
print(JoinType.toString(x.getJoinType()));
|
||||
printJoinType(x.getJoinType());
|
||||
}
|
||||
print(" ");
|
||||
x.getRight().accept(this);
|
||||
|
||||
if (x.getCondition() != null) {
|
||||
incrementIndent();
|
||||
print(" ON ");
|
||||
println();
|
||||
print("ON ");
|
||||
x.getCondition().accept(this);
|
||||
decrementIndent();
|
||||
}
|
||||
@@ -686,4 +690,16 @@ public class OdpsOutputVisitor extends SQLASTOutputVisitor implements OdpsASTVis
|
||||
print(")");
|
||||
return false;
|
||||
}
|
||||
|
||||
protected void printJoinType(JoinType joinType) {
|
||||
if (joinType.equals(JoinType.LEFT_OUTER_JOIN)) {
|
||||
print("LEFT OUTER JOIN");
|
||||
} else if (joinType.equals(JoinType.RIGHT_OUTER_JOIN)) {
|
||||
print("RIGHT OUTER JOIN");
|
||||
} else if (joinType.equals(JoinType.FULL_OUTER_JOIN)) {
|
||||
print("FULL OUTER JOIN");
|
||||
} else {
|
||||
print(JoinType.toString(joinType));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1283,7 +1283,7 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Printab
|
||||
print(",");
|
||||
} else {
|
||||
println();
|
||||
print(JoinType.toString(x.getJoinType()));
|
||||
printJoinType(x.getJoinType());
|
||||
}
|
||||
print(" ");
|
||||
x.getRight().accept(this);
|
||||
@@ -1311,6 +1311,10 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Printab
|
||||
return false;
|
||||
}
|
||||
|
||||
protected void printJoinType(JoinType joinType) {
|
||||
print(JoinType.toString(joinType));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(ValuesClause x) {
|
||||
print("(");
|
||||
|
||||
@@ -18,8 +18,8 @@ public class OdpsFormatCommentTest extends TestCase {
|
||||
String sql = "select f1 -- aaa"
|
||||
+ "\n, f2 -- bbb"
|
||||
+ "\n from t1";
|
||||
Assert.assertEquals("SELECT f1, -- aaa" //
|
||||
+ "\n\tf2 -- bbb" //
|
||||
Assert.assertEquals("SELECT f1 -- aaa" //
|
||||
+ "\n\t, f2 -- bbb" //
|
||||
+ "\nFROM t1", SQLUtils.formatOdps(sql));
|
||||
}
|
||||
|
||||
@@ -27,8 +27,8 @@ public class OdpsFormatCommentTest extends TestCase {
|
||||
String sql = "select f1 /*aa*/"
|
||||
+ "\n, f2 -- bbb"
|
||||
+ "\n from t1";
|
||||
Assert.assertEquals("SELECT f1, /*aa*/" //
|
||||
+ "\n\tf2 -- bbb" //
|
||||
Assert.assertEquals("SELECT f1 /*aa*/" //
|
||||
+ "\n\t, f2 -- bbb" //
|
||||
+ "\nFROM t1", SQLUtils.formatOdps(sql));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.alibaba.druid.bvt.sql.odps;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
import com.alibaba.druid.sql.SQLUtils;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
public class OdpsFormatCommentTest20 extends TestCase {
|
||||
public void test_column_comment() throws Exception {
|
||||
String sql = "select * from a full outer join b on a.id=b.id";
|
||||
Assert.assertEquals("SELECT *"
|
||||
+ "\nFROM a"
|
||||
+ "\nFULL OUTER JOIN b"
|
||||
+ "\n\tON a.id = b.id", SQLUtils.formatOdps(sql));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -24,15 +24,15 @@ public class OdpsFormatCommentTest4 extends TestCase {
|
||||
+ "\n) a" //
|
||||
+ "\n-- comment_0"//
|
||||
+ "\nINSERT OVERWRITE TABLE b PARTITION (ds='20150711')"//
|
||||
+ "\nSELECT f0, "//
|
||||
+ "\n\tf1, "//
|
||||
+ "\n\tf2"//
|
||||
+ "\nSELECT f0"//
|
||||
+ "\n\t, f1"//
|
||||
+ "\n\t, f2"//
|
||||
+ "\nWHERE name RLIKE 'xxxx'" //
|
||||
+ "\n-- comment_1"//
|
||||
+ "\nINSERT OVERWRITE TABLE c PARTITION (ds='20150711')"//
|
||||
+ "\nSELECT f0, "//
|
||||
+ "\n\tf1, "//
|
||||
+ "\n\tf2"//
|
||||
+ "\nSELECT f0"//
|
||||
+ "\n\t, f1"//
|
||||
+ "\n\t, f2"//
|
||||
+ "\nWHERE name RLIKE 'kk';", SQLUtils.formatOdps(sql));
|
||||
}
|
||||
|
||||
|
||||
@@ -30,11 +30,12 @@ public class OdpsSelectTest_mapjoin extends TestCase {
|
||||
+ "\nb.total_price"
|
||||
+ "\nfrom shop a join sale_detail b"
|
||||
+ "\non a.shop_name = b.shop_name;";
|
||||
Assert.assertEquals("SELECT /*+ mapjoin(a) */ a.shop_name, "
|
||||
+ "\n\tb.customer_id, "
|
||||
+ "\n\tb.total_price"
|
||||
Assert.assertEquals("SELECT /*+ mapjoin(a) */ a.shop_name"
|
||||
+ "\n\t, b.customer_id"
|
||||
+ "\n\t, b.total_price"
|
||||
+ "\nFROM shop a"
|
||||
+ "\nJOIN sale_detail b ON a.shop_name = b.shop_name;", SQLUtils.formatOdps(sql));
|
||||
+ "\nJOIN sale_detail b"
|
||||
+ "\n\tON a.shop_name = b.shop_name;", SQLUtils.formatOdps(sql));
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user