Skip to content

Commit

Permalink
plan: fix a bug about prefix index. (pingcap#2445)
Browse files Browse the repository at this point in the history
  • Loading branch information
hanfei1991 authored Feb 2, 2017
1 parent 9e4a4d3 commit 5230e39
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions plan/refiner.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ func detachIndexScanConditions(conditions []expression.Expression, indexScan *Ph
tableName: indexScan.Table.Name,
idx: indexScan.Index,
columnOffset: curIndex,
length: indexScan.Index.Columns[curIndex].Length,
}
// First of all, we should extract all of in/eq expressions from rest conditions for every continuous index column.
// e.g. For index (a,b,c) and conditions a in (1,2) and b < 1 and c in (3,4), we should only extract column a in (1,2).
Expand Down Expand Up @@ -264,7 +265,9 @@ func detachTableScanConditions(conditions []expression.Expression, table *model.
var accessConditions, filterConditions []expression.Expression
checker := conditionChecker{
tableName: table.Name,
pkName: pkName}
pkName: pkName,
length: types.UnspecifiedLength,
}
for _, cond := range conditions {
cond = pushDownNot(cond, false, nil)
if !checker.check(cond) {
Expand Down Expand Up @@ -307,6 +310,7 @@ type conditionChecker struct {
columnOffset int // the offset of the indexed column to be checked.
pkName model.CIStr
shouldReserve bool // check if a access condition should be reserved in filter conditions.
length int
}

func (c *conditionChecker) check(condition expression.Expression) bool {
Expand Down Expand Up @@ -359,10 +363,14 @@ func (c *conditionChecker) checkScalarFunction(scalar *expression.ScalarFunction
return c.check(scalar.GetArgs()[0]) && c.check(scalar.GetArgs()[1])
case ast.EQ, ast.NE, ast.GE, ast.GT, ast.LE, ast.LT:
if _, ok := scalar.GetArgs()[0].(*expression.Constant); ok {
return c.checkColumn(scalar.GetArgs()[1])
if c.checkColumn(scalar.GetArgs()[1]) {
return scalar.FuncName.L != ast.NE || c.length == types.UnspecifiedLength
}
}
if _, ok := scalar.GetArgs()[1].(*expression.Constant); ok {
return c.checkColumn(scalar.GetArgs()[0])
if c.checkColumn(scalar.GetArgs()[0]) {
return scalar.FuncName.L != ast.NE || c.length == types.UnspecifiedLength
}
}
case ast.IsNull, ast.IsTruth, ast.IsFalsity:
return c.checkColumn(scalar.GetArgs()[0])
Expand Down

0 comments on commit 5230e39

Please sign in to comment.