Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

parser: support OPTIMIZE statement #49205

Merged
merged 5 commits into from
Dec 7, 2023
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
6 changes: 6 additions & 0 deletions pkg/ddl/db_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2955,3 +2955,9 @@ func TestDefaultCollationForUTF8MB4(t *testing.T) {
tk.MustQuery("show create database dby").Check(testkit.Rows(
"dby CREATE DATABASE `dby` /*!40100 DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci */"))
}

func TestOptimizeTable(t *testing.T) {
store := testkit.CreateMockStore(t, mockstore.WithDDLChecker())
tk := testkit.NewTestKit(t, store)
tk.MustGetErrMsg("optimize table t", "[ddl:8200]OPTIMIZE TABLE is not supported")
}
2 changes: 2 additions & 0 deletions pkg/parser/ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,8 @@ func GetStmtLabel(stmtNode StmtNode) string {
return "Shutdown"
case *SavepointStmt:
return "Savepoint"
case *OptimizeTableStmt:
return "Optimize"
}
return "other"
}
35 changes: 35 additions & 0 deletions pkg/parser/ast/ddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ var (
_ DDLNode = &DropSequenceStmt{}
_ DDLNode = &DropPlacementPolicyStmt{}
_ DDLNode = &DropResourceGroupStmt{}
_ DDLNode = &OptimizeTableStmt{}
_ DDLNode = &RenameTableStmt{}
_ DDLNode = &TruncateTableStmt{}
_ DDLNode = &RepairTableStmt{}
Expand Down Expand Up @@ -1322,6 +1323,40 @@ func (n *DropResourceGroupStmt) Accept(v Visitor) (Node, bool) {
return v.Leave(n)
}

type OptimizeTableStmt struct {
ddlNode

NoWriteToBinLog bool
Tables []*TableName
}

func (n *OptimizeTableStmt) Restore(ctx *format.RestoreCtx) error {
ctx.WriteKeyWord("OPTIMIZE ")
if n.NoWriteToBinLog {
ctx.WriteKeyWord("NO_WRITE_TO_BINLOG ")
}
ctx.WriteKeyWord("TABLE ")

for index, table := range n.Tables {
if index != 0 {
ctx.WritePlain(", ")
}
if err := table.Restore(ctx); err != nil {
return errors.Annotatef(err, "An error occurred while restore OptimizeTableStmt.Tables[%d]", index)
}
}
return nil
}

func (n *OptimizeTableStmt) Accept(v Visitor) (Node, bool) {
newNode, skipChildren := v.Enter(n)
if skipChildren {
return v.Leave(newNode)
}
n = newNode.(*OptimizeTableStmt)
return v.Leave(n)
}

// DropSequenceStmt is a statement to drop a Sequence.
type DropSequenceStmt struct {
ddlNode
Expand Down
15 changes: 15 additions & 0 deletions pkg/parser/ast/ddl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,21 @@ func TestAdminRepairTableRestore(t *testing.T) {
runNodeRestoreTest(t, testCases, "%s", extractNodeFunc)
}

func TestAdminOptimizeTableRestore(t *testing.T) {
testCases := []NodeRestoreTestCase{
{"OPTIMIZE TABLE t", "OPTIMIZE TABLE `t`"},
{"OPTIMIZE LOCAL TABLE t", "OPTIMIZE NO_WRITE_TO_BINLOG TABLE `t`"},
{"OPTIMIZE NO_WRITE_TO_BINLOG TABLE t", "OPTIMIZE NO_WRITE_TO_BINLOG TABLE `t`"},
{"OPTIMIZE TABLE t1, t2", "OPTIMIZE TABLE `t1`, `t2`"},
{"optimize table t1,t2", "OPTIMIZE TABLE `t1`, `t2`"},
{"optimize tables t1, t2", "OPTIMIZE TABLE `t1`, `t2`"},
}
extractNodeFunc := func(node Node) Node {
return node
}
runNodeRestoreTest(t, testCases, "%s", extractNodeFunc)
}

func TestSequenceRestore(t *testing.T) {
testCases := []NodeRestoreTestCase{
{"create sequence seq", "CREATE SEQUENCE `seq`"},
Expand Down
Loading