Skip to content

Commit

Permalink
proxy:fix bug for partition key may be not same with primary/unique key
Browse files Browse the repository at this point in the history
  • Loading branch information
hustjieke authored and BohuTANG committed Dec 3, 2018
1 parent 1f7f6e9 commit 86cfc1e
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 10 deletions.
28 changes: 26 additions & 2 deletions src/proxy/ddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,47 @@ func CheckCreateTable(ddl *sqlparser.DDL) error {
return fmt.Errorf("spanner.ddl.check.create.table[%s].error:not support", table)
}

// UNIQUE/PRIMARY constraint check.
// shard key and UNIQUE/PRIMARY KEY constraint check.
if shardKey != "" {
shardKeyOK := false
constraintCheckOK := true
// shardKey check and constraint check in column definition
for _, col := range ddl.TableSpec.Columns {
colName := col.Name.String()
if colName == shardKey {
shardKeyOK = true
} else {
switch col.Type.KeyOpt {
case sqlparser.ColKeyUnique, sqlparser.ColKeyUniqueKey, sqlparser.ColKeyPrimary:
return fmt.Errorf("The unique/primary constraint only be defined on the sharding key column[%s] not [%s]", shardKey, colName)
constraintCheckOK = false
}
}
}

if !shardKeyOK {
return fmt.Errorf("Sharding Key column '%s' doesn't exist in table", shardKey)
}
if !constraintCheckOK {
return fmt.Errorf("The unique/primary constraint should be only defined on the sharding key column[%s]", shardKey)
}

// constraint check in index definition
for _, index := range ddl.TableSpec.Indexes {
constraintCheckOK = false
info := index.Info
if info.Unique || info.Primary {
for _, colIdx := range index.Columns {
colName := colIdx.Column.String()
if colName == shardKey {
constraintCheckOK = true
break
}
}
if !constraintCheckOK {
return fmt.Errorf("The unique/primary constraint should be only defined on the sharding key column[%s]", shardKey)
}
}
}
}

check := false
Expand Down
81 changes: 73 additions & 8 deletions src/proxy/ddl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -540,17 +540,82 @@ func TestProxyDDLConstraint(t *testing.T) {
}

querys := []string{
"CREATE TABLE t1(a int primary key,b int ) PARTITION BY HASH(a);",
"CREATE TABLE t2(a int unique,b int ) PARTITION BY HASH(a);",
"CREATE TABLE t2(a int ,b int primary key) PARTITION BY HASH(a);",
"CREATE TABLE t3(a int primary key,b int unique) PARTITION BY HASH(a);",
"CREATE TABLE t0(a int unique,b int ) PARTITION BY HASH(a);",
"create table t1(a int key, b int) partition by hash(a)",
"create table t2(a int key, b int key) partition by hash(a)",
"create table t3(a int unique, b int, c int) PARTITION BY hash(a)",
"create table t4(a int unique key, b int) PARTITION BY hash(a) ",
"create table t5(a int primary key, b int) partition by hash(a)",
"create table t6(a int unique, b int key) PARTITION BY hash(a)",
"create table t7(a int unique key, b int key) PARTITION BY hash(a) ",
"create table t8(a int primary key, b int key) partition by hash(a)",
"create table t9(a int, b int, primary key(a))engine=tokudb PARTITION BY hash(a) ",
"create table t10(a int key, b int, primary key(a))engine=tokudb PARTITION BY hash(a) ",
"create table t11(a int key, b int key, primary key(a))engine=tokudb PARTITION BY hash(a) ",
"create table t12(a int, b int, primary key(a,b)) default charset=utf8 PARTITION BY hash(a) ",
"create table t13(a int key, b int, primary key(a,b)) default charset=utf8 PARTITION BY hash(a) ",
"create table t14(a int key, b int key, primary key(a,b)) default charset=utf8 PARTITION BY hash(a) ",
"create table t15(a int unique, b int, primary key(a,b))engine=tokudb default charset=utf8 PARTITION BY hash(a) ",
"create table t16(a int unique key, b int key, primary key(a,b))engine=tokudb default charset=utf8 PARTITION BY hash(a) ",
"create table t17(a int unique, b int key, primary key(a))engine=tokudb default charset=utf8 PARTITION BY hash(a) ",
"create table t18(a int unique, b int key, key `name` (`a`))engine=tokudb default charset=utf8 PARTITION BY hash(a) ",
"create table t19(a int unique, b int key, index `name` (a))engine=tokudb default charset=utf8 PARTITION BY hash(a) ",
"create table t20(a int unique, b int key, unique index `name` (a))engine=tokudb default charset=utf8 PARTITION BY hash(a) ",
"create table t21(a int unique, b int key, unique key `name` (a))engine=tokudb default charset=utf8 PARTITION BY hash(a) ",
}

for _, query := range querys {
client, err := driver.NewConn("mock", "mock", address, "test", "utf8")
assert.Nil(t, err)
_, err = client.FetchAll(query, -1)
assert.Nil(t, err)
}
}

func TestProxyDDLConstraintError(t *testing.T) {
log := xlog.NewStdLog(xlog.Level(xlog.PANIC))
fakedbs, proxy, cleanup := MockProxy(log)
defer cleanup()
address := proxy.Address()

// fakedbs.
{
fakedbs.AddQueryPattern("use .*", &sqltypes.Result{})
fakedbs.AddQueryPattern("create table .*", &sqltypes.Result{})
}

querys := []string{
"create table t1(a int unique index, b int unique) partition by hash(a)",
"create table t2(a int, b int unique) partition by hash(a)",
"create table t3(a int unique, b int unique) partition by hash(a)",
"create table t4(a int, b int primary key) PARTITION BY hash(a)",
"create table t5(a int unique key, b int primary key) PARTITION BY hash(a) ",
"create table t6(a int primary key, b int primary key) partition by hash(a)",
"create table t7(a int, b int unique, primary key(a))engine=tokudb PARTITION BY hash(a) ",
"create table t8(a int, b int unique key, primary key(a))engine=tokudb PARTITION BY hash(a) ",
"create table t9(a int unique key, b int unique key, primary key(a))engine=tokudb PARTITION BY hash(a)",
"create table t10(a int unique, b int unique, c int unique, primary key(a,b)) default charset=utf8 PARTITION BY hash(a) ",
"create table t11(a int unique, b int, c int, primary key(b)) default charset=utf8 PARTITION BY hash(a) ",
"create table t12(a int unique, b int, c int, primary key(b, c)) default charset=utf8 PARTITION BY hash(a) ",
"create table t13(a int unique, b int, c int, unique key `name` (`b`)) default charset=utf8 PARTITION BY hash(a) ",
"create table t14(a int unique, b int, c int, unique key `name` (`b`, `c`)) default charset=utf8 PARTITION BY hash(a) ",
}

results := []string{
"",
"",
"The unique/primary constraint only be defined on the sharding key column[a] not [b] (errno 1105) (sqlstate HY000)",
"The unique/primary constraint only be defined on the sharding key column[a] not [b] (errno 1105) (sqlstate HY000)",
"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use, syntax error at position 35 near 'index' (errno 1149) (sqlstate 42000)",
"The unique/primary constraint should be only defined on the sharding key column[a] (errno 1105) (sqlstate HY000)",
"The unique/primary constraint should be only defined on the sharding key column[a] (errno 1105) (sqlstate HY000)",
"The unique/primary constraint should be only defined on the sharding key column[a] (errno 1105) (sqlstate HY000)",
"The unique/primary constraint should be only defined on the sharding key column[a] (errno 1105) (sqlstate HY000)",
"The unique/primary constraint should be only defined on the sharding key column[a] (errno 1105) (sqlstate HY000)",
"The unique/primary constraint should be only defined on the sharding key column[a] (errno 1105) (sqlstate HY000)",
"The unique/primary constraint should be only defined on the sharding key column[a] (errno 1105) (sqlstate HY000)",
"The unique/primary constraint should be only defined on the sharding key column[a] (errno 1105) (sqlstate HY000)",
"The unique/primary constraint should be only defined on the sharding key column[a] (errno 1105) (sqlstate HY000)",
"The unique/primary constraint should be only defined on the sharding key column[a] (errno 1105) (sqlstate HY000)",
"The unique/primary constraint should be only defined on the sharding key column[a] (errno 1105) (sqlstate HY000)",
"The unique/primary constraint should be only defined on the sharding key column[a] (errno 1105) (sqlstate HY000)",
"The unique/primary constraint should be only defined on the sharding key column[a] (errno 1105) (sqlstate HY000)",
}

for i, query := range querys {
Expand Down

0 comments on commit 86cfc1e

Please sign in to comment.