Skip to content

Commit cf4d1ea

Browse files
committed
Merge branch 'master' into yiding/opt-trace
2 parents 2f29e50 + 43a9376 commit cf4d1ea

40 files changed

Lines changed: 11661 additions & 8955 deletions

.gitignore

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,4 @@ bin/
22
y.go
33
*.output
44
.idea/
5-
go.mod
6-
go.sum
75
coverage.txt

README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,33 @@ TiDB SQL Parser
77

88
## How to use it
99

10+
```go
11+
import (
12+
"fmt"
13+
"github.com/pingcap/parser"
14+
_ "github.com/pingcap/tidb/types/parser_driver"
15+
)
16+
17+
// This example show how to parse a text sql into ast.
18+
func example() {
19+
20+
// 0. make sure import parser_driver implemented by TiDB(user also can implement own driver by self).
21+
// and add `import _ "github.com/pingcap/tidb/types/parser_driver"` in the head of file.
22+
23+
// 1. Create a parser. The parser is NOT goroutine safe and should
24+
// not be shared among multiple goroutines. However, parser is also
25+
// heavy, so each goroutine should reuse its own local instance if
26+
// possible.
27+
p := parser.New()
28+
29+
// 2. Parse a text SQL into AST([]ast.StmtNode).
30+
stmtNodes, _, err := p.Parse("select * from tbl where id = 1", "", "")
31+
32+
// 3. Use AST to do cool things.
33+
fmt.Println(stmtNodes[0], err)
34+
}
35+
```
36+
1037
See [https://godoc.org/github.com/pingcap/parser](https://godoc.org/github.com/pingcap/parser)
1138

1239
## How to update parser for TiDB

ast/advisor.go

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
// Copyright 2019 PingCAP, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
package ast
15+
16+
import (
17+
. "github.com/pingcap/parser/format"
18+
)
19+
20+
var _ StmtNode = &IndexAdviseStmt{}
21+
22+
// IndexAdviseStmt is used to advise indexes
23+
type IndexAdviseStmt struct {
24+
stmtNode
25+
26+
IsLocal bool
27+
Path string
28+
MaxMinutes uint64
29+
MaxIndexNum *MaxIndexNumClause
30+
LinesInfo *LinesClause
31+
}
32+
33+
// Restore implements Node Accept interface.
34+
func (n *IndexAdviseStmt) Restore(ctx *RestoreCtx) error {
35+
ctx.WriteKeyWord("INDEX ADVISE ")
36+
if n.IsLocal {
37+
ctx.WriteKeyWord("LOCAL ")
38+
}
39+
ctx.WriteKeyWord("INFILE ")
40+
ctx.WriteString(n.Path)
41+
if n.MaxMinutes != UnspecifiedSize {
42+
ctx.WriteKeyWord(" MAX_MINUTES ")
43+
ctx.WritePlainf("%d", n.MaxMinutes)
44+
}
45+
if n.MaxIndexNum != nil {
46+
n.MaxIndexNum.Restore(ctx)
47+
}
48+
n.LinesInfo.Restore(ctx)
49+
return nil
50+
}
51+
52+
// Accept implements Node Accept interface.
53+
func (n *IndexAdviseStmt) Accept(v Visitor) (Node, bool) {
54+
newNode, skipChildren := v.Enter(n)
55+
if skipChildren {
56+
return v.Leave(newNode)
57+
}
58+
n = newNode.(*IndexAdviseStmt)
59+
return v.Leave(n)
60+
}
61+
62+
// MaxIndexNumClause represents 'maximum number of indexes' clause in index advise statement.
63+
type MaxIndexNumClause struct {
64+
PerTable uint64
65+
PerDB uint64
66+
}
67+
68+
// Restore for max index num clause
69+
func (n *MaxIndexNumClause) Restore(ctx *RestoreCtx) error {
70+
ctx.WriteKeyWord(" MAX_IDXNUM")
71+
if n.PerTable != UnspecifiedSize {
72+
ctx.WriteKeyWord(" PER_TABLE ")
73+
ctx.WritePlainf("%d", n.PerTable)
74+
}
75+
if n.PerDB != UnspecifiedSize {
76+
ctx.WriteKeyWord(" PER_DB ")
77+
ctx.WritePlainf("%d", n.PerDB)
78+
}
79+
return nil
80+
}

0 commit comments

Comments
 (0)