Skip to content

Commit

Permalink
parser: implement Restore for IndexOption (pingcap#88)
Browse files Browse the repository at this point in the history
  • Loading branch information
feloxx authored and leoppro committed Dec 21, 2018
1 parent 06bda4c commit 4caba24
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 3 deletions.
25 changes: 24 additions & 1 deletion ast/ddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
18 changes: 18 additions & 0 deletions ast/ddl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
4 changes: 3 additions & 1 deletion parser.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -2937,7 +2939,7 @@ IndexOption:
{
$$ = &ast.IndexOption{
// TODO bug should be fix here!
// KeyBlockSize: $1.(uint64),
KeyBlockSize: $3.(uint64),
}
}
| IndexType
Expand Down

0 comments on commit 4caba24

Please sign in to comment.