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
26 changes: 23 additions & 3 deletions go/vt/sqlparser/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,7 @@ type DDL struct {
NewName TableName
IfExists bool
TableSpec *TableSpec
OptLike *OptLike
PartitionSpec *PartitionSpec
VindexSpec *VindexSpec
VindexCols []ColIdent
Expand All @@ -706,10 +707,12 @@ const (
func (node *DDL) Format(buf *TrackedBuffer) {
switch node.Action {
case CreateStr:
if node.TableSpec == nil {
buf.Myprintf("%s table %v", node.Action, node.NewName)
} else {
if node.OptLike != nil {
buf.Myprintf("%s table %v %v", node.Action, node.NewName, node.OptLike)
} else if node.TableSpec != nil {
buf.Myprintf("%s table %v %v", node.Action, node.NewName, node.TableSpec)
} else {
buf.Myprintf("%s table %v", node.Action, node.NewName)
}
case DropStr:
exists := ""
Expand Down Expand Up @@ -763,6 +766,23 @@ const (
ReorganizeStr = "reorganize partition"
)

// OptLike works for create table xxx like xxx
type OptLike struct {
LikeTable TableName
}

// Format formats the node.
func (node *OptLike) Format(buf *TrackedBuffer) {
buf.Myprintf("like %v", node.LikeTable)
}

func (node *OptLike) walkSubtree(visit Visit) error {
if node == nil {
return nil
}
return Walk(visit, node.LikeTable)
}

// PartitionSpec describe partition actions (for alter and create)
type PartitionSpec struct {
Action string
Expand Down
31 changes: 31 additions & 0 deletions go/vt/sqlparser/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1945,6 +1945,37 @@ func TestCreateTable(t *testing.T) {
}
}

func TestCreateTableLike(t *testing.T) {
normal := "create table a like b"
testCases := []struct {
input string
output string
}{
{
"create table a like b",
normal,
},
{
"create table a (like b)",
normal,
},
{
"create table ks.a like unsharded_ks.b",
"create table ks.a like unsharded_ks.b",
},
}
for _, tcase := range testCases {
tree, err := ParseStrictDDL(tcase.input)
if err != nil {
t.Errorf("input: %s, err: %v", tcase.input, err)
continue
}
if got, want := String(tree.(*DDL)), tcase.output; got != want {
t.Errorf("Parse(%s):\n%s, want\n%s", tcase.input, got, want)
}
}
}

func TestCreateTableEscaped(t *testing.T) {
testCases := []struct {
input string
Expand Down
Loading