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
23 changes: 20 additions & 3 deletions go/vt/sqlparser/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -2360,7 +2360,11 @@ func (node *DDL) Format(buf *TrackedBuffer) {
if view.CheckOption != ViewCheckOptionUnspecified {
checkOpt = fmt.Sprintf(" with %s check option", view.CheckOption)
}
buf.Myprintf("%s %sview %v%v as %v%s", node.Action, afterCreate, view.ViewName, view.Columns, view.ViewExpr, checkOpt)
notExists := ""
if node.IfNotExists {
notExists = " if not exists"
}
buf.Myprintf("%s %sview%s %v%v as %v%s", node.Action, afterCreate, notExists, view.ViewName, view.Columns, view.ViewExpr, checkOpt)
} else if node.TriggerSpec != nil {
trigger := node.TriggerSpec
triggerDef := ""
Expand Down Expand Up @@ -3480,6 +3484,11 @@ type IndexSpec struct {
Columns []*IndexColumn
// Options contains the index options when creating an index
Options []*IndexOption

// ifExists and ifNotExists states whether `IF [NOT] EXISTS` was present in query
// This is solely for printing purposes; we rely on the one in ast.DDL for actual logic
ifExists bool
ifNotExists bool
}

func (idx *IndexSpec) Format(buf *TrackedBuffer) {
Expand All @@ -3497,9 +3506,13 @@ func (idx *IndexSpec) Format(buf *TrackedBuffer) {
buf.Myprintf("%s ", idx.Type)
}
}
notExists := ""
if idx.ifNotExists {
notExists = " if not exists"
}

if idx.Type != PrimaryStr {
buf.Myprintf("index %s ", idx.ToName.val)
buf.Myprintf("index%s %s ", notExists, idx.ToName.val)
}

if idx.Using.val != "" {
Expand Down Expand Up @@ -3530,10 +3543,14 @@ func (idx *IndexSpec) Format(buf *TrackedBuffer) {
}
}
case "drop":
exists := ""
if idx.ifExists {
exists = " if exists"
}
if idx.Type == PrimaryStr {
buf.Myprintf("drop primary key")
} else {
buf.Myprintf("drop index %s", idx.ToName.val)
buf.Myprintf("drop index%s %s", exists, idx.ToName.val)
}
case "rename":
buf.Myprintf("rename index %s to %s", idx.FromName.val, idx.ToName.val)
Expand Down
95 changes: 70 additions & 25 deletions go/vt/sqlparser/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1830,6 +1830,9 @@ var (
{
input: "alter table a add index idx (id)",
},
{
input: "alter table a add index if not exists idx (id)",
},
{
input: "alter table a add fulltext index idx (id)",
},
Expand All @@ -1839,36 +1842,59 @@ var (
{
input: "alter table a add vector index idx (id)",
},
{
input: "alter table a add constraint unique index idx (id)",
output: "alter table a add unique index idx (id)",
},
{
input: "alter table a add constraint unique index if not exists idx (id)",
output: "alter table a add unique index if not exists idx (id)",
},
{
input: "alter table a add foreign key (x) references y(z)",
output: "alter table a add foreign key (x) references y (z)",
}, {
},
{
input: "alter table a add constraint foreign key (x) references y(z)",
output: "alter table a add foreign key (x) references y (z)",
}, {
},
{
input: "alter table a add constraint abc foreign key country_code (country_code) REFERENCES premium_country (country_code)",
output: "alter table a add constraint abc foreign key country_code (country_code) references premium_country (country_code)",
}, {
},
{
input: "alter table a add constraint abc foreign key country_code (country_code) REFERENCES premium_country (country_code) on delete cascade",
output: "alter table a add constraint abc foreign key country_code (country_code) references premium_country (country_code) on delete cascade",
}, {
},
{
input: "alter table a add constraint abc foreign key country_code (country_code) REFERENCES premium_country (country_code) on update set null",
output: "alter table a add constraint abc foreign key country_code (country_code) references premium_country (country_code) on update set null",
}, {
},
{
input: "alter table a add primary key (a, b)",
}, {
},
{
input: "alter table a add constraint a_pk primary key (a, b)",
}, {
},
{
input: "alter table a add constraint a_pk primary key (value)",
}, {
},
{
input: "alter table a add primary key (value)",
}, {
},
{
input: "alter table a drop primary key",
}, {
},
{
input: "alter table a drop column id",
}, {
},
{
input: "alter table a drop index idx",
}, {
},
{
input: "alter table a drop index if exists idx",
},
{
input: "alter table a add constraint check (b > 0)",
output: "alter table a add check (b > 0)",
}, {
Expand Down Expand Up @@ -1963,19 +1989,24 @@ var (
}, {
input: "create temporary table if not exists a (\n\t`a` int\n)",
output: "create temporary table if not exists a (\n\ta int\n)",
}, {
},
{
input: "create index a on b (id)",
output: "alter table b add index a (id)",
}, {
input: "CREATE INDEX a ON b (id)",
output: "alter table b add index a (id)",
}, {
},
{
input: "create index if not exists a ON b (id)",
output: "alter table b add index if not exists a (id)",
},
{
input: "create index a on b (foo(6) desc, foo asc)",
output: "alter table b add index a (foo(6) desc, foo)",
}, {
},
{
input: "CREATE INDEX `c` on `dolt_test`.`a`(`b` ASC) INVISIBLE",
output: "alter table dolt_test.a add index c (b) INVISIBLE",
}, {
},
{
input: "CREATE INDEX `c` on `dolt_test`.`a`(`b` ASC) VISIBLE",
output: "alter table dolt_test.a add index c (b) VISIBLE",
},
Expand Down Expand Up @@ -2020,13 +2051,20 @@ var (
}, {
input: "create SQL SECURITY INVOKER view a as select current_timestamp()",
output: "create sql security invoker view a as select current_timestamp(0)",
}, {
},
{
input: "CREATE VIEW a AS SELECT current_timestamp()",
output: "create view a as select current_timestamp(0)",
}, {
},
{
input: "CREATE VIEW IF NOT EXISTS a AS SELECT 1",
output: "create view if not exists a as select 1",
},
{
input: "create view a_view as select * from table_1 join table_2 on table_1.table_2_id_fk = table_2.id where city = 'my city'",
output: "create view a_view as select * from table_1 join table_2 on table_1.table_2_id_fk = table_2.id where city = 'my city'",
}, {
},
{
input: "CREATE OR REPLACE VIEW a AS SELECT current_timestamp()",
output: "create or replace view a as select current_timestamp(0)",
},
Expand Down Expand Up @@ -2098,13 +2136,20 @@ var (
}, {
input: "drop table b ",
output: "drop table b",
}, {
},
{
input: "drop view if exists a",
output: "drop view if exists a",
}, {
},
{
input: "drop index b on a",
output: "alter table a drop index b",
}, {
},
{
input: "drop index if exists b on a",
output: "alter table a drop index if exists b",
},
{
input: "analyze table a",
output: "analyze table a",
}, {
Expand Down
Loading