improved sql parser for mysql ddl subpartition

This commit is contained in:
高铁
2016-02-25 23:36:40 +08:00
parent ec76334784
commit 4150256309
12 changed files with 303 additions and 13 deletions
@@ -31,6 +31,11 @@ public class MySqlPartitioningDef extends MySqlObjectImpl {
private SQLExpr dataDirectory;
private SQLExpr indexDirectory;
private SQLName tableSpace;
private SQLExpr maxRows;
private SQLExpr minRows;
private SQLExpr engine;
private SQLExpr comment;
@Override
public void accept0(MySqlASTVisitor visitor) {
@@ -39,6 +44,11 @@ public class MySqlPartitioningDef extends MySqlObjectImpl {
acceptChild(visitor, values);
acceptChild(visitor, dataDirectory);
acceptChild(visitor, indexDirectory);
acceptChild(visitor, tableSpace);
acceptChild(visitor, maxRows);
acceptChild(visitor, minRows);
acceptChild(visitor, engine);
acceptChild(visitor, comment);
}
visitor.endVisit(this);
}
@@ -48,6 +58,9 @@ public class MySqlPartitioningDef extends MySqlObjectImpl {
}
public void setIndexDirectory(SQLExpr indexDirectory) {
if (indexDirectory != null) {
indexDirectory.setParent(this);
}
this.indexDirectory = indexDirectory;
}
@@ -56,6 +69,9 @@ public class MySqlPartitioningDef extends MySqlObjectImpl {
}
public void setDataDirectory(SQLExpr dataDirectory) {
if (dataDirectory != null) {
dataDirectory.setParent(this);
}
this.dataDirectory = dataDirectory;
}
@@ -72,9 +88,67 @@ public class MySqlPartitioningDef extends MySqlObjectImpl {
}
public void setName(SQLName name) {
if (name != null) {
name.setParent(this);
}
this.name = name;
}
public SQLName getTableSpace() {
return tableSpace;
}
public void setTableSpace(SQLName tableSpace) {
if (tableSpace != null) {
tableSpace.setParent(this);
}
this.tableSpace = tableSpace;
}
public SQLExpr getMaxRows() {
return maxRows;
}
public void setMaxRows(SQLExpr maxRows) {
if (maxRows != null) {
maxRows.setParent(this);
}
this.maxRows = maxRows;
}
public SQLExpr getMinRows() {
return minRows;
}
public void setMinRows(SQLExpr minRows) {
if (minRows != null) {
minRows.setParent(this);
}
this.minRows = minRows;
}
public SQLExpr getEngine() {
return engine;
}
public void setEngine(SQLExpr engine) {
if (engine != null) {
engine.setParent(this);
}
this.engine = engine;
}
public SQLExpr getComment() {
return comment;
}
public void setComment(SQLExpr comment) {
if (comment != null) {
comment.setParent(this);
}
this.comment = comment;
}
public static abstract class Values extends MySqlObjectImpl {
private final List<SQLExpr> items = new ArrayList<SQLExpr>();
@@ -20,6 +20,7 @@ import com.alibaba.druid.sql.ast.SQLExpr;
import com.alibaba.druid.sql.ast.SQLName;
import com.alibaba.druid.sql.ast.SQLOrderingSpecification;
import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
import com.alibaba.druid.sql.ast.expr.SQLIntegerExpr;
import com.alibaba.druid.sql.ast.statement.SQLAssignItem;
import com.alibaba.druid.sql.ast.statement.SQLCheck;
import com.alibaba.druid.sql.ast.statement.SQLColumnDefinition;
@@ -536,14 +537,22 @@ public class MySqlCreateTableParser extends SQLCreateTableParser {
MySqlPartitioningDef.LessThanValues values = new MySqlPartitioningDef.LessThanValues();
accept(Token.LPAREN);
this.exprParser.exprList(values.getItems(), values);
accept(Token.RPAREN);
if (identifierEquals("MAXVALUE")) {
SQLIdentifierExpr maxValue = new SQLIdentifierExpr(lexer.stringVal());
lexer.nextToken();
maxValue.setParent(values);
values.getItems().add(maxValue);
} else {
accept(Token.LPAREN);
this.exprParser.exprList(values.getItems(), values);
accept(Token.RPAREN);
}
partitionDef.setValues(values);
}
}
for (;;) {
boolean storage = false;
if (identifierEquals("DATA")) {
lexer.nextToken();
acceptIdentifier("DIRECTORY");
@@ -551,6 +560,13 @@ public class MySqlCreateTableParser extends SQLCreateTableParser {
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");
@@ -558,6 +574,41 @@ public class MySqlCreateTableParser extends SQLCreateTableParser {
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;
}
@@ -605,7 +656,10 @@ public class MySqlCreateTableParser extends SQLCreateTableParser {
protected void partitionClauseRest(MySqlPartitionByClause clause) {
if (identifierEquals("PARTITIONS")) {
lexer.nextToken();
clause.setPartitionsCount(this.exprParser.expr());
SQLIntegerExpr countExpr = new SQLIntegerExpr(lexer.integerValue());
accept(Token.LITERAL_INT);
clause.setPartitionsCount(countExpr);
}
if (identifierEquals("PARTITION")) {
@@ -28,6 +28,7 @@ import com.alibaba.druid.sql.ast.SQLSetQuantifier;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.ast.expr.SQLAggregateExpr;
import com.alibaba.druid.sql.ast.expr.SQLCharExpr;
import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
import com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr;
import com.alibaba.druid.sql.ast.expr.SQLNullExpr;
import com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr;
@@ -524,7 +525,7 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
}
if (x.getPartitioning() != null) {
print(' ');
println();
x.getPartitioning().accept(this);
}
@@ -1568,7 +1569,7 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
List<MySqlPartitioningDef> partitions = x.getPartitions();
int partitionsSize = partitions.size();
if (partitionsSize > 0) {
print('(');
print0(" (");
incrementIndent();
for (int i = 0; i < partitionsSize; ++i) {
println();
@@ -3001,6 +3002,7 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
x.getDataDirectory().accept(this);
decrementIndent();
}
if (x.getIndexDirectory() != null) {
incrementIndent();
println();
@@ -3008,6 +3010,31 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
x.getIndexDirectory().accept(this);
decrementIndent();
}
if (x.getTableSpace() != null) {
print0(ucase ? " TABLESPACE " : " tablespace ");
x.getTableSpace().accept(this);
}
if (x.getEngine() != null) {
print0(ucase ? " STORAGE ENGINE " : " storage engine ");
x.getEngine().accept(this);
}
if (x.getMaxRows() != null) {
print0(ucase ? " MAX_ROWS " : " max_rows ");
x.getMaxRows().accept(this);
}
if (x.getMinRows() != null) {
print0(ucase ? " MIN_ROWS " : " min_rows ");
x.getMinRows().accept(this);
}
if (x.getComment() != null) {
print0(ucase ? " COMMENT " : " comment ");
x.getComment().accept(this);
}
return false;
}
@@ -3019,6 +3046,13 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
@Override
public boolean visit(LessThanValues x) {
if (x.getItems().size() == 1 && x.getItems().get(0) instanceof SQLIdentifierExpr) {
SQLIdentifierExpr ident = (SQLIdentifierExpr) x.getItems().get(0);
if ("MAXVALUE".equalsIgnoreCase(ident.getName())) {
print0(ucase ? "VALUES LESS THAN MAXVALUE" : "values less than maxvalue");
return false;
}
}
print0(ucase ? "VALUES LESS THAN (" : "values less than (");
printAndAccept(x.getItems(), ", ");
print(')');
@@ -57,7 +57,8 @@ public class MySqlCreateTableTest40 extends MysqlTest {
Assert.assertEquals("CREATE TABLE t1 (" + //
"\n\tcol1 INT, " + //
"\n\tcol2 CHAR(5)" + //
"\n) PARTITION BY HASH (col1)", output);
"\n)"
+ "\nPARTITION BY HASH (col1)", output);
}
}
@@ -59,7 +59,8 @@ public class MySqlCreateTableTest41 extends MysqlTest {
"\n\tcol1 INT, " + //
"\n\tcol2 CHAR(5), " + //
"\n\tcol3 DATE" + //
"\n) PARTITION BY LINEAR KEY (col3) PARTITIONS 5", output);
"\n)"
+ "\nPARTITION BY LINEAR KEY (col3) PARTITIONS 5", output);
}
}
@@ -65,7 +65,8 @@ public class MySqlCreateTableTest42 extends MysqlTest {
Assert.assertEquals("CREATE TABLE rc (" + //
"\n\ta INT NOT NULL, " + //
"\n\tb INT NOT NULL" + //
"\n) PARTITION BY RANGE COLUMNS (a, b)(" + //
"\n)"
+ "\nPARTITION BY RANGE COLUMNS (a, b) (" + //
"\n\tPARTITION p0 VALUES LESS THAN (10, 5), " + //
"\n\tPARTITION p1 VALUES LESS THAN (20, 10), " + //
"\n\tPARTITION p2 VALUES LESS THAN (MAXVALUE, 15), " + //
@@ -65,7 +65,8 @@ public class MySqlCreateTableTest43 extends MysqlTest {
Assert.assertEquals("CREATE TABLE client_firms (" + //
"\n\tid INT, " + //
"\n\tname VARCHAR(35)" + //
"\n) PARTITION BY LIST (id) (" + //
"\n)"
+ "\nPARTITION BY LIST (id) (" + //
"\n\tPARTITION r0 VALUES IN (1, 5, 9, 13, 17, 21), " + //
"\n\tPARTITION r1 VALUES IN (2, 6, 10, 14, 18, 22), " + //
"\n\tPARTITION r2 VALUES IN (3, 7, 11, 15, 19, 23), " + //
@@ -65,7 +65,8 @@ public class MySqlCreateTableTest44 extends MysqlTest {
Assert.assertEquals("CREATE TABLE lc (" + //
"\n\ta INT NULL, " + //
"\n\tb INT NULL" + //
"\n) PARTITION BY LIST COLUMNS (a, b) (" + //
"\n)" + //
"\nPARTITION BY LIST COLUMNS (a, b) (" + //
"\n\tPARTITION p0 VALUES IN ((0, 0), (NULL, NULL)), " + //
"\n\tPARTITION p1 VALUES IN ((0, 1), (0, 2), (0, 3), (1, 1), (1, 2)), " + //
"\n\tPARTITION p2 VALUES IN ((1, 0), (2, 0), (2, 1), (3, 0), (3, 1)), " + //
@@ -72,7 +72,8 @@ public class MySqlCreateTableTest45 extends MysqlTest {
"\n\tid INT, " + //
"\n\tname VARCHAR(30), " + //
"\n\tadate DATE" + //
"\n) PARTITION BY LIST (YEAR(adate)) (" + //
"\n)" + //
"\nPARTITION BY LIST (YEAR(adate)) (" + //
"\n\tPARTITION p1999 VALUES IN (1995, 1999, 2003)" + //
"\n\t\tDATA DIRECTORY '/var/appdata/95/data'" + //
"\n\t\tINDEX DIRECTORY '/var/appdata/95/idx', " + //
@@ -51,7 +51,8 @@ public class MySqlCreateTableTest66 extends MysqlTest {
+ "\n\tb_iden_string varchar, "
+ "\n\tcounter bigint, "
+ "\n\tdurationtime bigint"
+ "\n) PARTITION BY HASH KEY(a_iden_string) PARTITION NUM 100"
+ "\n)"
+ "\nPARTITION BY HASH KEY(a_iden_string) PARTITION NUM 100"
+ "\nSUBPARTITION BY LIST (bdt bigint)"
+ "\nSUBPARTITION OPTIONS (available_Partition_Num = 90)"
+ "\nTABLEGROUP g20_test_group", output);
@@ -0,0 +1,56 @@
/*
* 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.bvt.sql.mysql;
import org.junit.Assert;
import org.junit.Test;
import com.alibaba.druid.sql.MysqlTest;
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser;
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor;
public class MySqlCreateTableTest67 extends MysqlTest {
@Test
public void test_one() throws Exception {
String sql = "CREATE TABLE t1 ( a INT NOT NULL, PRIMARY KEY (a))"
+ " ENGINE=InnoDB TABLESPACE ts1 "
+ " PARTITION BY RANGE (a) PARTITIONS 3 ("
+ " PARTITION P1 VALUES LESS THAN (2),"
+ " PARTITION P2 VALUES LESS THAN (4) TABLESPACE ts2,"
+ " PARTITION P3 VALUES LESS THAN (6) TABLESPACE ts3);";
MySqlStatementParser parser = new MySqlStatementParser(sql);
SQLStatement stmt = parser.parseCreateTable();
MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor();
stmt.accept(visitor);
String output = SQLUtils.toMySqlString(stmt);
Assert.assertEquals("CREATE TABLE t1 ("
+ "\n\ta INT NOT NULL, "
+ "\n\tPRIMARY KEY (a)"
+ "\n) ENGINE = InnoDB TABLESPACE ts1"
+ "\nPARTITION BY RANGE (a) PARTITIONS 3 ("
+ "\n\tPARTITION P1 VALUES LESS THAN (2), "
+ "\n\tPARTITION P2 VALUES LESS THAN (4) TABLESPACE ts2, "
+ "\n\tPARTITION P3 VALUES LESS THAN (6) TABLESPACE ts3"
+ "\n)", output);
}
}
@@ -0,0 +1,65 @@
/*
* 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.bvt.sql.mysql;
import org.junit.Assert;
import org.junit.Test;
import com.alibaba.druid.sql.MysqlTest;
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser;
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor;
public class MySqlCreateTableTest68 extends MysqlTest {
@Test
public void test_one() throws Exception {
String sql = "CREATE TABLE t1 ("
+ "\n\tyear_col INT,"
+ "\n\tsome_data INT"
+ "\n)"
+ "\nPARTITION BY RANGE (year_col) ("
+ " PARTITION p0 VALUES LESS THAN (1991),"
+ " PARTITION p1 VALUES LESS THAN (1995),"
+ " PARTITION p2 VALUES LESS THAN (1999),"
+ " PARTITION p3 VALUES LESS THAN (2002),"
+ " PARTITION p4 VALUES LESS THAN (2006),"
+ " PARTITION p5 VALUES LESS THAN MAXVALUE"
+ ");";
MySqlStatementParser parser = new MySqlStatementParser(sql);
SQLStatement stmt = parser.parseCreateTable();
MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor();
stmt.accept(visitor);
String output = SQLUtils.toMySqlString(stmt);
Assert.assertEquals("CREATE TABLE t1 ("
+ "\n\tyear_col INT, "
+ "\n\tsome_data INT"
+ "\n)"
+ "\nPARTITION BY RANGE (year_col) ("
+ "\n\tPARTITION p0 VALUES LESS THAN (1991), "
+ "\n\tPARTITION p1 VALUES LESS THAN (1995), "
+ "\n\tPARTITION p2 VALUES LESS THAN (1999), "
+ "\n\tPARTITION p3 VALUES LESS THAN (2002), "
+ "\n\tPARTITION p4 VALUES LESS THAN (2006), "
+ "\n\tPARTITION p5 VALUES LESS THAN MAXVALUE"
+ "\n)", output);
}
}