bug fixed for sql parser.

This commit is contained in:
wenshao
2017-01-18 11:36:30 +08:00
parent c75a3b52e0
commit a84cdc70a0
11 changed files with 428 additions and 3 deletions
@@ -40,6 +40,9 @@ public class SQLAlterTableStatement extends SQLStatementImpl implements SQLDDLSt
private boolean upgradePatiting = false;
private Map<String, SQLObject> tableOptions = new LinkedHashMap<String, SQLObject>();
// odps
private boolean mergeSmallFiles = false;
public SQLAlterTableStatement(){
}
@@ -88,6 +91,14 @@ public class SQLAlterTableStatement extends SQLStatementImpl implements SQLDDLSt
this.invalidateGlobalIndexes = invalidateGlobalIndexes;
}
public boolean isMergeSmallFiles() {
return mergeSmallFiles;
}
public void setMergeSmallFiles(boolean mergeSmallFiles) {
this.mergeSmallFiles = mergeSmallFiles;
}
public List<SQLAlterTableItem> getItems() {
return items;
}
@@ -65,6 +65,9 @@ public class SQLSelectQueryBlock extends SQLObjectImpl implements SQLSelectQuery
}
public void setGroupBy(SQLSelectGroupByClause groupBy) {
if (groupBy != null) {
groupBy.setParent(this);
}
this.groupBy = groupBy;
}
@@ -113,6 +116,9 @@ public class SQLSelectQueryBlock extends SQLObjectImpl implements SQLSelectQuery
}
public void setFrom(SQLTableSource from) {
if (from != null) {
from.setParent(this);
}
this.from = from;
}
@@ -991,6 +991,10 @@ public class SQLStatementParser extends SQLParser {
}
stmt.addItem(item);
} else if (JdbcConstants.ODPS.equals(dbType) && identifierEquals("MERGE")) {
lexer.nextToken();
acceptIdentifier("SMALLFILES");
stmt.setMergeSmallFiles(true);
} else {
break;
}
@@ -846,9 +846,9 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet
public boolean visit(SQLPropertyExpr x) {
SQLExpr owner = x.getOwner();
String mapTableName = null;
String mapTableName = null, ownerName = null;
if (owner instanceof SQLIdentifierExpr) {
String ownerName = ((SQLIdentifierExpr) owner).getName();
ownerName = ((SQLIdentifierExpr) owner).getName();
if (tableMapping != null) {
mapTableName = tableMapping.get(ownerName);
@@ -856,7 +856,20 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet
&& ownerName.length() > 2
&& ownerName.charAt(0) == '`'
&& ownerName.charAt(ownerName.length() - 1) == '`') {
mapTableName = tableMapping.get(ownerName.substring(1, ownerName.length() - 1));
ownerName = ownerName.substring(1, ownerName.length() - 1);
mapTableName = tableMapping.get(ownerName);
}
}
}
if (mapTableName != null) {
for (SQLObject parent = x.getParent();parent != null; parent = parent.getParent()) {
if (parent instanceof SQLSelectQueryBlock) {
SQLTableSource from = ((SQLSelectQueryBlock) parent).getFrom();
if (containsAlias(from, mapTableName, ownerName)) {
mapTableName = null;
}
break;
}
}
}
@@ -874,6 +887,32 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet
return false;
}
protected boolean containsAlias(SQLTableSource from, String... tableNames) {
String alias = from.getAlias();
if (alias != null) {
for (String tableName : tableNames) {
if (alias.equalsIgnoreCase(tableName)) {
return true;
}
}
if (alias.length() > 2 && alias.charAt(0) == '`' && alias.charAt(alias.length() -1) == '`') {
alias = alias.substring(1, alias.length() -1);
for (String tableName : tableNames) {
if (alias.equalsIgnoreCase(tableName)) {
return true;
}
}
}
}
if (from instanceof SQLJoinTableSource) {
SQLJoinTableSource join = (SQLJoinTableSource) from;
return containsAlias(join.getLeft(), tableNames)
|| containsAlias(join.getRight(), tableNames);
}
return false;
}
public boolean visit(SQLQueryExpr x) {
SQLObject parent = x.getParent();
if (parent instanceof SQLSelect) {
@@ -2344,6 +2383,10 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet
item.accept(this);
}
decrementIndent();
if (x.isMergeSmallFiles()) {
print0(ucase ? " MERGE SMALLFILES" : " merge smallfiles");
}
return false;
}