Skip to content

Commit

Permalink
Merge branch 'master' into exp
Browse files Browse the repository at this point in the history
  • Loading branch information
jacklightChen authored Sep 19, 2019
2 parents d8e5156 + f9724db commit 0db84d7
Show file tree
Hide file tree
Showing 46 changed files with 1,216 additions and 305 deletions.
29 changes: 29 additions & 0 deletions cmd/explaintest/r/explain-non-select-stmt.result
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use test;
drop table if exists t;
create table t(a bigint, b bigint);
explain insert into t values(1, 1);
id count task operator info
Insert_1 N/A root N/A
explain insert into t select * from t;
id count task operator info
Insert_1 N/A root N/A
└─TableReader_7 10000.00 root data:TableScan_6
└─TableScan_6 10000.00 cop table:t, range:[-inf,+inf], keep order:false, stats:pseudo
explain delete from t where a > 100;
id count task operator info
Delete_3 N/A root N/A
└─TableReader_6 3333.33 root data:Selection_5
└─Selection_5 3333.33 cop gt(Column#1, 100)
└─TableScan_4 10000.00 cop table:t, range:[-inf,+inf], keep order:false, stats:pseudo
explain update t set b = 100 where a = 200;
id count task operator info
Update_3 N/A root N/A
└─TableReader_6 10.00 root data:Selection_5
└─Selection_5 10.00 cop eq(Column#1, 200)
└─TableScan_4 10000.00 cop table:t, range:[-inf,+inf], keep order:false, stats:pseudo
explain replace into t select a, 100 from t;
id count task operator info
Insert_1 N/A root N/A
└─Projection_5 10000.00 root Column#3, 100
└─TableReader_7 10000.00 root data:TableScan_6
└─TableScan_6 10000.00 cop table:t, range:[-inf,+inf], keep order:false, stats:pseudo
34 changes: 29 additions & 5 deletions cmd/explaintest/r/explain_easy.result
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,14 @@ HashLeftJoin_19 4166.67 root left outer join, inner:TableReader_32, equal:[eq(Co
└─TableScan_30 10000.00 cop table:t2, range:[-inf,+inf], keep order:false, stats:pseudo
explain update t1 set t1.c2 = 2 where t1.c1 = 1;
id count task operator info
Point_Get_1 1.00 root table:t1, handle:1
Update_2 N/A root N/A
└─Point_Get_1 1.00 root table:t1, handle:1
explain delete from t1 where t1.c2 = 1;
id count task operator info
IndexLookUp_9 10.00 root
├─IndexScan_7 10.00 cop table:t1, index:c2, range:[1,1], keep order:false, stats:pseudo
└─TableScan_8 10.00 cop table:t1, keep order:false, stats:pseudo
Delete_3 N/A root N/A
└─IndexLookUp_9 10.00 root
├─IndexScan_7 10.00 cop table:t1, index:c2, range:[1,1], keep order:false, stats:pseudo
└─TableScan_8 10.00 cop table:t1, keep order:false, stats:pseudo
explain select count(b.c2) from t1 a, t2 b where a.c1 = b.c2 group by a.c1;
id count task operator info
Projection_11 9990.00 root cast(Column#9)
Expand Down Expand Up @@ -658,7 +660,8 @@ begin;
insert into t values (1, 1);
explain update t set j = -j where i = 1 and j = 1;
id count task operator info
Point_Get_1 1.00 root table:t, index:i j
Update_2 N/A root N/A
└─Point_Get_1 1.00 root table:t, index:i j
rollback;
drop table if exists t;
create table t(a int);
Expand Down Expand Up @@ -722,3 +725,24 @@ Projection_8 2666.67 root Column#1, Column#2
└─Selection_12 3333.33 cop gt(Column#1, 10)
└─TableScan_11 10000.00 cop table:t, range:[-inf,+inf], keep order:false, stats:pseudo
drop table if exists t;
create table t(a int, b int);
explain format="dot" select * from t where a < 2;
dot contents

digraph TableReader_7 {
subgraph cluster7{
node [style=filled, color=lightgrey]
color=black
label = "root"
"TableReader_7"
}
subgraph cluster6{
node [style=filled, color=lightgrey]
color=black
label = "cop"
"Selection_6" -> "TableScan_5"
}
"TableReader_7" -> "Selection_6"
}

drop table if exists t;
10 changes: 6 additions & 4 deletions cmd/explaintest/r/explain_easy_stats.result
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,14 @@ HashLeftJoin_19 2481.25 root left outer join, inner:TableReader_32, equal:[eq(Co
└─TableScan_30 1985.00 cop table:t2, range:[-inf,+inf], keep order:false
explain update t1 set t1.c2 = 2 where t1.c1 = 1;
id count task operator info
Point_Get_1 1.00 root table:t1, handle:1
Update_2 N/A root N/A
└─Point_Get_1 1.00 root table:t1, handle:1
explain delete from t1 where t1.c2 = 1;
id count task operator info
IndexLookUp_9 0.00 root
├─IndexScan_7 0.00 cop table:t1, index:c2, range:[1,1], keep order:false
└─TableScan_8 0.00 cop table:t1, keep order:false
Delete_3 N/A root N/A
└─IndexLookUp_9 0.00 root
├─IndexScan_7 0.00 cop table:t1, index:c2, range:[1,1], keep order:false
└─TableScan_8 0.00 cop table:t1, keep order:false
explain select count(b.c2) from t1 a, t2 b where a.c1 = b.c2 group by a.c1;
id count task operator info
Projection_11 1985.00 root cast(Column#9)
Expand Down
33 changes: 33 additions & 0 deletions cmd/explaintest/r/explain_indexmerge.result
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,36 @@ IndexMerge_17 0.00 root
├─IndexScan_14 9.00 cop table:t, index:d, range:[-inf,10), keep order:false
└─Selection_16 0.00 cop lt(Column#6, 10), or(lt(Column#2, 10000), lt(Column#3, 10000))
└─TableScan_15 18.00 cop table:t, keep order:false
explain format="dot" select * from t where (a < 50 or b < 50) and f > 100;
dot contents

digraph IndexMerge_12 {
subgraph cluster12{
node [style=filled, color=lightgrey]
color=black
label = "root"
"IndexMerge_12"
}
subgraph cluster8{
node [style=filled, color=lightgrey]
color=black
label = "cop"
"TableScan_8"
}
subgraph cluster9{
node [style=filled, color=lightgrey]
color=black
label = "cop"
"IndexScan_9"
}
subgraph cluster11{
node [style=filled, color=lightgrey]
color=black
label = "cop"
"Selection_11" -> "TableScan_10"
}
"IndexMerge_12" -> "TableScan_8"
"IndexMerge_12" -> "IndexScan_9"
"IndexMerge_12" -> "Selection_11"
}

8 changes: 8 additions & 0 deletions cmd/explaintest/t/explain-non-select-stmt.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use test;
drop table if exists t;
create table t(a bigint, b bigint);
explain insert into t values(1, 1);
explain insert into t select * from t;
explain delete from t where a > 100;
explain update t set b = 100 where a = 200;
explain replace into t select a, 100 from t;
4 changes: 4 additions & 0 deletions cmd/explaintest/t/explain_easy.test
Original file line number Diff line number Diff line change
Expand Up @@ -178,3 +178,7 @@ drop table if exists t;
create table t(a int, b int);
explain select a, b from (select a, b, avg(b) over (partition by a)as avg_b from t) as tt where a > 10 and b < 10 and a > avg_b;
drop table if exists t;

create table t(a int, b int);
explain format="dot" select * from t where a < 2;
drop table if exists t;
1 change: 1 addition & 0 deletions cmd/explaintest/t/explain_indexmerge.test
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ explain select * from t where b < 50 or c < 50;
explain select * from t where b < 50 or c < 5000000;
explain select * from t where a < 50 or b < 50 or c < 50;
explain select * from t where (b < 10000 or c < 10000) and (a < 10 or d < 10) and f < 10;
explain format="dot" select * from t where (a < 50 or b < 50) and f > 100;
2 changes: 1 addition & 1 deletion domain/global_vars_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func checkEnableStmtSummary(rows []chunk.Row, fields []*ast.ResultField) {
}
}

stmtsummary.OnEnableStmtSummaryModified(sVal)
stmtsummary.StmtSummaryByDigestMap.SetEnabled(sVal, false)
break
}
}
Expand Down
8 changes: 4 additions & 4 deletions domain/global_vars_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
package domain

import (
"sync/atomic"
"time"

. "github.com/pingcap/check"
Expand All @@ -26,6 +25,7 @@ import (
"github.com/pingcap/tidb/store/mockstore"
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/util/chunk"
"github.com/pingcap/tidb/util/stmtsummary"
"github.com/pingcap/tidb/util/testleak"
)

Expand Down Expand Up @@ -127,18 +127,18 @@ func (gvcSuite *testGVCSuite) TestCheckEnableStmtSummary(c *C) {
Collate: charset.CollationBin,
}

atomic.StoreInt32(&variable.EnableStmtSummary, 0)
stmtsummary.StmtSummaryByDigestMap.SetEnabled("0", false)
ck := chunk.NewChunkWithCapacity([]*types.FieldType{ft, ft1}, 1024)
ck.AppendString(0, variable.TiDBEnableStmtSummary)
ck.AppendString(1, "1")
row := ck.GetRow(0)
gvc.Update([]chunk.Row{row}, []*ast.ResultField{rf, rf1})
c.Assert(atomic.LoadInt32(&variable.EnableStmtSummary), Equals, int32(1))
c.Assert(stmtsummary.StmtSummaryByDigestMap.Enabled(), Equals, true)

ck = chunk.NewChunkWithCapacity([]*types.FieldType{ft, ft1}, 1024)
ck.AppendString(0, variable.TiDBEnableStmtSummary)
ck.AppendString(1, "0")
row = ck.GetRow(0)
gvc.Update([]chunk.Row{row}, []*ast.ResultField{rf, rf1})
c.Assert(atomic.LoadInt32(&variable.EnableStmtSummary), Equals, int32(0))
c.Assert(stmtsummary.StmtSummaryByDigestMap.Enabled(), Equals, false)
}
2 changes: 1 addition & 1 deletion executor/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -816,7 +816,7 @@ func (a *ExecStmt) LogSlowQuery(txnTS uint64, succ bool) {
// SummaryStmt collects statements for performance_schema.events_statements_summary_by_digest
func (a *ExecStmt) SummaryStmt() {
sessVars := a.Ctx.GetSessionVars()
if sessVars.InRestrictedSQL || atomic.LoadInt32(&variable.EnableStmtSummary) == 0 {
if sessVars.InRestrictedSQL || !stmtsummary.StmtSummaryByDigestMap.Enabled() {
return
}
stmtCtx := sessVars.StmtCtx
Expand Down
2 changes: 1 addition & 1 deletion executor/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -833,7 +833,7 @@ func (b *executorBuilder) buildExplain(v *plannercore.Explain) Executor {
}
if v.Analyze {
b.ctx.GetSessionVars().StmtCtx.RuntimeStatsColl = execdetails.NewRuntimeStatsColl()
explainExec.analyzeExec = b.build(v.ExecPlan)
explainExec.analyzeExec = b.build(v.TargetPlan)
}
return explainExec
}
Expand Down
44 changes: 24 additions & 20 deletions executor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -1385,61 +1385,65 @@ func extractStmtHintsFromStmtNode(stmtNode ast.StmtNode) []*ast.TableOptimizerHi
}

func handleStmtHints(hints []*ast.TableOptimizerHint) (stmtHints stmtctx.StmtHints, warns []error) {
var memoryQuotaHintList, noIndexMergeHintList, useToJAHintList, readReplicaHintList []*ast.TableOptimizerHint
if len(hints) == 0 {
return
}
var memoryQuotaHint, useToJAHint *ast.TableOptimizerHint
var memoryQuotaHintCnt, useToJAHintCnt, noIndexMergeHintCnt, readReplicaHintCnt int
for _, hint := range hints {
switch hint.HintName.L {
case "memory_quota":
memoryQuotaHintList = append(memoryQuotaHintList, hint)
case "no_index_merge":
noIndexMergeHintList = append(noIndexMergeHintList, hint)
memoryQuotaHint = hint
memoryQuotaHintCnt++
case "use_toja":
useToJAHintList = append(useToJAHintList, hint)
useToJAHint = hint
useToJAHintCnt++
case "no_index_merge":
noIndexMergeHintCnt++
case "read_consistent_replica":
readReplicaHintList = append(readReplicaHintList, hint)
readReplicaHintCnt++
}
}
// Handle MEMORY_QUOTA
if len(memoryQuotaHintList) != 0 {
if len(memoryQuotaHintList) > 1 {
if memoryQuotaHintCnt != 0 {
if memoryQuotaHintCnt > 1 {
warn := errors.New("There are multiple MEMORY_QUOTA hints, only the last one will take effect")
warns = append(warns, warn)
}
hint := memoryQuotaHintList[len(memoryQuotaHintList)-1]
// Executor use MemoryQuota <= 0 to indicate no memory limit, here use < 0 to handle hint syntax error.
if hint.MemoryQuota < 0 {
if memoryQuotaHint.MemoryQuota < 0 {
warn := errors.New("The use of MEMORY_QUOTA hint is invalid, valid usage: MEMORY_QUOTA(10 MB) or MEMORY_QUOTA(10 GB)")
warns = append(warns, warn)
} else {
stmtHints.HasMemQuotaHint = true
stmtHints.MemQuotaQuery = hint.MemoryQuota
if hint.MemoryQuota == 0 {
stmtHints.MemQuotaQuery = memoryQuotaHint.MemoryQuota
if memoryQuotaHint.MemoryQuota == 0 {
warn := errors.New("Setting the MEMORY_QUOTA to 0 means no memory limit")
warns = append(warns, warn)
}
}
}
// Handle USE_TOJA
if len(useToJAHintList) != 0 {
if len(useToJAHintList) > 1 {
if useToJAHintCnt != 0 {
if useToJAHintCnt > 1 {
warn := errors.New("There are multiple USE_TOJA hints, only the last one will take effect")
warns = append(warns, warn)
}
hint := useToJAHintList[len(useToJAHintList)-1]
stmtHints.HasAllowInSubqToJoinAndAggHint = true
stmtHints.AllowInSubqToJoinAndAgg = hint.HintFlag
stmtHints.AllowInSubqToJoinAndAgg = useToJAHint.HintFlag
}
// Handle NO_INDEX_MERGE
if len(noIndexMergeHintList) != 0 {
if len(noIndexMergeHintList) > 1 {
if noIndexMergeHintCnt != 0 {
if noIndexMergeHintCnt > 1 {
warn := errors.New("There are multiple NO_INDEX_MERGE hints, only the last one will take effect")
warns = append(warns, warn)
}
stmtHints.HasEnableIndexMergeHint = true
stmtHints.EnableIndexMerge = false
}
// Handle READ_CONSISTENT_REPLICA
if len(readReplicaHintList) != 0 {
if len(readReplicaHintList) > 1 {
if readReplicaHintCnt != 0 {
if readReplicaHintCnt > 1 {
warn := errors.New("There are multiple READ_CONSISTENT_REPLICA hints, only the last one will take effect")
warns = append(warns, warn)
}
Expand Down
13 changes: 9 additions & 4 deletions executor/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"github.com/pingcap/tidb/util/chunk"
"github.com/pingcap/tidb/util/gcutil"
"github.com/pingcap/tidb/util/logutil"
"github.com/pingcap/tidb/util/stmtsummary"
"go.uber.org/zap"
)

Expand Down Expand Up @@ -119,6 +120,7 @@ func (e *SetExecutor) setSysVariable(name string, v *expression.VarAssignment) e
if sysVar.Scope == variable.ScopeNone {
return errors.Errorf("Variable '%s' is a read only variable", name)
}
var valStr string
if v.IsGlobal {
// Set global scope system variable.
if sysVar.Scope&variable.ScopeGlobal == 0 {
Expand All @@ -131,18 +133,18 @@ func (e *SetExecutor) setSysVariable(name string, v *expression.VarAssignment) e
if value.IsNull() {
value.SetString("")
}
svalue, err := value.ToString()
valStr, err = value.ToString()
if err != nil {
return err
}
err = sessionVars.GlobalVarsAccessor.SetGlobalSysVar(name, svalue)
err = sessionVars.GlobalVarsAccessor.SetGlobalSysVar(name, valStr)
if err != nil {
return err
}
err = plugin.ForeachPlugin(plugin.Audit, func(p *plugin.Plugin) error {
auditPlugin := plugin.DeclareAuditManifest(p.Manifest)
if auditPlugin.OnGlobalVariableEvent != nil {
auditPlugin.OnGlobalVariableEvent(context.Background(), e.ctx.GetSessionVars(), name, svalue)
auditPlugin.OnGlobalVariableEvent(context.Background(), e.ctx.GetSessionVars(), name, valStr)
}
return nil
})
Expand Down Expand Up @@ -179,7 +181,6 @@ func (e *SetExecutor) setSysVariable(name string, v *expression.VarAssignment) e
sessionVars.SnapshotTS = oldSnapshotTS
return err
}
var valStr string
if value.IsNull() {
valStr = "NULL"
} else {
Expand All @@ -190,6 +191,10 @@ func (e *SetExecutor) setSysVariable(name string, v *expression.VarAssignment) e
logutil.BgLogger().Info("set session var", zap.Uint64("conn", sessionVars.ConnectionID), zap.String("name", name), zap.String("val", valStr))
}

if name == variable.TiDBEnableStmtSummary {
stmtsummary.StmtSummaryByDigestMap.SetEnabled(valStr, !v.IsGlobal)
}

return nil
}

Expand Down
Loading

0 comments on commit 0db84d7

Please sign in to comment.