mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
improved odps sql formmatter
This commit is contained in:
@@ -15,7 +15,9 @@
|
||||
*/
|
||||
package com.alibaba.druid.sql.dialect.odps.visitor;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import com.alibaba.druid.sql.ast.SQLDataType;
|
||||
import com.alibaba.druid.sql.ast.SQLHint;
|
||||
@@ -49,7 +51,24 @@ import com.alibaba.druid.sql.dialect.odps.ast.OdpsUDTFSQLSelectItem;
|
||||
import com.alibaba.druid.sql.visitor.SQLASTOutputVisitor;
|
||||
|
||||
public class OdpsOutputVisitor extends SQLASTOutputVisitor implements OdpsASTVisitor {
|
||||
|
||||
|
||||
private Set<String> builtInFunctions = new HashSet<String>();
|
||||
|
||||
{
|
||||
builtInFunctions.add("IF");
|
||||
builtInFunctions.add("COALESCE");
|
||||
builtInFunctions.add("TO_DATE");
|
||||
builtInFunctions.add("SUBSTR");
|
||||
builtInFunctions.add("INSTR");
|
||||
builtInFunctions.add("LENGTH");
|
||||
builtInFunctions.add("SPLIT");
|
||||
builtInFunctions.add("TOLOWER");
|
||||
builtInFunctions.add("TOUPPER");
|
||||
builtInFunctions.add("EXPLODE");
|
||||
builtInFunctions.add("LEAST");
|
||||
builtInFunctions.add("GREATEST");
|
||||
}
|
||||
|
||||
public OdpsOutputVisitor(Appendable appender){
|
||||
super(appender);
|
||||
}
|
||||
@@ -683,7 +702,7 @@ public class OdpsOutputVisitor extends SQLASTOutputVisitor implements OdpsASTVis
|
||||
x.getOwner().accept(this);
|
||||
print(":");
|
||||
}
|
||||
print(x.getMethodName());
|
||||
printFunctionName(x.getMethodName());
|
||||
print("(");
|
||||
printAndAccept(x.getParameters(), ", ");
|
||||
print(")");
|
||||
@@ -712,4 +731,13 @@ public class OdpsOutputVisitor extends SQLASTOutputVisitor implements OdpsASTVis
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
protected void printFunctionName(String name) {
|
||||
String upperName = name.toUpperCase();
|
||||
if (builtInFunctions.contains(upperName)) {
|
||||
print(upperName);
|
||||
} else {
|
||||
print(name);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -562,12 +562,16 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Printab
|
||||
x.getOwner().accept(this);
|
||||
print(".");
|
||||
}
|
||||
print(x.getMethodName());
|
||||
printFunctionName(x.getMethodName());
|
||||
print("(");
|
||||
printAndAccept(x.getParameters(), ", ");
|
||||
print(")");
|
||||
return false;
|
||||
}
|
||||
|
||||
protected void printFunctionName(String name) {
|
||||
print(name);
|
||||
}
|
||||
|
||||
public boolean visit(SQLAggregateExpr x) {
|
||||
print(x.getMethodName());
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
package com.alibaba.druid.bvt.sql.odps;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
import com.alibaba.druid.sql.SQLUtils;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
public class OdpsFormatCommentTest22 extends TestCase {
|
||||
public void test_if() throws Exception {
|
||||
String sql = "select if(a>0,1, 0) from dual";
|
||||
Assert.assertEquals("SELECT IF(a > 0, 1, 0)"
|
||||
+ "\nFROM dual", SQLUtils.formatOdps(sql));
|
||||
}
|
||||
|
||||
public void test_coalesce() throws Exception {
|
||||
String sql = "select coalesce(f1,f2) from dual";
|
||||
Assert.assertEquals("SELECT COALESCE(f1, f2)"
|
||||
+ "\nFROM dual", SQLUtils.formatOdps(sql));
|
||||
}
|
||||
public void test_count() throws Exception {
|
||||
String sql = "select count(*) from dual";
|
||||
Assert.assertEquals("SELECT COUNT(*)"
|
||||
+ "\nFROM dual", SQLUtils.formatOdps(sql));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -10,7 +10,7 @@ public class OdpsIfTest extends TestCase {
|
||||
|
||||
public void test_if() throws Exception {
|
||||
String sql = "select sum(if(a > 0, 1, 0)) from t1";
|
||||
Assert.assertEquals("SELECT SUM(if(a > 0, 1, 0))" //
|
||||
Assert.assertEquals("SELECT SUM(IF(a > 0, 1, 0))" //
|
||||
+ "\nFROM t1", SQLUtils.formatOdps(sql));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user