Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion ast/misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -2026,25 +2026,53 @@ type TableOptimizerHint struct {
// It allows only table name or alias (if table has an alias)
HintName model.CIStr
Tables []model.CIStr
Indexes []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
MemoryQuota uint64
QueryType model.CIStr
HintFlag bool
}

// Restore implements Node interface.
func (n *TableOptimizerHint) Restore(ctx *RestoreCtx) error {
ctx.WriteKeyWord(n.HintName.String())
// Hints without args.
switch n.HintName.L {
case "hash_agg", "stream_agg", "read_consistent_replica", "no_index_merge":
return nil
}
// Hints with args.
ctx.WritePlain("(")
switch n.HintName.L {
case "max_execution_time":
ctx.WritePlainf("%d", n.MaxExecutionTime)
case "tidb_hj", "tidb_smj", "tidb_inlj":
case "tidb_hj", "tidb_smj", "tidb_inlj", "hash_join", "sm_join", "inl_join":
for i, table := range n.Tables {
if i != 0 {
ctx.WritePlain(", ")
}
ctx.WriteName(table.String())
}
case "index", "use_index_merge":
if len(n.Tables) != 0 {
ctx.WriteName(n.Tables[0].String())
}
for _, index := range n.Indexes {
ctx.WritePlain(", ")
ctx.WriteName(index.String())
}
case "use_toja", "enable_plan_cache":
if n.HintFlag {
ctx.WritePlain("TRUE")
} else {
ctx.WritePlain("FALSE")
}
case "query_type":
ctx.WriteKeyWord(n.QueryType.String())
case "memory_quota":
ctx.WritePlainf("%d M", n.MemoryQuota)
}
ctx.WritePlain(")")
return nil
Expand Down
14 changes: 14 additions & 0 deletions ast/misc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,12 +205,26 @@ func (ts *testMiscSuite) TestUserSpec(c *C) {

func (ts *testMiscSuite) TestTableOptimizerHintRestore(c *C) {
testCases := []NodeRestoreTestCase{
{"INDEX(t1, c1)", "INDEX(`t1`, `c1`)"},
{"TIDB_SMJ(`t1`)", "TIDB_SMJ(`t1`)"},
{"TIDB_SMJ(t1)", "TIDB_SMJ(`t1`)"},
{"TIDB_SMJ(t1,t2)", "TIDB_SMJ(`t1`, `t2`)"},
{"TIDB_INLJ(t1,t2)", "TIDB_INLJ(`t1`, `t2`)"},
{"TIDB_HJ(t1,t2)", "TIDB_HJ(`t1`, `t2`)"},
{"SM_JOIN(t1,t2)", "SM_JOIN(`t1`, `t2`)"},
{"INL_JOIN(t1,t2)", "INL_JOIN(`t1`, `t2`)"},
{"HASH_JOIN(t1,t2)", "HASH_JOIN(`t1`, `t2`)"},
{"MAX_EXECUTION_TIME(3000)", "MAX_EXECUTION_TIME(3000)"},
{"USE_INDEX_MERGE(t1, c1)", "USE_INDEX_MERGE(`t1`, `c1`)"},
{"USE_TOJA(TRUE)", "USE_TOJA(TRUE)"},
{"USE_TOJA(FALSE)", "USE_TOJA(FALSE)"},
{"QUERY_TYPE(OLAP)", "QUERY_TYPE(OLAP)"},
{"QUERY_TYPE(OLTP)", "QUERY_TYPE(OLTP)"},
{"MEMORY_QUOTA(1 G)", "MEMORY_QUOTA(1024 M)"},
{"HASH_AGG", "HASH_AGG"},
{"STREAM_AGG", "STREAM_AGG"},
{"NO_INDEX_MERGE", "NO_INDEX_MERGE"},
{"READ_CONSISTENT_REPLICA", "READ_CONSISTENT_REPLICA"},
}
extractNodeFunc := func(node Node) Node {
return node.(*SelectStmt).TableHints[0]
Expand Down
34 changes: 25 additions & 9 deletions misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ var tokenMap = map[string]int{
"DYNAMIC": dynamic,
"ELSE": elseKwd,
"ENABLE": enable,
"ENABLE_PLAN_CACHE": hintEnablePlanCache,
"ENCLOSED": enclosed,
"ENCRYPTION": encryption,
"END": end,
Expand Down Expand Up @@ -294,6 +295,8 @@ var tokenMap = map[string]int{
"GROUP": group,
"GROUP_CONCAT": groupConcat,
"HASH": hash,
"HASH_AGG": hintHASHAGG,
"HASH_JOIN": hintHJ,
"HAVING": having,
"HIGH_PRIORITY": highPriority,
"HISTORY": history,
Expand All @@ -309,6 +312,7 @@ var tokenMap = map[string]int{
"INDEX": index,
"INDEXES": indexes,
"INFILE": infile,
"INL_JOIN": hintINLJ,
"INNER": inner,
"INPLACE": inplace,
"INSTANT": instant,
Expand All @@ -330,6 +334,7 @@ var tokenMap = map[string]int{
"IS": is,
"ISSUER": issuer,
"ISOLATION": isolation,
"USE_TOJA": hintUseToja,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is toja?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Convert in subquery into inner join and aggregation. Seems like this one.
The name is advised by product team.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about adding a comment for this abbreviation.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, and I will contact PM team also.

"JOBS": jobs,
"JOB": job,
"JOIN": join,
Expand Down Expand Up @@ -371,6 +376,7 @@ var tokenMap = map[string]int{
"MEDIUMINT": mediumIntType,
"MEDIUMTEXT": mediumtextType,
"MEMORY": memory,
"MEMORY_QUOTA": hintMemoryQuota,
"MERGE": merge,
"MICROSECOND": microsecond,
"MIN": min,
Expand All @@ -388,6 +394,7 @@ var tokenMap = map[string]int{
"NEVER": never,
"NEXT_ROW_ID": next_row_id,
"NO": no,
"NO_INDEX_MERGE": hintNoIndexMerge,
"NO_WRITE_TO_BINLOG": noWriteToBinLog,
"NODE_ID": nodeID,
"NODE_STATE": nodeState,
Expand All @@ -401,6 +408,8 @@ var tokenMap = map[string]int{
"NCHAR": ncharType,
"NVARCHAR": nvarcharType,
"OFFSET": offset,
"OLAP": hintOLAP,
"OLTP": hintOLTP,
"ON": on,
"ONLY": only,
"OPTIMISTIC": optimistic,
Expand Down Expand Up @@ -432,13 +441,15 @@ var tokenMap = map[string]int{
"PUMP": pump,
"QUARTER": quarter,
"QUERY": query,
"QUERY_TYPE": hintQueryType,
"QUERIES": queries,
"QUICK": quick,
"SHARD_ROW_ID_BITS": shardRowIDBits,
"PRE_SPLIT_REGIONS": preSplitRegions,
"RANGE": rangeKwd,
"RECOVER": recover,
"READ": read,
"READ_CONSISTENT_REPLICA": hintReadConsistentReplica,
"REAL": realType,
"RECENT": recent,
"REDUNDANT": redundant,
Expand Down Expand Up @@ -485,6 +496,7 @@ var tokenMap = map[string]int{
"SIMPLE": simple,
"SLAVE": slave,
"SLOW": slow,
"SM_JOIN": hintSMJ,
"SMALLINT": smallIntType,
"SNAPSHOT": snapshot,
"SOME": some,
Expand Down Expand Up @@ -519,6 +531,7 @@ var tokenMap = map[string]int{
"STDDEV_SAMP": stddevSamp,
"STORED": stored,
"STRAIGHT_JOIN": straightJoin,
"STREAM_AGG": hintSTREAMAGG,
"SUBDATE": subDate,
"SUBJECT": subject,
"SUBPARTITION": subpartition,
Expand All @@ -537,11 +550,9 @@ var tokenMap = map[string]int{
"THAN": than,
"THEN": then,
"TIDB": tidb,
"TIDB_HJ": tidbHJ,
"TIDB_INLJ": tidbINLJ,
"TIDB_SMJ": tidbSMJ,
"TIDB_HASHAGG": tidbHASHAGG,
"TIDB_STREAMAGG": tidbSTREAMAGG,
"TIDB_HJ": hintHJ,
"TIDB_INLJ": hintINLJ,
"TIDB_SMJ": hintSMJ,
"TIME": timeType,
"TIMESTAMP": timestampType,
"TIMESTAMPADD": timestampAdd,
Expand Down Expand Up @@ -580,6 +591,8 @@ var tokenMap = map[string]int{
"UPDATE": update,
"USAGE": usage,
"USE": use,
"USE_INDEX_MERGE": hintUseIndexMerge,
"USE_PLAN_CACHE": hintUsePlanCache,
"USER": user,
"USING": using,
"UTC_DATE": utcDate,
Expand Down Expand Up @@ -673,10 +686,13 @@ var windowFuncTokenMap = map[string]int{

// aliases are strings directly map to another string and use the same token.
var aliases = map[string]string{
"SCHEMA": "DATABASE",
"SCHEMAS": "DATABASES",
"DEC": "DECIMAL",
"SUBSTR": "SUBSTRING",
"SCHEMA": "DATABASE",
"SCHEMAS": "DATABASES",
"DEC": "DECIMAL",
"SUBSTR": "SUBSTRING",
"TIDB_HJ": "HASH_JOIN",
"TIDB_INLJ": "INL_JOIN",
"TIDB_SMJ": "SM_JOIN",
}

func (s *Scanner) isTokenIdentifier(lit string, offset int) int {
Expand Down
Loading