improved schemastat

This commit is contained in:
高铁
2015-11-03 14:24:10 +08:00
parent 8cbaefe173
commit 2107d236c0
10 changed files with 173 additions and 38 deletions
@@ -24,14 +24,16 @@ import com.alibaba.druid.sql.visitor.SQLASTVisitor;
public class SQLCreateTableStatement extends SQLStatementImpl implements SQLDDLStatement {
protected boolean ifNotExiists = false;
protected Type type;
protected SQLExprTableSource tableSource;
protected boolean ifNotExiists = false;
protected Type type;
protected SQLExprTableSource tableSource;
protected List<SQLTableElement> tableElementList = new ArrayList<SQLTableElement>();
// for postgresql
private SQLExprTableSource inherits;
private SQLExprTableSource inherits;
protected SQLSelect select;
public SQLCreateTableStatement(){
@@ -73,7 +75,7 @@ public class SQLCreateTableStatement extends SQLStatementImpl implements SQLDDLS
}
public static enum Type {
GLOBAL_TEMPORARY, LOCAL_TEMPORARY
GLOBAL_TEMPORARY, LOCAL_TEMPORARY
}
public List<SQLTableElement> getTableElementList() {
@@ -99,12 +101,21 @@ public class SQLCreateTableStatement extends SQLStatementImpl implements SQLDDLS
this.inherits = inherits;
}
public SQLSelect getSelect() {
return select;
}
public void setSelect(SQLSelect select) {
this.select = select;
}
@Override
protected void accept0(SQLASTVisitor visitor) {
if (visitor.visit(this)) {
this.acceptChild(visitor, tableSource);
this.acceptChild(visitor, tableElementList);
this.acceptChild(visitor, inherits);
this.acceptChild(visitor, select);
}
visitor.endVisit(this);
}
@@ -37,8 +37,6 @@ public class MySqlCreateTableStatement extends SQLCreateTableStatement implement
private Map<String, SQLObject> tableOptions = new LinkedHashMap<String, SQLObject>();
protected SQLSelect query;
private SQLPartitioningClause partitioning;
private List<SQLCommentHint> hints = new ArrayList<SQLCommentHint>();
@@ -90,12 +88,14 @@ public class MySqlCreateTableStatement extends SQLCreateTableStatement implement
return tableOptions;
}
@Deprecated
public SQLSelect getQuery() {
return query;
return select;
}
@Deprecated
public void setQuery(SQLSelect query) {
this.query = query;
this.select = query;
}
@Override
@@ -113,7 +113,7 @@ public class MySqlCreateTableStatement extends SQLCreateTableStatement implement
this.acceptChild(visitor, getTableSource());
this.acceptChild(visitor, getTableElementList());
this.acceptChild(visitor, getLike());
this.acceptChild(visitor, getQuery());
this.acceptChild(visitor, getSelect());
}
visitor.endVisit(this);
}
@@ -581,7 +581,7 @@ public class MySqlCreateTableParser extends SQLCreateTableParser {
if (lexer.token() == (Token.SELECT)) {
SQLSelect query = new MySqlSelectParser(this.exprParser).select();
stmt.setQuery(query);
stmt.setSelect(query);
}
while (lexer.token() == (Token.HINT)) {
@@ -520,10 +520,10 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
x.getPartitioning().accept(this);
}
if (x.getQuery() != null) {
if (x.getSelect() != null) {
incrementIndent();
println();
x.getQuery().accept(this);
x.getSelect().accept(this);
decrementIndent();
}
@@ -23,22 +23,19 @@ import com.alibaba.druid.sql.ast.SQLName;
import com.alibaba.druid.sql.ast.statement.SQLColumnDefinition;
import com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement;
import com.alibaba.druid.sql.ast.statement.SQLExprTableSource;
import com.alibaba.druid.sql.ast.statement.SQLSelect;
import com.alibaba.druid.sql.dialect.odps.visitor.OdpsASTVisitor;
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
import com.alibaba.druid.util.JdbcConstants;
public class OdpsCreateTableStatement extends SQLCreateTableStatement {
private SQLExprTableSource like;
private SQLExprTableSource like;
protected SQLExpr comment;
protected SQLExpr comment;
protected List<SQLColumnDefinition> partitionColumns = new ArrayList<SQLColumnDefinition>(2);
protected SQLExpr lifecycle;
protected SQLSelect select;
protected SQLExpr lifecycle;
public OdpsCreateTableStatement(){
super(JdbcConstants.ODPS);
@@ -76,14 +73,6 @@ public class OdpsCreateTableStatement extends SQLCreateTableStatement {
this.lifecycle = lifecycle;
}
public SQLSelect getSelect() {
return select;
}
public void setSelect(SQLSelect select) {
this.select = select;
}
@Override
protected void accept0(SQLASTVisitor visitor) {
accept0((OdpsASTVisitor) visitor);
@@ -19,7 +19,6 @@ import com.alibaba.druid.sql.ast.SQLExpr;
import com.alibaba.druid.sql.ast.SQLName;
import com.alibaba.druid.sql.ast.SQLPartitioningClause;
import com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement;
import com.alibaba.druid.sql.ast.statement.SQLSelect;
import com.alibaba.druid.sql.dialect.oracle.ast.clause.OracleLobStorageClause;
import com.alibaba.druid.sql.dialect.oracle.ast.clause.OracleStorageClause;
import com.alibaba.druid.sql.dialect.oracle.visitor.OracleASTVisitor;
@@ -30,8 +29,6 @@ public class OracleCreateTableStatement extends SQLCreateTableStatement implemen
private SQLName tablespace;
private SQLSelect select;
private boolean inMemoryMetadata;
private boolean cursorSpecificSegment;
@@ -200,14 +197,6 @@ public class OracleCreateTableStatement extends SQLCreateTableStatement implemen
this.tablespace = tablespace;
}
public SQLSelect getSelect() {
return select;
}
public void setSelect(SQLSelect select) {
this.select = select;
}
protected void accept0(SQLASTVisitor visitor) {
accept0((OracleASTVisitor) visitor);
}
@@ -1136,6 +1136,10 @@ public class SchemaStatVisitor extends SQLASTVisitorAdapter {
x.getInherits().accept(this);
}
if (x.getSelect() != null) {
x.getSelect().accept(this);
}
return false;
}
@@ -0,0 +1,61 @@
/*
* Copyright 1999-2101 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
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 ExportConditionColumns extends UDF {
public String evaluate(String sql) {
return evaluate(sql, null);
}
public String evaluate(String sql, String dbType) {
try {
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 ((!column.isWhere()) && !column.isJoin()) {
continue;
}
if (buf.length() != 0) {
buf.append(',');
}
buf.append(column.toString());
}
return buf.toString();
} catch (Throwable ex) {
System.err.println("error sql : " + sql);
ex.printStackTrace();
return null;
}
}
}
@@ -0,0 +1,61 @@
/*
* Copyright 1999-2101 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
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 ExportSelectListColumns extends UDF {
public String evaluate(String sql) {
return evaluate(sql, null);
}
public String evaluate(String sql, String dbType) {
try {
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 (!column.isSelect()) {
continue;
}
if (buf.length() != 0) {
buf.append(',');
}
buf.append(column.toString());
}
return buf.toString();
} catch (Throwable ex) {
System.err.println("error sql : " + sql);
ex.printStackTrace();
return null;
}
}
}
@@ -0,0 +1,20 @@
package com.alibaba.druid.bvt.sql.odps.udf;
import com.alibaba.druid.support.opds.udf.ExportSelectListColumns;
import junit.framework.TestCase;
public class ExportSelectListColumnsTest extends TestCase {
private ExportSelectListColumns udf = new ExportSelectListColumns();
public void test_0() throws Exception {
String sql = "SELECT fund_base_cv_creative_ocr_judge_control_words_1422189630695(a.tfs, b.ocr_text, a.cates)"
+ "\n AS (tfs, control_word, ocr_word, control_type, cates)"
+ "\n FROM fund_base_cv_ad_auction_ocr_pv_tfs a"
+ "\n JOIN fund_base_cv_ad_auction_ocr_pv_tfs_ocr b ON a.tfs = b.tfs"
+ "\n WHERE a.ds=20150819 and b.ds=20150819;";
System.out.println(udf.evaluate(sql, "odps"));
}
}