Skip to content

Commit

Permalink
refine table optimizer hint and make it more generic (#747)
Browse files Browse the repository at this point in the history
* refine table optimizer hint and make it more generic

Signed-off-by: Lonng <heng@lonng.org>

* implement the restore

Signed-off-by: Lonng <heng@lonng.org>

* address comment

Signed-off-by: Lonng <heng@lonng.org>

* address comment

Signed-off-by: Lonng <heng@lonng.org>

* fix typo

Signed-off-by: Lonng <heng@lonng.org>
  • Loading branch information
lonng committed Feb 18, 2020
1 parent d376009 commit 517beb2
Show file tree
Hide file tree
Showing 7 changed files with 647 additions and 567 deletions.
47 changes: 33 additions & 14 deletions ast/misc.go
Expand Up @@ -2198,17 +2198,31 @@ type TableOptimizerHint struct {
// Table hints has no schema info
// It allows only table name or alias (if table has an alias)
HintName model.CIStr
// QBName is the default effective query block of this hint.
QBName model.CIStr
Tables []HintTable
Indexes []model.CIStr
StoreType model.CIStr
// HintData is the payload of the hint. The actual type of this field
// is defined differently as according `HintName`. Define as following:
//
// 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 int64
QueryType model.CIStr
HintFlag bool
// - MAX_EXECUTION_TIME => uint64
// - MEMORY_QUOTA => int64
// - QUERY_TYPE => model.CIStr
//
// Time Range is used to hint the time range of inspection tables
// e.g: select /*+ time_range('','') */ * from information_schema.inspection_result.
// - TIME_RANGE => ast.HintTimeRange
// - READ_FROM_STORAGE => model.CIStr
// - USE_TOJA => bool
HintData interface{}
// QBName is the default effective query block of this hint.
QBName model.CIStr
Tables []HintTable
Indexes []model.CIStr
}

// HintTimeRange is the payload of `TIME_RANGE` hint
type HintTimeRange struct {
From string
To string
}

// HintTable is table in the hint. It may have query block info.
Expand Down Expand Up @@ -2252,7 +2266,7 @@ func (n *TableOptimizerHint) Restore(ctx *format.RestoreCtx) error {
// Hints with args except query block.
switch n.HintName.L {
case "max_execution_time":
ctx.WritePlainf("%d", n.MaxExecutionTime)
ctx.WritePlainf("%d", n.HintData.(uint64))
case "tidb_hj", "tidb_smj", "tidb_inlj", "hash_join", "sm_join", "inl_join":
for i, table := range n.Tables {
if i != 0 {
Expand All @@ -2270,17 +2284,17 @@ func (n *TableOptimizerHint) Restore(ctx *format.RestoreCtx) error {
ctx.WriteName(index.String())
}
case "use_toja", "enable_plan_cache":
if n.HintFlag {
if n.HintData.(bool) {
ctx.WritePlain("TRUE")
} else {
ctx.WritePlain("FALSE")
}
case "query_type":
ctx.WriteKeyWord(n.QueryType.String())
ctx.WriteKeyWord(n.HintData.(model.CIStr).String())
case "memory_quota":
ctx.WritePlainf("%d MB", n.MemoryQuota/1024/1024)
ctx.WritePlainf("%d MB", n.HintData.(int64)/1024/1024)
case "read_from_storage":
ctx.WriteKeyWord(n.StoreType.String())
ctx.WriteKeyWord(n.HintData.(model.CIStr).String())
for i, table := range n.Tables {
if i == 0 {
ctx.WritePlain("[")
Expand All @@ -2292,6 +2306,11 @@ func (n *TableOptimizerHint) Restore(ctx *format.RestoreCtx) error {
ctx.WritePlain(", ")
}
}
case "time_range":
hintData := n.HintData.(HintTimeRange)
ctx.WriteString(hintData.From)
ctx.WritePlain(", ")
ctx.WriteString(hintData.To)
}
ctx.WritePlain(")")
return nil
Expand Down
1 change: 1 addition & 0 deletions ast/misc_test.go
Expand Up @@ -255,6 +255,7 @@ func (ts *testMiscSuite) TestTableOptimizerHintRestore(c *C) {
{"READ_CONSISTENT_REPLICA(@sel1)", "READ_CONSISTENT_REPLICA(@`sel1`)"},
{"QB_NAME(sel1)", "QB_NAME(`sel1`)"},
{"READ_FROM_STORAGE(@sel TIFLASH[t1, t2])", "READ_FROM_STORAGE(@`sel` TIFLASH[`t1`, `t2`])"},
{"TIME_RANGE('2020-02-02 10:10:10','2020-02-02 11:10:10')", "TIME_RANGE('2020-02-02 10:10:10', '2020-02-02 11:10:10')"},
}
extractNodeFunc := func(node Node) Node {
return node.(*SelectStmt).TableHints[0]
Expand Down

0 comments on commit 517beb2

Please sign in to comment.