improved odps support

This commit is contained in:
shaojin.wensj
2015-07-30 13:22:13 +08:00
parent 9d45ec9d12
commit eb7b7037c4
12 changed files with 260 additions and 68 deletions
+49 -56
View File
@@ -1,14 +1,8 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!--
<parent>
<groupId>com.alibaba</groupId>
<artifactId>parent-pom</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
-->
<!-- <parent> <groupId>com.alibaba</groupId> <artifactId>parent-pom</artifactId> <version>1.0.0-SNAPSHOT</version> </parent> -->
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.16-SNAPSHOT</version>
@@ -38,18 +32,9 @@
</repository>
</repositories>
<!--
<distributionManagement>
<repository>
<id>opensesame</id>
<url>dav:http://code.alibabatech.com/mvn/releases/</url>
</repository>
<snapshotRepository>
<id>opensesame</id>
<url>dav:http://code.alibabatech.com/mvn/snapshots/</url>
</snapshotRepository>
</distributionManagement>
-->
<!-- <distributionManagement> <repository> <id>opensesame</id> <url>dav:http://code.alibabatech.com/mvn/releases/</url>
</repository> <snapshotRepository> <id>opensesame</id> <url>dav:http://code.alibabatech.com/mvn/snapshots/</url> </snapshotRepository>
</distributionManagement> -->
<developers>
<developer>
<id>wenshao</id>
@@ -303,32 +288,32 @@
</dependencies>
</profile>
<profile>
<id>mac-profile-oracle-jdk</id>
<activation>
<activeByDefault>false</activeByDefault>
<file>
<exists>${java.home}/../lib/jconsole.jar</exists>
</file>
</activation>
<dependencies>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>jconsole</artifactId>
<version>1.8.0</version>
<scope>system</scope>
<systemPath>${java.home}/../lib/jconsole.jar</systemPath>
</dependency>
<profile>
<id>mac-profile-oracle-jdk</id>
<activation>
<activeByDefault>false</activeByDefault>
<file>
<exists>${java.home}/../lib/jconsole.jar</exists>
</file>
</activation>
<dependencies>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>jconsole</artifactId>
<version>1.8.0</version>
<scope>system</scope>
<systemPath>${java.home}/../lib/jconsole.jar</systemPath>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>tools</artifactId>
<version>1.8.0</version>
<scope>system</scope>
<systemPath>${java.home}/../lib/tools.jar</systemPath>
</dependency>
</dependencies>
</profile>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>tools</artifactId>
<version>1.8.0</version>
<scope>system</scope>
<systemPath>${java.home}/../lib/tools.jar</systemPath>
</dependency>
</dependencies>
</profile>
</profiles>
<dependencies>
@@ -469,7 +454,7 @@
<version>11.2.0.3</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
@@ -478,14 +463,8 @@
<optional>true</optional>
</dependency>
<!--
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>sqljdbc4</artifactId>
<version>4.0.2206.100</version>
<scope>test</scope>
</dependency>
-->
<!-- <dependency> <groupId>com.microsoft.sqlserver</groupId> <artifactId>sqljdbc4</artifactId> <version>4.0.2206.100</version>
<scope>test</scope> </dependency> -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
@@ -571,7 +550,7 @@
<version>1.b.52</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.taobao.tbdatasource</groupId>
<artifactId>tbdatasource</artifactId>
@@ -584,7 +563,7 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.javasimon</groupId>
<artifactId>javasimon-spring</artifactId>
@@ -609,5 +588,19 @@
<version>14.0.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.aliyun.odps</groupId>
<artifactId>odps-sdk-udf</artifactId>
<version>0.17.3</version>
<scope>provided</scope>
<optional>true</optional>
<exclusions>
<exclusion>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</project>
@@ -337,14 +337,15 @@ public class SQLUtils {
return new SQLASTOutputVisitor(out);
}
@Deprecated
public static SchemaStatVisitor createSchemaStatVisitor(List<SQLStatement> statementList, String dbType) {
return createSchemaStatVisitor(dbType);
}
public static SchemaStatVisitor createSchemaStatVisitor(String dbType) {
if (JdbcUtils.ORACLE.equals(dbType) || JdbcUtils.ALI_ORACLE.equals(dbType)) {
if (statementList.size() == 1) {
return new OracleSchemaStatVisitor();
} else {
return new OracleSchemaStatVisitor();
}
return new OracleSchemaStatVisitor();
}
if (JdbcUtils.MYSQL.equals(dbType) || //
@@ -153,6 +153,8 @@ public class OdpsOutputVisitor extends SQLASTOutputVisitor implements OdpsASTVis
}
if (x.getSelect() != null) {
println();
print("AS");
println();
x.getSelect().accept(this);
}
@@ -216,44 +216,91 @@ public class SQLStatementParser extends SQLParser {
}
if (lexer.token() == Token.DROP) {
List<String> beforeComments = null;
if (lexer.isKeepComments() && lexer.hasComment()) {
beforeComments = lexer.readAndResetComments();
}
lexer.nextToken();
if (lexer.token() == Token.TABLE || identifierEquals("TEMPORARY")) {
SQLDropTableStatement stmt = parseDropTable(false);
if (beforeComments != null) {
stmt.addBeforeComment(beforeComments);
}
statementList.add(stmt);
continue;
} else if (lexer.token() == Token.USER) {
SQLStatement stmt = parseDropUser();
if (beforeComments != null) {
stmt.addBeforeComment(beforeComments);
}
statementList.add(stmt);
continue;
} else if (lexer.token() == Token.INDEX) {
SQLStatement stmt = parseDropIndex();
if (beforeComments != null) {
stmt.addBeforeComment(beforeComments);
}
statementList.add(stmt);
continue;
} else if (lexer.token() == Token.VIEW) {
SQLStatement stmt = parseDropView(false);
if (beforeComments != null) {
stmt.addBeforeComment(beforeComments);
}
statementList.add(stmt);
continue;
} else if (lexer.token() == Token.TRIGGER) {
SQLStatement stmt = parseDropTrigger(false);
if (beforeComments != null) {
stmt.addBeforeComment(beforeComments);
}
statementList.add(stmt);
continue;
} else if (lexer.token() == Token.DATABASE) {
SQLStatement stmt = parseDropDatabase(false);
if (beforeComments != null) {
stmt.addBeforeComment(beforeComments);
}
statementList.add(stmt);
continue;
} else if (lexer.token() == Token.FUNCTION) {
SQLStatement stmt = parseDropFunction(false);
if (beforeComments != null) {
stmt.addBeforeComment(beforeComments);
}
statementList.add(stmt);
continue;
} else if (lexer.token() == Token.TABLESPACE) {
SQLStatement stmt = parseDropTablespace(false);
if (beforeComments != null) {
stmt.addBeforeComment(beforeComments);
}
statementList.add(stmt);
continue;
} else if (lexer.token() == Token.PROCEDURE) {
SQLStatement stmt = parseDropProcedure(false);
if (beforeComments != null) {
stmt.addBeforeComment(beforeComments);
}
statementList.add(stmt);
continue;
} else {
@@ -1255,12 +1302,16 @@ public class SQLStatementParser extends SQLParser {
}
protected SQLDropTableSpaceStatement parseDropTablespace(boolean acceptDrop) {
SQLDropTableSpaceStatement stmt = new SQLDropTableSpaceStatement(getDbType());
if (lexer.isKeepComments() && lexer.hasComment()) {
stmt.addBeforeComment(lexer.readAndResetComments());
}
if (acceptDrop) {
accept(Token.DROP);
}
SQLDropTableSpaceStatement stmt = new SQLDropTableSpaceStatement(getDbType());
accept(Token.TABLESPACE);
if (lexer.token() == Token.IF) {
@@ -317,9 +317,9 @@ public final class DruidStatService implements DruidStatServiceMBean {
List<SQLStatement> statementList = SQLUtils.parseStatements(sql, dbType);
if (!statementList.isEmpty()) {
SQLStatement statemen = statementList.get(0);
SchemaStatVisitor visitor = SQLUtils.createSchemaStatVisitor(statementList, dbType);
statemen.accept(visitor);
SQLStatement sqlStmt = statementList.get(0);
SchemaStatVisitor visitor = SQLUtils.createSchemaStatVisitor(dbType);
sqlStmt.accept(visitor);
map.put("parsedTable", visitor.getTables().toString());
map.put("parsedFields", visitor.getColumns().toString());
map.put("parsedConditions", visitor.getConditions().toString());
@@ -0,0 +1,35 @@
package com.alibaba.druid.support.opds.udf;
import java.util.List;
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.visitor.SchemaStatVisitor;
import com.alibaba.druid.stat.TableStat;
import com.aliyun.odps.udf.UDF;
public class ExportColumns extends UDF {
public String evaluate(String sql) {
return evaluate(sql, null);
}
public String evaluate(String sql, String dbType) {
List<SQLStatement> statementList = SQLUtils.parseStatements(sql, dbType);
SchemaStatVisitor visitor = SQLUtils.createSchemaStatVisitor(dbType);
for (SQLStatement stmt : statementList) {
stmt.accept(visitor);
}
StringBuffer buf = new StringBuffer();
for (TableStat.Column column : visitor.getColumns()) {
if (buf.length() != 0) {
buf.append(',');
}
buf.append(column.toString());
}
return buf.toString();
}
}
@@ -0,0 +1,38 @@
package com.alibaba.druid.support.opds.udf;
import java.util.List;
import java.util.Map;
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.visitor.SchemaStatVisitor;
import com.alibaba.druid.stat.TableStat;
import com.aliyun.odps.udf.UDF;
public class ExportTables extends UDF {
public String evaluate(String sql) {
return evaluate(sql, null);
}
public String evaluate(String sql, String dbType) {
List<SQLStatement> statementList = SQLUtils.parseStatements(sql, dbType);
SchemaStatVisitor visitor = SQLUtils.createSchemaStatVisitor(dbType);
for (SQLStatement stmt : statementList) {
stmt.accept(visitor);
}
StringBuffer buf = new StringBuffer();
for (Map.Entry<TableStat.Name, TableStat> entry : visitor.getTables().entrySet()) {
TableStat.Name name = entry.getKey();
if (buf.length() != 0) {
buf.append(',');
}
buf.append(name.toString());
}
return buf.toString();
}
}
@@ -18,6 +18,7 @@ public class OdpsCreateTableTest2 extends TestCase {
parser.match(Token.EOF);
String output = SQLUtils.toOdpsString(stmt);
Assert.assertEquals("CREATE TABLE sale_detail"
+ "\nAS"
+ "\nSELECT *" //
+ "\nFROM dual", output);
}
@@ -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 OdpsFormatCommentTest23 extends TestCase {
public void test_drop_table() throws Exception {
String sql = "-- xxx"
+ "\n -- yyy"
+ "\ndrop table if exists mytables;";
Assert.assertEquals("-- xxx"
+ "\n-- yyy"
+ "\nDROP TABLE IF EXISTS mytables;", 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 OdpsFormatCommentTest24 extends TestCase {
public void test_drop_view() throws Exception {
String sql = "-- xxx"
+ "\n -- yyy"
+ "\ndrop view if exists mytables;";
Assert.assertEquals("-- xxx"
+ "\n-- yyy"
+ "\nDROP VIEW IF EXISTS mytables;", 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 OdpsFormatCommentTest25 extends TestCase {
public void test_drop_function() throws Exception {
String sql = "-- xxx"
+ "\n -- yyy"
+ "\ndrop function if exists mytables;";
Assert.assertEquals("-- xxx"
+ "\n-- yyy"
+ "\nDROP FUNCTION IF EXISTS mytables;", SQLUtils.formatOdps(sql));
}
}
@@ -0,0 +1,17 @@
package com.alibaba.druid.bvt.sql.odps;
import org.junit.Assert;
import com.alibaba.druid.sql.SQLUtils;
import junit.framework.TestCase;
public class OdpsFormatCommentTest26 extends TestCase {
public void test_drop_function() throws Exception {
String sql = "create table t as select * from dual;";
Assert.assertEquals("CREATE TABLE t"
+ "\nAS"
+ "\nSELECT *"
+ "\nFROM dual;", SQLUtils.formatOdps(sql));
}
}