sql parser support mysql 5.7 & oceanbase 1.0

This commit is contained in:
高铁
2016-02-29 23:12:13 +08:00
parent bebef5ea9e
commit 0ee24a11d4
44 changed files with 2675 additions and 957 deletions
@@ -24,11 +24,11 @@ import com.alibaba.druid.sql.ast.SQLPartitionBy;
import com.alibaba.druid.sql.ast.SQLPartitionByHash;
import com.alibaba.druid.sql.ast.SQLPartitionByList;
import com.alibaba.druid.sql.ast.SQLPartitionByRange;
import com.alibaba.druid.sql.ast.SQLPartitionValue;
import com.alibaba.druid.sql.ast.SQLSubPartitionBy;
import com.alibaba.druid.sql.ast.SQLSubPartitionByHash;
import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
import com.alibaba.druid.sql.ast.expr.SQLIntegerExpr;
import com.alibaba.druid.sql.ast.expr.SQLNumberExpr;
import com.alibaba.druid.sql.ast.statement.SQLAssignItem;
import com.alibaba.druid.sql.ast.statement.SQLCheck;
import com.alibaba.druid.sql.ast.statement.SQLColumnDefinition;
@@ -406,7 +406,7 @@ public class MySqlCreateTableParser extends SQLCreateTableParser {
continue;
}
if (identifierEquals("PARTITION")) {
if (lexer.token() == Token.PARTITION) {
lexer.nextToken();
accept(Token.BY);
@@ -427,13 +427,15 @@ public class MySqlCreateTableParser extends SQLCreateTableParser {
}
accept(Token.LPAREN);
for (;;) {
clause.addColumn(this.exprParser.name());
if (lexer.token() == Token.COMMA) {
lexer.nextToken();
continue;
if (lexer.token() != Token.RPAREN) {
for (;;) {
clause.addColumn(this.exprParser.name());
if (lexer.token() == Token.COMMA) {
lexer.nextToken();
continue;
}
break;
}
break;
}
accept(Token.RPAREN);
@@ -497,79 +499,7 @@ public class MySqlCreateTableParser extends SQLCreateTableParser {
if (lexer.token() == Token.LPAREN) {
lexer.nextToken();
for (;;) {
acceptIdentifier("PARTITION");
SQLPartition partitionDef = new SQLPartition();
partitionDef.setName(this.exprParser.name());
SQLPartitionValue values = parsePartitionValues();
if (values != null) {
partitionDef.setValues(values);
}
for (;;) {
boolean storage = false;
if (identifierEquals("DATA")) {
lexer.nextToken();
acceptIdentifier("DIRECTORY");
if (lexer.token() == Token.EQ) {
lexer.nextToken();
}
partitionDef.setDataDirectory(this.exprParser.expr());
} else if (lexer.token() == Token.TABLESPACE) {
lexer.nextToken();
if (lexer.token() == Token.EQ) {
lexer.nextToken();
}
SQLName tableSpace = this.exprParser.name();
partitionDef.setTableSpace(tableSpace);
} else if (lexer.token() == Token.INDEX) {
lexer.nextToken();
acceptIdentifier("DIRECTORY");
if (lexer.token() == Token.EQ) {
lexer.nextToken();
}
partitionDef.setIndexDirectory(this.exprParser.expr());
} else if (identifierEquals("MAX_ROWS")) {
lexer.nextToken();
if (lexer.token() == Token.EQ) {
lexer.nextToken();
}
SQLExpr maxRows = this.exprParser.primary();
partitionDef.setMaxRows(maxRows);
} else if (identifierEquals("MIN_ROWS")) {
lexer.nextToken();
if (lexer.token() == Token.EQ) {
lexer.nextToken();
}
SQLExpr minRows = this.exprParser.primary();
partitionDef.setMaxRows(minRows);
} else if (identifierEquals("ENGINE") || //
(storage = (lexer.token() == Token.STORAGE || identifierEquals("STORAGE")))) {
if (storage) {
lexer.nextToken();
}
acceptIdentifier("ENGINE");
if (lexer.token() == Token.EQ) {
lexer.nextToken();
}
SQLName engine = this.exprParser.name();
partitionDef.setEngine(engine);
} else if (lexer.token() == Token.COMMENT) {
lexer.nextToken();
if (lexer.token() == Token.EQ) {
lexer.nextToken();
}
SQLExpr comment = this.exprParser.primary();
partitionDef.setComment(comment);
} else {
break;
}
}
SQLPartition partitionDef = this.getExprParser().parsePartition();
partitionClause.addPartition(partitionDef);
@@ -610,9 +540,11 @@ public class MySqlCreateTableParser extends SQLCreateTableParser {
return stmt;
}
protected SQLPartitionByRange partitionByRange() {
acceptIdentifier("RANGE");
SQLPartitionByRange clause = new SQLPartitionByRange();
if (lexer.token() == Token.LPAREN) {
@@ -639,12 +571,11 @@ public class MySqlCreateTableParser extends SQLCreateTableParser {
if (identifierEquals("PARTITIONS")) {
lexer.nextToken();
SQLIntegerExpr countExpr = new SQLIntegerExpr(lexer.integerValue());
accept(Token.LITERAL_INT);
SQLIntegerExpr countExpr = this.exprParser.integerExpr();
clause.setPartitionsCount(countExpr);
}
if (identifierEquals("PARTITION")) {
if (lexer.token() == Token.PARTITION) {
lexer.nextToken();
if (identifierEquals("NUM")) {
@@ -757,6 +688,14 @@ public class MySqlCreateTableParser extends SQLCreateTableParser {
subPartitionByClause.getOptions().add(option);
}
if (identifierEquals("SUBPARTITIONS")) {
lexer.nextToken();
Number intValue = lexer.integerValue();
SQLNumberExpr numExpr = new SQLNumberExpr(intValue);
subPartitionByClause.setSubPartitionsCount(numExpr);
lexer.nextToken();
}
if (subPartitionByClause != null) {
subPartitionByClause.setLinear(linear);
@@ -20,6 +20,9 @@ import com.alibaba.druid.sql.ast.SQLExpr;
import com.alibaba.druid.sql.ast.SQLName;
import com.alibaba.druid.sql.ast.SQLOrderBy;
import com.alibaba.druid.sql.ast.SQLOrderingSpecification;
import com.alibaba.druid.sql.ast.SQLPartition;
import com.alibaba.druid.sql.ast.SQLPartitionValue;
import com.alibaba.druid.sql.ast.SQLSubPartition;
import com.alibaba.druid.sql.ast.expr.SQLAggregateExpr;
import com.alibaba.druid.sql.ast.expr.SQLBinaryExpr;
import com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr;
@@ -553,7 +556,7 @@ public class MySqlExprParser extends SQLExprParser {
lexer.nextToken();
}
if (identifierEquals("PARTITION")) {
if (lexer.token() == Token.PARTITION) {
throw new ParserException("syntax error " + lexer.token() + " " + lexer.stringVal());
}
@@ -894,4 +897,102 @@ public class MySqlExprParser extends SQLExprParser {
return item;
}
public SQLPartition parsePartition() {
accept(Token.PARTITION);
SQLPartition partitionDef = new SQLPartition();
partitionDef.setName(this.name());
SQLPartitionValue values = this.parsePartitionValues();
if (values != null) {
partitionDef.setValues(values);
}
for (;;) {
boolean storage = false;
if (identifierEquals("DATA")) {
lexer.nextToken();
acceptIdentifier("DIRECTORY");
if (lexer.token() == Token.EQ) {
lexer.nextToken();
}
partitionDef.setDataDirectory(this.expr());
} else if (lexer.token() == Token.TABLESPACE) {
lexer.nextToken();
if (lexer.token() == Token.EQ) {
lexer.nextToken();
}
SQLName tableSpace = this.name();
partitionDef.setTableSpace(tableSpace);
} else if (lexer.token() == Token.INDEX) {
lexer.nextToken();
acceptIdentifier("DIRECTORY");
if (lexer.token() == Token.EQ) {
lexer.nextToken();
}
partitionDef.setIndexDirectory(this.expr());
} else if (identifierEquals("MAX_ROWS")) {
lexer.nextToken();
if (lexer.token() == Token.EQ) {
lexer.nextToken();
}
SQLExpr maxRows = this.primary();
partitionDef.setMaxRows(maxRows);
} else if (identifierEquals("MIN_ROWS")) {
lexer.nextToken();
if (lexer.token() == Token.EQ) {
lexer.nextToken();
}
SQLExpr minRows = this.primary();
partitionDef.setMaxRows(minRows);
} else if (identifierEquals("ENGINE") || //
(storage = (lexer.token() == Token.STORAGE || identifierEquals("STORAGE")))) {
if (storage) {
lexer.nextToken();
}
acceptIdentifier("ENGINE");
if (lexer.token() == Token.EQ) {
lexer.nextToken();
}
SQLName engine = this.name();
partitionDef.setEngine(engine);
} else if (lexer.token() == Token.COMMENT) {
lexer.nextToken();
if (lexer.token() == Token.EQ) {
lexer.nextToken();
}
SQLExpr comment = this.primary();
partitionDef.setComment(comment);
} else {
break;
}
}
if (lexer.token() == Token.LPAREN) {
lexer.nextToken();
for (;;) {
acceptIdentifier("SUBPARTITION");
SQLName subPartitionName = this.name();
SQLSubPartition subPartition = new SQLSubPartition();
subPartition.setName(subPartitionName);
partitionDef.addSubPartition(subPartition);
if (lexer.token() == Token.COMMA) {
lexer.nextToken();
continue;
}
break;
}
accept(Token.RPAREN);
}
return partitionDef;
}
}
@@ -53,6 +53,9 @@ public class MySqlLexer extends Lexer {
map.put("ROW", Token.ROW);
map.put("BEGIN", Token.BEGIN);
map.put("END", Token.END);
// for oceanbase & mysql 5.7
map.put("PARTITION", Token.PARTITION);
DEFAULT_MYSQL_KEYWORDS = new Keywords(map);
}
@@ -342,6 +342,13 @@ public class MySqlSelectParser extends SQLSelectParser {
parseIndexHint(hint);
tableSource.getHints().add(hint);
}
if (lexer.token() == Token.PARTITION) {
lexer.nextToken();
accept(Token.LPAREN);
this.exprParser.names(((SQLExprTableSource) tableSource).getPartitions(), tableSource);
accept(Token.RPAREN);
}
return super.parseTableSourceRest(tableSource);
}
File diff suppressed because it is too large Load Diff
@@ -2575,6 +2575,12 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
print(' ');
x.getHints().get(i).accept(this);
}
if (x.getPartitionSize() > 0) {
print0(ucase ? " PARTITION (" : " partition (");
printlnAndAccept(x.getPartitions(), ", ");
print(')');
}
return false;
}
@@ -20,7 +20,6 @@ import java.util.List;
import com.alibaba.druid.sql.ast.SQLExpr;
import com.alibaba.druid.sql.ast.SQLHint;
import com.alibaba.druid.sql.ast.SQLPartition;
import com.alibaba.druid.sql.ast.statement.SQLInsertInto;
import com.alibaba.druid.sql.ast.statement.SQLSelect;
import com.alibaba.druid.sql.dialect.oracle.ast.OracleSQLObject;
@@ -263,39 +263,9 @@ public class OracleCreateTableParser extends SQLCreateTableParser {
accept(Token.LPAREN);
for (;;) {
acceptIdentifier("PARTITION");
SQLPartition range = new SQLPartition();
range.setName(this.exprParser.name());
SQLPartition partition = parsePartition();
SQLPartitionValue values = parsePartitionValues();
if (values != null) {
range.setValues(values);
}
if (lexer.token() == Token.LPAREN) {
lexer.nextToken();
for (;;) {
SQLSubPartition subPartition = parseSubPartition();
range.addSubPartition(subPartition);
if (lexer.token() == Token.COMMA) {
lexer.nextToken();
continue;
}
break;
}
accept(Token.RPAREN);
} else if (identifierEquals("SUBPARTITIONS")) {
lexer.nextToken();
SQLExpr subPartitionsCount = this.exprParser.primary();
range.setSubPartitionsCount(subPartitionsCount);
}
clause.addPartition(range);
clause.addPartition(partition);
if (lexer.token() == Token.COMMA) {
lexer.nextToken();
@@ -308,6 +278,41 @@ public class OracleCreateTableParser extends SQLCreateTableParser {
accept(Token.RPAREN);
}
protected SQLPartition parsePartition() {
acceptIdentifier("PARTITION");
SQLPartition partition = new SQLPartition();
partition.setName(this.exprParser.name());
SQLPartitionValue values = this.exprParser.parsePartitionValues();
if (values != null) {
partition.setValues(values);
}
if (lexer.token() == Token.LPAREN) {
lexer.nextToken();
for (;;) {
SQLSubPartition subPartition = parseSubPartition();
partition.addSubPartition(subPartition);
if (lexer.token() == Token.COMMA) {
lexer.nextToken();
continue;
}
break;
}
accept(Token.RPAREN);
} else if (identifierEquals("SUBPARTITIONS")) {
lexer.nextToken();
SQLExpr subPartitionsCount = this.exprParser.primary();
partition.setSubPartitionsCount(subPartitionsCount);
}
return partition;
}
protected SQLSubPartition parseSubPartition() {
acceptIdentifier("SUBPARTITION");
@@ -315,7 +320,7 @@ public class OracleCreateTableParser extends SQLCreateTableParser {
SQLName name = this.exprParser.name();
subPartition.setName(name);
SQLPartitionValue values = parsePartitionValues();
SQLPartitionValue values = this.exprParser.parsePartitionValues();
if (values != null) {
subPartition.setValues(values);
}
@@ -327,8 +332,7 @@ public class OracleCreateTableParser extends SQLCreateTableParser {
if (identifierEquals("PARTITIONS")) {
lexer.nextToken();
SQLIntegerExpr countExpr = new SQLIntegerExpr(lexer.integerValue());
accept(Token.LITERAL_INT);
SQLIntegerExpr countExpr = this.exprParser.integerExpr();
clause.setPartitionsCount(countExpr);
}