Skip to content

Commit

Permalink
[parser] 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 de39b1a commit 5ee7839
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 3 deletions.
25 changes: 24 additions & 1 deletion parser/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 parser/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/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/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 5ee7839

Please sign in to comment.