refactor sql parser.

This commit is contained in:
wenshao
2017-08-30 16:51:50 +08:00
parent aa260c9ee3
commit 99fe9112b4
75 changed files with 994 additions and 974 deletions
@@ -36,10 +36,10 @@ public class DB2ExprParser extends SQLExprParser {
static {
String[] strings = { "AVG", "COUNT", "MAX", "MIN", "STDDEV", "SUM", "ROW_NUMBER",
"ROWNUMBER" };
AGGREGATE_FUNCTIONS_CODES = FnvHash.fnv_64_lower(strings, true);
AGGREGATE_FUNCTIONS_CODES = FnvHash.fnv1a_64_lower(strings, true);
AGGREGATE_FUNCTIONS = new String[AGGREGATE_FUNCTIONS_CODES.length];
for (String str : strings) {
long hash = FnvHash.fnv_64_lower(str);
long hash = FnvHash.fnv1a_64_lower(str);
int index = Arrays.binarySearch(AGGREGATE_FUNCTIONS_CODES, hash);
AGGREGATE_FUNCTIONS[index] = str;
}
@@ -63,10 +63,10 @@ public class MySqlExprParser extends SQLExprParser {
static {
String[] strings = { "AVG", "COUNT", "GROUP_CONCAT", "MAX", "MIN", "STDDEV", "SUM" };
AGGREGATE_FUNCTIONS_CODES = FnvHash.fnv_64_lower(strings, true);
AGGREGATE_FUNCTIONS_CODES = FnvHash.fnv1a_64_lower(strings, true);
AGGREGATE_FUNCTIONS = new String[AGGREGATE_FUNCTIONS_CODES.length];
for (String str : strings) {
long hash = FnvHash.fnv_64_lower(str);
long hash = FnvHash.fnv1a_64_lower(str);
int index = Arrays.binarySearch(AGGREGATE_FUNCTIONS_CODES, hash);
AGGREGATE_FUNCTIONS[index] = str;
}
@@ -69,9 +69,6 @@ public class MySqlSchemaStatVisitor extends SchemaStatVisitor implements MySqlAS
repository.resolve(x);
}
setAliasMap();
getAliasMap().put("DUAL", null);
return true;
}
@@ -87,8 +84,6 @@ public class MySqlSchemaStatVisitor extends SchemaStatVisitor implements MySqlAS
repository.resolve(x);
}
setAliasMap();
setMode(x, Mode.Delete);
accept(x.getFrom());
@@ -96,10 +91,7 @@ public class MySqlSchemaStatVisitor extends SchemaStatVisitor implements MySqlAS
x.getTableSource().accept(this);
if (x.getTableSource() instanceof SQLExprTableSource) {
SQLName tableName = (SQLName) ((SQLExprTableSource) x.getTableSource()).getExpr();
String ident = tableName.toString();
TableStat stat = this.getTableStat(ident);
TableStat stat = this.getTableStat(x.getExprTableSource());
stat.incrementDeleteCount();
}
@@ -112,7 +104,6 @@ public class MySqlSchemaStatVisitor extends SchemaStatVisitor implements MySqlAS
}
public void endVisit(MySqlDeleteStatement x) {
setAliasMap(null);
}
@Override
@@ -129,27 +120,10 @@ public class MySqlSchemaStatVisitor extends SchemaStatVisitor implements MySqlAS
setMode(x, Mode.Insert);
setAliasMap();
TableStat stat = getTableStat(x.getTableSource());
SQLName tableName = x.getTableName();
String ident = null;
if (tableName instanceof SQLIdentifierExpr) {
ident = ((SQLIdentifierExpr) tableName).getName();
} else if (tableName instanceof SQLPropertyExpr) {
SQLPropertyExpr propertyExpr = (SQLPropertyExpr) tableName;
if (propertyExpr.getOwner() instanceof SQLIdentifierExpr) {
ident = propertyExpr.toString();
}
}
if (ident != null) {
TableStat stat = getTableStat(ident);
if (stat != null) {
stat.incrementInsertCount();
Map<String, String> aliasMap = getAliasMap();
putAliasMap(aliasMap, x.getAlias(), ident);
putAliasMap(aliasMap, ident, ident);
}
accept(x.getColumns());
@@ -299,26 +273,12 @@ public class MySqlSchemaStatVisitor extends SchemaStatVisitor implements MySqlAS
setMode(x, Mode.Replace);
setAliasMap();
SQLName tableName = x.getTableName();
String ident = null;
if (tableName instanceof SQLIdentifierExpr) {
ident = ((SQLIdentifierExpr) tableName).getName();
} else if (tableName instanceof SQLPropertyExpr) {
SQLPropertyExpr propertyExpr = (SQLPropertyExpr) tableName;
if (propertyExpr.getOwner() instanceof SQLIdentifierExpr) {
ident = propertyExpr.toString();
}
}
TableStat stat = getTableStat(tableName);
if (ident != null) {
TableStat stat = getTableStat(ident);
if (stat != null) {
stat.incrementInsertCount();
Map<String, String> aliasMap = getAliasMap();
putAliasMap(aliasMap, ident, ident);
}
accept(x.getColumns());
@@ -480,7 +440,7 @@ public class MySqlSchemaStatVisitor extends SchemaStatVisitor implements MySqlAS
SQLName tableName = x.getTableName();
if (tableName != null) {
String table = tableName.toString();
getTableStat(table);
getTableStat(tableName);
SQLName columnName = x.getColumnName();
if (columnName != null) {
@@ -1217,12 +1177,7 @@ public class MySqlSchemaStatVisitor extends SchemaStatVisitor implements MySqlAS
@Override
public boolean visit(MySqlDeclareStatement x) {
for (SQLDeclareItem item : x.getVarList()) {
SQLName var = (SQLName) item.getName();
this.variants.put(var.toString(), var);
}
return false;
return true;
}
@Override
@@ -1355,4 +1310,6 @@ public class MySqlSchemaStatVisitor extends SchemaStatVisitor implements MySqlAS
public void endVisit(MySqlFlushStatement x) {
}
}
@@ -35,11 +35,9 @@ public class OdpsSelectQueryBlock extends SQLSelectQueryBlock {
protected List<SQLHint> hints;
protected List<SQLExpr> distributeBy = new ArrayList<SQLExpr>();
protected List<SQLSelectOrderByItem> sortBy = new ArrayList<SQLSelectOrderByItem>(2);
public OdpsSelectQueryBlock(){
distributeBy = new ArrayList<SQLExpr>();
sortBy = new ArrayList<SQLSelectOrderByItem>(2);
}
public SQLOrderBy getOrderBy() {
@@ -50,13 +48,7 @@ public class OdpsSelectQueryBlock extends SQLSelectQueryBlock {
this.orderBy = orderBy;
}
public List<SQLExpr> getDistributeBy() {
return distributeBy;
}
public List<SQLSelectOrderByItem> getSortBy() {
return sortBy;
}
@Override
public int hashCode() {
@@ -46,10 +46,10 @@ public class OdpsExprParser extends SQLExprParser {
"ROW_NUMBER",
"WM_CONCAT"//
};
AGGREGATE_FUNCTIONS_CODES = FnvHash.fnv_64_lower(strings, true);
AGGREGATE_FUNCTIONS_CODES = FnvHash.fnv1a_64_lower(strings, true);
AGGREGATE_FUNCTIONS = new String[AGGREGATE_FUNCTIONS_CODES.length];
for (String str : strings) {
long hash = FnvHash.fnv_64_lower(str);
long hash = FnvHash.fnv1a_64_lower(str);
int index = Arrays.binarySearch(AGGREGATE_FUNCTIONS_CODES, hash);
AGGREGATE_FUNCTIONS[index] = str;
}
@@ -64,20 +64,14 @@ public class OdpsSchemaStatVisitor extends SchemaStatVisitor implements OdpsASTV
public boolean visit(OdpsInsert x) {
setMode(x, TableStat.Mode.Insert);
setAliasMap();
SQLExprTableSource tableSource = x.getTableSource();
SQLExpr tableName = tableSource.getExpr();
if (tableName instanceof SQLName) {
String ident = ((SQLName) tableName).toString();
TableStat stat = getTableStat(ident);
TableStat stat = getTableStat(tableName);
stat.incrementInsertCount();
Map<String, String> aliasMap = getAliasMap();
putAliasMap(aliasMap, tableSource.getAlias(), ident);
putAliasMap(aliasMap, ident, ident);
}
for (SQLAssignItem partition : x.getPartitions()) {
@@ -289,11 +283,6 @@ public class OdpsSchemaStatVisitor extends SchemaStatVisitor implements OdpsASTV
@Override
public boolean visit(OdpsValuesTableSource x) {
Map<String, String> aliasMap = getAliasMap();
if (aliasMap != null && x.getAlias() != null) {
putAliasMap(aliasMap, x.getAlias(), null);
addSubQuery(x.getAlias(), x);
}
return false;
}
}
@@ -143,7 +143,7 @@ public class OracleDbLinkExpr extends SQLExprImpl implements SQLName, OracleExpr
} else if (expr == null){
hash = FnvHash.BASIC;
} else {
hash = FnvHash.fnv_64_lower(expr.toString());
hash = FnvHash.fnv1a_64_lower(expr.toString());
hash ^= '.';
hash *= FnvHash.PRIME;
@@ -111,10 +111,10 @@ public class OracleExprParser extends SQLExprParser {
"VARIANCE", //
"WM_CONCAT"
};
AGGREGATE_FUNCTIONS_CODES = FnvHash.fnv_64_lower(strings, true);
AGGREGATE_FUNCTIONS_CODES = FnvHash.fnv1a_64_lower(strings, true);
AGGREGATE_FUNCTIONS = new String[AGGREGATE_FUNCTIONS_CODES.length];
for (String str : strings) {
long hash = FnvHash.fnv_64_lower(str);
long hash = FnvHash.fnv1a_64_lower(str);
int index = Arrays.binarySearch(AGGREGATE_FUNCTIONS_CODES, hash);
AGGREGATE_FUNCTIONS[index] = str;
}
@@ -63,6 +63,7 @@ import com.alibaba.druid.stat.TableStat;
import com.alibaba.druid.stat.TableStat.Column;
import com.alibaba.druid.stat.TableStat.Mode;
import com.alibaba.druid.stat.TableStat.Relationship;
import com.alibaba.druid.util.FnvConstants;
import com.alibaba.druid.util.JdbcConstants;
import com.alibaba.druid.util.JdbcUtils;
@@ -74,10 +75,6 @@ public class OracleSchemaStatVisitor extends SchemaStatVisitor implements Oracle
public OracleSchemaStatVisitor(List<Object> parameters){
super(JdbcConstants.ORACLE, parameters);
this.variants.put("DUAL", null);
this.variants.put("NOTFOUND", null);
this.variants.put("TRUE", null);
this.variants.put("FALSE", null);
}
protected Column getColumn(SQLExpr expr) {
@@ -90,42 +87,25 @@ public class OracleSchemaStatVisitor extends SchemaStatVisitor implements Oracle
public boolean visit(OracleSelectTableReference x) {
SQLExpr expr = x.getExpr();
if (expr instanceof SQLMethodInvokeExpr) {
SQLMethodInvokeExpr methodInvoke = (SQLMethodInvokeExpr) expr;
if ("TABLE".equalsIgnoreCase(methodInvoke.getMethodName()) && methodInvoke.getParameters().size() == 1) {
expr = methodInvoke.getParameters().get(0);
}
}
Map<String, String> aliasMap = getAliasMap();
TableStat stat = getTableStat(x);
if (expr instanceof SQLName) {
String ident;
if (((SQLName) expr).nameHashCode64() == FnvConstants.DUAL) {
return false;
}
if (expr instanceof SQLPropertyExpr) {
String owner = ((SQLPropertyExpr) expr).getOwner().toString();
String name = ((SQLPropertyExpr) expr).getName();
if (aliasMap.containsKey(owner)) {
owner = aliasMap.get(owner);
SQLPropertyExpr propertyExpr = (SQLPropertyExpr) expr;
if (isSubQueryOrParamOrVariant(propertyExpr)) {
return false;
}
} else if (expr instanceof SQLIdentifierExpr) {
SQLIdentifierExpr identifierExpr = (SQLIdentifierExpr) expr;
if (isSubQueryOrParamOrVariant(identifierExpr)) {
return false;
}
ident = owner + "." + name;
} else {
ident = expr.toString();
}
if (containsSubQuery(ident)) {
return false;
}
if ("DUAL".equalsIgnoreCase(ident)) {
return false;
}
x.putAttribute(ATTR_TABLE, ident);
TableStat stat = getTableStat(ident);
Mode mode = getMode();
switch (mode) {
case Delete:
@@ -147,23 +127,15 @@ public class OracleSchemaStatVisitor extends SchemaStatVisitor implements Oracle
break;
}
putAliasMap(aliasMap, x.getAlias(), ident);
putAliasMap(aliasMap, ident, ident);
return false;
}
accept(x.getExpr());
// accept(x.getExpr());
return false;
}
public void endVisit(SQLSelect x) {
if (x.getQuery() != null) {
String table = (String) x.getQuery().getAttribute(ATTR_TABLE);
if (table != null) {
x.putAttribute(ATTR_TABLE, table);
}
}
}
public boolean visit(OracleUpdateStatement x) {
@@ -172,7 +144,6 @@ public class OracleSchemaStatVisitor extends SchemaStatVisitor implements Oracle
repository.resolve(x);
}
setAliasMap();
setMode(x, Mode.Update);
SQLTableSource tableSource = x.getTableSource();
@@ -187,10 +158,6 @@ public class OracleSchemaStatVisitor extends SchemaStatVisitor implements Oracle
TableStat stat = getTableStat(ident);
stat.incrementUpdateCount();
Map<String, String> aliasMap = getAliasMap();
aliasMap.put(ident, ident);
aliasMap.put(tableSource.getAlias(), ident);
} else {
tableSource.accept(this);
}
@@ -220,9 +187,7 @@ public class OracleSchemaStatVisitor extends SchemaStatVisitor implements Oracle
}
}
visit((SQLSelectQueryBlock) x);
return true;
return visit((SQLSelectQueryBlock) x);
}
public void endVisit(OracleSelectQueryBlock x) {
@@ -366,29 +331,29 @@ public class OracleSchemaStatVisitor extends SchemaStatVisitor implements Oracle
@Override
public boolean visit(OracleSelectJoin x) {
super.visit(x);
for (SQLExpr item : x.getUsing()) {
if (item instanceof SQLIdentifierExpr) {
String columnName = ((SQLIdentifierExpr) item).getName();
String leftTable = (String) x.getLeft().getAttribute(ATTR_TABLE);
String rightTable = (String) x.getRight().getAttribute(ATTR_TABLE);
if (leftTable != null && rightTable != null) {
Relationship relationship = new Relationship();
relationship.setLeft(new Column(leftTable, columnName));
relationship.setRight(new Column(rightTable, columnName));
relationship.setOperator("USING");
relationships.add(relationship);
}
if (leftTable != null) {
addColumn(leftTable, columnName);
}
if (rightTable != null) {
addColumn(rightTable, columnName);
}
}
}
//
// for (SQLExpr item : x.getUsing()) {
// if (item instanceof SQLIdentifierExpr) {
// String columnName = ((SQLIdentifierExpr) item).getName();
// String leftTable = (String) x.getLeft().getAttribute(ATTR_TABLE);
// String rightTable = (String) x.getRight().getAttribute(ATTR_TABLE);
// if (leftTable != null && rightTable != null) {
// Relationship relationship = new Relationship();
// relationship.setLeft(new Column(leftTable, columnName));
// relationship.setRight(new Column(rightTable, columnName));
// relationship.setOperator("USING");
// relationships.add(relationship);
// }
//
// if (leftTable != null) {
// addColumn(leftTable, columnName);
// }
//
// if (rightTable != null) {
// addColumn(rightTable, columnName);
// }
// }
// }
return false;
}
@@ -422,18 +387,6 @@ public class OracleSchemaStatVisitor extends SchemaStatVisitor implements Oracle
accept(x.getSelect());
accept(x.getPivot());
accept(x.getFlashback());
String table = (String) x.getSelect().getAttribute(ATTR_TABLE);
if (x.getAlias() != null) {
if (table != null) {
this.aliasMap.put(x.getAlias(), table);
}
addSubQuery(x.getAlias(), x.getSelect());
}
if (table != null) {
x.putAttribute(ATTR_TABLE, table);
}
return false;
}
@@ -471,15 +424,6 @@ public class OracleSchemaStatVisitor extends SchemaStatVisitor implements Oracle
@Override
public boolean visit(OracleWithSubqueryEntry x) {
Map<String, String> aliasMap = getAliasMap();
if (aliasMap != null) {
String alias = x.getAlias();
if (alias != null) {
putAliasMap(aliasMap, alias, null);
addSubQuery(alias, x.getSubQuery());
}
}
x.getSubQuery().accept(this);
return false;
}
@@ -701,14 +645,6 @@ public class OracleSchemaStatVisitor extends SchemaStatVisitor implements Oracle
TableStat stat = getTableStat(ident);
stat.incrementInsertCount();
Map<String, String> aliasMap = getAliasMap();
if (aliasMap != null) {
if (x.getAlias() != null) {
putAliasMap(aliasMap, x.getAlias(), ident);
}
putAliasMap(aliasMap, ident, ident);
}
}
accept(x.getColumns());
@@ -734,8 +670,6 @@ public class OracleSchemaStatVisitor extends SchemaStatVisitor implements Oracle
x.putAttribute("_original_use_mode", getMode());
setMode(x, Mode.Insert);
setAliasMap();
accept(x.getSubQuery());
accept(x.getEntries());
@@ -1008,12 +942,8 @@ public class OracleSchemaStatVisitor extends SchemaStatVisitor implements Oracle
@Override
public boolean visit(OracleForStatement x) {
SQLName index = x.getIndex();
this.getVariants().put(index.toString(), x);
x.getRange().accept(this);
accept(x.getStatements());
return false;
}
@@ -25,8 +25,6 @@ public class PGUpdateStatement extends SQLUpdateStatement implements PGSQLStatem
private boolean only = false;
private SQLWithSubqueryClause with;
public PGUpdateStatement(){
super (JdbcConstants.POSTGRESQL);
}
@@ -39,14 +37,6 @@ public class PGUpdateStatement extends SQLUpdateStatement implements PGSQLStatem
this.only = only;
}
public SQLWithSubqueryClause getWith() {
return with;
}
public void setWith(SQLWithSubqueryClause with) {
this.with = with;
}
protected void accept0(SQLASTVisitor visitor) {
accept0((PGASTVisitor) visitor);
}
@@ -54,10 +54,10 @@ public class PGExprParser extends SQLExprParser {
static {
String[] strings = { "AVG", "COUNT", "MAX", "MIN", "STDDEV", "SUM", "ROW_NUMBER" };
AGGREGATE_FUNCTIONS_CODES = FnvHash.fnv_64_lower(strings, true);
AGGREGATE_FUNCTIONS_CODES = FnvHash.fnv1a_64_lower(strings, true);
AGGREGATE_FUNCTIONS = new String[AGGREGATE_FUNCTIONS_CODES.length];
for (String str : strings) {
long hash = FnvHash.fnv_64_lower(str);
long hash = FnvHash.fnv1a_64_lower(str);
int index = Arrays.binarySearch(AGGREGATE_FUNCTIONS_CODES, hash);
AGGREGATE_FUNCTIONS[index] = str;
}
@@ -101,8 +101,6 @@ public class PGSchemaStatVisitor extends SchemaStatVisitor implements PGASTVisit
x.getWith().accept(this);
}
setAliasMap();
SQLTableSource using = x.getUsing();
if (using != null) {
using.accept(this);
@@ -111,9 +109,7 @@ public class PGSchemaStatVisitor extends SchemaStatVisitor implements PGASTVisit
x.putAttribute("_original_use_mode", getMode());
setMode(x, Mode.Delete);
String ident = ((SQLIdentifierExpr) x.getTableName()).getName();
TableStat stat = getTableStat(ident, x.getAlias());
TableStat stat = getTableStat(x.getTableName());
stat.incrementDeleteCount();
accept(x.getWhere());
@@ -133,8 +129,6 @@ public class PGSchemaStatVisitor extends SchemaStatVisitor implements PGASTVisit
repository.resolve(x);
}
setAliasMap();
if (x.getWith() != null) {
x.getWith().accept(this);
}
@@ -149,14 +143,6 @@ public class PGSchemaStatVisitor extends SchemaStatVisitor implements PGASTVisit
TableStat stat = getTableStat(ident);
stat.incrementInsertCount();
Map<String, String> aliasMap = getAliasMap();
if (aliasMap != null) {
if (x.getAlias() != null) {
aliasMap.put(x.getAlias(), ident);
}
aliasMap.put(ident, ident);
}
}
accept(x.getColumns());
@@ -191,10 +177,6 @@ public class PGSchemaStatVisitor extends SchemaStatVisitor implements PGASTVisit
repository.resolve(x);
}
Map<String, String> oldAliasMap = getAliasMap();
setAliasMap();
if (x.getWith() != null) {
x.getWith().accept(this);
}
@@ -206,14 +188,9 @@ public class PGSchemaStatVisitor extends SchemaStatVisitor implements PGASTVisit
accept(x.getFrom());
Map<String, String> aliasMap = getAliasMap();
aliasMap.put(ident, ident);
accept(x.getItems());
accept(x.getWhere());
setAliasMap(oldAliasMap);
return false;
}
@@ -44,10 +44,10 @@ public class SQLServerExprParser extends SQLExprParser {
static {
String[] strings = { "AVG", "COUNT", "MAX", "MIN", "ROW_NUMBER", "STDDEV", "SUM" };
AGGREGATE_FUNCTIONS_CODES = FnvHash.fnv_64_lower(strings, true);
AGGREGATE_FUNCTIONS_CODES = FnvHash.fnv1a_64_lower(strings, true);
AGGREGATE_FUNCTIONS = new String[AGGREGATE_FUNCTIONS_CODES.length];
for (String str : strings) {
long hash = FnvHash.fnv_64_lower(str);
long hash = FnvHash.fnv1a_64_lower(str);
int index = Arrays.binarySearch(AGGREGATE_FUNCTIONS_CODES, hash);
AGGREGATE_FUNCTIONS[index] = str;
}
@@ -84,16 +84,11 @@ public class SQLServerSchemaStatVisitor extends SchemaStatVisitor implements SQL
@Override
public boolean visit(SQLServerUpdateStatement x) {
setAliasMap();
String ident = x.getTableName().toString();
TableStat stat = getTableStat(ident);
stat.incrementUpdateCount();
Map<String, String> aliasMap = getAliasMap();
aliasMap.put(ident, ident);
accept(x.getItems());
accept(x.getFrom());
accept(x.getWhere());