Skip to content

Commit

Permalink
* : complete create index syntax support #592
Browse files Browse the repository at this point in the history
[summary]
1. create index syntax.
CREATE [UNIQUE | FULLTEXT | SPATIAL] INDEX index_name
    ON tbl_name (key_part,...)
    [index_option]
    [algorithm_option | lock_option] ...

key_part:
    col_name [(length)]

index_option:
    KEY_BLOCK_SIZE [=] value
  | index_type
  | WITH PARSER NGRAM
  | COMMENT 'string'

index_type:
    USING {BTREE | HASH}

algorithm_option:
    ALGORITHM [=] {DEFAULT | INPLACE | COPY}

lock_option:
    LOCK [=] {DEFAULT | NONE | SHARED | EXCLUSIVE}

2. create table index definition syntax.
index_definition:
    PRIMARY KEY (key_part,...)
  | UNIQUE [index_or_key] index_name (key_part,...) [index_option]
  | index_or_key index_name (key_part,...) [index_option]
  | FULLTEXT index_or_key index_name (key_part,...) [index_option]
  | SPATIAL index_or_key index_name (key_part,...) [index_option]

index_or_key:
    INDEX
  | KEY

[test case]
src/planner/ddl_plan_test.go
src/vendor/github.com/xelabs/go-mysqlstack/sqlparser/ddl_test.go
src/vendor/github.com/xelabs/go-mysqlstack/sqlparser/parse_test.go
[patch codecov]
src/planner/ddl_plan.go 93.8%
src/vendor/github.com/xelabs/go-mysqlstack/sqlparser/ast.go 86.4%
  • Loading branch information
zhyass authored and BohuTANG committed Apr 5, 2020
1 parent f024ec2 commit 28709ba
Show file tree
Hide file tree
Showing 12 changed files with 3,204 additions and 2,473 deletions.
23 changes: 22 additions & 1 deletion docs/radon_sql_support.md
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,28 @@ RadonDB only supports the `CREATE/DROP INDEX` syntax in order to simplify the in

`Syntax`
```
CREATE INDEX index_name ON table_name (index_col_name,...)
CREATE [UNIQUE | FULLTEXT | SPATIAL] INDEX index_name
ON tbl_name (key_part,...)
[index_option]
[algorithm_option | lock_option] ...
key_part:
col_name [(length)]
index_option:
KEY_BLOCK_SIZE [=] value
| index_type
| WITH PARSER NGRAM
| COMMENT 'string'
index_type:
USING {BTREE | HASH}
algorithm_option:
ALGORITHM [=] {DEFAULT | INPLACE | COPY}
lock_option:
LOCK [=] {DEFAULT | NONE | SHARED | EXCLUSIVE}
```

`Instructions`
Expand Down
34 changes: 34 additions & 0 deletions intergration/radon-test/r/ddl.result
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,38 @@ t CREATE TABLE `t` (\n `a` int(11) NOT NULL,\n `b` int(11) NOT NULL,\n `c` ch
drop table integrate_test.t;


create table integrate_test.t(
a int,
b int,
c char(10),
unique key a_idx(a) using btree comment 'unique',
fulltext index c_idx(c) KEY_BLOCK_SIZE=10 WITH PARSER ngram
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

show create table integrate_test.t;
Table Create Table
t CREATE TABLE `t` (\n `a` int(11) DEFAULT NULL,\n `b` int(11) DEFAULT NULL,\n `c` char(10) DEFAULT NULL,\n UNIQUE KEY `a_idx` (`a`) USING BTREE COMMENT 'unique',\n FULLTEXT KEY `c_idx` (`c`) KEY_BLOCK_SIZE=10 /*!50100 WITH PARSER `ngram` */ \n) ENGINE=InnoDB DEFAULT CHARSET=utf8\n/*!50100 PARTITION BY HASH(a) */

drop table integrate_test.t;


create table integrate_test.t(
a int,
b int,
c char(10)
) PARTITION BY HASH(a) ENGINE=InnoDB DEFAULT CHARSET=utf8;

create unique index a_idx on integrate_test.t(a);

create index b_idx on integrate_test.t(b) using btree KEY_BLOCK_SIZE=10 LOCK=DEFAULT ALGORITHM=COPY;

create FULLTEXT index c_idx on integrate_test.t(c) WITH PARSER ngram comment 'fulltext' ALGORITHM=DEFAULT LOCK=SHARED;

show create table integrate_test.t;
Table Create Table
t CREATE TABLE `t` (\n `a` int(11) DEFAULT NULL,\n `b` int(11) DEFAULT NULL,\n `c` char(10) DEFAULT NULL,\n UNIQUE KEY `a_idx` (`a`),\n KEY `b_idx` (`b`) USING BTREE KEY_BLOCK_SIZE=10,\n FULLTEXT KEY `c_idx` (`c`) COMMENT 'fulltext' /*!50100 WITH PARSER `ngram` */ \n) ENGINE=InnoDB DEFAULT CHARSET=utf8\n/*!50100 PARTITION BY HASH(a) */

drop table integrate_test.t;


drop database integrate_test;
21 changes: 21 additions & 0 deletions intergration/radon-test/t/ddl.test
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,25 @@ create table integrate_test.t(
show create table integrate_test.t;
drop table integrate_test.t;

create table integrate_test.t(
a int,
b int,
c char(10),
unique key a_idx(a) using btree comment 'unique',
fulltext index c_idx(c) KEY_BLOCK_SIZE=10 WITH PARSER ngram
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
show create table integrate_test.t;
drop table integrate_test.t;

create table integrate_test.t(
a int,
b int,
c char(10)
) PARTITION BY HASH(a) ENGINE=InnoDB DEFAULT CHARSET=utf8;
create unique index a_idx on integrate_test.t(a);
create index b_idx on integrate_test.t(b) using btree KEY_BLOCK_SIZE=10 LOCK=DEFAULT ALGORITHM=COPY;
create FULLTEXT index c_idx on integrate_test.t(c) WITH PARSER ngram comment 'fulltext' ALGORITHM=DEFAULT LOCK=SHARED;
show create table integrate_test.t;
drop table integrate_test.t;

drop database integrate_test;
3 changes: 1 addition & 2 deletions src/planner/ddl_plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,7 @@ func (p *DDLPlan) checkUnsupportedOperations(database, table string) error {
}
// constraint check in index definition
for _, index := range node.TableSpec.Indexes {
info := index.Info
if info.Unique || info.Primary {
if index.Unique || index.Primary {
err := fmt.Sprintf("The unique/primary constraint should be only defined on the sharding key column[%s]", shardKey)
return errors.New(err)
}
Expand Down
116 changes: 112 additions & 4 deletions src/planner/ddl_plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func TestDDLPlan1(t *testing.T) {
"{\n\t\"RawQuery\": \"create table G(a int)\",\n\t\"Partitions\": [\n\t\t{\n\t\t\t\"Query\": \"create table sbtest.G (\\n\\t`a` int\\n)\",\n\t\t\t\"Backend\": \"backend1\",\n\t\t\t\"Range\": \"\"\n\t\t},\n\t\t{\n\t\t\t\"Query\": \"create table sbtest.G (\\n\\t`a` int\\n)\",\n\t\t\t\"Backend\": \"backend2\",\n\t\t\t\"Range\": \"\"\n\t\t}\n\t]\n}",
"{\n\t\"RawQuery\": \"create table S(a int)\",\n\t\"Partitions\": [\n\t\t{\n\t\t\t\"Query\": \"create table sbtest.S (\\n\\t`a` int\\n)\",\n\t\t\t\"Backend\": \"backend1\",\n\t\t\t\"Range\": \"\"\n\t\t}\n\t]\n}",
"{\n\t\"RawQuery\": \"alter table A engine = tokudb\",\n\t\"Partitions\": [\n\t\t{\n\t\t\t\"Query\": \"alter table sbtest.A0 engine = tokudb\",\n\t\t\t\"Backend\": \"backend0\",\n\t\t\t\"Range\": \"[0-2)\"\n\t\t},\n\t\t{\n\t\t\t\"Query\": \"alter table sbtest.A2 engine = tokudb\",\n\t\t\t\"Backend\": \"backend2\",\n\t\t\t\"Range\": \"[2-4)\"\n\t\t},\n\t\t{\n\t\t\t\"Query\": \"alter table sbtest.A4 engine = tokudb\",\n\t\t\t\"Backend\": \"backend4\",\n\t\t\t\"Range\": \"[4-8)\"\n\t\t},\n\t\t{\n\t\t\t\"Query\": \"alter table sbtest.A8 engine = tokudb\",\n\t\t\t\"Backend\": \"backend8\",\n\t\t\t\"Range\": \"[8-4096)\"\n\t\t}\n\t]\n}",
"{\n\t\"RawQuery\": \"create index idx_a on A(a)\",\n\t\"Partitions\": [\n\t\t{\n\t\t\t\"Query\": \"create index idx_a on sbtest.A0\",\n\t\t\t\"Backend\": \"backend0\",\n\t\t\t\"Range\": \"[0-2)\"\n\t\t},\n\t\t{\n\t\t\t\"Query\": \"create index idx_a on sbtest.A2\",\n\t\t\t\"Backend\": \"backend2\",\n\t\t\t\"Range\": \"[2-4)\"\n\t\t},\n\t\t{\n\t\t\t\"Query\": \"create index idx_a on sbtest.A4\",\n\t\t\t\"Backend\": \"backend4\",\n\t\t\t\"Range\": \"[4-8)\"\n\t\t},\n\t\t{\n\t\t\t\"Query\": \"create index idx_a on sbtest.A8\",\n\t\t\t\"Backend\": \"backend8\",\n\t\t\t\"Range\": \"[8-4096)\"\n\t\t}\n\t]\n}",
"{\n\t\"RawQuery\": \"create index idx_a on A(a)\",\n\t\"Partitions\": [\n\t\t{\n\t\t\t\"Query\": \"create index idx_a on sbtest.A0(`a`)\",\n\t\t\t\"Backend\": \"backend0\",\n\t\t\t\"Range\": \"[0-2)\"\n\t\t},\n\t\t{\n\t\t\t\"Query\": \"create index idx_a on sbtest.A2(`a`)\",\n\t\t\t\"Backend\": \"backend2\",\n\t\t\t\"Range\": \"[2-4)\"\n\t\t},\n\t\t{\n\t\t\t\"Query\": \"create index idx_a on sbtest.A4(`a`)\",\n\t\t\t\"Backend\": \"backend4\",\n\t\t\t\"Range\": \"[4-8)\"\n\t\t},\n\t\t{\n\t\t\t\"Query\": \"create index idx_a on sbtest.A8(`a`)\",\n\t\t\t\"Backend\": \"backend8\",\n\t\t\t\"Range\": \"[8-4096)\"\n\t\t}\n\t]\n}",
"{\n\t\"RawQuery\": \"drop index idx_a on sbtest.A\",\n\t\"Partitions\": [\n\t\t{\n\t\t\t\"Query\": \"drop index idx_a on sbtest.A0\",\n\t\t\t\"Backend\": \"backend0\",\n\t\t\t\"Range\": \"[0-2)\"\n\t\t},\n\t\t{\n\t\t\t\"Query\": \"drop index idx_a on sbtest.A2\",\n\t\t\t\"Backend\": \"backend2\",\n\t\t\t\"Range\": \"[2-4)\"\n\t\t},\n\t\t{\n\t\t\t\"Query\": \"drop index idx_a on sbtest.A4\",\n\t\t\t\"Backend\": \"backend4\",\n\t\t\t\"Range\": \"[4-8)\"\n\t\t},\n\t\t{\n\t\t\t\"Query\": \"drop index idx_a on sbtest.A8\",\n\t\t\t\"Backend\": \"backend8\",\n\t\t\t\"Range\": \"[8-4096)\"\n\t\t}\n\t]\n}",
"{\n\t\"RawQuery\": \"alter table A add column(b int)\",\n\t\"Partitions\": [\n\t\t{\n\t\t\t\"Query\": \"alter table sbtest.A0 add column (\\n\\t`b` int\\n)\",\n\t\t\t\"Backend\": \"backend0\",\n\t\t\t\"Range\": \"[0-2)\"\n\t\t},\n\t\t{\n\t\t\t\"Query\": \"alter table sbtest.A2 add column (\\n\\t`b` int\\n)\",\n\t\t\t\"Backend\": \"backend2\",\n\t\t\t\"Range\": \"[2-4)\"\n\t\t},\n\t\t{\n\t\t\t\"Query\": \"alter table sbtest.A4 add column (\\n\\t`b` int\\n)\",\n\t\t\t\"Backend\": \"backend4\",\n\t\t\t\"Range\": \"[4-8)\"\n\t\t},\n\t\t{\n\t\t\t\"Query\": \"alter table sbtest.A8 add column (\\n\\t`b` int\\n)\",\n\t\t\t\"Backend\": \"backend8\",\n\t\t\t\"Range\": \"[8-4096)\"\n\t\t}\n\t]\n}",
"{\n\t\"RawQuery\": \"alter table sbtest.A add column(b int)\",\n\t\"Partitions\": [\n\t\t{\n\t\t\t\"Query\": \"alter table sbtest.A0 add column (\\n\\t`b` int\\n)\",\n\t\t\t\"Backend\": \"backend0\",\n\t\t\t\"Range\": \"[0-2)\"\n\t\t},\n\t\t{\n\t\t\t\"Query\": \"alter table sbtest.A2 add column (\\n\\t`b` int\\n)\",\n\t\t\t\"Backend\": \"backend2\",\n\t\t\t\"Range\": \"[2-4)\"\n\t\t},\n\t\t{\n\t\t\t\"Query\": \"alter table sbtest.A4 add column (\\n\\t`b` int\\n)\",\n\t\t\t\"Backend\": \"backend4\",\n\t\t\t\"Range\": \"[4-8)\"\n\t\t},\n\t\t{\n\t\t\t\"Query\": \"alter table sbtest.A8 add column (\\n\\t`b` int\\n)\",\n\t\t\t\"Backend\": \"backend8\",\n\t\t\t\"Range\": \"[8-4096)\"\n\t\t}\n\t]\n}",
Expand Down Expand Up @@ -202,13 +202,117 @@ func TestDDLAlterError(t *testing.T) {
}
}

func TestDDLPlanCreateIndexWithTableNameIssue10(t *testing.T) {
func TestDDLPlanCreateIndex(t *testing.T) {
results := []string{
"{\n\t\"RawQuery\": \"create index idx_A_id on A(a)\",\n\t\"Partitions\": [\n\t\t{\n\t\t\t\"Query\": \"create index idx_A_id on sbtest.A0\",\n\t\t\t\"Backend\": \"backend0\",\n\t\t\t\"Range\": \"[0-2)\"\n\t\t},\n\t\t{\n\t\t\t\"Query\": \"create index idx_A_id on sbtest.A2\",\n\t\t\t\"Backend\": \"backend2\",\n\t\t\t\"Range\": \"[2-4)\"\n\t\t},\n\t\t{\n\t\t\t\"Query\": \"create index idx_A_id on sbtest.A4\",\n\t\t\t\"Backend\": \"backend4\",\n\t\t\t\"Range\": \"[4-8)\"\n\t\t},\n\t\t{\n\t\t\t\"Query\": \"create index idx_A_id on sbtest.A8\",\n\t\t\t\"Backend\": \"backend8\",\n\t\t\t\"Range\": \"[8-4096)\"\n\t\t}\n\t]\n}",
`{
"RawQuery": "create index idx_A_id on A(a)",
"Partitions": [
{
"Query": "create index idx_A_id on sbtest.A0(` + "`a`" + `)",
"Backend": "backend0",
"Range": "[0-2)"
},
{
"Query": "create index idx_A_id on sbtest.A2(` + "`a`" + `)",
"Backend": "backend2",
"Range": "[2-4)"
},
{
"Query": "create index idx_A_id on sbtest.A4(` + "`a`" + `)",
"Backend": "backend4",
"Range": "[4-8)"
},
{
"Query": "create index idx_A_id on sbtest.A8(` + "`a`" + `)",
"Backend": "backend8",
"Range": "[8-4096)"
}
]
}`,
`{
"RawQuery": "create fulltext index idx on A(a) with parser ngram lock=none algorithm=copy",
"Partitions": [
{
"Query": "create fulltext index idx on sbtest.A0(` + "`a`" + `) WITH PARSER ngram algorithm = copy lock = none",
"Backend": "backend0",
"Range": "[0-2)"
},
{
"Query": "create fulltext index idx on sbtest.A2(` + "`a`" + `) WITH PARSER ngram algorithm = copy lock = none",
"Backend": "backend2",
"Range": "[2-4)"
},
{
"Query": "create fulltext index idx on sbtest.A4(` + "`a`" + `) WITH PARSER ngram algorithm = copy lock = none",
"Backend": "backend4",
"Range": "[4-8)"
},
{
"Query": "create fulltext index idx on sbtest.A8(` + "`a`" + `) WITH PARSER ngram algorithm = copy lock = none",
"Backend": "backend8",
"Range": "[8-4096)"
}
]
}`,
`{
"RawQuery": "create index idx on A(a) using hash comment 'c' lock=shared",
"Partitions": [
{
"Query": "create index idx on sbtest.A0(` + "`a`" + `) using hash comment 'c' lock = shared",
"Backend": "backend0",
"Range": "[0-2)"
},
{
"Query": "create index idx on sbtest.A2(` + "`a`" + `) using hash comment 'c' lock = shared",
"Backend": "backend2",
"Range": "[2-4)"
},
{
"Query": "create index idx on sbtest.A4(` + "`a`" + `) using hash comment 'c' lock = shared",
"Backend": "backend4",
"Range": "[4-8)"
},
{
"Query": "create index idx on sbtest.A8(` + "`a`" + `) using hash comment 'c' lock = shared",
"Backend": "backend8",
"Range": "[8-4096)"
}
]
}`,
`{
"RawQuery": "create spatial index idx on A(gis) key_block_size=10 algorithm=default",
"Partitions": [
{
"Query": "create spatial index idx on sbtest.A0(` + "`gis`" + `) key_block_size = 10 algorithm = default",
"Backend": "backend0",
"Range": "[0-2)"
},
{
"Query": "create spatial index idx on sbtest.A2(` + "`gis`" + `) key_block_size = 10 algorithm = default",
"Backend": "backend2",
"Range": "[2-4)"
},
{
"Query": "create spatial index idx on sbtest.A4(` + "`gis`" + `) key_block_size = 10 algorithm = default",
"Backend": "backend4",
"Range": "[4-8)"
},
{
"Query": "create spatial index idx on sbtest.A8(` + "`gis`" + `) key_block_size = 10 algorithm = default",
"Backend": "backend8",
"Range": "[8-4096)"
}
]
}`,
}

querys := []string{
// issue 10.
"create index idx_A_id on A(a)",
// issue 592.
"create fulltext index idx on A(a) with parser ngram lock=none algorithm=copy",
"create index idx on A(a) using hash comment 'c' lock=shared",
"create spatial index idx on A(gis) key_block_size=10 algorithm=default",
}

log := xlog.NewStdLog(xlog.Level(xlog.PANIC))
Expand Down Expand Up @@ -252,6 +356,8 @@ func TestDDLPlanWithQuote(t *testing.T) {
"{\n\t\"RawQuery\": \"create table sbtest.A (\\n\\t`a` int\\n)\",\n\t\"Partitions\": [\n\t\t{\n\t\t\t\"Query\": \"create table sbtest.A0 (\\n\\t`a` int\\n)\",\n\t\t\t\"Backend\": \"backend0\",\n\t\t\t\"Range\": \"[0-2)\"\n\t\t},\n\t\t{\n\t\t\t\"Query\": \"create table sbtest.A2 (\\n\\t`a` int\\n)\",\n\t\t\t\"Backend\": \"backend2\",\n\t\t\t\"Range\": \"[2-4)\"\n\t\t},\n\t\t{\n\t\t\t\"Query\": \"create table sbtest.A4 (\\n\\t`a` int\\n)\",\n\t\t\t\"Backend\": \"backend4\",\n\t\t\t\"Range\": \"[4-8)\"\n\t\t},\n\t\t{\n\t\t\t\"Query\": \"create table sbtest.A8 (\\n\\t`a` int\\n)\",\n\t\t\t\"Backend\": \"backend8\",\n\t\t\t\"Range\": \"[8-4096)\"\n\t\t}\n\t]\n}",
"{\n\t\"RawQuery\": \"create table sbtest.A (\\n\\t`a` int\\n)\",\n\t\"Partitions\": [\n\t\t{\n\t\t\t\"Query\": \"create table sbtest.A0 (\\n\\t`a` int\\n)\",\n\t\t\t\"Backend\": \"backend0\",\n\t\t\t\"Range\": \"[0-2)\"\n\t\t},\n\t\t{\n\t\t\t\"Query\": \"create table sbtest.A2 (\\n\\t`a` int\\n)\",\n\t\t\t\"Backend\": \"backend2\",\n\t\t\t\"Range\": \"[2-4)\"\n\t\t},\n\t\t{\n\t\t\t\"Query\": \"create table sbtest.A4 (\\n\\t`a` int\\n)\",\n\t\t\t\"Backend\": \"backend4\",\n\t\t\t\"Range\": \"[4-8)\"\n\t\t},\n\t\t{\n\t\t\t\"Query\": \"create table sbtest.A8 (\\n\\t`a` int\\n)\",\n\t\t\t\"Backend\": \"backend8\",\n\t\t\t\"Range\": \"[8-4096)\"\n\t\t}\n\t]\n}",
"{\n\t\"RawQuery\": \"create table sbtest.A (\\n\\t`a` int\\n)\",\n\t\"Partitions\": [\n\t\t{\n\t\t\t\"Query\": \"create table sbtest.A0 (\\n\\t`a` int\\n)\",\n\t\t\t\"Backend\": \"backend0\",\n\t\t\t\"Range\": \"[0-2)\"\n\t\t},\n\t\t{\n\t\t\t\"Query\": \"create table sbtest.A2 (\\n\\t`a` int\\n)\",\n\t\t\t\"Backend\": \"backend2\",\n\t\t\t\"Range\": \"[2-4)\"\n\t\t},\n\t\t{\n\t\t\t\"Query\": \"create table sbtest.A4 (\\n\\t`a` int\\n)\",\n\t\t\t\"Backend\": \"backend4\",\n\t\t\t\"Range\": \"[4-8)\"\n\t\t},\n\t\t{\n\t\t\t\"Query\": \"create table sbtest.A8 (\\n\\t`a` int\\n)\",\n\t\t\t\"Backend\": \"backend8\",\n\t\t\t\"Range\": \"[8-4096)\"\n\t\t}\n\t]\n}",
"{\n\t\"RawQuery\": \"create table B (\\n\\t`a` int,\\n\\t`b` varchar(10),\\n\\tunique key `a_idx` (`a`) using btree comment 'key'\\n)\",\n\t\"Partitions\": [\n\t\t{\n\t\t\t\"Query\": \"create table sbtest.B0 (\\n\\t`a` int,\\n\\t`b` varchar(10),\\n\\tunique key `a_idx` (`a`) using btree comment 'key'\\n)\",\n\t\t\t\"Backend\": \"backend1\",\n\t\t\t\"Range\": \"[0-512)\"\n\t\t},\n\t\t{\n\t\t\t\"Query\": \"create table sbtest.B1 (\\n\\t`a` int,\\n\\t`b` varchar(10),\\n\\tunique key `a_idx` (`a`) using btree comment 'key'\\n)\",\n\t\t\t\"Backend\": \"backend2\",\n\t\t\t\"Range\": \"[512-4096)\"\n\t\t}\n\t]\n}",
"{\n\t\"RawQuery\": \"create table B (\\n\\t`b` varchar(10),\\n\\tfulltext index `b_idx` (`b`) key_block_size = 10 WITH PARSER ngram\\n)\",\n\t\"Partitions\": [\n\t\t{\n\t\t\t\"Query\": \"create table sbtest.B0 (\\n\\t`b` varchar(10),\\n\\tfulltext index `b_idx` (`b`) key_block_size = 10 WITH PARSER ngram\\n)\",\n\t\t\t\"Backend\": \"backend1\",\n\t\t\t\"Range\": \"[0-512)\"\n\t\t},\n\t\t{\n\t\t\t\"Query\": \"create table sbtest.B1 (\\n\\t`b` varchar(10),\\n\\tfulltext index `b_idx` (`b`) key_block_size = 10 WITH PARSER ngram\\n)\",\n\t\t\t\"Backend\": \"backend2\",\n\t\t\t\"Range\": \"[512-4096)\"\n\t\t}\n\t]\n}",
}

querys := []string{
Expand All @@ -262,6 +368,8 @@ func TestDDLPlanWithQuote(t *testing.T) {
"create table sbtest.`A`(a int)",
"create table `sbtest`.A(a int)",
"create table `sbtest`.`A`(a int)",
"create table B(a int, b varchar(10), unique key a_idx(a) using btree comment 'key')",
"create table B(b varchar(10), fulltext index b_idx(b) with parser ngram key_block_size=10)",
}

log := xlog.NewStdLog(xlog.Level(xlog.PANIC))
Expand All @@ -270,7 +378,7 @@ func TestDDLPlanWithQuote(t *testing.T) {
route, cleanup := router.MockNewRouter(log)
defer cleanup()

err := route.AddForTest(database, router.MockTableAConfig())
err := route.AddForTest(database, router.MockTableAConfig(), router.MockTableBConfig())
assert.Nil(t, err)
for i, query := range querys {
log.Debug("%v", query)
Expand Down
12 changes: 5 additions & 7 deletions src/proxy/ddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,8 @@ func tryGetShardKey(ddl *sqlparser.DDL) (string, error) {
// 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 {
if index.Unique || index.Primary {
for _, colIdx := range index.Opts.Columns {
colName := colIdx.Column.String()
if colName == shardKey {
constraintCheckOK = true
Expand All @@ -103,10 +102,9 @@ func tryGetShardKey(ddl *sqlparser.DDL) (string, error) {
}
// constraint check in index definition.
for _, index := range ddl.TableSpec.Indexes {
info := index.Info
if info.Unique || info.Primary {
if len(index.Columns) == 1 {
return index.Columns[0].Column.String(), nil
if index.Unique || index.Primary {
if len(index.Opts.Columns) == 1 {
return index.Opts.Columns[0].Column.String(), nil
}
}
}
Expand Down
Loading

0 comments on commit 28709ba

Please sign in to comment.