Skip to content

Commit

Permalink
Merge pull request vitessio#4412 from tinyspeck/vschema-alter-syntax
Browse files Browse the repository at this point in the history
vschema alter syntax
  • Loading branch information
sougou authored Dec 8, 2018
2 parents 2a95023 + b55d155 commit eccf912
Show file tree
Hide file tree
Showing 9 changed files with 3,219 additions and 3,025 deletions.
33 changes: 21 additions & 12 deletions go/vt/sqlparser/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -746,15 +746,18 @@ type DDL struct {

// DDL strings.
const (
CreateStr = "create"
AlterStr = "alter"
DropStr = "drop"
RenameStr = "rename"
TruncateStr = "truncate"
FlushStr = "flush"
CreateVindexStr = "create vindex"
AddColVindexStr = "add vindex"
DropColVindexStr = "drop vindex"
CreateStr = "create"
AlterStr = "alter"
DropStr = "drop"
RenameStr = "rename"
TruncateStr = "truncate"
FlushStr = "flush"
CreateVindexStr = "create vindex"
DropVindexStr = "drop vindex"
AddVschemaTableStr = "add vschema table"
DropVschemaTableStr = "drop vschema table"
AddColVindexStr = "on table add vindex"
DropColVindexStr = "on table drop vindex"

// Vindex DDL param to specify the owner of a vindex
VindexOwnerStr = "owner"
Expand Down Expand Up @@ -791,9 +794,15 @@ func (node *DDL) Format(buf *TrackedBuffer) {
case FlushStr:
buf.Myprintf("%s", node.Action)
case CreateVindexStr:
buf.Myprintf("%s %v %v", node.Action, node.VindexSpec.Name, node.VindexSpec)
buf.Myprintf("alter vschema create vindex %v %v", node.VindexSpec.Name, node.VindexSpec)
case DropVindexStr:
buf.Myprintf("alter vschema drop vindex %v", node.VindexSpec.Name)
case AddVschemaTableStr:
buf.Myprintf("alter vschema add table %v", node.Table)
case DropVschemaTableStr:
buf.Myprintf("alter vschema drop table %v", node.Table)
case AddColVindexStr:
buf.Myprintf("alter table %v %s %v (", node.Table, node.Action, node.VindexSpec.Name)
buf.Myprintf("alter vschema on %v add vindex %v (", node.Table, node.VindexSpec.Name)
for i, col := range node.VindexCols {
if i != 0 {
buf.Myprintf(", %v", col)
Expand All @@ -806,7 +815,7 @@ func (node *DDL) Format(buf *TrackedBuffer) {
buf.Myprintf(" %v", node.VindexSpec)
}
case DropColVindexStr:
buf.Myprintf("alter table %v %s %v", node.Table, node.Action, node.VindexSpec.Name)
buf.Myprintf("alter vschema on %v drop vindex %v", node.Table, node.VindexSpec.Name)
default:
buf.Myprintf("%s table %v", node.Action, node.Table)
}
Expand Down
82 changes: 43 additions & 39 deletions go/vt/sqlparser/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -903,58 +903,64 @@ var (
input: "alter table a drop id",
output: "alter table a",
}, {
input: "alter table a add vindex hash (id)",
input: "create table a",
}, {
input: "alter table a add vindex `hash` (`id`)",
output: "alter table a add vindex hash (id)",
input: "create table a (\n\t`a` int\n)",
output: "create table a (\n\ta int\n)",
}, {
input: "alter table a add vindex hash (id) using `hash`",
output: "alter table a add vindex hash (id) using hash",
input: "create table `by` (\n\t`by` char\n)",
}, {
input: "alter table a add vindex `add` (`add`)",
input: "create table if not exists a (\n\t`a` int\n)",
output: "create table a (\n\ta int\n)",
}, {
input: "alter table a add vindex hash (id) using hash",
input: "create table a ignore me this is garbage",
output: "create table a",
}, {
input: "alter table a add vindex hash (id) using `hash`",
output: "alter table a add vindex hash (id) using hash",
input: "create table a (a int, b char, c garbage)",
output: "create table a",
}, {
input: "alter table user add vindex name_lookup_vdx (name) using lookup_hash with owner=user, table=name_user_idx, from=name, to=user_id",
input: "alter vschema create vindex hash_vdx using hash",
}, {
input: "alter table user2 add vindex name_lastname_lookup_vdx (name,lastname) using lookup with owner=`user`, table=`name_lastname_keyspace_id_map`, from=`name,lastname`, to=`keyspace_id`",
output: "alter table user2 add vindex name_lastname_lookup_vdx (name, lastname) using lookup with owner=user, table=name_lastname_keyspace_id_map, from=name,lastname, to=keyspace_id",
input: "alter vschema create vindex lookup_vdx using lookup with owner=user, table=name_user_idx, from=name, to=user_id",
}, {
input: "alter table a drop vindex hash",
input: "alter vschema create vindex xyz_vdx using xyz with param1=hello, param2='world', param3=123",
}, {
input: "alter table a drop vindex `hash`",
output: "alter table a drop vindex hash",
input: "alter vschema drop vindex hash_vdx",
}, {
input: "alter table a drop vindex hash",
output: "alter table a drop vindex hash",
input: "alter vschema add table a",
}, {
input: "alter table a drop vindex `add`",
output: "alter table a drop vindex `add`",
input: "alter vschema drop table a",
}, {
input: "create table a",
input: "alter vschema on a add vindex hash (id)",
}, {
input: "create table a (\n\t`a` int\n)",
output: "create table a (\n\ta int\n)",
input: "alter vschema on a add vindex `hash` (`id`)",
output: "alter vschema on a add vindex hash (id)",
}, {
input: "create table `by` (\n\t`by` char\n)",
input: "alter vschema on a add vindex hash (id) using `hash`",
output: "alter vschema on a add vindex hash (id) using hash",
}, {
input: "create table if not exists a (\n\t`a` int\n)",
output: "create table a (\n\ta int\n)",
input: "alter vschema on a add vindex `add` (`add`)",
}, {
input: "create table a ignore me this is garbage",
output: "create table a",
input: "alter vschema on a add vindex hash (id) using hash",
}, {
input: "create table a (a int, b char, c garbage)",
output: "create table a",
input: "alter vschema on a add vindex hash (id) using `hash`",
output: "alter vschema on a add vindex hash (id) using hash",
}, {
input: "alter vschema on user add vindex name_lookup_vdx (name) using lookup_hash with owner=user, table=name_user_idx, from=name, to=user_id",
}, {
input: "create vindex hash_vdx using hash",
input: "alter vschema on user2 add vindex name_lastname_lookup_vdx (name,lastname) using lookup with owner=`user`, table=`name_lastname_keyspace_id_map`, from=`name,lastname`, to=`keyspace_id`",
output: "alter vschema on user2 add vindex name_lastname_lookup_vdx (name, lastname) using lookup with owner=user, table=name_lastname_keyspace_id_map, from=name,lastname, to=keyspace_id",
}, {
input: "create vindex lookup_vdx using lookup with owner=user, table=name_user_idx, from=name, to=user_id",
input: "alter vschema on a drop vindex hash",
}, {
input: "create vindex xyz_vdx using xyz with param1=hello, param2='world', param3=123",
input: "alter vschema on a drop vindex `hash`",
output: "alter vschema on a drop vindex hash",
}, {
input: "alter vschema on a drop vindex hash",
output: "alter vschema on a drop vindex hash",
}, {
input: "alter vschema on a drop vindex `add`",
output: "alter vschema on a drop vindex `add`",
}, {
input: "create index a on b",
output: "alter table b",
Expand Down Expand Up @@ -1194,20 +1200,18 @@ var (
}, {
input: "show session variables",
output: "show session variables",
}, {
input: "show vindexes",
output: "show vindexes",
}, {
input: "show vindexes on t",
output: "show vindexes on t",
}, {
input: "show vitess_keyspaces",
}, {
input: "show vitess_shards",
}, {
input: "show vitess_tablets",
}, {
input: "show vschema_tables",
input: "show vschema tables",
}, {
input: "show vschema vindexes",
}, {
input: "show vschema vindexes on t",
}, {
input: "show warnings",
output: "show warnings",
Expand Down
Loading

0 comments on commit eccf912

Please sign in to comment.