Skip to content

Commit

Permalink
[parser] parser: implement Restore for BeginStmt,CommitStmt,RollbackS…
Browse files Browse the repository at this point in the history
…tmt, AnalyzeTableStmt and LoadStatsStmt (pingcap#144)
  • Loading branch information
shinytang6 authored and xhebox committed Oct 8, 2021
1 parent ecde2f6 commit 23128a3
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 20 deletions.
9 changes: 6 additions & 3 deletions parser/ast/misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,8 @@ type BeginStmt struct {

// Restore implements Node interface.
func (n *BeginStmt) Restore(ctx *RestoreCtx) error {
return errors.New("Not implemented")
ctx.WriteKeyWord("START TRANSACTION")
return nil
}

// Accept implements Node Accept interface.
Expand Down Expand Up @@ -297,7 +298,8 @@ type CommitStmt struct {

// Restore implements Node interface.
func (n *CommitStmt) Restore(ctx *RestoreCtx) error {
return errors.New("Not implemented")
ctx.WriteKeyWord("COMMIT")
return nil
}

// Accept implements Node Accept interface.
Expand All @@ -318,7 +320,8 @@ type RollbackStmt struct {

// Restore implements Node interface.
func (n *RollbackStmt) Restore(ctx *RestoreCtx) error {
return errors.New("Not implemented")
ctx.WriteKeyWord("ROLLBACK")
return nil
}

// Accept implements Node Accept interface.
Expand Down
40 changes: 38 additions & 2 deletions parser/ast/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,41 @@ type AnalyzeTableStmt struct {

// Restore implements Node interface.
func (n *AnalyzeTableStmt) Restore(ctx *RestoreCtx) error {
return errors.New("Not implemented")
ctx.WriteKeyWord("ANALYZE TABLE ")
for i, table := range n.TableNames {
if i != 0 {
ctx.WritePlain(",")
}
if err := table.Restore(ctx); err != nil {
return errors.Annotatef(err, "An error occurred while restore AnalyzeTableStmt.TableNames[%d]", i)
}
}
if len(n.PartitionNames) != 0 {
ctx.WriteKeyWord(" PARTITION ")
}
for i, partition := range n.PartitionNames {
if i != 0 {
ctx.WritePlain(",")
}
ctx.WriteName(partition.O)
}
if n.IndexFlag {
ctx.WriteKeyWord(" INDEX")
}
for i, index := range n.IndexNames {
if i != 0 {
ctx.WritePlain(",")
} else {
ctx.WritePlain(" ")
}
ctx.WriteName(index.O)
}
if n.MaxNumBuckets != 0 {
ctx.WriteKeyWord(" WITH ")
ctx.WritePlainf("%d", n.MaxNumBuckets)
ctx.WriteKeyWord(" BUCKETS")
}
return nil
}

// Accept implements Node Accept interface.
Expand Down Expand Up @@ -100,7 +134,9 @@ type LoadStatsStmt struct {

// Restore implements Node interface.
func (n *LoadStatsStmt) Restore(ctx *RestoreCtx) error {
return errors.New("Not implemented")
ctx.WriteKeyWord("LOAD STATS ")
ctx.WriteString(n.Path)
return nil
}

// Accept implements Node Accept interface.
Expand Down
30 changes: 15 additions & 15 deletions parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,10 +367,10 @@ func (s *testParserSuite) TestDMLStmt(c *C) {
WHERE stuff.value >= ALL (SELECT stuff.value
FROM stuff)`, true, ""},
{"BEGIN", true, ""},
{"START TRANSACTION", true, ""},
{"START TRANSACTION", true, "START TRANSACTION"},
// 45
{"COMMIT", true, ""},
{"ROLLBACK", true, ""},
{"COMMIT", true, "COMMIT"},
{"ROLLBACK", true, "ROLLBACK"},
{`BEGIN;
INSERT INTO foo VALUES (42, 3.14);
INSERT INTO foo VALUES (-1, 2.78);
Expand Down Expand Up @@ -604,7 +604,7 @@ func (s *testParserSuite) TestDBAStmt(c *C) {
{"show stats_healthy where table_name = 't'", true, ""},

// for load stats
{"load stats '/tmp/stats.json'", true, ""},
{"load stats '/tmp/stats.json'", true, "LOAD STATS '/tmp/stats.json'"},
// set
// user defined
{"SET @ = 1", true, ""},
Expand Down Expand Up @@ -2443,17 +2443,17 @@ func (s *testParserSuite) TestDDLStatements(c *C) {

func (s *testParserSuite) TestAnalyze(c *C) {
table := []testCase{
{"analyze table t1", true, ""},
{"analyze table t,t1", true, ""},
{"analyze table t1 index", true, ""},
{"analyze table t1 index a", true, ""},
{"analyze table t1 index a,b", true, ""},
{"analyze table t with 4 buckets", true, ""},
{"analyze table t index a with 4 buckets", true, ""},
{"analyze table t partition a", true, ""},
{"analyze table t partition a with 4 buckets", true, ""},
{"analyze table t partition a index b", true, ""},
{"analyze table t partition a index b with 4 buckets", true, ""},
{"analyze table t1", true, "ANALYZE TABLE `t1`"},
{"analyze table t,t1", true, "ANALYZE TABLE `t`,`t1`"},
{"analyze table t1 index", true, "ANALYZE TABLE `t1` INDEX"},
{"analyze table t1 index a", true, "ANALYZE TABLE `t1` INDEX `a`"},
{"analyze table t1 index a,b", true, "ANALYZE TABLE `t1` INDEX `a`,`b`"},
{"analyze table t with 4 buckets", true, "ANALYZE TABLE `t` WITH 4 BUCKETS"},
{"analyze table t index a with 4 buckets", true, "ANALYZE TABLE `t` INDEX `a` WITH 4 BUCKETS"},
{"analyze table t partition a", true, "ANALYZE TABLE `t` PARTITION `a`"},
{"analyze table t partition a with 4 buckets", true, "ANALYZE TABLE `t` PARTITION `a` WITH 4 BUCKETS"},
{"analyze table t partition a index b", true, "ANALYZE TABLE `t` PARTITION `a` INDEX `b`"},
{"analyze table t partition a index b with 4 buckets", true, "ANALYZE TABLE `t` PARTITION `a` INDEX `b` WITH 4 BUCKETS"},
}
s.RunTest(c, table)
}
Expand Down

0 comments on commit 23128a3

Please sign in to comment.