mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
refactor mysql ddl partition by clause
This commit is contained in:
+6
-16
@@ -22,9 +22,8 @@ public class MySqlPartitionByHash extends MySqlPartitioningClause {
|
||||
|
||||
private SQLExpr expr;
|
||||
|
||||
private SQLExpr partitionCount;
|
||||
|
||||
private boolean linear;
|
||||
// for aliyun ads
|
||||
private boolean key;
|
||||
|
||||
@Override
|
||||
public void accept0(MySqlASTVisitor visitor) {
|
||||
@@ -36,14 +35,6 @@ public class MySqlPartitionByHash extends MySqlPartitioningClause {
|
||||
visitor.endVisit(this);
|
||||
}
|
||||
|
||||
public SQLExpr getPartitionCount() {
|
||||
return partitionCount;
|
||||
}
|
||||
|
||||
public void setPartitionCount(SQLExpr partitionCount) {
|
||||
this.partitionCount = partitionCount;
|
||||
}
|
||||
|
||||
public SQLExpr getExpr() {
|
||||
return expr;
|
||||
}
|
||||
@@ -52,12 +43,11 @@ public class MySqlPartitionByHash extends MySqlPartitioningClause {
|
||||
this.expr = expr;
|
||||
}
|
||||
|
||||
public boolean isLinear() {
|
||||
return linear;
|
||||
public boolean isKey() {
|
||||
return key;
|
||||
}
|
||||
|
||||
public void setLinear(boolean linear) {
|
||||
this.linear = linear;
|
||||
public void setKey(boolean key) {
|
||||
this.key = key;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
-21
@@ -18,7 +18,6 @@ package com.alibaba.druid.sql.dialect.mysql.ast.statement;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import com.alibaba.druid.sql.ast.SQLExpr;
|
||||
import com.alibaba.druid.sql.ast.SQLName;
|
||||
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitor;
|
||||
|
||||
@@ -26,10 +25,6 @@ public class MySqlPartitionByKey extends MySqlPartitioningClause {
|
||||
|
||||
private List<SQLName> columns = new ArrayList<SQLName>();
|
||||
|
||||
private SQLExpr partitionCount;
|
||||
|
||||
private boolean linear;
|
||||
|
||||
@Override
|
||||
public void accept0(MySqlASTVisitor visitor) {
|
||||
if (visitor.visit(this)) {
|
||||
@@ -40,14 +35,6 @@ public class MySqlPartitionByKey extends MySqlPartitioningClause {
|
||||
visitor.endVisit(this);
|
||||
}
|
||||
|
||||
public SQLExpr getPartitionCount() {
|
||||
return partitionCount;
|
||||
}
|
||||
|
||||
public void setPartitionCount(SQLExpr partitionCount) {
|
||||
this.partitionCount = partitionCount;
|
||||
}
|
||||
|
||||
public List<SQLName> getColumns() {
|
||||
return columns;
|
||||
}
|
||||
@@ -56,12 +43,4 @@ public class MySqlPartitionByKey extends MySqlPartitioningClause {
|
||||
this.columns = columns;
|
||||
}
|
||||
|
||||
public boolean isLinear() {
|
||||
return linear;
|
||||
}
|
||||
|
||||
public void setLinear(boolean linear) {
|
||||
this.linear = linear;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
-10
@@ -28,8 +28,6 @@ public class MySqlPartitionByList extends MySqlPartitioningClause {
|
||||
|
||||
private List<SQLName> columns = new ArrayList<SQLName>();
|
||||
|
||||
private SQLExpr partitionCount;
|
||||
|
||||
@Override
|
||||
public void accept0(MySqlASTVisitor visitor) {
|
||||
if (visitor.visit(this)) {
|
||||
@@ -52,14 +50,6 @@ public class MySqlPartitionByList extends MySqlPartitioningClause {
|
||||
this.expr = expr;
|
||||
}
|
||||
|
||||
public SQLExpr getPartitionCount() {
|
||||
return partitionCount;
|
||||
}
|
||||
|
||||
public void setPartitionCount(SQLExpr partitionCount) {
|
||||
this.partitionCount = partitionCount;
|
||||
}
|
||||
|
||||
public List<SQLName> getColumns() {
|
||||
return columns;
|
||||
}
|
||||
|
||||
-2
@@ -28,8 +28,6 @@ public class MySqlPartitionByRange extends MySqlPartitioningClause {
|
||||
|
||||
private List<SQLName> columns = new ArrayList<SQLName>();
|
||||
|
||||
private SQLExpr partitionCount;
|
||||
|
||||
@Override
|
||||
public void accept0(MySqlASTVisitor visitor) {
|
||||
if (visitor.visit(this)) {
|
||||
|
||||
+20
@@ -18,6 +18,7 @@ package com.alibaba.druid.sql.dialect.mysql.ast.statement;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import com.alibaba.druid.sql.ast.SQLExpr;
|
||||
import com.alibaba.druid.sql.ast.SQLPartitioningClause;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.MySqlObjectImpl;
|
||||
|
||||
@@ -25,6 +26,10 @@ public abstract class MySqlPartitioningClause extends MySqlObjectImpl implements
|
||||
|
||||
private List<MySqlPartitioningDef> partitions = new ArrayList<MySqlPartitioningDef>();
|
||||
|
||||
protected SQLExpr partitionCount;
|
||||
|
||||
protected boolean linear;
|
||||
|
||||
public List<MySqlPartitioningDef> getPartitions() {
|
||||
return partitions;
|
||||
}
|
||||
@@ -33,4 +38,19 @@ public abstract class MySqlPartitioningClause extends MySqlObjectImpl implements
|
||||
this.partitions = partitions;
|
||||
}
|
||||
|
||||
public SQLExpr getPartitionCount() {
|
||||
return partitionCount;
|
||||
}
|
||||
|
||||
public void setPartitionCount(SQLExpr partitionCount) {
|
||||
this.partitionCount = partitionCount;
|
||||
}
|
||||
|
||||
public boolean isLinear() {
|
||||
return linear;
|
||||
}
|
||||
|
||||
public void setLinear(boolean linear) {
|
||||
this.linear = linear;
|
||||
}
|
||||
}
|
||||
|
||||
+16
-17
@@ -422,10 +422,7 @@ public class MySqlCreateTableParser extends SQLCreateTableParser {
|
||||
|
||||
partitionClause = clause;
|
||||
|
||||
if (identifierEquals("PARTITIONS")) {
|
||||
lexer.nextToken();
|
||||
clause.setPartitionCount(this.exprParser.expr());
|
||||
}
|
||||
partitionClauseRest(clause);
|
||||
} else if (identifierEquals("HASH")) {
|
||||
lexer.nextToken();
|
||||
MySqlPartitionByHash clause = new MySqlPartitionByHash();
|
||||
@@ -433,16 +430,18 @@ public class MySqlCreateTableParser extends SQLCreateTableParser {
|
||||
if (linera) {
|
||||
clause.setLinear(true);
|
||||
}
|
||||
|
||||
if (lexer.token() == Token.KEY) {
|
||||
lexer.nextToken();
|
||||
clause.setKey(true);
|
||||
}
|
||||
|
||||
accept(Token.LPAREN);
|
||||
clause.setExpr(this.exprParser.expr());
|
||||
accept(Token.RPAREN);
|
||||
partitionClause = clause;
|
||||
|
||||
if (identifierEquals("PARTITIONS")) {
|
||||
lexer.nextToken();
|
||||
clause.setPartitionCount(this.exprParser.expr());
|
||||
}
|
||||
partitionClauseRest(clause);
|
||||
|
||||
} else if (identifierEquals("RANGE")) {
|
||||
lexer.nextToken();
|
||||
@@ -467,11 +466,7 @@ public class MySqlCreateTableParser extends SQLCreateTableParser {
|
||||
}
|
||||
partitionClause = clause;
|
||||
|
||||
if (identifierEquals("PARTITIONS")) {
|
||||
lexer.nextToken();
|
||||
clause.setPartitionCount(this.exprParser.expr());
|
||||
}
|
||||
//
|
||||
partitionClauseRest(clause);
|
||||
|
||||
} else if (identifierEquals("LIST")) {
|
||||
lexer.nextToken();
|
||||
@@ -496,10 +491,7 @@ public class MySqlCreateTableParser extends SQLCreateTableParser {
|
||||
}
|
||||
partitionClause = clause;
|
||||
|
||||
if (identifierEquals("PARTITIONS")) {
|
||||
lexer.nextToken();
|
||||
clause.setPartitionCount(this.exprParser.expr());
|
||||
}
|
||||
partitionClauseRest(clause);
|
||||
} else {
|
||||
throw new ParserException("TODO " + lexer.token() + " " + lexer.stringVal());
|
||||
}
|
||||
@@ -593,6 +585,13 @@ public class MySqlCreateTableParser extends SQLCreateTableParser {
|
||||
return stmt;
|
||||
}
|
||||
|
||||
protected void partitionClauseRest(MySqlPartitioningClause clause) {
|
||||
if (identifierEquals("PARTITIONS")) {
|
||||
lexer.nextToken();
|
||||
clause.setPartitionCount(this.exprParser.expr());
|
||||
}
|
||||
}
|
||||
|
||||
private boolean parseTableOptionCharsetOrCollate(MySqlCreateTableStatement stmt) {
|
||||
if (identifierEquals("CHARACTER")) {
|
||||
lexer.nextToken();
|
||||
|
||||
@@ -1620,11 +1620,16 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
|
||||
@Override
|
||||
public boolean visit(MySqlPartitionByHash x) {
|
||||
if (x.isLinear()) {
|
||||
print0(ucase ? "PARTITION BY LINEAR HASH (" : "partition by linear hash (");
|
||||
print0(ucase ? "PARTITION BY LINEAR HASH " : "partition by linear hash ");
|
||||
} else {
|
||||
print0(ucase ? "PARTITION BY HASH (" : "partition by hash (");
|
||||
print0(ucase ? "PARTITION BY HASH " : "partition by hash ");
|
||||
}
|
||||
//
|
||||
|
||||
if (x.isKey()) {
|
||||
print0(ucase ? "KEY" : "key");
|
||||
}
|
||||
|
||||
print('(');
|
||||
x.getExpr().accept(this);
|
||||
print(')');
|
||||
|
||||
|
||||
@@ -32,8 +32,12 @@ public class ExportConditions extends UDF {
|
||||
public String evaluate(String sql) {
|
||||
return evaluate(sql, null);
|
||||
}
|
||||
|
||||
|
||||
public String evaluate(String sql, String dbType) {
|
||||
return evaluate(sql, dbType, null);
|
||||
}
|
||||
|
||||
public String evaluate(String sql, String dbType, Boolean compactValues) {
|
||||
try {
|
||||
List<SQLStatement> statementList = SQLUtils.parseStatements(sql, dbType);
|
||||
SchemaStatVisitor visitor = SQLUtils.createSchemaStatVisitor(dbType);
|
||||
@@ -57,7 +61,11 @@ public class ExportConditions extends UDF {
|
||||
if (values.size() == 0) {
|
||||
row.add(null);
|
||||
} else if (values.size() == 1) {
|
||||
row.add(values.get(0));
|
||||
if (compactValues != null && compactValues.booleanValue()) {
|
||||
row.add(values);
|
||||
} else {
|
||||
row.add(values.get(0));
|
||||
}
|
||||
} else {
|
||||
row.add(values);
|
||||
}
|
||||
|
||||
@@ -62,6 +62,9 @@ public interface JdbcConstants {
|
||||
|
||||
public static final String KINGBASE = "kingbase";
|
||||
public static final String KINGBASE_DRIVER = "com.kingbase.Driver";
|
||||
|
||||
public static final String OCEANBASE = "oceanbase";
|
||||
public static final String OCEANBASE_DRIVER = "com.mysql.jdbc.Driver";
|
||||
|
||||
/**
|
||||
* 阿里云odps
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
/*
|
||||
* 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 MySqlCreateTableTest66 extends MysqlTest {
|
||||
|
||||
@Test
|
||||
public void test_one() throws Exception {
|
||||
String sql = "create table hp_db.g20_relationship_communication_daily( "
|
||||
+ " a_iden_string varchar,"
|
||||
+ " b_iden_string varchar,"
|
||||
+ " counter bigint,"
|
||||
+ " durationtime bigint"
|
||||
+ ") "
|
||||
+ "PARTITION BY HASH KEY(a_iden_string) PARTITION NUM 100"
|
||||
+ "SUBPARTITION BY LIST(bdt bigint)"
|
||||
+ "SUBPARTITION OPTIONS(available_Partition_Num=90)"
|
||||
+ "TABLEGROUP g20_test_group;";
|
||||
|
||||
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 hp_db.g20_relationship_communication_daily ("
|
||||
+ "\n\ta_iden_string varchar, "
|
||||
+ "\n\tb_iden_string varchar, "
|
||||
+ "\n\tcounter bigint, "
|
||||
+ "\n\tdurationtime bigint"
|
||||
+ "\n) PARTITION BY HASH KEY(a_iden_string)", output);
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.alibaba.druid.bvt.support.odps.udf;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
import com.alibaba.druid.support.opds.udf.ExportConditions;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
public class ExportConditionsTest5 extends TestCase {
|
||||
ExportConditions udf = new ExportConditions();
|
||||
|
||||
public void test_export_conditions() throws Exception {
|
||||
String result = udf.evaluate("select * from t where name = 'a1' or name = 'a2'");
|
||||
Assert.assertEquals("[[\"t\",\"name\",\"=\",[\"a1\",\"a2\"]]]", result);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.alibaba.druid.bvt.support.odps.udf;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
import com.alibaba.druid.support.opds.udf.ExportConditions;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
public class ExportConditionsTest6 extends TestCase {
|
||||
ExportConditions udf = new ExportConditions();
|
||||
|
||||
public void test_export_conditions() throws Exception {
|
||||
String result = udf.evaluate("select * from t where name = 'a1'", "odbps", true);
|
||||
Assert.assertEquals("[[\"t\",\"name\",\"=\",[\"a1\"]]]", result);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user