Skip to content

Commit

Permalink
Merge pull request pingcap#839 from pingcap/qiuyesuifeng/cleanup
Browse files Browse the repository at this point in the history
optimizer: tiny clean up.
  • Loading branch information
coocood committed Jan 13, 2016
2 parents 61a4ce5 + c1e8a31 commit dacbca8
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 92 deletions.
68 changes: 34 additions & 34 deletions optimizer/evaluator/evaluator.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,72 +90,72 @@ func (e *Evaluator) Enter(in ast.Node) (out ast.Node, skipChildren bool) {
// Leave implements ast.Visitor interface.
func (e *Evaluator) Leave(in ast.Node) (out ast.Node, ok bool) {
switch v := in.(type) {
case *ast.ValueExpr:
ok = true
case *ast.AggregateFuncExpr:
ok = e.aggregateFunc(v)
case *ast.BetweenExpr:
ok = e.between(v)
case *ast.BinaryOperationExpr:
ok = e.binaryOperation(v)
case *ast.CaseExpr:
ok = e.caseExpr(v)
case *ast.WhenClause:
ok = true
case *ast.SubqueryExpr:
ok = e.subquery(v)
case *ast.CompareSubqueryExpr:
ok = e.compareSubquery(v)
case *ast.ColumnName:
ok = true
case *ast.ColumnNameExpr:
ok = e.columnName(v)
case *ast.CompareSubqueryExpr:
ok = e.compareSubquery(v)
case *ast.DefaultExpr:
ok = e.defaultExpr(v)
case *ast.ExistsSubqueryExpr:
ok = e.existsSubquery(v)
case *ast.PatternInExpr:
ok = e.patternIn(v)
case *ast.FuncCallExpr:
ok = e.funcCall(v)
case *ast.FuncCastExpr:
ok = e.funcCast(v)
case *ast.FuncConvertExpr:
ok = e.funcConvert(v)
case *ast.FuncDateArithExpr:
ok = e.funcDateArith(v)
case *ast.FuncExtractExpr:
ok = e.funcExtract(v)
case *ast.FuncLocateExpr:
ok = e.funcLocate(v)
case *ast.FuncSubstringExpr:
ok = e.funcSubstring(v)
case *ast.FuncSubstringIndexExpr:
ok = e.funcSubstringIndex(v)
case *ast.FuncTrimExpr:
ok = e.funcTrim(v)
case *ast.IsNullExpr:
ok = e.isNull(v)
case *ast.IsTruthExpr:
ok = e.isTruth(v)
case *ast.PatternLikeExpr:
ok = e.patternLike(v)
case *ast.ParamMarkerExpr:
ok = e.paramMarker(v)
case *ast.ParenthesesExpr:
ok = e.parentheses(v)
case *ast.PositionExpr:
ok = e.position(v)
case *ast.PatternInExpr:
ok = e.patternIn(v)
case *ast.PatternLikeExpr:
ok = e.patternLike(v)
case *ast.PatternRegexpExpr:
ok = e.patternRegexp(v)
case *ast.PositionExpr:
ok = e.position(v)
case *ast.RowExpr:
ok = e.row(v)
case *ast.SubqueryExpr:
ok = e.subquery(v)
case *ast.UnaryOperationExpr:
ok = e.unaryOperation(v)
case *ast.ValueExpr:
ok = true
case *ast.ValuesExpr:
ok = e.values(v)
case *ast.VariableExpr:
ok = e.variable(v)
case *ast.FuncCallExpr:
ok = e.funcCall(v)
case *ast.FuncExtractExpr:
ok = e.funcExtract(v)
case *ast.FuncConvertExpr:
ok = e.funcConvert(v)
case *ast.FuncCastExpr:
ok = e.funcCast(v)
case *ast.FuncSubstringExpr:
ok = e.funcSubstring(v)
case *ast.FuncSubstringIndexExpr:
ok = e.funcSubstringIndex(v)
case *ast.FuncLocateExpr:
ok = e.funcLocate(v)
case *ast.FuncTrimExpr:
ok = e.funcTrim(v)
case *ast.FuncDateArithExpr:
ok = e.funcDateArith(v)
case *ast.AggregateFuncExpr:
ok = e.aggregateFunc(v)
case *ast.WhenClause:
ok = true
}
out = in
return
Expand Down
20 changes: 10 additions & 10 deletions optimizer/plan/cost.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,16 @@ func (c *costEstimator) Enter(p Plan) (Plan, bool) {
// Leave implements Visitor Leave interface.
func (c *costEstimator) Leave(p Plan) (Plan, bool) {
switch v := p.(type) {
case *Filter:
v.startupCost = v.Src().StartupCost()
v.rowCount = v.Src().RowCount() * FilterRate
v.totalCost = v.Src().TotalCost()
case *IndexScan:
c.indexScan(v)
case *TableScan:
c.tableScan(v)
case *Limit:
v.rowCount = v.Src().RowCount()
v.startupCost = v.Src().StartupCost()
v.totalCost = v.Src().TotalCost()
case *SelectFields:
if v.Src() != nil {
v.startupCost = v.Src().StartupCost()
Expand All @@ -54,10 +60,6 @@ func (c *costEstimator) Leave(p Plan) (Plan, bool) {
v.startupCost = v.Src().StartupCost()
v.rowCount = v.Src().RowCount()
v.totalCost = v.Src().TotalCost()
case *Filter:
v.startupCost = v.Src().StartupCost()
v.rowCount = v.Src().RowCount() * FilterRate
v.totalCost = v.Src().TotalCost()
case *Sort:
if v.Bypass {
// Bypassed sort doesn't add extra cost.
Expand All @@ -74,10 +76,8 @@ func (c *costEstimator) Leave(p Plan) (Plan, bool) {
}
v.totalCost = v.startupCost + v.rowCount*RowCost
}
case *Limit:
v.rowCount = v.Src().RowCount()
v.startupCost = v.Src().StartupCost()
v.totalCost = v.Src().TotalCost()
case *TableScan:
c.tableScan(v)
}
return p, true
}
Expand Down
38 changes: 19 additions & 19 deletions optimizer/plan/explainer.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,36 +38,36 @@ func (e *explainer) Enter(in Plan) (Plan, bool) {
func (e *explainer) Leave(in Plan) (Plan, bool) {
var str string
switch x := in.(type) {
case *TableScan:
if len(x.Ranges) > 0 {
ran := x.Ranges[0]
if ran.LowVal != math.MinInt64 || ran.HighVal != math.MaxInt64 {
str = fmt.Sprintf("Range(%s)", x.Table.Name.L)
} else {
str = fmt.Sprintf("Table(%s)", x.Table.Name.L)
}
} else {
str = fmt.Sprintf("Table(%s)", x.Table.Name.L)
}
case *IndexScan:
str = fmt.Sprintf("Index(%s.%s)", x.Table.Name.L, x.Index.Name.L)
case *ShowDDL:
str = "ShowDDL"
case *CheckTable:
str = "CheckTable"
case *Filter:
str = "Filter"
case *IndexScan:
str = fmt.Sprintf("Index(%s.%s)", x.Table.Name.L, x.Index.Name.L)
case *Limit:
str = "Limit"
case *SelectFields:
str = "Fields"
case *SelectLock:
str = "Lock"
case *ShowDDL:
str = "ShowDDL"
case *Sort:
if x.Bypass {
return in, true
}
str = "Sort"
case *SelectLock:
str = "Lock"
case *Limit:
str = "Limit"
case *TableScan:
if len(x.Ranges) > 0 {
ran := x.Ranges[0]
if ran.LowVal != math.MinInt64 || ran.HighVal != math.MaxInt64 {
str = fmt.Sprintf("Range(%s)", x.Table.Name.L)
} else {
str = fmt.Sprintf("Table(%s)", x.Table.Name.L)
}
} else {
str = fmt.Sprintf("Table(%s)", x.Table.Name.L)
}
default:
e.err = ErrUnsupportedType.Gen("Unknown plan type %T", in)
return in, false
Expand Down
22 changes: 11 additions & 11 deletions optimizer/plan/planbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,36 +33,36 @@ var (

// Error codes.
const (
CodeUnsupportedType = iota + 1
CodeUnsupportedType = iota
)

// BuildPlan builds a plan from a node.
// returns ErrUnsupportedType if ast.Node type is not supported yet.
// It returns ErrUnsupportedType if ast.Node type is not supported yet.
func BuildPlan(node ast.Node) (Plan, error) {
var builder planBuilder
p := builder.build(node)
return p, builder.err
}

// planBuilder builds Plan from an ast.Node.
// It just build the ast node straightforwardly.
// It just builds the ast node straightforwardly.
type planBuilder struct {
err error
hasAgg bool
}

func (b *planBuilder) build(node ast.Node) Plan {
switch x := node.(type) {
case *ast.SelectStmt:
return b.buildSelect(x)
case *ast.PrepareStmt:
return b.buildPrepare(x)
case *ast.ExecuteStmt:
return &Execute{Name: x.Name, UsingVars: x.UsingVars}
case *ast.DeallocateStmt:
return &Deallocate{Name: x.Name}
case *ast.AdminStmt:
return b.buildAdmin(x)
case *ast.DeallocateStmt:
return &Deallocate{Name: x.Name}
case *ast.ExecuteStmt:
return &Execute{Name: x.Name, UsingVars: x.UsingVars}
case *ast.PrepareStmt:
return b.buildPrepare(x)
case *ast.SelectStmt:
return b.buildSelect(x)
}
b.err = ErrUnsupportedType.Gen("Unsupported type %T", node)
return nil
Expand Down
36 changes: 18 additions & 18 deletions optimizer/plan/refiner.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@ func (r *refiner) Enter(in Plan) (Plan, bool) {

func (r *refiner) Leave(in Plan) (Plan, bool) {
switch x := in.(type) {
case *TableScan:
r.buildTableRange(x)
case *IndexScan:
r.buildIndexRange(x)
case *Sort:
r.sortBypass(x)
case *Limit:
x.SetLimit(0)
case *Sort:
r.sortBypass(x)
case *TableScan:
r.buildTableRange(x)
}
return in, r.err == nil
}
Expand Down Expand Up @@ -183,6 +183,20 @@ func (c *conditionChecker) check(condition ast.ExprNode) bool {
switch x := condition.(type) {
case *ast.BinaryOperationExpr:
return c.checkBinaryOperation(x)
case *ast.BetweenExpr:
if ast.IsPreEvaluable(x.Left) && ast.IsPreEvaluable(x.Right) && c.checkColumnExpr(x.Expr) {
return true
}
case *ast.ColumnNameExpr:
return c.checkColumnExpr(x)
case *ast.IsNullExpr:
if c.checkColumnExpr(x.Expr) {
return true
}
case *ast.IsTruthExpr:
if c.checkColumnExpr(x.Expr) {
return true
}
case *ast.ParenthesesExpr:
return c.check(x.Expr)
case *ast.PatternInExpr:
Expand Down Expand Up @@ -211,20 +225,6 @@ func (c *conditionChecker) check(condition ast.ExprNode) bool {
patternStr := x.Pattern.GetValue().(string)
firstChar := patternStr[0]
return firstChar != '%' && firstChar != '.'
case *ast.BetweenExpr:
if ast.IsPreEvaluable(x.Left) && ast.IsPreEvaluable(x.Right) && c.checkColumnExpr(x.Expr) {
return true
}
case *ast.IsNullExpr:
if c.checkColumnExpr(x.Expr) {
return true
}
case *ast.IsTruthExpr:
if c.checkColumnExpr(x.Expr) {
return true
}
case *ast.ColumnNameExpr:
return c.checkColumnExpr(x)
}
return false
}
Expand Down

0 comments on commit dacbca8

Please sign in to comment.