Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
115 changes: 110 additions & 5 deletions go/vt/sqlparser/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -600,22 +600,32 @@ func (node *Set) WalkSubtree(visit Visit) error {
// DDL represents a CREATE, ALTER, DROP, RENAME or TRUNCATE statement.
// Table is set for AlterStr, DropStr, RenameStr, TruncateStr
// NewName is set for AlterStr, CreateStr, RenameStr.
// VindexSpec is set for CreateVindexStr, DropVindexStr, AddColVindexStr, DropColVindexStr
// VindexCols is set for AddColVindexStr
type DDL struct {
Action string
Table TableName
NewName TableName
IfExists bool
TableSpec *TableSpec
PartitionSpec *PartitionSpec
VindexSpec *VindexSpec
VindexCols []ColIdent
}

// DDL strings.
const (
CreateStr = "create"
AlterStr = "alter"
DropStr = "drop"
RenameStr = "rename"
TruncateStr = "truncate"
CreateStr = "create"
AlterStr = "alter"
DropStr = "drop"
RenameStr = "rename"
TruncateStr = "truncate"
CreateVindexStr = "create vindex"
AddColVindexStr = "add vindex"
DropColVindexStr = "drop vindex"

// Vindex DDL param to specify the owner of a vindex
VindexOwnerStr = "owner"
)

// Format formats the node.
Expand All @@ -641,6 +651,23 @@ func (node *DDL) Format(buf *TrackedBuffer) {
} else {
buf.Myprintf("%s table %v", node.Action, node.Table)
}
case CreateVindexStr:
buf.Myprintf("%s %v %v", node.Action, node.VindexSpec.Name, node.VindexSpec)
case AddColVindexStr:
buf.Myprintf("alter table %v %s %v (", node.Table, node.Action, node.VindexSpec.Name)
for i, col := range node.VindexCols {
if i != 0 {
buf.Myprintf(", %v", col)
} else {
buf.Myprintf("%v", col)
}
}
buf.Myprintf(")")
if node.VindexSpec.Type.String() != "" {
buf.Myprintf(" %v", node.VindexSpec)
}
case DropColVindexStr:
buf.Myprintf("alter table %v %s %v", node.Table, node.Action, node.VindexSpec.Name)
default:
buf.Myprintf("%s table %v", node.Action, node.Table)
}
Expand Down Expand Up @@ -1096,6 +1123,84 @@ const (
colKey
)

// VindexSpec defines a vindex for a CREATE VINDEX or DROP VINDEX statement
type VindexSpec struct {
Name ColIdent
Type ColIdent
Params []VindexParam
}

// ParseParams parses the vindex parameter list, pulling out the special-case
// "owner" parameter
func (node *VindexSpec) ParseParams() (string, map[string]string) {
var owner string
params := map[string]string{}
for _, p := range node.Params {
if p.Key.Lowered() == VindexOwnerStr {
owner = p.Val
} else {
params[p.Key.String()] = p.Val
}
}
return owner, params
}

// Format formats the node. The "CREATE VINDEX" preamble was formatted in
// the containing DDL node Format, so this just prints the type, any
// parameters, and optionally the owner
func (node *VindexSpec) Format(buf *TrackedBuffer) {
buf.Myprintf("using %v", node.Type)

numParams := len(node.Params)
if numParams != 0 {
buf.Myprintf(" with ")
for i, p := range node.Params {
if i != 0 {
buf.Myprintf(", ")
}
buf.Myprintf("%v", p)
}
}
}

// WalkSubtree walks the nodes of the subtree.
func (node *VindexSpec) WalkSubtree(visit Visit) error {
err := Walk(visit,
node.Name,
)

if err != nil {
return err
}

for _, p := range node.Params {
err := Walk(visit, p)

if err != nil {
return err
}
}
return nil
}

// VindexParam defines a key/value parameter for a CREATE VINDEX statement
type VindexParam struct {
Key ColIdent
Val string
}

// Format formats the node.
func (node VindexParam) Format(buf *TrackedBuffer) {
buf.Myprintf("%s=%s", node.Key.String(), node.Val)
}

// WalkSubtree walks the nodes of the subtree.
func (node VindexParam) WalkSubtree(visit Visit) error {
return Walk(visit,
node.Key,
)
}

// Show represents a show statement.
type Show struct {
Type string
Expand Down
85 changes: 85 additions & 0 deletions go/vt/sqlparser/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,85 @@ var (
}, {
input: "alter table a partition by range (id) (partition p0 values less than (10), partition p1 values less than (maxvalue))",
output: "alter table a",
}, {
input: "alter table a add column id int",
output: "alter table a",
}, {
input: "alter table a add index idx (id)",
output: "alter table a",
}, {
input: "alter table a add fulltext index idx (id)",
output: "alter table a",
}, {
input: "alter table a add spatial index idx (id)",
output: "alter table a",
}, {
input: "alter table a add foreign key",
output: "alter table a",
}, {
input: "alter table a add primary key",
output: "alter table a",
}, {
input: "alter table a add constraint",
output: "alter table a",
}, {
input: "alter table a add id",
output: "alter table a",
}, {
input: "alter table a drop column id int",
output: "alter table a",
}, {
input: "alter table a drop index idx (id)",
output: "alter table a",
}, {
input: "alter table a drop fulltext index idx (id)",
output: "alter table a",
}, {
input: "alter table a drop spatial index idx (id)",
output: "alter table a",
}, {
input: "alter table a drop foreign key",
output: "alter table a",
}, {
input: "alter table a drop primary key",
output: "alter table a",
}, {
input: "alter table a drop constraint",
output: "alter table a",
}, {
input: "alter table a drop id",
output: "alter table a",
}, {
input: "alter table a add vindex hash (id)",
}, {
input: "alter table a add vindex `hash` (`id`)",
output: "alter table a add vindex hash (id)",
}, {
input: "alter table a add vindex hash (id) using `hash`",
output: "alter table a add vindex hash (id) using hash",
}, {
input: "alter table a add vindex `add` (`add`)",
}, {
input: "alter table a add vindex hash (id) using hash",
}, {
input: "alter table a add vindex hash (id) using `hash`",
output: "alter table a add vindex hash (id) using hash",
}, {
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 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 table a drop vindex hash",
}, {
input: "alter table a drop vindex `hash`",
output: "alter table a drop vindex hash",
}, {
input: "alter table a drop vindex hash",
output: "alter table a drop vindex hash",
}, {
input: "alter table a drop vindex `add`",
output: "alter table a drop vindex `add`",
}, {
input: "create table a",
}, {
Expand All @@ -766,6 +845,12 @@ var (
}, {
input: "create table a (a int, b char, c garbage)",
output: "create table a",
}, {
input: "create vindex hash_vdx using hash",
}, {
input: "create vindex lookup_vdx using lookup with owner=user, table=name_user_idx, from=name, to=user_id",
}, {
input: "create vindex xyz_vdx using xyz with param1=hello, param2='world', param3=123",
}, {
input: "create index a on b",
output: "alter table b",
Expand Down
Loading