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
17 changes: 15 additions & 2 deletions go/vt/sqlparser/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"errors"
"fmt"
"io"
"sort"
"strings"
"sync"
"unicode"
Expand Down Expand Up @@ -2683,10 +2684,22 @@ func (ct *ColumnType) merge(other ColumnType) error {
}

if other.KeyOpt != colKeyNone {
if ct.KeyOpt != colKeyNone {
keyOptions := []ColumnKeyOption{ct.KeyOpt, other.KeyOpt}
sort.Slice(keyOptions, func(i, j int) bool {return keyOptions[i] < keyOptions[j]})
if other.KeyOpt == ct.KeyOpt {
// MySQL will deduplicate key options when they are repeated.
} else if keyOptions[0] == colKeyPrimary && (keyOptions[1] == colKeyUnique || keyOptions[1] == colKeyUniqueKey) {
// If UNIQUE is specified with PRIMARY KEY, we ignore UNIQUE for now since
// the PRIMARY KEY option will ensure uniqueness already. MySQL does still
// generate a UNIQUE index for the column, but we can add that later if needed.
ct.KeyOpt = colKeyPrimary
} else if ct.KeyOpt == colKeyNone {
// If this column doesn't have a key option yet, just use the new one.
ct.KeyOpt = other.KeyOpt
} else {
// Otherwise throw an error if there are multiple key options that need to be applied.
return errors.New("cannot include more than one key option for a column definition")
}
ct.KeyOpt = other.KeyOpt
}

if other.ForeignKeyDef != nil {
Expand Down
23 changes: 19 additions & 4 deletions go/vt/sqlparser/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1640,6 +1640,21 @@ var (
}, {
input: "alter table a rename column a as b",
output: "alter table a rename column a to b",
}, {
input: "create table t1 (id serial primary key, c1 text not null);",
output: "create table t1 (\n\tid bigint not null auto_increment primary key,\n\tc1 text not null\n)",
}, {
input: "create table t1 (id int primary key unique, c1 varchar(111) not null);",
output: "create table t1 (\n\tid int primary key,\n\tc1 varchar(111) not null\n)",
}, {
input: "create table t1 (id int primary key unique key, c1 varchar(111) not null);",
output: "create table t1 (\n\tid int primary key,\n\tc1 varchar(111) not null\n)",
}, {
input: "create table t1 (id int primary key primary key, c1 varchar(111) not null);",
output: "create table t1 (\n\tid int primary key,\n\tc1 varchar(111) not null\n)",
}, {
input: "create table t1 (id int unique unique unique unique, c1 varchar(111) not null);",
output: "create table t1 (\n\tid int unique,\n\tc1 varchar(111) not null\n)",
}, {
input: "create table a (b1 bool not null primary key, b2 boolean not null)",
output: "create table a (\n\tb1 bool not null primary key,\n\tb2 boolean not null\n)",
Expand Down Expand Up @@ -4656,8 +4671,8 @@ func TestInvalid(t *testing.T) {
input string
err string
}{{
input: "create table t (c int not null default 0 on update current_timestamp() auto_increment comment 'a comment here' unique primary key)",
err: "cannot include more than one key option for a column definition at position 130 near 'key'",
input: "create table t (c int not null default 0 on update current_timestamp() auto_increment comment 'a comment here' fulltext key primary key)",
err: "cannot include more than one key option for a column definition at position 136 near 'key'",
}, {
input: "create table t (c int not null default 0 on update current_timestamp() auto_increment comment 'a comment here' unique comment 'another')",
err: "cannot include more than one comment for a column definition at position 136 near 'another'",
Expand All @@ -4677,8 +4692,8 @@ func TestInvalid(t *testing.T) {
input: "create table t (c default 0 int on update current_timestamp() auto_increment comment 'a comment here' unique)",
err: "syntax error at position 26 near 'default'",
}, {
input: "alter table t add (c int not null default 0 on update current_timestamp() auto_increment comment 'a comment here' unique primary key)",
err: "cannot include more than one key option for a column definition at position 133 near 'key'",
input: "alter table t add (c int not null default 0 on update current_timestamp() auto_increment comment 'a comment here' fulltext key primary key)",
err: "cannot include more than one key option for a column definition at position 139 near 'key'",
}, {
input: "alter table t add (c int not null default 0 on update current_timestamp() auto_increment comment 'a comment here' unique comment 'another')",
err: "cannot include more than one comment for a column definition at position 139 near 'another'",
Expand Down