From f20e4c0ec0d687fb7b679ed276684a7714b2226d Mon Sep 17 00:00:00 2001 From: lance6716 Date: Fri, 26 Nov 2021 14:52:26 +0800 Subject: [PATCH 1/3] parser: respect TiDB comment when DROP INDEX IF EXISTS --- parser/ast/ddl.go | 9 ++++++++- parser/ast/ddl_test.go | 27 ++++++++++++++++++++++++++- 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/parser/ast/ddl.go b/parser/ast/ddl.go index b537012a9c226..b04ce3252838e 100644 --- a/parser/ast/ddl.go +++ b/parser/ast/ddl.go @@ -15,6 +15,7 @@ package ast import ( "github.com/pingcap/errors" + "github.com/pingcap/tidb/parser/auth" "github.com/pingcap/tidb/parser/format" "github.com/pingcap/tidb/parser/model" @@ -1689,7 +1690,13 @@ type DropIndexStmt struct { func (n *DropIndexStmt) Restore(ctx *format.RestoreCtx) error { ctx.WriteKeyWord("DROP INDEX ") if n.IfExists { - ctx.WriteKeyWord("IF EXISTS ") + if ctx.Flags.HasTiDBSpecialCommentFlag() { + ctx.WriteWithSpecialComments("", func() { + ctx.WriteKeyWord("IF EXISTS") + }) + } else { + ctx.WriteKeyWord("IF EXISTS ") + } } ctx.WriteName(n.IndexName) ctx.WriteKeyWord(" ON ") diff --git a/parser/ast/ddl_test.go b/parser/ast/ddl_test.go index a4809d5791cb4..4eb2668f34ae4 100644 --- a/parser/ast/ddl_test.go +++ b/parser/ast/ddl_test.go @@ -16,9 +16,10 @@ package ast_test import ( "testing" + "github.com/stretchr/testify/require" + . "github.com/pingcap/tidb/parser/ast" "github.com/pingcap/tidb/parser/format" - "github.com/stretchr/testify/require" ) func TestDDLVisitorCover(t *testing.T) { @@ -623,3 +624,27 @@ func TestSequenceRestore(t *testing.T) { } runNodeRestoreTest(t, testCases, "%s", extractNodeFunc) } + + +func TestDropIndexRestore(t *testing.T) { + t.Parallel() + sourceSQL := "drop index if exists idx on t" + cases := [] struct { + flags format.RestoreFlags + expectSQL string + }{ + {format.DefaultRestoreFlags, "DROP INDEX IF EXISTS `idx` ON `t`"}, + {format.DefaultRestoreFlags|format.RestoreTiDBSpecialComment, "DROP INDEX /*T! IF EXISTS */`idx` ON `t`"}, + } + + extractNodeFunc := func(node Node) Node { + return node + } + + for _, ca := range cases { + testCases := []NodeRestoreTestCase{ + {sourceSQL, ca.expectSQL}, + } + runNodeRestoreTestWithFlags(t, testCases, "%s", extractNodeFunc, ca.flags) + } +} From eb37727dce871522a23f336d330bad3bbe3e77e1 Mon Sep 17 00:00:00 2001 From: lance6716 Date: Fri, 26 Nov 2021 15:04:59 +0800 Subject: [PATCH 2/3] address comment --- parser/ast/ddl.go | 8 ++------ parser/ast/ddl_test.go | 2 +- 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/parser/ast/ddl.go b/parser/ast/ddl.go index b04ce3252838e..28834697b1961 100644 --- a/parser/ast/ddl.go +++ b/parser/ast/ddl.go @@ -1690,13 +1690,9 @@ type DropIndexStmt struct { func (n *DropIndexStmt) Restore(ctx *format.RestoreCtx) error { ctx.WriteKeyWord("DROP INDEX ") if n.IfExists { - if ctx.Flags.HasTiDBSpecialCommentFlag() { - ctx.WriteWithSpecialComments("", func() { - ctx.WriteKeyWord("IF EXISTS") - }) - } else { + ctx.WriteWithSpecialComments("", func() { ctx.WriteKeyWord("IF EXISTS ") - } + }) } ctx.WriteName(n.IndexName) ctx.WriteKeyWord(" ON ") diff --git a/parser/ast/ddl_test.go b/parser/ast/ddl_test.go index 4eb2668f34ae4..3e8b1243b5c2f 100644 --- a/parser/ast/ddl_test.go +++ b/parser/ast/ddl_test.go @@ -634,7 +634,7 @@ func TestDropIndexRestore(t *testing.T) { expectSQL string }{ {format.DefaultRestoreFlags, "DROP INDEX IF EXISTS `idx` ON `t`"}, - {format.DefaultRestoreFlags|format.RestoreTiDBSpecialComment, "DROP INDEX /*T! IF EXISTS */`idx` ON `t`"}, + {format.DefaultRestoreFlags|format.RestoreTiDBSpecialComment, "DROP INDEX /*T! IF EXISTS */`idx` ON `t`"}, } extractNodeFunc := func(node Node) Node { From ef0bbf33cfe7f7498a0f677e3a383099642ccfce Mon Sep 17 00:00:00 2001 From: lance6716 Date: Fri, 26 Nov 2021 15:34:02 +0800 Subject: [PATCH 3/3] fix make fmt --- parser/ast/ddl_test.go | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/parser/ast/ddl_test.go b/parser/ast/ddl_test.go index 3e8b1243b5c2f..cb0e9c9f25dc8 100644 --- a/parser/ast/ddl_test.go +++ b/parser/ast/ddl_test.go @@ -625,16 +625,15 @@ func TestSequenceRestore(t *testing.T) { runNodeRestoreTest(t, testCases, "%s", extractNodeFunc) } - func TestDropIndexRestore(t *testing.T) { t.Parallel() sourceSQL := "drop index if exists idx on t" - cases := [] struct { - flags format.RestoreFlags + cases := []struct { + flags format.RestoreFlags expectSQL string }{ {format.DefaultRestoreFlags, "DROP INDEX IF EXISTS `idx` ON `t`"}, - {format.DefaultRestoreFlags|format.RestoreTiDBSpecialComment, "DROP INDEX /*T! IF EXISTS */`idx` ON `t`"}, + {format.DefaultRestoreFlags | format.RestoreTiDBSpecialComment, "DROP INDEX /*T! IF EXISTS */`idx` ON `t`"}, } extractNodeFunc := func(node Node) Node {