Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public class MySqlCreateTableStatement extends SQLCreateTableStatement implement
// for ads
protected SQLName distributeByType;
protected List<SQLName> distributeBy = new ArrayList<SQLName>();
protected List<SQLName> distributeByHashGroup = new ArrayList<SQLName>();
protected boolean isBroadCast;
// for ads
protected Map<String, SQLName> with = new HashMap<String, SQLName>(3);
Expand Down Expand Up @@ -567,6 +568,10 @@ public List<SQLName> getDistributeBy() {
return distributeBy;
}

public List<SQLName> getDistributeByHashGroup() {
return distributeByHashGroup;
}

public SQLExpr getTbpartitions() {
return tablePartitions;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -864,6 +864,21 @@ protected void parseOptions(MySqlCreateTableStatement stmt) {
}
accept(Token.RPAREN);
stmt.setDistributeByType(new SQLIdentifierExpr("HASH"));
// syntax: hash(col1,col2)(g1,g2,g3)
if (lexer.token() == Token.LPAREN) {
accept(Token.LPAREN);
Comment thread
mswdwk marked this conversation as resolved.
Outdated
for (; ; ) {
SQLName name = this.exprParser.name();
stmt.getDistributeByHashGroup().add(name);
if (lexer.token() == Token.COMMA) {
lexer.nextToken();
continue;
}
break;
}
accept(Token.RPAREN);
Comment thread
mswdwk marked this conversation as resolved.
}

} else if (lexer.identifierEquals(FnvHash.Constants.DUPLICATE)) {
lexer.nextToken();
accept(Token.LPAREN);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10505,6 +10505,11 @@ public boolean visit(MySqlCreateTableStatement x) {
print0(ucase ? "HASH(" : "hash(");
printAndAccept(x.getDistributeBy(), ",");
print0(")");
if (!x.getDistributeByHashGroup().isEmpty()) {
print0("(");
printAndAccept(x.getDistributeByHashGroup(), ",");
print0(")");
}

} else if ("DUPLICATE".equalsIgnoreCase(distributeByType.getSimpleName())) {
print0(ucase ? "DUPLICATE(" : "duplicate(");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.alibaba.druid.bvt.sql.mysql.createTable;

import com.alibaba.druid.DbType;
import com.alibaba.druid.sql.MysqlTest;
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlCreateTableStatement;
import com.alibaba.druid.sql.visitor.VisitorFeature;


public class MySqlCreateTableTest163_distributed_by_hash_group extends MysqlTest {
public void test_0() throws Exception {
String sql = "CREATE TABLE `test_user` ( `id` int(11) NOT NULL AUTO_INCREMENT ) DISTRIBUTE BY HASH(id,name);";

MySqlCreateTableStatement stmt = (MySqlCreateTableStatement) SQLUtils.parseSingleMysqlStatement(sql);

assertEquals(sql, SQLUtils.toSQLString(stmt, DbType.mysql, new SQLUtils.FormatOption(VisitorFeature.OutputUCase)));
}

public void test_1() throws Exception {
String sql = "CREATE TABLE `test_user` ( `id` int(11) NOT NULL AUTO_INCREMENT ) DISTRIBUTED BY HASH(col1,col2)(g1,g2,g3);";

MySqlCreateTableStatement stmt = (MySqlCreateTableStatement) SQLUtils.parseSingleMysqlStatement(sql);

assertEquals(sql, SQLUtils.toSQLString(stmt, DbType.mysql, new SQLUtils.FormatOption(
VisitorFeature.OutputDistributedLiteralInCreateTableStmt, VisitorFeature.OutputUCase)));
}

}