diff --git a/parser/ast/ddl.go b/parser/ast/ddl.go index b1d382f20709c..f1bae2514e9d7 100644 --- a/parser/ast/ddl.go +++ b/parser/ast/ddl.go @@ -363,7 +363,30 @@ type IndexOption struct { // Restore implements Node interface. func (n *IndexOption) Restore(ctx *RestoreCtx) error { - return errors.New("Not implemented") + hasPrevOption := false + if n.KeyBlockSize > 0 { + ctx.WriteKeyWord("KEY_BLOCK_SIZE") + ctx.WritePlainf("=%d", n.KeyBlockSize) + hasPrevOption = true + } + + if n.Tp != model.IndexTypeInvalid { + if hasPrevOption { + ctx.WritePlain(" ") + } + ctx.WriteKeyWord("USING ") + ctx.WritePlain(n.Tp.String()) + hasPrevOption = true + } + + if n.Comment != "" { + if hasPrevOption { + ctx.WritePlain(" ") + } + ctx.WriteKeyWord("COMMENT ") + ctx.WriteString(n.Comment) + } + return nil } // Accept implements Node Accept interface. diff --git a/parser/ast/ddl_test.go b/parser/ast/ddl_test.go index a7fbd0f64264d..df7bb972c1816 100644 --- a/parser/ast/ddl_test.go +++ b/parser/ast/ddl_test.go @@ -62,3 +62,21 @@ func (ts *testDDLSuite) TestDDLVisitorCover(c *C) { v.node.Accept(visitor1{}) } } + +func (ts *testDDLSuite) TestDDLIndexOption(c *C) { + testCases := []NodeRestoreTestCase{ + {"key_block_size=16","KEY_BLOCK_SIZE=16"}, + {"USING HASH","USING HASH"}, + {"comment 'hello'","COMMENT 'hello'"}, + {"key_block_size=16 USING HASH","KEY_BLOCK_SIZE=16 USING HASH"}, + {"USING HASH KEY_BLOCK_SIZE=16","KEY_BLOCK_SIZE=16 USING HASH"}, + {"USING HASH COMMENT 'foo'","USING HASH COMMENT 'foo'"}, + {"COMMENT 'foo'","COMMENT 'foo'"}, + {"key_block_size = 32 using hash comment 'hello'","KEY_BLOCK_SIZE=32 USING HASH COMMENT 'hello'"}, + {"key_block_size=32 using btree comment 'hello'","KEY_BLOCK_SIZE=32 USING BTREE COMMENT 'hello'"}, + } + extractNodeFunc := func(node Node) Node { + return node.(*CreateIndexStmt).IndexOption + } + RunNodeRestoreTest(c, testCases, "CREATE INDEX idx ON t (a) %s", extractNodeFunc) +} \ No newline at end of file diff --git a/parser/parser.go b/parser/parser.go index 9c719af725b40..90ac05a6d0874 100644 --- a/parser/parser.go +++ b/parser/parser.go @@ -8563,6 +8563,8 @@ yynewstate: opt1.Comment = opt2.Comment } else if opt2.Tp != 0 { opt1.Tp = opt2.Tp + } else if opt2.KeyBlockSize > 0 { + opt1.KeyBlockSize = opt2.KeyBlockSize } parser.yyVAL.item = opt1 } @@ -8571,7 +8573,7 @@ yynewstate: { parser.yyVAL.item = &ast.IndexOption{ // TODO bug should be fix here! - // KeyBlockSize: $1.(uint64), + KeyBlockSize: yyS[yypt-0].item.(uint64), } } case 342: diff --git a/parser/parser.y b/parser/parser.y index 617ae4f084389..118f5cdf163fe 100644 --- a/parser/parser.y +++ b/parser/parser.y @@ -2926,6 +2926,8 @@ IndexOptionList: opt1.Comment = opt2.Comment } else if opt2.Tp != 0 { opt1.Tp = opt2.Tp + } else if opt2.KeyBlockSize > 0 { + opt1.KeyBlockSize = opt2.KeyBlockSize } $$ = opt1 } @@ -2937,7 +2939,7 @@ IndexOption: { $$ = &ast.IndexOption{ // TODO bug should be fix here! - // KeyBlockSize: $1.(uint64), + KeyBlockSize: $3.(uint64), } } | IndexType