Skip to content

Commit

Permalink
parser,ast: parse statement execution time optimizer hints (#7012)
Browse files Browse the repository at this point in the history
  • Loading branch information
tiancaiamao authored Jul 8, 2018
1 parent 3f574d9 commit 10151e1
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 25 deletions.
3 changes: 3 additions & 0 deletions ast/misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -822,6 +822,9 @@ type TableOptimizerHint struct {
// It allows only table name or alias (if table has an alias)
HintName model.CIStr
Tables []model.CIStr
// Statement Execution Time Optimizer Hints
// See https://dev.mysql.com/doc/refman/5.7/en/optimizer-hints.html#optimizer-hints-execution-time
MaxExecutionTime uint64
}

// Accept implements Node Accept interface.
Expand Down
1 change: 1 addition & 0 deletions parser/misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,7 @@ var tokenMap = map[string]int{
"MASTER": master,
"MAX": max,
"MAX_CONNECTIONS_PER_HOUR": maxConnectionsPerHour,
"MAX_EXECUTION_TIME": maxExecutionTime,
"MAX_QUERIES_PER_HOUR": maxQueriesPerHour,
"MAX_ROWS": maxRows,
"MAX_UPDATES_PER_HOUR": maxUpdatesPerHour,
Expand Down
55 changes: 30 additions & 25 deletions parser/parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -403,30 +403,31 @@ import (
yearType "YEAR"

/* The following tokens belong to NotKeywordToken. */
addDate "ADDDATE"
bitAnd "BIT_AND"
bitOr "BIT_OR"
bitXor "BIT_XOR"
cast "CAST"
copyKwd "COPY"
count "COUNT"
curTime "CURTIME"
dateAdd "DATE_ADD"
dateSub "DATE_SUB"
extract "EXTRACT"
getFormat "GET_FORMAT"
groupConcat "GROUP_CONCAT"
inplace "INPLACE"
min "MIN"
max "MAX"
now "NOW"
position "POSITION"
subDate "SUBDATE"
sum "SUM"
substring "SUBSTRING"
timestampAdd "TIMESTAMPADD"
timestampDiff "TIMESTAMPDIFF"
trim "TRIM"
addDate "ADDDATE"
bitAnd "BIT_AND"
bitOr "BIT_OR"
bitXor "BIT_XOR"
cast "CAST"
copyKwd "COPY"
count "COUNT"
curTime "CURTIME"
dateAdd "DATE_ADD"
dateSub "DATE_SUB"
extract "EXTRACT"
getFormat "GET_FORMAT"
groupConcat "GROUP_CONCAT"
inplace "INPLACE"
min "MIN"
max "MAX"
maxExecutionTime "MAX_EXECUTION_TIME"
now "NOW"
position "POSITION"
subDate "SUBDATE"
sum "SUM"
substring "SUBSTRING"
timestampAdd "TIMESTAMPADD"
timestampDiff "TIMESTAMPDIFF"
trim "TRIM"

/* The following tokens belong to TiDBKeyword. */
admin "ADMIN"
Expand Down Expand Up @@ -2760,7 +2761,7 @@ TiDBKeyword:

NotKeywordToken:
"ADDDATE" | "BIT_AND" | "BIT_OR" | "BIT_XOR" | "CAST" | "COPY" | "COUNT" | "CURTIME" | "DATE_ADD" | "DATE_SUB" | "EXTRACT" | "GET_FORMAT" | "GROUP_CONCAT"
| "INPLACE" |"MIN" | "MAX" | "NOW" | "POSITION" | "SUBDATE" | "SUBSTRING" | "SUM" | "TIMESTAMPADD" | "TIMESTAMPDIFF" | "TRIM"
| "INPLACE" |"MIN" | "MAX" | "MAX_EXECUTION_TIME" | "NOW" | "POSITION" | "SUBDATE" | "SUBSTRING" | "SUM" | "TIMESTAMPADD" | "TIMESTAMPDIFF" | "TRIM"

/************************************************************************************
*
Expand Down Expand Up @@ -4598,6 +4599,10 @@ TableOptimizerHintOpt:
{
$$ = &ast.TableOptimizerHint{HintName: model.NewCIStr($1), Tables: $3.([]model.CIStr)}
}
| maxExecutionTime '(' NUM ')'
{
$$ = &ast.TableOptimizerHint{HintName: model.NewCIStr($1), MaxExecutionTime: getUint64FromNUM($3)}
}

SelectStmtCalcFoundRows:
{
Expand Down
8 changes: 8 additions & 0 deletions parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1756,6 +1756,14 @@ func (s *testParserSuite) TestOptimizerHints(c *C) {
c.Assert(hints[1].HintName.L, Equals, "tidb_hj")
c.Assert(hints[1].Tables[0].L, Equals, "t3")
c.Assert(hints[1].Tables[1].L, Equals, "t4")

stmt, err = parser.Parse("SELECT /*+ MAX_EXECUTION_TIME(1000) */ * FROM t1 INNER JOIN t2 where t1.c1 = t2.c1", "", "")
c.Assert(err, IsNil)
selectStmt = stmt[0].(*ast.SelectStmt)
hints = selectStmt.TableHints
c.Assert(len(hints), Equals, 1)
c.Assert(hints[0].HintName.L, Equals, "max_execution_time")
c.Assert(hints[0].MaxExecutionTime, Equals, uint64(1000))
}

func (s *testParserSuite) TestType(c *C) {
Expand Down

0 comments on commit 10151e1

Please sign in to comment.