Skip to content

Commit

Permalink
Fix 'SHOW WARNINGS' query error caused by 22.sql
Browse files Browse the repository at this point in the history
  • Loading branch information
zouhuan1215 committed Aug 9, 2019
1 parent 79200e4 commit cdb143f
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 53 deletions.
13 changes: 7 additions & 6 deletions executor/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import (
"go.uber.org/zap"
)

const tblname string = "idxadv"
const tblname string = "orders"

var (
stmtNodeCounterUse = metrics.StmtNodeCounter.WithLabelValues("Use")
Expand Down Expand Up @@ -85,7 +85,8 @@ func (c *Compiler) compile(ctx context.Context, stmtNode ast.StmtNode, skipBind
return nil, err
}

if c.Ctx.GetSessionVars().EnableIndexAdvisor {
// If current query is "SHOW WARNINGS", ignore it
if c.Ctx.GetSessionVars().EnableIndexAdvisor && !strings.HasPrefix(plannercore.ToString(finalPlan), "Show") {
// Get final physical plan.
p, err := plannercore.GetPhysicalPlan(finalPlan)
if err != nil {
Expand All @@ -107,11 +108,11 @@ func (c *Compiler) compile(ctx context.Context, stmtNode ast.StmtNode, skipBind
if err != nil {
panic(err)
}

// Construct virtual infoschema
dbname := c.Ctx.GetSessionVars().CurrentDB
virtualIS := idxadvisor.GetVirtualInfoschema(infoSchema, dbname, tblname)

// Get virtual final plan.
vFinalPlan, err := planner.Optimize(ctx, c.Ctx, stmtNode, virtualIS)
if err != nil {
Expand All @@ -124,13 +125,13 @@ func (c *Compiler) compile(ctx context.Context, stmtNode ast.StmtNode, skipBind
if err != nil {
panic(err)
}

// Get virtual final physical plan.
vPhysicalPlan, err := plannercore.GetPhysicalPlan(vFinalPlan)
if err != nil {
panic(err)
}

// Get virtual indices with cost.
selectedIndices := idxadvisor.FindVirtualIndices(vPhysicalPlan)
iwc := idxadvisor.IndicesWithCost{Indices: selectedIndices, Cost: vcost}
Expand Down
1 change: 0 additions & 1 deletion executor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -912,7 +912,6 @@ func init() {
}

e := &executorBuilder{is: is, ctx: sctx}
p, _ = plannercore.GetPhysicalPlan(p)
exec := e.build(p)
if e.err != nil {
return rows, err
Expand Down
56 changes: 16 additions & 40 deletions idxadvisor/idxadvisor.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package idxadvisor

import (
"reflect"
"database/sql"
"errors"
"fmt"
"reflect"
"sync/atomic"

"github.com/pingcap/parser/ast"
Expand Down Expand Up @@ -82,38 +82,14 @@ func (ia *IdxAdvisor) IsReady() bool {
func (ia *IdxAdvisor) StartTask(query string) {
if ia.IsReady() {
fmt.Printf("********idxadvisor/outline.go: Set variable has done, StartTask starts query\n")
if _, err := ia.dbClient.Exec(query); err != nil {
fmt.Printf("**********query execution error: %v\n", err)
panic(err)
}
if _, err := ia.dbClient.Exec("select * from IDXADV where a = 1 and c = 3"); err != nil {
fmt.Printf("**********query execution error: %v\n", err)
panic(err)
}
if _, err := ia.dbClient.Exec("select c from IDXADV where a in (1,3)"); err != nil {
fmt.Printf("**********query execution error: %v\n", err)
panic(err)
}
if _, err := ia.dbClient.Exec("select c from IDXADV where a+c=2"); err != nil {
fmt.Printf("**********query execution error: %v\n", err)
panic(err)
}
if _, err := ia.dbClient.Exec("select * from IDXADV where c in (select a from IDXADV where a>0)"); err != nil {
fmt.Printf("**********query execution error: %v\n", err)
panic(err)
}
if _, err := ia.dbClient.Exec("select * from IDXADV where c in (select a from t1 where a>0)"); err != nil {
fmt.Printf("**********query execution error: %v\n", err)
panic(err)
}
if _, err := ia.dbClient.Exec("select * from IDXADV, t1 where IDXADV.c = t1.c"); err != nil {
fmt.Printf("**********query execution error: %v\n", err)
panic(err)
}
if _, err := ia.dbClient.Exec("select c,sum(a) as v from idxadv where b=1 group by c having sum(a) >= (select sum(a)*0.1 from t1 where b = 1) order by v"); err != nil {
fmt.Printf("**********query execution error: %v\n", err)
panic(err)
// var err error
sqlFile := "/tmp/queries"
queries := readQuery(&sqlFile)
for i, query := range queries {
fmt.Printf("$$$$$$$$$$$$$$$$$$$$$$[%v]$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n", i+1)
ia.dbClient.Exec(query)
}

}
}

Expand Down Expand Up @@ -165,14 +141,14 @@ func GenVirtualIndexCols(tblInfo *model.TableInfo, dbname, tblname model.CIStr)

nCols := len(columnInfos)
for i := 0; i < nCols; i++ {
for j := 0; j < nCols; j++ {
if i != j {
idxTwoCols := make([]*ast.IndexColName, 2, 2)
idxTwoCols[0] = BuildIdxColNameFromColInfo(columnInfos[i], dbname, tblname)
idxTwoCols[1] = BuildIdxColNameFromColInfo(columnInfos[j], dbname, tblname)
result = append(result, idxTwoCols)
}
for j := 0; j < nCols; j++ {
if i != j {
idxTwoCols := make([]*ast.IndexColName, 2, 2)
idxTwoCols[0] = BuildIdxColNameFromColInfo(columnInfos[i], dbname, tblname)
idxTwoCols[1] = BuildIdxColNameFromColInfo(columnInfos[j], dbname, tblname)
result = append(result, idxTwoCols)
}
}
}

return result
Expand Down Expand Up @@ -219,4 +195,4 @@ func (ia *IdxAdvisor) addCandidate(virtualIdx *CandidateIdx) {
if !in {
ia.Candidate_idx = append(ia.Candidate_idx, virtualIdx)
}
}
}
2 changes: 1 addition & 1 deletion idxadvisor/sqlclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
const statusPort uint = 10080
const user string = "root"
const address string = "127.0.0.1:4000"
const dbname string = "test"
const dbname string = "tpch"

const retryTime = 100

Expand Down
15 changes: 13 additions & 2 deletions planner/core/optimizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,19 @@ func physicalOptimize(logic LogicalPlan) (PhysicalPlan, error) {

err = t.plan().ResolveIndices()

if vt, ok := t.(*rootTask); ok && logic.SCtx().GetSessionVars().EnableIndexAdvisor {
return vt, err
if logic.SCtx().GetSessionVars().EnableIndexAdvisor {
if _, ok := logic.(*LogicalMaxOneRow); !ok {
switch vt := t.(type) {
case *rootTask:
return vt, nil
case *copTask:
return vt, nil
default:
return nil, errors.New("GetRootTaskCost: Plan interface is not implemented by rootTask")

}

}
}
return t.plan(), err
}
Expand Down
3 changes: 0 additions & 3 deletions session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -1044,9 +1044,6 @@ func (s *session) execute(ctx context.Context, sql string) (recordSets []sqlexec
return nil, err
}

if strings.Contains(sql, "IDXADV") || strings.Contains(sql, "t1") {
fmt.Printf("###############%v##############\n", sql)
}
charsetInfo, collation := s.sessionVars.GetCharsetInfo()

// Step1: Compile query string to abstract syntax trees(ASTs).
Expand Down

0 comments on commit cdb143f

Please sign in to comment.