-
Notifications
You must be signed in to change notification settings - Fork 489
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ast, parser: extract create view parameters to CreateViewStmt #55
Conversation
@XuHuaiyu PTAL |
@AndrewDi Please create an issue in tidb repo to trace the P1 tasks. |
parser_test.go
Outdated
c.Assert(ok, IsTrue) | ||
c.Assert(v.OrReplace, IsTrue) | ||
c.Assert(v.Algorithm, Equals, ast.AlgorithmUndefined) | ||
c.Assert(v.Definer, NotNil) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not check Equals?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add check for viewName, colList, view_select_stmt?
ast/ddl.go
Outdated
AlgorithmTemptable | ||
) | ||
|
||
var AlgorithmType = map[Algorithm]string { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
how about defining a String()
function for Algorithm
:
func (a *Algorithm) String() string {
switch *a {
case AlgorithmUndefined: return "UNDEFINED"
case AlgorithmMerge : return "MERGE"
case AlgorithmTemptable: return "TEMPTABLE"
}
return "WRONG_ALGORITHM"
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here is the benchmark result:
[jianzhang.zj:/tmp]
➜ go test -bench Benchmark -run xx . -count 5
goos: darwin
goarch: amd64
BenchmarkString-8 2000000000 0.62 ns/op
BenchmarkString-8 2000000000 0.63 ns/op
BenchmarkString-8 2000000000 0.63 ns/op
BenchmarkString-8 2000000000 0.62 ns/op
BenchmarkString-8 2000000000 0.62 ns/op
BenchmarkMapAcess-8 500000000 3.75 ns/op
BenchmarkMapAcess-8 500000000 3.79 ns/op
BenchmarkMapAcess-8 500000000 3.75 ns/op
BenchmarkMapAcess-8 500000000 3.74 ns/op
BenchmarkMapAcess-8 500000000 3.75 ns/op
PASS
ok _/tmp 17.924s
Here is the benchmark code:
package main
import "testing"
type Algorithm int
const (
AlgorithmUndefined Algorithm = iota
AlgorithmMerge
AlgorithmTemptable
)
func (a *Algorithm) String() string {
switch *a {
case AlgorithmUndefined:
return "UNDEFINED"
case AlgorithmMerge:
return "MERGE"
case AlgorithmTemptable:
return "TEMPTABLE"
}
return "WRONG_ALGORITHM"
}
var AlgorithmType = map[Algorithm]string{
AlgorithmUndefined: "UNDEFINED",
AlgorithmMerge: "MERGE",
AlgorithmTemptable: "TEMPTABLE",
}
func BenchmarkString(b *testing.B) {
var a Algorithm = AlgorithmUndefined
b.ResetTimer()
for i := 0; i < b.N; i++ {
a.String()
}
}
func BenchmarkMapAcess(b *testing.B) {
var a Algorithm = AlgorithmUndefined
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = AlgorithmType[a]
}
}
ast/ddl.go
Outdated
|
||
// Algorithm is VIEW's SQL AlGORITHM characteristic | ||
// See https://dev.mysql.com/doc/refman/5.7/en/view-algorithms.html | ||
type Algorithm int |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/Algorithm/ViewAlgorithm/?
ast/ddl.go
Outdated
|
||
// Security is VIEW's SQL SECURITY characteristic | ||
// See https://dev.mysql.com/doc/refman/5.7/en/create-view.html | ||
type Security int |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/Security/ViewSecurity/
ast/ddl.go
Outdated
SecurityInvoker | ||
) | ||
|
||
var SecurityType = map[Security]string { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ditto
ast/ddl.go
Outdated
|
||
// CheckOption is VIEW's WITH CHECK OPTION clause part | ||
// See https://dev.mysql.com/doc/refman/5.7/en/view-check-option.html | ||
type CheckOption int |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/CheckOption/ViewCheckOption/
ast/ddl.go
Outdated
CheckOptionCascaded | ||
) | ||
|
||
var CheckOptionType = map[CheckOption]string { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ditto
return v.Leave(newNode) | ||
} | ||
n = newNode.(*CreateViewStmt) | ||
node, ok := n.ViewName.Accept(v) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should we visit n.ddlNode
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Currently ddlNode is not able to visit, and it only contains a statement string.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
got it.
parser_test.go
Outdated
@@ -15,6 +15,7 @@ package parser | |||
|
|||
import ( | |||
"fmt" | |||
"github.com/pingcap/parser/model" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
move this line down out of the native packages
parser_test.go
Outdated
c.Assert(v.Cols[0], Equals, model.CIStr{"a", "a"}) | ||
c.Assert(v.Cols[1], Equals, model.CIStr{"b", "b"}) | ||
c.Assert(v.Cols[2], Equals, model.CIStr{"c", "c"}) | ||
c.Assert(strings.TrimSpace(v.Select.Text()), Equals, "select c,d,e from t") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we TrimSpace here?
If there are redundant spaces, we need to change parser.y to extract the exact sql text.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just done yet...
ast/ddl.go
Outdated
return "DEFINER" | ||
} | ||
|
||
// ViewCheckOption is VIEW's WITH CHECK OPTION clause part |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a .
at the end of this comment.
So as the other comment lines if they are complete sentences.
if $11 !=nil { | ||
x.CheckOption = $11.(ast.ViewCheckOption) | ||
endOffset := parser.startOffset(&yyS[yypt]) | ||
selStmt.SetText(strings.TrimSpace(parser.src[startOffset:endOffset])) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tested it locally. Seems we do not need this strings.TrimSpace
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I remove strings.TrimSpace, extra space would make test fail.
FAIL: parser_test.go:2220: testParserSuite.TestView
parser_test.go:2265:
c.Assert(v.Select.Text(), Equals, "select c,d,e from t")
... obtained string = "" +
... "select c,d,e from t \n" +
... " "
... expected string = "select c,d,e from t"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can use sql string for test:
src := `CREATE OR REPLACE ALGORITHM = UNDEFINED DEFINER = root@localhost
SQL SECURITY DEFINER
VIEW V(a,b,c) AS select c,d,e from t
WITH CASCADED CHECK OPTION;`
LGTM |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
As part of view feature implement, extract create view parameters to CreateViewStmt, ref pingcap#8416