Skip to content

Commit

Permalink
Refactor parser: Avoid some assertion for StmtNode (pingcap#4705)
Browse files Browse the repository at this point in the history
  • Loading branch information
shenli authored and XuHuaiyu committed Oct 9, 2017
1 parent 56ddb40 commit 0d0685a
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 71 deletions.
50 changes: 50 additions & 0 deletions parser/bench_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright 2017 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.

package parser

import (
"testing"
)

func BenchmarkSysbenchSelect(b *testing.B) {
parser := New()
sql := "SELECT pad FROM sbtest1 WHERE id=1;"
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err := parser.Parse(sql, "", "")
if err != nil {
b.Fatal(err)
}
}
b.ReportAllocs()
}

func BenchmarkParse(b *testing.B) {
var table = []string{
"insert into t values (1), (2), (3)",
"insert into t values (4), (5), (6), (7)",
"select c from t where c > 2",
}
parser := New()
b.ResetTimer()
for i := 0; i < b.N; i++ {
for _, v := range table {
_, err := parser.Parse(v, "", "")
if err != nil {
b.Failed()
}
}
}
b.ReportAllocs()
}
110 changes: 58 additions & 52 deletions parser/parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import (
item interface{}
ident string
expr ast.ExprNode
statement ast.StmtNode
}

%token <ident>
Expand Down Expand Up @@ -437,21 +438,61 @@ import (
DefaultValueExpr "DefaultValueExpr(Now or Signed Literal)"
NowSymOptionFraction "NowSym with optional fraction part"

%type <item>
%type <statement>
AdminStmt "Check table statement or show ddl statement"
AlterTableStmt "Alter table statement"
AlterTableSpec "Alter table specification"
AlterTableSpecList "Alter table specification list"
AlterUserStmt "Alter user statement"
AnalyzeTableStmt "Analyze table statement"
BeginTransactionStmt "BEGIN TRANSACTION statement"
BinlogStmt "Binlog base64 statement"
CommitStmt "COMMIT statement"
CreateTableStmt "CREATE TABLE statement"
CreateUserStmt "CREATE User statement"
CreateDatabaseStmt "Create Database Statement"
CreateIndexStmt "CREATE INDEX statement"
DoStmt "Do statement"
DropDatabaseStmt "DROP DATABASE statement"
DropIndexStmt "DROP INDEX statement"
DropStatsStmt "DROP STATS statement"
DropTableStmt "DROP TABLE statement"
DropUserStmt "DROP USER"
DropViewStmt "DROP VIEW statement"
DeallocateStmt "Deallocate prepared statement"
DeleteFromStmt "DELETE FROM statement"
EmptyStmt "empty statement"
ExecuteStmt "Execute statement"
ExplainStmt "EXPLAIN statement"
FlushStmt "Flush statement"
GrantStmt "Grant statement"
InsertIntoStmt "INSERT INTO statement"
KillStmt "Kill statement"
LoadDataStmt "Load data statement"
LockTablesStmt "Lock tables statement"
PreparedStmt "PreparedStmt"
SelectStmt "SELECT statement"
RenameTableStmt "rename table statement"
ReplaceIntoStmt "REPLACE INTO statement"
RevokeStmt "Revoke statement"
RollbackStmt "ROLLBACK statement"
SetStmt "Set variable statement"
ShowStmt "Show engines/databases/tables/columns/warnings/status statement"
Statement "statement"
ExplainableStmt "explainable statement"
TruncateTableStmt "TRANSACTION TABLE statement"
UnlockTablesStmt "Unlock tables statement"
UpdateStmt "UPDATE statement"
UnionStmt "Union select state ment"
UseStmt "USE statement"

%type <item>
AlterTableSpec "Alter table specification"
AlterTableSpecList "Alter table specification list"
AnyOrAll "Any or All for subquery"
Assignment "assignment"
AssignmentList "assignment list"
AssignmentListOpt "assignment list opt"
AuthOption "User auth option"
AuthString "Password string value"
BeginTransactionStmt "BEGIN TRANSACTION statement"
BinlogStmt "Binlog base64 statement"
OptionalBraces "optional braces"
CastType "Cast function target type"
CharsetName "Character set name"
Expand All @@ -462,7 +503,6 @@ import (
ColumnNameListOptWithBrackets "column name list opt with brackets"
ColumnSetValue "insert statement set value by column name"
ColumnSetValueList "insert statement set value by column name list"
CommitStmt "COMMIT statement"
CompareOp "Compare opcode"
ColumnOption "column definition option"
ColumnOptionList "column definition option list"
Expand All @@ -471,35 +511,19 @@ import (
Constraint "table constraint"
ConstraintElem "table constraint element"
ConstraintKeywordOpt "Constraint Keyword or empty"
CreateDatabaseStmt "Create Database Statement"
CreateIndexStmt "CREATE INDEX statement"
CreateIndexStmtUnique "CREATE INDEX optional UNIQUE clause"
DatabaseOption "CREATE Database specification"
DatabaseOptionList "CREATE Database specification list"
DatabaseOptionListOpt "CREATE Database specification list opt"
CreateTableStmt "CREATE TABLE statement"
CreateUserStmt "CREATE User statement"
DBName "Database Name"
DeallocateStmt "Deallocate prepared statement"
DeleteFromStmt "DELETE FROM statement"
DistinctOpt "Explicit distinct option"
DefaultFalseDistinctOpt "Distinct option which defaults to false"
DefaultTrueDistinctOpt "Distinct option which defaults to true"
BuggyDefaultFalseDistinctOpt "Distinct option which accepts DISTINCT ALL and defaults to false"
DoStmt "Do statement"
DropDatabaseStmt "DROP DATABASE statement"
DropIndexStmt "DROP INDEX statement"
DropStatsStmt "DROP STATS statement"
DropTableStmt "DROP TABLE statement"
DropUserStmt "DROP USER"
DropViewStmt "DROP VIEW statement"
EmptyStmt "empty statement"
Enclosed "Enclosed by"
EqOpt "= or empty"
EscapedTableRef "escaped table reference"
Escaped "Escaped by"
ExecuteStmt "Execute statement"
ExplainStmt "EXPLAIN statement"
ExpressionList "expression list"
ExpressionListOpt "expression list opt"
FuncDatetimePrecListOpt "Function datetime precision list opt"
Expand All @@ -510,12 +534,10 @@ import (
FieldAsName "Field alias name"
FieldAsNameOpt "Field alias name opt"
FieldList "field expression list"
FlushStmt "Flush statement"
FlushOption "Flush option"
TableRefsClause "Table references clause"
FuncDatetimePrec "Function datetime precision"
GlobalScope "The scope of variable"
GrantStmt "Grant statement"
GroupByClause "GROUP BY clause"
HashString "Hashed string"
HavingClause "HAVING clause"
Expand All @@ -535,20 +557,16 @@ import (
IndexOptionList "Index Option List or empty"
IndexType "index type"
IndexTypeOpt "Optional index type"
InsertIntoStmt "INSERT INTO statement"
InsertValues "Rest part of INSERT/REPLACE INTO statement"
JoinTable "join table"
JoinType "join type"
KillStmt "Kill statement"
KillOrKillTiDB "Kill or Kill TiDB"
LikeEscapeOpt "like escape option"
LimitClause "LIMIT clause"
LimitOption "Limit option could be integer or parameter marker."
Lines "Lines clause"
LinesTerminated "Lines terminated by"
LoadDataStmt "Load data statement"
LocalOpt "Local opt"
LockTablesStmt "Lock tables statement"
LockClause "Alter table lock clause"
LowPriorityOptional "LOW_PRIORITY or empty"
NumLiteral "Num/Int/Float/Decimal Literal"
Expand All @@ -571,7 +589,6 @@ import (
PartDefStorageOpt "ENGINE = xxx or empty"
PasswordOpt "Password option"
ColumnPosition "Column position [First|After ColumnName]"
PreparedStmt "PreparedStmt"
PrepareSQL "Prepare statement sql string"
Priority "insert statement priority"
PrivElem "Privilege element"
Expand All @@ -582,34 +599,25 @@ import (
OnDeleteOpt "optional ON DELETE clause"
OnUpdateOpt "optional ON UPDATE clause"
ReferOpt "reference option"
RenameTableStmt "rename table statement"
ReplaceIntoStmt "REPLACE INTO statement"
ReplacePriority "replace statement priority"
RevokeStmt "Revoke statement"
RollbackStmt "ROLLBACK statement"
RowFormat "Row format option"
RowValue "Row value"
SelectLockOpt "FOR UPDATE or LOCK IN SHARE MODE,"
SelectStmt "SELECT statement"
SelectStmtCalcFoundRows "SELECT statement optional SQL_CALC_FOUND_ROWS"
SelectStmtSQLCache "SELECT statement optional SQL_CAHCE/SQL_NO_CACHE"
SelectStmtFieldList "SELECT statement field list"
SelectStmtLimit "SELECT statement optional LIMIT clause"
SelectStmtOpts "Select statement options"
SelectStmtGroup "SELECT statement optional GROUP BY clause"
SetStmt "Set variable statement"
ShowStmt "Show engines/databases/tables/columns/warnings/status statement"
ShowTargetFilterable "Show target that can be filtered by WHERE or LIKE"
ShowDatabaseNameOpt "Show tables/columns statement database name option"
ShowTableAliasOpt "Show table alias option"
ShowLikeOrWhereOpt "Show like or where clause option"
Starting "Starting by"
Statement "statement"
StatementList "statement list"
StatsPersistentVal "stats_persistent value"
StringName "string literal or identifier"
StringList "string list"
ExplainableStmt "explainable statement"
Symbol "Constraint Symbol"
TableAsName "table alias name"
TableAsNameOpt "table alias name optional"
Expand All @@ -632,19 +640,14 @@ import (
TransactionChar "Transaction characteristic"
TransactionChars "Transaction characteristic list"
TrimDirection "Trim string direction"
TruncateTableStmt "TRANSACTION TABLE statement"
UnionOpt "Union Option(empty/ALL/DISTINCT)"
UnionStmt "Union select state ment"
UnionClauseList "Union select clause list"
UnionSelect "Union (select) item"
UnlockTablesStmt "Unlock tables statement"
UpdateStmt "UPDATE statement"
Username "Username"
UsernameList "UsernameList"
UserSpec "Username and auth option"
UserSpecList "Username and auth option list"
UserVariableList "User defined variable name list"
UseStmt "USE statement"
Values "values"
ValuesList "values list"
ValuesOpt "values optional"
Expand Down Expand Up @@ -1792,14 +1795,14 @@ ExplainStmt:
| ExplainSym ExplainableStmt
{
$$ = &ast.ExplainStmt{
Stmt: $2.(ast.StmtNode),
Stmt: $2,
Format: "row",
}
}
| ExplainSym "FORMAT" "=" stringLit ExplainableStmt
{
$$ = &ast.ExplainStmt{
Stmt: $5.(ast.StmtNode),
Stmt: $5,
Format: $4,
}
}
Expand Down Expand Up @@ -4158,12 +4161,15 @@ UnionClauseList:

UnionSelect:
SelectStmt
{
$$ = $1.(interface{})
}
| '(' SelectStmt ')'
{
st := $2.(*ast.SelectStmt)
endOffset := parser.endOffset(&yyS[yypt])
parser.setLastSelectFieldText(st, endOffset)
$$ = st
$$ = $2
}

UnionOpt:
Expand Down Expand Up @@ -4810,15 +4816,15 @@ Statement:
| UnionStmt
| SetStmt
| ShowStmt
| TruncateTableStmt
| UpdateStmt
| UseStmt
| SubSelect
{
// `(select 1)`; is a valid select statement
// TODO: This is used to fix issue #320. There may be a better solution.
$$ = $1.(*ast.SubqueryExpr).Query
$$ = $1.(*ast.SubqueryExpr).Query.(ast.StmtNode)
}
| TruncateTableStmt
| UpdateStmt
| UseStmt
| UnlockTablesStmt
| LockTablesStmt

Expand All @@ -4834,7 +4840,7 @@ StatementList:
Statement
{
if $1 != nil {
s := $1.(ast.StmtNode)
s := $1
if lexer, ok := yylex.(stmtTexter); ok {
s.SetText(lexer.stmtText())
}
Expand All @@ -4844,7 +4850,7 @@ StatementList:
| StatementList ';' Statement
{
if $3 != nil {
s := $3.(ast.StmtNode)
s := $3
if lexer, ok := yylex.(stmtTexter); ok {
s.SetText(lexer.stmtText())
}
Expand Down
19 changes: 0 additions & 19 deletions parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1773,25 +1773,6 @@ func (s *testParserSuite) TestInsertStatementMemoryAllocation(c *C) {
c.Assert(int(newStats.TotalAlloc-oldStats.TotalAlloc), Less, 1024*500)
}

func BenchmarkParse(b *testing.B) {
var table = []string{
"insert into t values (1), (2), (3)",
"insert into t values (4), (5), (6), (7)",
"select c from t where c > 2",
}
parser := New()
b.ResetTimer()
for i := 0; i < b.N; i++ {
for _, v := range table {
_, err := parser.Parse(v, "", "")
if err != nil {
b.Failed()
}
}
}
b.ReportAllocs()
}

func (s *testParserSuite) TestExplain(c *C) {
defer testleak.AfterTest(c)()
table := []testCase{
Expand Down

0 comments on commit 0d0685a

Please sign in to comment.